This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits 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 10af74be85 fix(S2): fix envelope conputation from S2 polygon
10af74be85 is described below

commit 10af74be8523cd7df24ae9d6dd572bf26d9f534c
Author: jsorel <[email protected]>
AuthorDate: Mon Nov 23 15:53:30 2026 +0100

    fix(S2): fix envelope conputation from S2 polygon
---
 .../org/apache/sis/geometry/wrapper/s2/S2.java     | 28 ++++++++++++++++++++++
 .../apache/sis/geometry/wrapper/s2/Wrapper.java    | 12 +---------
 .../main/org/apache/sis/referencing/dggs/Zone.java | 17 +++++++++----
 .../sis/referencing/dggs/AbstractDggrsTest.java    |  3 ---
 .../org/apache/sis/referencing/dggs/s2/S2Test.java |  5 ++--
 5 files changed, 44 insertions(+), 21 deletions(-)

diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/geometry/wrapper/s2/S2.java
 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/geometry/wrapper/s2/S2.java
index 50823515ad..c7aee6fc7b 100644
--- 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/geometry/wrapper/s2/S2.java
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/geometry/wrapper/s2/S2.java
@@ -26,6 +26,7 @@ import org.locationtech.jts.geom.MultiPolygon;
 import org.locationtech.jts.geom.Polygon;
 import org.locationtech.jts.geom.impl.PackedCoordinateSequence;
 import com.google.common.geometry.S2LatLng;
+import com.google.common.geometry.S2LatLngRect;
 import com.google.common.geometry.S2Loop;
 import com.google.common.geometry.S2Point;
 import com.google.common.geometry.S2Polygon;
@@ -187,6 +188,33 @@ public final class S2 {
         return new 
DefaultBoundingPolygon(Factory.INSTANCE.castOrWrap(polygon));
     }
 
+    /**
+     * Returns the CRS:84 envelope of a S2 region.
+     * The envelope includes the edges bulging between vertices and the poles 
inside the region.
+     * The lower longitude is greater than the upper one if the region crosses 
the anti-meridian.
+     * S2 bounds are slightly enlarged to be robust to rounding errors.
+     *
+     * @param region not null
+     * @return envelope in CRS:84, NaN if the region is empty
+     */
+    public static GeneralEnvelope toEnvelope(S2Region region) {
+        final S2LatLngRect rect = region.getRectBound();
+        final GeneralEnvelope envelope = new 
GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
+        if (rect.isEmpty()) {
+            envelope.setToNaN();
+        } else {
+            double west = rect.lng().lo();
+            final double east = rect.lng().hi();
+            if (west == Math.PI && east < Math.PI) {
+                // S2 writes the -180° meridian as 180°, the region does not 
cross the anti-meridian.
+                west = -Math.PI;
+            }
+            envelope.setRange(0, Math.toDegrees(west), Math.toDegrees(east));
+            envelope.setRange(1, Math.toDegrees(rect.lat().lo()), 
Math.toDegrees(rect.lat().hi()));
+        }
+        return envelope;
+    }
+
     /**
      * Convert JTS LinearRing to S2Loop.
      *
diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/geometry/wrapper/s2/Wrapper.java
 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/geometry/wrapper/s2/Wrapper.java
index ab748a2a05..48efd8898f 100644
--- 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/geometry/wrapper/s2/Wrapper.java
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/geometry/wrapper/s2/Wrapper.java
@@ -17,11 +17,9 @@
 package org.apache.sis.geometry.wrapper.s2;
 
 import java.util.Iterator;
-import com.google.common.geometry.S2LatLngRect;
 import com.google.common.geometry.S2Region;
 import org.opengis.geometry.DirectPosition;
 import org.apache.sis.geometry.GeneralEnvelope;
-import org.apache.sis.referencing.CommonCRS;
 import org.apache.sis.geometry.wrapper.Geometries;
 import org.apache.sis.geometry.wrapper.GeometryWrapper;
 
@@ -69,15 +67,7 @@ final class Wrapper extends GeometryWrapper {
      */
     @Override
     public GeneralEnvelope getEnvelope() {
-        final S2LatLngRect rect = geometry.getRectBound();
-        final GeneralEnvelope envelope = new 
GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
-        if (rect.lat().lo() > rect.lat().hi()) {
-            envelope.setToNaN();
-        } else {
-            envelope.setRange(0, Math.toDegrees(rect.lng().lo()), 
Math.toDegrees(rect.lng().hi()));
-            envelope.setRange(1, Math.toDegrees(rect.lat().lo()), 
Math.toDegrees(rect.lat().hi()));
-        }
-        return envelope;
+        return S2.toEnvelope(geometry);
     }
 
     @Override
diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/Zone.java
 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/Zone.java
index ee9d757a4b..7ad88636eb 100644
--- 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/Zone.java
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/Zone.java
@@ -27,8 +27,8 @@ import org.opengis.geometry.Envelope;
 import org.opengis.metadata.extent.GeographicExtent;
 import org.opengis.referencing.gazetteer.Location;
 import org.opengis.util.InternationalString;
-import org.apache.sis.geometries.Geometry;
-import org.apache.sis.geometry.GeneralEnvelope;
+import com.google.common.geometry.S2Polygon;
+import org.apache.sis.geometry.wrapper.s2.S2;
 import org.apache.sis.referencing.CRS;
 import org.apache.sis.referencing.CommonCRS;
 import org.apache.sis.storage.dggs.DiscreteGlobalGridSystems;
@@ -75,13 +75,20 @@ public interface Zone extends Location, Comparable<Zone> {
         return new SimpleInternationalString(getTextIdentifier().toString());
     }
 
+    /**
+     * Returns the CRS:84 envelope of the zone geographic extent.
+     * The envelope includes the edges bulging between vertices and the poles 
inside the zone.
+     * The lower longitude is greater than the upper one if the zone crosses 
the anti-meridian.
+     *
+     * @return envelope in CRS:84, the whole world if the zone has no 
geographic extent
+     */
     @Override
     default Envelope getEnvelope() {
-        final Geometry geometry = 
DiscreteGlobalGridSystems.toSISPolygon(getGeographicExtent());
-        if (geometry == null) {
+        final S2Polygon polygon = 
DiscreteGlobalGridSystems.toS2Polygon(getGeographicExtent());
+        if (polygon == null) {
             return 
CRS.getDomainOfValidity(CommonCRS.WGS84.normalizedGeographic());
         }
-        return new GeneralEnvelope(geometry.getEnvelope());
+        return S2.toEnvelope(polygon);
     }
 
     /**
diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/AbstractDggrsTest.java
 
b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/AbstractDggrsTest.java
index 60e112c6b3..bca30fae90 100644
--- 
a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/AbstractDggrsTest.java
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/AbstractDggrsTest.java
@@ -50,7 +50,6 @@ import org.apache.sis.storage.rs.CodeIterator;
 
 // Test dependencies
 import static org.junit.jupiter.api.Assertions.*;
-import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
 
@@ -173,7 +172,6 @@ public abstract class AbstractDggrsTest {
         }
     }
 
-    @Disabled //TODO does not pass for all H3 and Healpix cells yet
     @Test
     public void testSearchEnvelope() throws TransformException, 
IncommensurableException {
 
@@ -190,7 +188,6 @@ public abstract class AbstractDggrsTest {
 
     }
 
-    @Disabled //TODO does not pass for all A5 cells yet
     @Test
     public void testPickingInCellGeometry() throws TransformException, 
IncommensurableException {
 
diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/s2/S2Test.java
 
b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/s2/S2Test.java
index efcd471308..27a9ea4edc 100644
--- 
a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/s2/S2Test.java
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/s2/S2Test.java
@@ -55,8 +55,9 @@ public class S2Test extends AbstractDggrsTest {
         final Envelope envelope = zone.getEnvelope();
         assertEquals(-180, envelope.getMinimum(0), 0.0);
         assertEquals(-169.38034472384487, envelope.getMaximum(0), 0.0);
-        assertEquals(22.270575488008195, envelope.getMinimum(1), 0.0);
-        assertEquals(34.50852298766839, envelope.getMaximum(1), 0.0);
+        // S2 enlarges latitude bounds slightly against rounding errors.
+        assertEquals(22.270575488008195, envelope.getMinimum(1), 1e-9);
+        assertEquals(34.50852298766839, envelope.getMaximum(1), 1e-9);
 
     }
 }

Reply via email to