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 08a8a879bd7c468eeeb90c232873dd074a16cb64
Author: jsorel <[email protected]>
AuthorDate: Fri Nov 20 16:21:58 2026 +0100

    fix(DGGRS): fix iteration issue in AddImCodedCoverage
---
 .../main/org/apache/sis/referencing/rs/Code.java   | 51 +++++++++----
 .../org/apache/sis/referencing/rs/CompoundRS.java  |  2 +-
 .../rs/internal/shared/AddDimCodedCoverage.java    |  7 +-
 .../internal/shared/AddDimCodedCoverageTest.java   | 89 ++++++++++++++++++++++
 4 files changed, 131 insertions(+), 18 deletions(-)

diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/Code.java
 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/Code.java
index fb6d81bf0c..654c7837a9 100644
--- 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/Code.java
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/Code.java
@@ -16,7 +16,6 @@
  */
 package org.apache.sis.referencing.rs;
 
-import java.time.Instant;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
@@ -28,6 +27,7 @@ import org.opengis.referencing.crs.TemporalCRS;
 import org.opengis.referencing.operation.TransformException;
 import org.opengis.util.FactoryException;
 import org.apache.sis.geometry.GeneralDirectPosition;
+import org.apache.sis.referencing.IdentifiedObjects;
 import org.apache.sis.referencing.dggs.Zone;
 import org.apache.sis.referencing.dggs.DiscreteGlobalGridReferenceSystem;
 import org.apache.sis.referencing.crs.DefaultTemporalCRS;
@@ -147,28 +147,51 @@ public final class Code {
         return dp;
     }
 
+    /**
+     * Returns one line per single component of the reference system, with its 
ordinates and its name.
+     * Incomplete codes (unset ordinates, no or unknown reference system) are 
formatted too.
+     */
     @Override
     public String toString() {
         final StringBuilder sb = new StringBuilder("Code");
-
-        final ReferenceSystem rs = getReferenceSystem();
-        final List<ReferenceSystem> singleComponents = 
ReferenceSystems.getSingleComponents(rs, true);
-        for (int i = 0; i < singleComponents.size(); i++) {
-            final ReferenceSystem srs = singleComponents.get(i);
-            if (srs instanceof TemporalCRS tcrs) {
-                Instant instant = 
DefaultTemporalCRS.castOrCopy(tcrs).toInstant(((Number)ordinates[i]).doubleValue());
-                sb.append("\n  " + instant + " " + tcrs.getName().toString());
-            } else if (srs instanceof CoordinateReferenceSystem crs) {
-                sb.append("\n  " + ordinates[i] + " " + 
crs.getName().toString());
-            } else if (srs instanceof ReferencingByIdentifiers rbi) {
-                sb.append("\n  " + ordinates[i] + " " + 
rbi.getName().toString());
+        final List<ReferenceSystem> components = (rs != null) ? 
ReferenceSystems.getSingleComponents(rs, true) : List.of();
+        int offset = 0;
+        for (final ReferenceSystem component : components) {
+            if (offset >= ordinates.length) break;
+            final int end;
+            if (component instanceof CoordinateReferenceSystem crs) {
+                end = Math.min(offset + 
crs.getCoordinateSystem().getDimension(), ordinates.length);
+            } else if (component instanceof ReferencingByIdentifiers) {
+                end = offset + 1;
             } else {
-                throw new UnsupportedOperationException("todo");
+                end = ordinates.length;     // Unknown number of dimensions, 
take all remaining ordinates.
+            }
+            sb.append("\n ");
+            for (int i = offset; i < end; i++) {
+                sb.append(' ').append(format(component, ordinates[i]));
+            }
+            sb.append(' ').append(IdentifiedObjects.getName(component, null));
+            offset = end;
+        }
+        if (offset < ordinates.length) {
+            sb.append("\n ");
+            for (int i = offset; i < ordinates.length; i++) {
+                sb.append(' ').append(ordinates[i]);
             }
         }
         return sb.toString();
     }
 
+    /**
+     * Returns the ordinate as an instant if the component is temporal, or the 
ordinate unchanged otherwise.
+     */
+    private static Object format(final ReferenceSystem component, final Object 
ordinate) {
+        if (component instanceof TemporalCRS tcrs && ordinate instanceof 
Number n) {
+            return 
DefaultTemporalCRS.castOrCopy(tcrs).toInstant(n.doubleValue());
+        }
+        return ordinate;
+    }
+
     @Override
     public int hashCode() {
         int hash = 3;
diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/CompoundRS.java
 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/CompoundRS.java
index fa2855768b..e7950296bc 100644
--- 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/CompoundRS.java
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/CompoundRS.java
@@ -171,7 +171,7 @@ public final class CompoundRS implements ReferenceSystem {
         for (int i = 0, n = components.size(); i < n; i++) {
             if (i > 0)sb.append(',');
             final ReferenceSystem rs = components.get(i);
-            sb.append(rs.toString().replaceAll("\n", "    \n"));
+            sb.append(rs.toString().replace("\n", "\n    "));
         }
         sb.append(']');
         return sb.toString();
diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/rs/internal/shared/AddDimCodedCoverage.java
 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/rs/internal/shared/AddDimCodedCoverage.java
index 7f87ba9739..79ed4b6560 100644
--- 
a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/rs/internal/shared/AddDimCodedCoverage.java
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/rs/internal/shared/AddDimCodedCoverage.java
@@ -131,10 +131,11 @@ public final class AddDimCodedCoverage extends 
CodedCoverage {
 
         @Override
         public void moveTo(int[] zid) {
-            if (zid[zid.length] != 0) {
-                throw new IllegalArgumentException("Zone " + zid[0] +" is not 
part of this coverage");
+            final int last = zid.length - 1;
+            if (zid[last] != 0) {
+                throw new IllegalArgumentException("Position " + 
Arrays.toString(zid) + " is not part of this coverage");
             }
-            base.moveTo(zid);
+            base.moveTo(Arrays.copyOf(zid, last));
         }
 
         @Override
diff --git 
a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/storage/rs/internal/shared/AddDimCodedCoverageTest.java
 
b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/storage/rs/internal/shared/AddDimCodedCoverageTest.java
new file mode 100644
index 0000000000..69256f9e68
--- /dev/null
+++ 
b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/storage/rs/internal/shared/AddDimCodedCoverageTest.java
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.storage.rs.internal.shared;
+
+import java.util.List;
+import org.opengis.util.GenericName;
+import org.apache.sis.coverage.SampleDimension;
+import org.apache.sis.coverage.grid.GridExtent;
+import org.apache.sis.coverage.grid.GridGeometry;
+import org.apache.sis.coverage.grid.GridOrientation;
+import org.apache.sis.geometry.GeneralEnvelope;
+import org.apache.sis.referencing.CommonCRS;
+import org.apache.sis.referencing.dggs.DiscreteGlobalGridReferenceSystem;
+import org.apache.sis.referencing.dggs.Zone;
+import org.apache.sis.referencing.dggs.s2.S2Dggrs;
+import org.apache.sis.storage.dggs.DiscreteGlobalGridGeometry;
+import org.apache.sis.storage.rs.CodeIterator;
+import org.apache.sis.storage.rs.WritableCodeIterator;
+import org.apache.sis.util.iso.Names;
+
+// Test dependencies
+import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.Test;
+
+
+/**
+ * Tests {@link AddDimCodedCoverage}.
+ *
+ * @author Johann Sorel (Geomatys)
+ */
+public class AddDimCodedCoverageTest {
+
+    private static final GenericName NAME = Names.createLocalName(null, null, 
"added");
+
+    /**
+     * Creates a coverage on the S2 level 0 zones, with an added vertical 
dimension of one cell.
+     * The cell value is the zone index multiplied by 10.
+     */
+    private static AddDimCodedCoverage createCoverage() throws Exception {
+        final DiscreteGlobalGridReferenceSystem dggrs = new S2Dggrs();
+        final List<Object> zones = 
dggrs.getGridSystem().getHierarchy().getGrids().get(0).getZones().map(Zone::getIdentifier).toList();
+        final ArrayCodedCoverage source = new ArrayCodedCoverage(NAME,
+                DiscreteGlobalGridGeometry.unstructured(dggrs, zones, null),
+                new SampleDimension.Builder().setName("value").build());
+        try (WritableCodeIterator iterator = source.createWritableIterator()) {
+            while (iterator.next()) {
+                iterator.setSample(0, 10.0 * iterator.getPosition()[0]);
+            }
+        }
+        final GeneralEnvelope vertical = new 
GeneralEnvelope(CommonCRS.Vertical.ELLIPSOIDAL.crs());
+        vertical.setRange(0, 0, 100);
+        final GridGeometry slice = new GridGeometry(new GridExtent(null, 0, 0, 
true), vertical, GridOrientation.HOMOTHETY);
+        return new AddDimCodedCoverage(source, slice);
+    }
+
+    /**
+     * Tests that moving to a position, including the added dimension, reads 
the cell at that position.
+     */
+    @Test
+    public void testMoveTo() throws Exception {
+        final CodeIterator iterator = createCoverage().createIterator();
+        iterator.moveTo(new int[] {2, 0});
+        assertEquals(20.0, iterator.getSampleDouble(0));
+        assertArrayEquals(new int[] {2, 0}, iterator.getPosition());
+    }
+
+    /**
+     * Tests that a position outside the added dimension is rejected.
+     */
+    @Test
+    public void testMoveToOutsideAddedDimension() throws Exception {
+        final CodeIterator iterator = createCoverage().createIterator();
+        assertThrows(IllegalArgumentException.class, () -> iterator.moveTo(new 
int[] {2, 1}));
+    }
+}

Reply via email to