sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ama...@apache.org
Subject [sis] 01/07: fix(Feature): Ensure coordinate order consistency when converting an envelope to a geometry
Date Fri, 30 Jul 2021 16:49:37 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

commit b7fcc0286a84b5a3a940bc876a97de6577f41bb5
Author: Alexis Manin <alexis.manin@geomatys.com>
AuthorDate: Wed Jul 28 15:49:29 2021 +0200

    fix(Feature): Ensure coordinate order consistency when converting an envelope to a geometry
    
    Previous behavior tried to build a geometry in a right-handed space. However, the coordinate
reference system was not adapted to the potential axis swapping.
    
    The strategy has been changed to preserve input coordinate order. The swapping is mostly
needed for interoperability with third-party contexts/applications. As such, it should be
managed in higher levels.
---
 .../java/org/apache/sis/internal/feature/Geometries.java   | 12 ++++++++----
 .../apache/sis/internal/feature/GeometriesTestCase.java    | 14 +++++++-------
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java
index 5e848c4..f1c221a 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java
@@ -21,6 +21,7 @@ import java.io.Serializable;
 import java.nio.ByteBuffer;
 import java.util.Optional;
 import java.util.Iterator;
+import org.apache.sis.referencing.cs.AxesConvention;
 import org.opengis.geometry.DirectPosition;
 import org.opengis.geometry.Envelope;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
@@ -398,7 +399,10 @@ public abstract class Geometries<G> implements Serializable {
      * and other points are the envelope corners in clockwise order. The specified envelope
      * should be two-dimensional (see for example {@link GeneralEnvelope#horizontal()}) but
      * the coordinates does not need to be in (longitude, latitude) order; this method will
-     * reorder coordinates as (x,y) on a best-effort basis.
+     * preserve envelope horizontal axis order. It means that any non-2D axis will be ignored,
+     * and the first horizontal axis in the envelope will be the first axis (x) in the resulting
+     * geometry. To force {@link AxesConvention#RIGHT_HANDED}, you have to transform your
bounding
+     * box before calling this method.
      *
      * @param  envelope  the envelope to convert.
      * @param  strategy  how to resolve wrap-around ambiguities on the envelope.
@@ -407,7 +411,7 @@ public abstract class Geometries<G> implements Serializable {
     public GeometryWrapper<G> toGeometry2D(final Envelope envelope, final WraparoundMethod
strategy) {
         final CoordinateReferenceSystem crs = envelope.getCoordinateReferenceSystem();
         int x = 0, y = 1;
-        if (crs != null) {
+        if (crs != null && envelope.getDimension() > 2) {
             final CoordinateSystem cs = crs.getCoordinateSystem();
             int cx = AxisDirections.indexOfColinear(cs, AxisDirection.EAST);
             int cy = AxisDirections.indexOfColinear(cs, AxisDirection.NORTH);
@@ -418,8 +422,8 @@ public abstract class Geometries<G> implements Serializable {
                     // May happen if the CRS has only one dimension.
                     throw new IllegalArgumentException(Errors.format(Errors.Keys.EmptyEnvelope2D));
                 }
-                x = cx;
-                y = cy;
+                x = Math.min(cx, cy);
+                y = Math.max(cy, cx);
             }
         }
         final GeometryWrapper<G> result;
diff --git a/core/sis-feature/src/test/java/org/apache/sis/internal/feature/GeometriesTestCase.java
b/core/sis-feature/src/test/java/org/apache/sis/internal/feature/GeometriesTestCase.java
index 376f005..cabd0d4 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/internal/feature/GeometriesTestCase.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/internal/feature/GeometriesTestCase.java
@@ -207,18 +207,18 @@ public abstract strictfp class GeometriesTestCase extends TestCase {
     }
 
     /**
-     * Tests {@link Geometries#toGeometry2D(Envelope, WraparoundMethod)} with automatic axis
swapping.
+     * Tests {@link Geometries#toGeometry2D(Envelope, WraparoundMethod)} and ensure <em>no</em>
axis swapping is done.
      */
     @Test
-    public void testToGeometryAxisSwapping() {
+    public void testToGeometryAxisOrderIsPreserved() {
         final GeneralEnvelope e = new GeneralEnvelope(HardCodedCRS.WGS84_LATITUDE_FIRST);
         e.setRange(0,  2,  3);
         e.setRange(1, 89, 19);
-        assertToGeometryEquals(e, WraparoundMethod.NONE,         89, 2,   89, 3,  19, 3,
  19, 2,   89, 2);
-        assertToGeometryEquals(e, WraparoundMethod.CONTIGUOUS, -271, 2, -271, 3,  19, 3,
  19, 2, -271, 2);
-        assertToGeometryEquals(e, WraparoundMethod.EXPAND,     -180, 2, -180, 3, 180, 3,
 180, 2, -180, 2);
-        assertToGeometryEquals(e, WraparoundMethod.SPLIT,        89, 2,   89, 3, 180, 3,
 180, 2,   89, 2,
-                                                               -180, 2, -180, 3,  19, 3,
  19, 2, -180, 2);
+        assertToGeometryEquals(e, WraparoundMethod.NONE,       2,   89, 2,  19, 3,  19, 3,
  89, 2,   89);
+        assertToGeometryEquals(e, WraparoundMethod.CONTIGUOUS, 2, -271, 2,  19, 3,  19, 3,
-271, 2, -271);
+        assertToGeometryEquals(e, WraparoundMethod.EXPAND,     2, -180, 2, 180, 3, 180, 3,
-180, 2, -180);
+        assertToGeometryEquals(e, WraparoundMethod.SPLIT,      2,   89, 2, 180, 3, 180, 3,
  89, 2,   89,
+                                                               2, -180, 2,  19, 3,  19, 3,
-180, 2, -180);
     }
 
     /**

Mime
View raw message