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 db30b7303fa3bc618f42c49ddabca5d68fda94a3 Author: jsorel <[email protected]> AuthorDate: Fri Nov 20 16:47:54 2026 +0100 fix(DGGRS): fix in transform concatenation with more then 2 reference systems --- .../dggs/internal/shared/AbstractZone.java | 2 +- .../rs/internal/shared/CodeOperations.java | 4 +- .../dggs/DiscreteGlobalGridCoverageProcessor.java | 2 +- .../storage/dggs/DiscreteGlobalGridTransform.java | 8 +- .../dggs/internal/shared/AbstractZoneTest.java | 54 ++++++++++++++ .../sis/referencing/rs/CodeOperationTest.java | 27 +++++++ .../DiscreteGlobalGridCoverageProcessorTest.java | 85 ++++++++++++++++++++++ .../dggs/DiscreteGlobalGridTransformTest.java | 72 ++++++++++++++++++ 8 files changed, 246 insertions(+), 8 deletions(-) diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractZone.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractZone.java index 2af8c6d659..e26a3d1a4f 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractZone.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractZone.java @@ -86,7 +86,7 @@ public abstract class AbstractZone<T extends DiscreteGlobalGridReferenceSystem> final int cdtz = cdt.getLocationType().getRefinementLevel(); final int relativeDepth = cdtz - getLocationType().getRefinementLevel(); if (relativeDepth <= 0 || relativeDepth > maxRelativeDepth) return false; - return searchInParents(cdt, this, maxRelativeDepth); + return searchInParents(cdt, this, relativeDepth); } private boolean searchInParents(Zone base, Zone searched, int toDepth) { diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/internal/shared/CodeOperations.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/internal/shared/CodeOperations.java index 9d28286bd3..68dc1ec119 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/internal/shared/CodeOperations.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/internal/shared/CodeOperations.java @@ -86,7 +86,7 @@ public final class CodeOperations { if (ops.length == 1) return ops[0]; CodeOperation cop = compound(ops[0], ops[1]); for (int i = 2; i < ops.length; i++) { - cop = compound(cop, ops[i], null, null); + cop = new Compound(cop, ops[i], null, null); } return cop; } @@ -202,7 +202,7 @@ public final class CodeOperations { @Override public CodeOperation inverse() throws NoninvertibleTransformException { - if (targetMapping.length != ReferenceSystems.getDimension(sourceRs)) { + if (targetMapping.length != componentOffsets.length) { throw new NoninvertibleTransformException(); } final int[] inv = new int[targetMapping.length]; diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridCoverageProcessor.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridCoverageProcessor.java index b12f0eca86..37d1ae59c2 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridCoverageProcessor.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridCoverageProcessor.java @@ -506,7 +506,7 @@ public final class DiscreteGlobalGridCoverageProcessor { if (counters[i][k] != null) { Entry<Object,AtomicLong> mostNumerous = null; for (Entry<Object,AtomicLong> entry : counters[i][k].entrySet()) { - if (mostNumerous == null || mostNumerous.getValue().get() > entry.getValue().get()) { + if (mostNumerous == null || mostNumerous.getValue().get() < entry.getValue().get()) { mostNumerous = entry; } } diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridTransform.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridTransform.java index 5dfee9a574..a97c12ab11 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridTransform.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridTransform.java @@ -377,16 +377,16 @@ public abstract class DiscreteGlobalGridTransform extends SubTransform { if (idx == offsets.length) { throw new TransformException("Position is outside grid : " + gp); } - tiles[idx].toAddress(new int[]{gp - (int)offsets[idx]}, location, offset); + location[offset] = tiles[idx].getZids()[gp - (int) offsets[idx]]; } @Override public void toGrid(Object[] location, int[] gridPosition, int offset) throws TransformException { final Object zid = location[offset]; - for (SubZoneTransform trs : tiles) { - Integer g = trs.toGridInternal(zid); + for (int i = 0; i < tiles.length; i++) { + final Integer g = tiles[i].toGridInternal(zid); if (g != null) { - gridPosition[offset] = g; + gridPosition[offset] = Math.toIntExact(offsets[i] + g); return; } } diff --git a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/internal/shared/AbstractZoneTest.java b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/internal/shared/AbstractZoneTest.java new file mode 100644 index 0000000000..6007e499ae --- /dev/null +++ b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/internal/shared/AbstractZoneTest.java @@ -0,0 +1,54 @@ +/* + * 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.referencing.dggs.internal.shared; + +import org.apache.sis.referencing.dggs.DiscreteGlobalGridReferenceSystem; +import org.apache.sis.referencing.dggs.Zone; +import org.apache.sis.referencing.dggs.s2.S2Dggrs; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + + +/** + * Tests {@link AbstractZone} through S2 zones. + * + * @author Johann Sorel (Geomatys) + */ +public class AbstractZoneTest { + + /** + * Tests {@link AbstractZone#isAncestorOf(Object, int)} and {@link AbstractZone#isDescendantOf(Object, int)} + * with a maximum depth equal to or greater than the actual depth. + */ + @Test + public void testAncestorAndDescendant() throws Exception { + final DiscreteGlobalGridReferenceSystem dggrs = new S2Dggrs(); + final Zone face = dggrs.getGridSystem().getHierarchy().getGrids().get(0).getZones().findFirst().orElseThrow(); + final Zone child = face.getChildren().iterator().next(); + final Zone grandChild = child.getChildren().iterator().next(); + + assertTrue (face.isAncestorOf(child, 1)); + assertTrue (face.isAncestorOf(child, 5)); + assertFalse(face.isAncestorOf(grandChild, 1)); + assertTrue (face.isAncestorOf(grandChild, 2)); + assertTrue (face.isAncestorOf(grandChild, 5)); + assertFalse(child.isAncestorOf(face, 5)); + assertTrue (grandChild.isDescendantOf(face, 5)); + } +} diff --git a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/rs/CodeOperationTest.java b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/rs/CodeOperationTest.java index cc51538bea..a88bb2e032 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/rs/CodeOperationTest.java +++ b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/rs/CodeOperationTest.java @@ -275,4 +275,31 @@ public class CodeOperationTest { assertEquals("87z9y8fhdbff", code.getOrdinate(0)); } } + + /** + * Tests an operation found by {@link ReferenceSystems#findOperation} between reference systems + * of three components, which requires a compound of three operations. + */ + @Test + public void compoundOfThreeComponents() throws FactoryException, TransformException { + final ReferenceSystem vertical = CommonCRS.Vertical.MEAN_SEA_LEVEL.crs(); + final ReferenceSystem source = ReferenceSystems.createCompound(MGRS, TIME_JAVA, vertical); + final ReferenceSystem target = ReferenceSystems.createCompound(TIME_JAVA, vertical, MGRS); + final CodeOperation operation = ReferenceSystems.findOperation(source, target, null); + final Code code = operation.transform(new Code(source, "4Q FJ 12 67", 1000.0, 50.0), null); + assertArrayEquals(new Object[]{1000.0, 50.0, "4Q FJ 12 67"}, code.getOrdinates()); + } + + /** + * Tests the inverse of {@link CodeOperations.Reorder} swapping a two-dimensional component with a one-dimensional one. + */ + @Test + public void reorderInverseMultiDimensionalComponent() throws TransformException { + final ReferenceSystem geographic = CommonCRS.WGS84.normalizedGeographic(); + final ReferenceSystem source = ReferenceSystems.createCompound(geographic, TIME_JAVA); + final ReferenceSystem target = ReferenceSystems.createCompound(TIME_JAVA, geographic); + final CodeOperation inverse = CodeOperations.reorder(source, target, new int[]{1, 0}).inverse(); + final Code code = inverse.transform(new Code(target, 1000.0, 10.0, 20.0), null); + assertArrayEquals(new Object[]{10.0, 20.0, 1000.0}, code.getOrdinates()); + } } diff --git a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/storage/dggs/DiscreteGlobalGridCoverageProcessorTest.java b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/storage/dggs/DiscreteGlobalGridCoverageProcessorTest.java new file mode 100644 index 0000000000..965974bcb1 --- /dev/null +++ b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/storage/dggs/DiscreteGlobalGridCoverageProcessorTest.java @@ -0,0 +1,85 @@ +/* + * 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.dggs; + +import java.util.ArrayList; +import java.util.List; +import org.locationtech.jts.geom.CoordinateXY; +import org.locationtech.jts.geom.GeometryFactory; +import org.locationtech.jts.geom.Point; +import org.opengis.feature.Feature; +import org.opengis.feature.FeatureType; +import org.apache.sis.coverage.SampleDimension; +import org.apache.sis.feature.builder.AttributeRole; +import org.apache.sis.feature.builder.FeatureTypeBuilder; +import org.apache.sis.geometry.DirectPosition2D; +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.MemoryFeatureSet; +import org.apache.sis.storage.rs.CodeIterator; +import org.apache.sis.storage.rs.CodedCoverage; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + + +/** + * Tests {@link DiscreteGlobalGridCoverageProcessor}. + * + * @author Johann Sorel (Geomatys) + */ +public class DiscreteGlobalGridCoverageProcessorTest { + + /** + * Tests that resampling a categorized feature property keeps the most frequent category of each zone. + */ + @Test + public void testResampleFeatureSetCategory() throws Exception { + final FeatureTypeBuilder ftb = new FeatureTypeBuilder(); + ftb.setName("parcels"); + ftb.addAttribute(Point.class).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY); + ftb.addAttribute(Integer.class).setName("landuse").setValidValues(1, 2, 3); + final FeatureType type = ftb.build(); + + final GeometryFactory gf = new GeometryFactory(); + final double[][] coordinates = {{19, 19}, {20, 20}, {21, 21}}; + final int[] categories = {1, 2, 2}; + final List<Feature> features = new ArrayList<>(); + for (int i = 0; i < categories.length; i++) { + final Feature feature = type.newInstance(); + feature.setPropertyValue("geom", gf.createPoint(new CoordinateXY(coordinates[i][0], coordinates[i][1]))); + feature.setPropertyValue("landuse", categories[i]); + features.add(feature); + } + + final DiscreteGlobalGridReferenceSystem dggrs = new S2Dggrs(); + final Zone zone = dggrs.getGridSystem().getHierarchy().getGrids().get(1) + .getZone(new DirectPosition2D(CommonCRS.WGS84.normalizedGeographic(), 20, 20)); + final DiscreteGlobalGridGeometry geometry = DiscreteGlobalGridGeometry.unstructured(dggrs, List.of(zone.getIdentifier()), null); + + final CodedCoverage coverage = new DiscreteGlobalGridCoverageProcessor().resample( + new MemoryFeatureSet(null, type, features), geometry, + List.of(new SampleDimension.Builder().setName("landuse").build())); + + final CodeIterator iterator = coverage.createIterator(); + iterator.moveTo(new int[] {0}); + assertEquals(2.0, iterator.getSampleDouble(0)); + } +} diff --git a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/storage/dggs/DiscreteGlobalGridTransformTest.java b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/storage/dggs/DiscreteGlobalGridTransformTest.java new file mode 100644 index 0000000000..10abe7ea75 --- /dev/null +++ b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/storage/dggs/DiscreteGlobalGridTransformTest.java @@ -0,0 +1,72 @@ +/* + * 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.dggs; + +import java.util.List; +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.referencing.rs.Code; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + + +/** + * Tests {@link DiscreteGlobalGridTransform}. + * + * @author Johann Sorel (Geomatys) + */ +public class DiscreteGlobalGridTransformTest { + + private final DiscreteGlobalGridReferenceSystem dggrs = new S2Dggrs(); + + /** + * Creates a transform on the children of the first two S2 faces, 4 zones per face. + */ + private DiscreteGlobalGridTransform createSubZones() throws Exception { + final List<Zone> faces = dggrs.getGridSystem().getHierarchy().getGrids().get(0).getZones().limit(2).toList(); + final Object[] bases = {faces.get(0).getIdentifier(), faces.get(1).getIdentifier()}; + return DiscreteGlobalGridTransform.subZones(dggrs, bases, 1); + } + + /** + * Tests that {@code toGrid} returns the position in the whole grid, including for the zones of the second tile. + */ + @Test + public void testSubZonesToGrid() throws Exception { + final DiscreteGlobalGridTransform transform = createSubZones(); + final List<Object> zoneIds = transform.getZoneIds(); + assertEquals(8, zoneIds.size()); + for (int i = 0; i < 8; i++) { + assertEquals(i, transform.toGrid(new Code(dggrs, zoneIds.get(i)))[0], "Grid position"); + } + } + + /** + * Tests {@code toAddress} when the zone identifier is not the first ordinate. + */ + @Test + public void testSubZonesToAddressAtOffset() throws Exception { + final DiscreteGlobalGridTransform transform = createSubZones(); + final Object[] location = new Object[2]; + transform.toAddress(new int[] {99, 5}, location, 1); + assertNull(location[0]); + assertEquals(transform.getZoneIds().get(5), location[1]); + } +}
