sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ama...@apache.org
Subject [sis] branch geoapi-4.0 updated: fix(Core): avoid returning null envelope for points or empty geometries.
Date Tue, 19 May 2020 08:56:38 GMT
This is an automated email from the ASF dual-hosted git repository.

amanin pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 53a7f09  fix(Core): avoid returning null envelope for points or empty geometries.
53a7f09 is described below

commit 53a7f091eb9f50a29eb617b058fa55d8c8e157c2
Author: Alexis Manin <amanin@apache.org>
AuthorDate: Tue May 19 10:30:24 2020 +0200

    fix(Core): avoid returning null envelope for points or empty geometries.
---
 .../org/apache/sis/internal/feature/GeometryWithCRS.java  |  2 ++
 .../org/apache/sis/internal/feature/GeometryWrapper.java  |  3 ++-
 .../org/apache/sis/internal/feature/esri/Wrapper.java     |  9 ++++-----
 .../org/apache/sis/internal/feature/j2d/PointWrapper.java |  7 ++++---
 .../java/org/apache/sis/internal/feature/j2d/Wrapper.java | 15 +++++++--------
 .../java/org/apache/sis/internal/feature/jts/Wrapper.java | 15 +++++++++------
 6 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryWithCRS.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryWithCRS.java
index 0bde1cb..fabd9dd 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryWithCRS.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryWithCRS.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.internal.feature;
 
+import org.apache.sis.util.ArgumentChecks;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 
 
@@ -60,6 +61,7 @@ public abstract class GeometryWithCRS<G> extends GeometryWrapper<G>
{
      */
     @Override
     public final void setCoordinateReferenceSystem(final CoordinateReferenceSystem crs) {
+        ArgumentChecks.ensureDimensionMatches("crs", 2, crs);
         this.crs = crs;
     }
 }
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryWrapper.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryWrapper.java
index b960908..d8f200f 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryWrapper.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryWrapper.java
@@ -113,7 +113,8 @@ public abstract class GeometryWrapper<G> implements Geometry {
     /**
      * Returns the geometry bounding box, together with its coordinate reference system.
      *
-     * @return the geometry envelope. Should never be {@code null} except for an empty geometry
or a single point.
+     * @return the geometry envelope. Should never be {@code null}. Note though that for
an empty geometry or a single
+     * point, the returned envelope will be empty.
      */
     @Override
     public abstract GeneralEnvelope getEnvelope();
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/esri/Wrapper.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/esri/Wrapper.java
index 7495603..04d586c 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/esri/Wrapper.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/esri/Wrapper.java
@@ -33,6 +33,7 @@ import org.apache.sis.geometry.GeneralEnvelope;
 import org.apache.sis.internal.feature.Geometries;
 import org.apache.sis.internal.feature.GeometryWithCRS;
 import org.apache.sis.util.Debug;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
 
 
 /**
@@ -84,10 +85,8 @@ final class Wrapper extends GeometryWithCRS<Geometry> {
     public GeneralEnvelope getEnvelope() {
         final Envelope2D bounds = new Envelope2D();
         geometry.queryEnvelope2D(bounds);
-        if (bounds.isEmpty()) {                 // Test if there is NaN values.
-            return null;
-        }
-        final GeneralEnvelope env = new GeneralEnvelope(Factory.BIDIMENSIONAL);
+        final CoordinateReferenceSystem crs = getCoordinateReferenceSystem();
+        final GeneralEnvelope env = crs == null ? new GeneralEnvelope(org.apache.sis.internal.feature.j2d.Factory.BIDIMENSIONAL)
: new GeneralEnvelope(crs);
         env.setRange(0, bounds.xmin, bounds.xmax);
         env.setRange(1, bounds.ymin, bounds.ymax);
         return env;
@@ -99,7 +98,7 @@ final class Wrapper extends GeometryWithCRS<Geometry> {
     @Override
     public DirectPosition getCentroid() {
         final Point2D c = getCentroidImpl();
-        return new DirectPosition2D(c.x, c.y);
+        return new DirectPosition2D(getCoordinateReferenceSystem(), c.x, c.y);
     }
 
     /**
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/PointWrapper.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/PointWrapper.java
index aa6faaf..35e1676 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/PointWrapper.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/PointWrapper.java
@@ -66,11 +66,12 @@ final class PointWrapper extends GeometryWithCRS<Shape> {
     }
 
     /**
-     * Returns {@code null} since envelopes of points are empty.
+     * @return An empty envelope centered on this point.
      */
     @Override
     public GeneralEnvelope getEnvelope() {
-        return null;
+        final DirectPosition centroid = getCentroid();
+        return new GeneralEnvelope(centroid, centroid);
     }
 
     /**
@@ -78,7 +79,7 @@ final class PointWrapper extends GeometryWithCRS<Shape> {
      */
     @Override
     public DirectPosition getCentroid() {
-        return new DirectPosition2D(point.getX(), point.getY());
+        return new DirectPosition2D(getCoordinateReferenceSystem(), point.getX(), point.getY());
     }
 
     /**
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Wrapper.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Wrapper.java
index 480916e..8a6446d 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Wrapper.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Wrapper.java
@@ -31,6 +31,7 @@ import org.apache.sis.internal.feature.GeometryWithCRS;
 import org.apache.sis.internal.referencing.j2d.ShapeUtilities;
 import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.Debug;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
 
 
 /**
@@ -81,13 +82,11 @@ final class Wrapper extends GeometryWithCRS<Shape> {
     @Override
     public GeneralEnvelope getEnvelope() {
         final Rectangle2D bounds = geometry.getBounds2D();
-        if (!bounds.isEmpty()) {                                     // Test if there is
NaN values.
-            final GeneralEnvelope env = new GeneralEnvelope(Factory.BIDIMENSIONAL);
-            env.setRange(0, bounds.getMinX(), bounds.getMaxX());
-            env.setRange(1, bounds.getMinY(), bounds.getMaxY());
-            return env;
-        }
-        return null;
+        final CoordinateReferenceSystem crs = getCoordinateReferenceSystem();
+        final GeneralEnvelope env = crs == null ? new GeneralEnvelope(Factory.BIDIMENSIONAL)
: new GeneralEnvelope(crs);
+        env.setRange(0, bounds.getMinX(), bounds.getMaxX());
+        env.setRange(1, bounds.getMinY(), bounds.getMaxY());
+        return env;
     }
 
     /**
@@ -97,7 +96,7 @@ final class Wrapper extends GeometryWithCRS<Shape> {
     public DirectPosition getCentroid() {
         final RectangularShape frame = (geometry instanceof RectangularShape)
                         ? (RectangularShape) geometry : geometry.getBounds2D();
-        return new DirectPosition2D(frame.getCenterX(), frame.getCenterY());
+        return new DirectPosition2D(getCoordinateReferenceSystem(), frame.getCenterX(), frame.getCenterY());
     }
 
     /**
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/jts/Wrapper.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/jts/Wrapper.java
index fef59da..c3457bc 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/jts/Wrapper.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/jts/Wrapper.java
@@ -20,6 +20,7 @@ import java.util.List;
 import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.Iterator;
+import org.apache.sis.util.ArgumentChecks;
 import org.locationtech.jts.geom.Coordinate;
 import org.locationtech.jts.geom.Envelope;
 import org.locationtech.jts.geom.Geometry;
@@ -100,14 +101,13 @@ final class Wrapper extends GeometryWrapper<Geometry> {
      */
     @Override
     public void setCoordinateReferenceSystem(final CoordinateReferenceSystem crs) {
+        if (crs != null) ArgumentChecks.ensureBetween("CRS dimension", 2, 3, crs.getCoordinateSystem().getDimension());
         JTS.setCoordinateReferenceSystem(geometry, crs);
     }
 
     /**
-     * If the JTS geometry is non-empty, returns its envelope as an Apache SIS implementation.
-     * Otherwise returns {@code null}.
      *
-     * @return the envelope, or {@code null} if empty.
+     * @return the envelope of the decorated JTS geometry. Never null, but can be empty.
      */
     @Override
     public GeneralEnvelope getEnvelope() {
@@ -115,7 +115,7 @@ final class Wrapper extends GeometryWrapper<Geometry> {
         final GeneralEnvelope env = new GeneralEnvelope(Factory.BIDIMENSIONAL);
         env.setRange(0, bounds.getMinX(), bounds.getMaxX());
         env.setRange(1, bounds.getMinY(), bounds.getMaxY());
-        return env.isEmpty() ? null : env;
+        return env;
     }
 
     /**
@@ -126,9 +126,12 @@ final class Wrapper extends GeometryWrapper<Geometry> {
         final Coordinate c = geometry.getCentroid().getCoordinate();
         final double z = c.getZ();
         if (Double.isNaN(z)) {
-            return new DirectPosition2D(c.x, c.y);
+            return new DirectPosition2D(getCoordinateReferenceSystem(), c.x, c.y);
         } else {
-            return new GeneralDirectPosition(c.x, c.y, z);
+            final GeneralDirectPosition point = new GeneralDirectPosition(c.x, c.y, z);
+            final CoordinateReferenceSystem crs = getCoordinateReferenceSystem();
+            if (crs != null) point.setCoordinateReferenceSystem(crs);
+            return point;
         }
     }
 


Mime
View raw message