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

commit 8de96a1e9258dd04693809cdda5ccefeed5568d3
Author: jsorel <[email protected]>
AuthorDate: Mon Nov 16 11:48:29 2026 +0100

    fix(DGGRS): fix one level missing in refinement levels, fix id transform 
exception
---
 .../dggs/DiscreteGlobalGridReferenceSystems.java   |  2 +-
 .../sis/referencing/dggs/RefinementLevel.java      |  4 +--
 .../shared/AbstractDiscreteGlobalGrid.java         | 27 ++++++++++-------
 .../sis/referencing/dggs/AbstractDggrsTest.java    | 34 ++++++++++++++++++----
 4 files changed, 47 insertions(+), 20 deletions(-)

diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/DiscreteGlobalGridReferenceSystems.java
 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/DiscreteGlobalGridReferenceSystems.java
index 7aaefc8f16..7c75101eee 100644
--- 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/DiscreteGlobalGridReferenceSystems.java
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/DiscreteGlobalGridReferenceSystems.java
@@ -99,7 +99,7 @@ public final class DiscreteGlobalGridReferenceSystems {
      */
     public static DiscreteGlobalGridReferenceSystem forCode(String code) 
throws FactoryException, NoSuchIdentifierException{
         if (code.startsWith("http")) {
-            code = code.substring(code.lastIndexOf('/'));
+            code = code.substring(code.lastIndexOf('/') + 1);
         }
         if (code.startsWith("[ogc-dggrs:") && code.endsWith("]")) {
             code = code.substring(11, code.length() - 1);
diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/RefinementLevel.java
 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/RefinementLevel.java
index 942f67fc69..d5c7bd15b9 100644
--- 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/RefinementLevel.java
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/RefinementLevel.java
@@ -91,8 +91,8 @@ public final class RefinementLevel implements LocationType {
 
     @Override
     public Collection<? extends LocationType> getChildren() {
-        final int maxLevel = 
dggrs.getGridSystem().getHierarchy().getGrids().size();
-        if (level >= maxLevel) return Collections.EMPTY_LIST;
+        final int gridCount = 
dggrs.getGridSystem().getHierarchy().getGrids().size();
+        if (level >= gridCount - 1) return Collections.EMPTY_LIST;
         return List.of(new RefinementLevel(dggrs, level+1));
     }
 
diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGrid.java
 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGrid.java
index 1ab74719aa..ea0d8f4357 100644
--- 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGrid.java
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGrid.java
@@ -59,8 +59,9 @@ public abstract class AbstractDiscreteGlobalGrid<T extends 
AbstractDiscreteGloba
     }
 
     /**
-     * Create the long identifer to/from transforms once.
-     * @return true if suceeded
+     * Creates the long identifier to/from CRS transforms once.
+     *
+     * @return {@code true} if long identifiers are supported and the 
transforms have been created.
      */
     private synchronized boolean initTransform() {
         if (crs != null) return crsToId != null;
@@ -82,8 +83,7 @@ public abstract class AbstractDiscreteGlobalGrid<T extends 
AbstractDiscreteGloba
 
             @Override
             public Matrix transform(double[] source, int soffset, double[] 
target, int toffset, boolean bln) throws TransformException {
-                final long zone = getZoneLongIdentifier(source, soffset);
-                target[toffset] = Double.longBitsToDouble(zone);
+                getZonePosition(Double.doubleToRawLongBits(source[soffset]), 
target, toffset);
                 return null;
             }
         };
@@ -100,7 +100,8 @@ public abstract class AbstractDiscreteGlobalGrid<T extends 
AbstractDiscreteGloba
 
             @Override
             public Matrix transform(double[] source, int soffset, double[] 
target, int toffset, boolean bln) throws TransformException {
-                getZonePosition(Double.doubleToRawLongBits(source[soffset]), 
target, toffset);
+                final long zone = getZoneLongIdentifier(source, soffset);
+                target[toffset] = Double.longBitsToDouble(zone);
                 return null;
             }
         };
@@ -110,24 +111,28 @@ public abstract class AbstractDiscreteGlobalGrid<T 
extends AbstractDiscreteGloba
 
     @Override
     public final MathTransform createTransformToCrs() throws 
UnsupportedOperationException {
-        if (initTransform()) throw new UnsupportedOperationException();
+        if (!initTransform()) throw new UnsupportedOperationException();
         return idToCrs;
     }
 
     @Override
     public final MathTransform createTransformToIdentifiers() throws 
UnsupportedOperationException {
-        if (initTransform()) throw new UnsupportedOperationException();
+        if (!initTransform()) throw new UnsupportedOperationException();
         return crsToId;
     }
 
     /**
-     * Convert on coordinate in base CRS to zone long identifier.
+     * Converts a coordinate in the base CRS to the identifier of the zone 
containing it.
+     *
+     * @throws TransformException if the coordinate cannot be converted or is 
outside the grid.
      */
-    protected abstract long getZoneLongIdentifier(double[] source, int 
soffset);
+    protected abstract long getZoneLongIdentifier(double[] source, int 
soffset) throws TransformException;
 
     /**
-     * Convert zone long identifier to coordinate in base CRS.
+     * Converts a zone identifier to the coordinate of the zone representative 
position in the base CRS.
+     *
+     * @throws TransformException if the position cannot be computed.
      */
-    protected abstract void getZonePosition(long zoneId, double[] target, int 
toffset);
+    protected abstract void getZonePosition(long zoneId, double[] target, int 
toffset) throws TransformException;
 
 }
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 0f90aeac49..4714bb2cb1 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
@@ -25,6 +25,7 @@ import javax.measure.IncommensurableException;
 import org.opengis.geometry.DirectPosition;
 import org.opengis.geometry.Envelope;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.opengis.referencing.operation.MathTransform;
 import org.opengis.referencing.operation.TransformException;
 import org.opengis.util.FactoryException;
 import org.apache.sis.coverage.BandedCoverage;
@@ -35,6 +36,7 @@ import org.apache.sis.coverage.grid.GridOrientation;
 import org.apache.sis.geometries.DataPoints;
 import org.apache.sis.geometries.curve.LinearRing;
 import org.apache.sis.geometries.surface.Polygon;
+import org.apache.sis.geometry.DirectPosition2D;
 import org.apache.sis.maths.Vector2D;
 import org.apache.sis.maths.NDArrays;
 import org.apache.sis.maths.Vector;
@@ -149,6 +151,28 @@ public abstract class AbstractDggrsTest {
 
     }
 
+    /**
+     * Tests the conversions between zone positions and long identifiers.
+     */
+    @Test
+    public void testTransforms() throws TransformException {
+        final DiscreteGlobalGrid grid = 
dggrs.getGridSystem().getHierarchy().getGrids().get(2);
+        final MathTransform toIdentifiers = 
grid.createTransformToIdentifiers();
+        final MathTransform toCrs = grid.createTransformToCrs();
+        final List<Zone> zones = grid.getZones().limit(20).toList();
+        assertFalse(zones.isEmpty());
+        for (Zone zone : zones) {
+            final DirectPosition position = zone.getPosition();
+            final double[] identifier = new double[1];
+            toIdentifiers.transform(position.getCoordinates(), 0, identifier, 
0, 1);
+            assertEquals(zone.getLongIdentifier(), 
Double.doubleToRawLongBits(identifier[0]));
+
+            final double[] back = new double[position.getDimension()];
+            toCrs.transform(identifier, 0, back, 0, 1);
+            assertEquals(zone, grid.getZone(new 
DirectPosition2D(position.getCoordinateReferenceSystem(), back[0], back[1])));
+        }
+    }
+
     @Disabled //TODO does not pass for all H3 and Healpix cells yet
     @Test
     public void testSearchEnvelope() throws TransformException, 
IncommensurableException {
@@ -180,7 +204,6 @@ public abstract class AbstractDggrsTest {
 
             //check the coder can find the zone using points inside the cell 
polygon
             for (Zone z : candidates) {
-                System.out.println(z.getGeographicIdentifier());
                 
coder.setPrecisionLevel(z.getLocationType().getRefinementLevel());
 
                 final Polygon geometry = (Polygon) 
DiscreteGlobalGridSystems.toSISPolygon(z.getGeographicExtent());
@@ -200,11 +223,10 @@ public abstract class AbstractDggrsTest {
 
                     final String candidateInsideHash = 
coder.encode(candidateInside.asDirectPostion(null));
                     final String candidateOutsideHash = 
coder.encode(candidateOutside.asDirectPostion(null));
-                    System.out.println(z.getGeographicExtent().toString());
-                    System.out.println(candidateInside + " " 
+candidateOutside);
-                    System.out.println(candidateInsideHash + " " 
+candidateOutsideHash);
-                    assertEquals(z.getGeographicIdentifier().toString(), 
candidateInsideHash);
-                    assertNotEquals(z.getGeographicIdentifier().toString(), 
candidateOutsideHash);
+                    assertEquals(z.getGeographicIdentifier().toString(), 
candidateInsideHash,
+                            () -> "Point " + candidateInside + " inside " + 
z.getGeographicExtent());
+                    assertNotEquals(z.getGeographicIdentifier().toString(), 
candidateOutsideHash,
+                            () -> "Point " + candidateOutside + " outside " + 
z.getGeographicExtent());
                 }
             }
         }

Reply via email to