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 8aa7eff1e95e6eaad8b49f2645f78fbe41b74aa5 Author: jsorel <[email protected]> AuthorDate: Mon Nov 30 15:55:42 2026 +0100 feat(Geometry): add normalized method and tests --- .../main/org/apache/sis/geometries/Geometry.java | 26 +++ .../geometries/operation/GeometryProcessor.java | 12 ++ .../apache/sis/geometries/operation/Normalize.java | 207 +++++++++++++++++++++ .../sis/geometries/operation/NormalizeTest.adoc | 153 +++++++++++++++ .../sis/geometries/operation/NormalizeTest.java | 67 +++++++ 5 files changed, 465 insertions(+) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java index e4fb730c89..679859c9b9 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java @@ -1205,4 +1205,30 @@ public sealed interface Geometry return new GeometryProcessor().tessellate(this, spacing, offset); } + /** + * Returns this geometry in normal form, where the parts which have no intrinsic order are in a canonical order. + * Two geometries made of the same parts and vertices then become structurally equal. + * + * <p>Constraints:</p> + * <ul> + * <li>Each closed ring starts at its smallest vertex, comparing coordinates in axis order. + * Its closing vertex repeats the new first vertex.</li> + * <li>The holes of a polygon, the patches of a polyhedral surface, the faces and the interior shells + * of a polyhedron, the shells of a solid and the members of a collection are sorted by geometry + * type, then by the positions of their vertices.</li> + * <li>Directions are never changed, and the curves of a compound curve keep their order.</li> + * <li>Vertex attributes stay with their vertices.</li> + * <li>Mesh primitives and the geometries with nothing to reorder are returned as is.</li> + * </ul> + * + * <p>Difference with ISO 19107: this operation has no equivalent in the standard.</p> + * + * @return this geometry in normal form. + * + * @see GeometryProcessor#normalize(org.apache.sis.geometries.Geometry) + */ + default Geometry normalized() { + return new GeometryProcessor().normalize(this); + } + } diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java index afd492bf39..a788922fd5 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java @@ -903,6 +903,18 @@ public final class GeometryProcessor { return GeometryFactory.DEFAULT.createGeometryCollection(elementType, geom.getCoordinateReferenceSystem(), elements); } + /** + * Returns the given geometry in normal form, where the parts which have no intrinsic order are in a canonical order. + * + * @param geom the geometry to normalize. + * @return the geometry in normal form. + * + * @see Geometry#normalized() + */ + public Geometry normalize(Geometry geom) { + return Normalize.normalize(geom); + } + /** * Returns a geometric object that represents a transformed version of the geometry. * diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Normalize.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Normalize.java new file mode 100644 index 0000000000..85a41c7986 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Normalize.java @@ -0,0 +1,207 @@ +/* + * 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.geometries.operation; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.stream.IntStream; +import java.util.stream.Stream; +import org.apache.sis.geometries.Curve; +import org.apache.sis.geometries.DataPoints; +import org.apache.sis.geometries.Geometry; +import org.apache.sis.geometries.GeometryCollection; +import org.apache.sis.geometries.GeometryFactory; +import org.apache.sis.geometries.Prism; +import org.apache.sis.geometries.Solid; +import org.apache.sis.geometries.Surface; +import org.apache.sis.geometries.curve.LineString; +import org.apache.sis.geometries.curve.LinearRing; +import org.apache.sis.geometries.mesh.MeshPrimitive; +import org.apache.sis.geometries.solid.Polyhedron; +import org.apache.sis.geometries.surface.CurvePolygon; +import org.apache.sis.geometries.surface.Polygon; +import org.apache.sis.geometries.surface.PolyhedralSurface; +import org.apache.sis.geometries.surface.TIN; +import org.apache.sis.geometries.surface.Triangle; +import org.apache.sis.maths.Vector; + + +/** + * Puts geometries in normal form, where the parts which have no intrinsic order are in a canonical order. + * Two geometries made of the same parts and vertices then become structurally equal. + * + * <ul> + * <li>Each closed ring starts at its smallest vertex, comparing coordinates in axis order. + * Its closing vertex repeats the new first vertex.</li> + * <li>The holes of a polygon, the patches of a polyhedral surface, the faces and the interior shells + * of a polyhedron, the shells of a solid and the members of a collection are sorted by geometry + * type, then by the positions of their vertices.</li> + * <li>Directions are never changed, and the curves of a compound curve keep their order.</li> + * <li>Vertex attributes stay with their vertices.</li> + * <li>Mesh primitives and the geometries with nothing to reorder are returned as is.</li> + * </ul> + * + * @author Johann Sorel (Geomatys) + */ +final class Normalize { + /** + * Orders geometries by type, then by the positions of their vertices, then by number of vertices. + */ + private static final Comparator<Geometry> ORDER = + Comparator.comparing(Geometry::getGeometryType).thenComparing(Geometry::getDataPoints, Normalize::compare); + + /** + * Do not allow instantiation of this class. + */ + private Normalize() { + } + + /** + * Returns the given geometry in normal form. + */ + static Geometry normalize(final Geometry geometry) { + if (geometry.isEmpty()) { + return geometry; + } + final GeometryFactory f = GeometryFactory.DEFAULT; + return switch (geometry) { + case MeshPrimitive g -> g; + case LinearRing g -> ring(g); + case Polygon g -> polygon(g); + case CurvePolygon g -> f.createCurvePolygon(curveRing(g.getExteriorRing()), + g.getInteriorRings().stream().map(Normalize::curveRing).sorted(ORDER).toList()); + case TIN g -> f.createTIN(g.getCoordinateReferenceSystem(), patches(g).toArray(Triangle[]::new)); + case PolyhedralSurface<?> g -> f.createPolyhedralSurface(g.getCoordinateReferenceSystem(), patches(g).toArray(Polygon[]::new)); + case Polyhedron g -> f.createPolyhedron(shell(g.getExteriorShell()), + g.getInteriorShells().stream().map(Normalize::shell).sorted(ORDER).toList()); + case Solid g -> f.createSolid(members(g.getBoundary()).map(Surface.class::cast).toList()); + case Prism g -> f.createPrism(normalize(g.getBase()), g.getExtrusionRange(), g.getExtrusionCrs()); + case GeometryCollection<?> g -> f.createGeometryCollection(g.getElementType(), g.getCoordinateReferenceSystem(), + members(g).toArray(Geometry[]::new)); + default -> geometry; + }; + } + + /** + * Returns a polygon whose rings start at their smallest vertex, with sorted holes. + */ + private static Polygon polygon(final Polygon polygon) { + if (polygon.isEmpty()) { + return polygon; + } + final LinearRing exterior = ring(polygon.getExteriorRing()); + if (polygon instanceof Triangle) { + return GeometryFactory.DEFAULT.createTriangle(exterior); + } + return GeometryFactory.DEFAULT.createPolygon(exterior, + polygon.getInteriorRings().stream().map(Normalize::ring).sorted(ORDER).toList()); + } + + /** + * Returns a ring starting at its smallest vertex. + */ + private static LinearRing ring(final LinearRing ring) { + return GeometryFactory.DEFAULT.createLinearRing(rotate(ring.getDataPoints())); + } + + /** + * Returns a ring of a curve polygon starting at its smallest vertex, if it is a line string. + */ + private static Curve curveRing(final Curve ring) { + if (ring instanceof LinearRing r) { + return ring(r); + } + if (ring instanceof LineString r && !(ring instanceof MeshPrimitive)) { + return GeometryFactory.DEFAULT.createLineString(rotate(r.getDataPoints())); + } + return ring; + } + + /** + * Returns the normalized and sorted patches of a polyhedral surface. + */ + private static Stream<Polygon> patches(final PolyhedralSurface<?> surface) { + return IntStream.range(0, surface.getNumPatches()).mapToObj(surface::getPatchN).map(Normalize::polygon).sorted(ORDER); + } + + /** + * Returns a shell of a polyhedron with normalized and sorted faces. + */ + private static GeometryCollection<Polygon> shell(final GeometryCollection<Polygon> shell) { + return GeometryFactory.DEFAULT.createMultiPolygon(shell.getCoordinateReferenceSystem(), + IntStream.range(0, shell.getNumGeometries()).mapToObj(shell::getGeometryN) + .map(Normalize::polygon).sorted(ORDER).toArray(Polygon[]::new)); + } + + /** + * Returns the normalized and sorted members of a collection. + */ + private static Stream<Geometry> members(final GeometryCollection<?> collection) { + return IntStream.range(0, collection.getNumGeometries()).mapToObj(collection::getGeometryN) + .map(Normalize::normalize).sorted(ORDER); + } + + /** + * Returns the vertices of a closed ring starting at its smallest vertex, the closing vertex repeating it. + * The vertices of a ring which is not closed are returned unchanged. + */ + private static DataPoints rotate(final DataPoints points) { + final int n = points.size() - 1; + if (n < 2 || !Arrays.equals(points.getPosition(0).toArrayDouble(), points.getPosition(n).toArrayDouble())) { + return points; + } + int start = 0; + for (int i = 1; i < n; i++) { + if (compare(points.getPosition(i), points.getPosition(start)) < 0) { + start = i; + } + } + if (start == 0) { + return points; + } + final int first = start; + return points.subset(IntStream.rangeClosed(0, n).map((i) -> (first + i) % n).toArray()); + } + + /** + * Compares the positions of two sequences of vertices in lexicographic order, then their lengths. + */ + private static int compare(final DataPoints a, final DataPoints b) { + final int n = Math.min(a.size(), b.size()); + for (int i = 0; i < n; i++) { + final int c = compare(a.getPosition(i), b.getPosition(i)); + if (c != 0) { + return c; + } + } + return Integer.compare(a.size(), b.size()); + } + + /** + * Compares two positions in lexicographic order of their coordinates, then their dimensions. + */ + private static int compare(final Vector<?> a, final Vector<?> b) { + final int n = Math.min(a.getDimension(), b.getDimension()); + for (int i = 0; i < n; i++) { + final int c = Double.compare(a.get(i), b.get(i)); + if (c != 0) { + return c; + } + } + return Integer.compare(a.getDimension(), b.getDimension()); + } +} diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/NormalizeTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/NormalizeTest.adoc new file mode 100644 index 0000000000..904bddd510 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/NormalizeTest.adoc @@ -0,0 +1,153 @@ += Normalize +:description: Test cases of GeometryProcessor.normalize(Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the input and the expected result +in Well-Known Text 3. + +The normal form puts the parts which have no intrinsic order in a canonical order: +closed rings start at their smallest vertex, and holes, patches, faces, shells and collection +members are sorted by geometry type, then by vertices. Directions are never changed. + +// ─── Geometries with nothing to reorder ────────────────────────────────────── + +== point + +A point has nothing to reorder. + +input:: `G_POINT [1 2]` +result:: `G_POINT [1 2]` + +== empty + +An empty geometry has nothing to reorder. + +input:: `G_GEOMETRY EMPTY` +result:: `G_GEOMETRY EMPTY` + +== linestring-direction-kept + +An open line string keeps its direction, even when it ends at a smaller vertex than it starts. + +input:: `G_LINESTRING [10 0, 0 0]` +result:: `G_LINESTRING [10 0, 0 0]` + +== compoundcurve-order-kept + +The curves of a compound curve are joined end to end, so they keep their order. + +input:: `G_COMPOUNDCURVE (G_CIRCULARSTRING [0 0, 1 1, 2 0], G_LINESTRING [2 0, 3 1])` +result:: `G_COMPOUNDCURVE (G_CIRCULARSTRING [0 0, 1 1, 2 0], G_LINESTRING [2 0, 3 1])` + +== mesh-unchanged + +Mesh primitives are returned as is. + +input:: `G_TRIANGLES [1 1, 0 0, 1 0]` +result:: `G_TRIANGLES [1 1, 0 0, 1 0]` + +// ─── Rings ─────────────────────────────────────────────────────────────────── + +== ring-rotated + +A closed ring starts at its smallest vertex, keeping its direction. + +input:: `G_LINEARRING [10 10, 0 10, 0 0, 10 0, 10 10]` +result:: `G_LINEARRING [0 0, 10 0, 10 10, 0 10, 0 0]` + +== ring-attributes + +The attributes of the vertices move with them when a ring is rotated. + +input:: `G_LINEARRING <P:2, M:1> [10 10, 0 10, 0 0, 10 0, 10 10] [1, 2, 3, 4, 1]` +result:: `G_LINEARRING <P:2, M:1> [0 0, 10 0, 10 10, 0 10, 0 0] [3, 4, 1, 2, 3]` + +== ring-not-closed + +A ring which is not closed is left unchanged, since rotating it would change its shape. + +input:: `G_LINEARRING [1 1, 0 0, 2 0]` +result:: `G_LINEARRING [1 1, 0 0, 2 0]` + +// ─── Surfaces ──────────────────────────────────────────────────────────────── + +== polygon-direction-kept + +A clockwise exterior ring stays clockwise: only its first vertex changes. + +input:: `G_POLYGON ([0 10, 10 10, 10 0, 0 0, 0 10])` +result:: `G_POLYGON ([0 0, 0 10, 10 10, 10 0, 0 0])` + +== polygon-holes-sorted + +Each ring of a polygon starts at its smallest vertex, and the holes are sorted by vertices. + +input:: `G_POLYGON ([10 10, 0 10, 0 0, 10 0, 10 10], [8 8, 8 6, 6 6, 6 8, 8 8], [2 4, 4 4, 4 2, 2 2, 2 4])` +result:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0], [2 2, 2 4, 4 4, 4 2, 2 2], [6 6, 6 8, 8 8, 8 6, 6 6])` + +== triangle-rotated + +The ring of a triangle starts at its smallest vertex. + +input:: `G_TRIANGLE ([4 0, 0 4, 0 0, 4 0])` +result:: `G_TRIANGLE ([0 0, 4 0, 0 4, 0 0])` + +== curvepolygon-ring-rotated + +A line string ring of a curve polygon starts at its smallest vertex, and a circular string ring is kept. + +input:: `G_CURVEPOLYGON (G_CIRCULARSTRING [-2 0, -1 -1, 0 0, 1 -1, 2 0, 0 2, -2 0], G_LINESTRING [1 0, 0 1, -1 0, 0 0.5, 1 0])` +result:: `G_CURVEPOLYGON (G_CIRCULARSTRING [-2 0, -1 -1, 0 0, 1 -1, 2 0, 0 2, -2 0], G_LINESTRING [-1 0, 0 0.5, 1 0, 0 1, -1 0])` + +== tin-patches-sorted + +The triangles of a TIN start at their smallest vertex and are sorted by vertices. + +input:: `G_TIN <P:3> (([1 1 0, 0 1 0, 1 0 0, 1 1 0]), ([0 1 0, 0 0 0, 1 0 0, 0 1 0]))` +result:: `G_TIN <P:3> (([0 0 0, 1 0 0, 0 1 0, 0 0 0]), ([0 1 0, 1 0 0, 1 1 0, 0 1 0]))` + +// ─── Solids ────────────────────────────────────────────────────────────────── + +== polyhedron-faces-sorted + +The faces of a polyhedron shell start at their smallest vertex and are sorted by vertices. + +input:: `G_POLYHEDRON <P:3> ((([1 0 0, 0 0 1, 0 0 0, 1 0 0]), ([0 0 0, 0 1 0, 1 0 0, 0 0 0])))` +result:: `G_POLYHEDRON <P:3> ((([0 0 0, 0 1 0, 1 0 0, 0 0 0]), ([0 0 0, 1 0 0, 0 0 1, 0 0 0])))` + +== solid-shells-sorted + +The shells of a solid are normalized and sorted by vertices. + +input:: `G_SOLID (G_TIN <P:3> (([6 5 5, 5 6 5, 5 5 5, 6 5 5])), G_TIN <P:3> (([0 0 0, 1 0 0, 0 1 0, 0 0 0])))` +result:: `G_SOLID (G_TIN <P:3> (([0 0 0, 1 0 0, 0 1 0, 0 0 0])), G_TIN <P:3> (([5 5 5, 6 5 5, 5 6 5, 5 5 5])))` + +== prism-base-normalized + +The base geometry of a prism is normalized. + +input:: `G_PRISM <;RANGE:2> (G_POLYGON ([10 10, 0 10, 0 0, 10 0, 10 10])) [0 12]` +result:: `G_PRISM <;RANGE:2> (G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])) [0 12]` + +// ─── Collections ───────────────────────────────────────────────────────────── + +== collection-sorted + +The members of a collection are sorted by geometry type, then by vertices. + +input:: `G_COLLECTION (G_LINESTRING [0 0, 1 1], G_POINT [5 5], G_POINT [1 2])` +result:: `G_COLLECTION (G_POINT [1 2], G_POINT [5 5], G_LINESTRING [0 0, 1 1])` + +== multipoint-sorted + +The points of a collection of points are sorted, with their attributes. + +input:: `G_COLLECTION POINT <P:2, M:1> [5 5, 1 2, 1 1] [10, 20, 30]` +result:: `G_COLLECTION POINT <P:2, M:1> [1 1, 1 2, 5 5] [30, 20, 10]` + +== multipolygon-sorted + +The polygons of a collection are normalized, then sorted by vertices. + +input:: `G_COLLECTION POLYGON (G_POLYGON ([30 30, 20 30, 20 20, 30 20, 30 30]), G_POLYGON ([10 10, 0 10, 0 0, 10 0, 10 10]))` +result:: `G_COLLECTION POLYGON (G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0]), G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20]))` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/NormalizeTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/NormalizeTest.java new file mode 100644 index 0000000000..fd6ff10317 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/NormalizeTest.java @@ -0,0 +1,67 @@ +/* + * 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.geometries.operation; + +import java.util.stream.Stream; +import org.apache.sis.geometries.Geometry; +import org.apache.sis.geometries.adapter.WellKnownText3; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + + +/** + * Tests the {@code normalize} operation of {@link GeometryProcessor}. + * The test cases are declared in {@code NormalizeTest.adoc}. + * + * @author Johann Sorel (Geomatys) + */ +public class NormalizeTest { + /** + * Creates a new test case. + */ + public NormalizeTest() { + } + + /** + * Returns the test cases declared in {@code NormalizeTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. + */ + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(NormalizeTest.class, "NormalizeTest.adoc"); + } + + /** + * Tests {@code normalize(Geometry)} on one test case, and verifies that normalizing again changes nothing. + * The geometries are compared by their Well-Known Text 3, which is written in canonical form. + * + * @param test the test case. + */ + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testNormalize(final AsciiDocTestCase test) { + final WellKnownText3 codec = new WellKnownText3(); + final String expected = codec.encode(test.geometry("result")); + final Geometry normalized = test.geometry("input").normalized(); + assertEquals(expected, codec.encode(normalized), test.description()); + assertEquals(expected, codec.encode(normalized.normalized()), "Normalizing twice shall change nothing."); + } +}
