This is an automated email from the ASF dual-hosted git repository. amanin pushed a commit to branch fix/envelope_of_points in repository https://gitbox.apache.org/repos/asf/sis.git
commit 90cefc4505876bc8ec2f9695e1a5029a1d2147a4 Author: Alexis Manin <[email protected]> AuthorDate: Tue May 19 10:30:24 2020 +0200 fix(Core): avoid returning null envelope for points or empty geometries. --- .../sis/internal/feature/GeometryWithCRS.java | 2 ++ .../sis/internal/feature/GeometryWrapper.java | 3 ++- .../apache/sis/internal/feature/esri/Wrapper.java | 21 ++++++++++----------- .../sis/internal/feature/j2d/PointWrapper.java | 9 +++++---- .../apache/sis/internal/feature/j2d/Wrapper.java | 15 +++++++-------- .../apache/sis/internal/feature/jts/Wrapper.java | 15 +++++++++------ 6 files changed, 35 insertions(+), 30 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..7609c34 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 @@ -16,23 +16,24 @@ */ package org.apache.sis.internal.feature.esri; -import java.util.Iterator; -import com.esri.core.geometry.Geometry; import com.esri.core.geometry.Envelope2D; +import com.esri.core.geometry.Geometry; import com.esri.core.geometry.MultiPath; import com.esri.core.geometry.MultiVertexGeometry; -import com.esri.core.geometry.Polyline; +import com.esri.core.geometry.OperatorCentroid2D; +import com.esri.core.geometry.OperatorExportToWkt; import com.esri.core.geometry.Point; import com.esri.core.geometry.Point2D; +import com.esri.core.geometry.Polyline; import com.esri.core.geometry.WktExportFlags; -import com.esri.core.geometry.OperatorExportToWkt; -import com.esri.core.geometry.OperatorCentroid2D; -import org.opengis.geometry.DirectPosition; +import java.util.Iterator; import org.apache.sis.geometry.DirectPosition2D; 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.geometry.DirectPosition; +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..2845051 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 @@ -19,12 +19,12 @@ package org.apache.sis.internal.feature.j2d; import java.awt.Shape; import java.awt.geom.Point2D; import java.util.Iterator; -import org.opengis.geometry.DirectPosition; import org.apache.sis.geometry.DirectPosition2D; 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.geometry.DirectPosition; /** @@ -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; } }
