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 5d2815469fa4b6f3b0e44ec1797f714cb71e7999 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:31:30 2026 +0100 feat(Geometry): add radial distance simplification --- .../sis/geometries/operation/DouglasPeucker.java | 123 ++++++++++++++------- .../geometries/operation/GeometryProcessor.java | 15 +++ .../sis/geometries/operation/RadialDistance.java | 80 ++++++++++++++ .../geometries/operation/RadialDistanceTest.java | 88 +++++++++++++++ 4 files changed, 264 insertions(+), 42 deletions(-) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/DouglasPeucker.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/DouglasPeucker.java index 7e1ed3036a..dfcf9343e0 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/DouglasPeucker.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/DouglasPeucker.java @@ -88,7 +88,7 @@ public final class DouglasPeucker { * @return the simplified geometry, of the same type as the given geometry. */ public static Geometry simplify(Geometry geometry, double tolerance) { - return (tolerance > 0) ? simplifyAny(geometry, tolerance * tolerance) : geometry; + return (tolerance > 0) ? simplifyAny(geometry, simplifier(tolerance)) : geometry; } /** @@ -101,7 +101,7 @@ public final class DouglasPeucker { * @return the simplified line string, of the same type as the given line string. */ public static LineString simplify(LineString line, double tolerance) { - return (tolerance > 0) ? simplifyLine(line, tolerance * tolerance, false) : line; + return (tolerance > 0) ? simplifyLine(line, simplifier(tolerance), false) : line; } /** @@ -113,7 +113,7 @@ public final class DouglasPeucker { * @return the simplified ring. */ public static LinearRing simplify(LinearRing ring, double tolerance) { - return (tolerance > 0) ? simplifyRing(ring, tolerance * tolerance) : ring; + return (tolerance > 0) ? simplifyRing(ring, simplifier(tolerance)) : ring; } /** @@ -126,16 +126,55 @@ public final class DouglasPeucker { * @return the simplified polygon. */ public static Polygon simplify(Polygon polygon, double tolerance) { - return (tolerance > 0) ? simplifyPolygon(polygon, tolerance * tolerance) : polygon; + return (tolerance > 0) ? simplifyPolygon(polygon, simplifier(tolerance)) : polygon; + } + + /** + * An algorithm selecting the points to keep in a section of a line whose coordinates are all finite. + * The traversal of the geometries is shared by the simplification algorithms. + * + * @see RadialDistance + */ + @FunctionalInterface + interface SectionSimplifier { + /** + * Flags the points to keep between the given indices inclusive. The end points shall be kept. + * + * @param coordinates the coordinates of all points. + * @param dimension number of coordinates per point. + * @param first index of the first point of the section. + * @param last index of the last point of the section. + * @param keep where to flag the points to keep. + */ + void simplify(double[] coordinates, int dimension, int first, int last, boolean[] keep); + } + + /** + * Returns the Douglas-Peucker algorithm for the given tolerance. + */ + private static SectionSimplifier simplifier(final double tolerance) { + final double tolerance2 = tolerance * tolerance; + return (coordinates, dimension, first, last, keep) -> simplifySection(coordinates, dimension, first, last, tolerance2, keep); + } + + /** + * Simplifies the given geometry with the given algorithm, as described in the class Javadoc. + * + * @param geometry the geometry to simplify. + * @param simplifier the algorithm selecting the points to keep in each section of a line. + * @return the simplified geometry, of the same type as the given geometry. + */ + static Geometry simplify(final Geometry geometry, final SectionSimplifier simplifier) { + return simplifyAny(geometry, simplifier); } /** * Simplifies a geometry of any type. * * @param geometry the geometry to simplify. - * @param tolerance2 square of the tolerance. + * @param simplifier the algorithm selecting the points to keep in each section of a line. */ - private static Geometry simplifyAny(final Geometry geometry, final double tolerance2) { + private static Geometry simplifyAny(final Geometry geometry, final SectionSimplifier simplifier) { if (geometry.isEmpty()) { return geometry; } @@ -145,12 +184,12 @@ public final class DouglasPeucker { * line strips, line loops and product curves are also of those types. */ return switch (geometry) { - case Curve cdt -> simplifyCurve(cdt, tolerance2, false); + case Curve cdt -> simplifyCurve(cdt, simplifier, false); case MeshPrimitive cdt -> cdt; // Points, segments or triangles: no removable point. - case Polygon cdt -> simplifyPolygon(cdt, tolerance2); - case CurvePolygon cdt -> simplifyCurvePolygon(cdt, tolerance2); - case GeometryCollection<?> cdt -> simplifyCollection(cdt, tolerance2); - case Prism cdt -> simplifyPrism(cdt, tolerance2); + case Polygon cdt -> simplifyPolygon(cdt, simplifier); + case CurvePolygon cdt -> simplifyCurvePolygon(cdt, simplifier); + case GeometryCollection<?> cdt -> simplifyCollection(cdt, simplifier); + case Prism cdt -> simplifyPrism(cdt, simplifier); default -> geometry; }; } @@ -160,13 +199,13 @@ public final class DouglasPeucker { * other curves are returned unchanged. * * @param curve the curve to simplify. - * @param tolerance2 square of the tolerance. + * @param simplifier the algorithm selecting the points to keep in each section of a line. * @param ring whether the curve is the ring of a surface. */ - private static Curve simplifyCurve(final Curve curve, final double tolerance2, final boolean ring) { + private static Curve simplifyCurve(final Curve curve, final SectionSimplifier simplifier, final boolean ring) { return switch (curve) { - case LineString cdt -> simplifyLine(cdt, tolerance2, ring); - case CompoundCurve cdt -> simplifyCompound(cdt, tolerance2); + case LineString cdt -> simplifyLine(cdt, simplifier, ring); + case CompoundCurve cdt -> simplifyCompound(cdt, simplifier); default -> curve; // Non-linear interpolation, the control points are not vertices. }; } @@ -175,16 +214,16 @@ public final class DouglasPeucker { * Simplifies a line string, preserving the mesh primitive types. * * @param line the line string to simplify. - * @param tolerance2 square of the tolerance. + * @param simplifier the algorithm selecting the points to keep in each section of a line. * @param ring whether the line string is the ring of a surface. */ - private static LineString simplifyLine(final LineString line, final double tolerance2, final boolean ring) { + private static LineString simplifyLine(final LineString line, final SectionSimplifier simplifier, final boolean ring) { if (line instanceof LinearRing cdt) { - return simplifyRing(cdt, tolerance2); + return simplifyRing(cdt, simplifier); } final boolean loop = (line instanceof MeshPrimitive.LineLoop); final DataPoints points = line.getDataPoints(); - final int[] indices = select(points, tolerance2, ring | loop); + final int[] indices = select(points, simplifier, ring | loop); if (indices == null) { return line; } @@ -200,11 +239,11 @@ public final class DouglasPeucker { * Simplifies a linear ring, keeping at least the four points of a triangle. * * @param ring the linear ring to simplify. - * @param tolerance2 square of the tolerance. + * @param simplifier the algorithm selecting the points to keep in each section of a line. */ - private static LinearRing simplifyRing(final LinearRing ring, final double tolerance2) { + private static LinearRing simplifyRing(final LinearRing ring, final SectionSimplifier simplifier) { final DataPoints points = ring.getDataPoints(); - final int[] indices = select(points, tolerance2, true); + final int[] indices = select(points, simplifier, true); return (indices == null) ? ring : GeometryFactory.DEFAULT.createLinearRing(points.subset(indices)); } @@ -213,14 +252,14 @@ public final class DouglasPeucker { * so the compound curve stays continuous. * * @param curve the compound curve to simplify. - * @param tolerance2 square of the tolerance. + * @param simplifier the algorithm selecting the points to keep in each section of a line. */ - private static CompoundCurve simplifyCompound(final CompoundCurve curve, final double tolerance2) { + private static CompoundCurve simplifyCompound(final CompoundCurve curve, final SectionSimplifier simplifier) { final Curve[] components = new Curve[curve.getNumCurves()]; boolean changed = false; for (int i = 0; i < components.length; i++) { final Curve component = curve.getCurveN(i); - components[i] = simplifyCurve(component, tolerance2, false); + components[i] = simplifyCurve(component, simplifier, false); changed |= (components[i] != component); } return changed ? GeometryFactory.DEFAULT.createCompoundCurve(curve.getCoordinateReferenceSystem(), components) : curve; @@ -230,16 +269,16 @@ public final class DouglasPeucker { * Simplifies the exterior and interior rings of a polygon. * * @param polygon the polygon to simplify. - * @param tolerance2 square of the tolerance. + * @param simplifier the algorithm selecting the points to keep in each section of a line. */ - private static Polygon simplifyPolygon(final Polygon polygon, final double tolerance2) { + private static Polygon simplifyPolygon(final Polygon polygon, final SectionSimplifier simplifier) { final LinearRing exterior = polygon.getExteriorRing(); - final LinearRing simplified = simplifyRing(exterior, tolerance2); + final LinearRing simplified = simplifyRing(exterior, simplifier); boolean changed = (simplified != exterior); final List<LinearRing> interiors = new ArrayList<>(polygon.getInteriorRings()); for (int i = 0, n = interiors.size(); i < n; i++) { final LinearRing interior = interiors.get(i); - final LinearRing ring = simplifyRing(interior, tolerance2); + final LinearRing ring = simplifyRing(interior, simplifier); if (ring != interior) { interiors.set(i, ring); changed = true; @@ -252,16 +291,16 @@ public final class DouglasPeucker { * Simplifies the exterior and interior rings of a curve polygon. * * @param polygon the curve polygon to simplify. - * @param tolerance2 square of the tolerance. + * @param simplifier the algorithm selecting the points to keep in each section of a line. */ - private static CurvePolygon simplifyCurvePolygon(final CurvePolygon polygon, final double tolerance2) { + private static CurvePolygon simplifyCurvePolygon(final CurvePolygon polygon, final SectionSimplifier simplifier) { final Curve exterior = polygon.getExteriorRing(); - final Curve simplified = simplifyCurve(exterior, tolerance2, true); + final Curve simplified = simplifyCurve(exterior, simplifier, true); boolean changed = (simplified != exterior); final List<Curve> interiors = new ArrayList<>(polygon.getInteriorRings()); for (int i = 0, n = interiors.size(); i < n; i++) { final Curve interior = interiors.get(i); - final Curve ring = simplifyCurve(interior, tolerance2, true); + final Curve ring = simplifyCurve(interior, simplifier, true); if (ring != interior) { interiors.set(i, ring); changed = true; @@ -274,14 +313,14 @@ public final class DouglasPeucker { * Simplifies each element of a collection. The element type of the collection is preserved. * * @param collection the collection to simplify. - * @param tolerance2 square of the tolerance. + * @param simplifier the algorithm selecting the points to keep in each section of a line. */ - private static Geometry simplifyCollection(final GeometryCollection<?> collection, final double tolerance2) { + private static Geometry simplifyCollection(final GeometryCollection<?> collection, final SectionSimplifier simplifier) { final Geometry[] elements = new Geometry[collection.getNumGeometries()]; boolean changed = false; for (int i = 0; i < elements.length; i++) { final Geometry element = collection.getGeometryN(i); - elements[i] = simplifyAny(element, tolerance2); + elements[i] = simplifyAny(element, simplifier); changed |= (elements[i] != element); } if (!changed) { @@ -295,11 +334,11 @@ public final class DouglasPeucker { * Simplifies the base of a prism. The extrusion is unchanged. * * @param prism the prism to simplify. - * @param tolerance2 square of the tolerance. + * @param simplifier the algorithm selecting the points to keep in each section of a line. */ - private static Prism simplifyPrism(final Prism prism, final double tolerance2) { + private static Prism simplifyPrism(final Prism prism, final SectionSimplifier simplifier) { final Geometry base = prism.getBase(); - final Geometry simplified = simplifyAny(base, tolerance2); + final Geometry simplified = simplifyAny(base, simplifier); if (simplified == base) { return prism; } @@ -317,11 +356,11 @@ public final class DouglasPeucker { * Selects the points to keep in the given sequence of data points. * * @param points the data points of a line string. - * @param tolerance2 square of the tolerance. + * @param simplifier the algorithm selecting the points to keep in each section of a line. * @param ring whether to keep at least the four points of a triangle. * @return indices in increasing order of the points to keep, or {@code null} if all points are kept. */ - private static int[] select(final DataPoints points, final double tolerance2, final boolean ring) { + private static int[] select(final DataPoints points, final SectionSimplifier simplifier, final boolean ring) { final int n = points.size(); if (n <= (ring ? MIN_RING_POINTS : 2)) { return null; @@ -338,7 +377,7 @@ public final class DouglasPeucker { for (int i = 0; i <= n; i++) { if (i == n || !isFinite(coordinates, dimension, i)) { if (start >= 0) { - simplifySection(coordinates, dimension, start, i - 1, tolerance2, keep); + simplifier.simplify(coordinates, dimension, start, i - 1, keep); start = -1; } if (i != n) { 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 2211325e60..77af37316e 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 @@ -517,6 +517,21 @@ public final class GeometryProcessor { return DouglasPeucker.simplify(geom, tolerance); } + /** + * Returns a simplified version of the geometry without the points too close to the previous point kept. + * This is faster than {@link #simplify(Geometry, double)} but keeps more points. + * + * @param geom geometry to simplify. + * @param tolerance minimal distance in CRS units between two consecutive points of the simplified geometry. + * If not strictly positive, the geometry is returned unchanged. + * @return simplified geometry of the same type. + * + * @see RadialDistance + */ + public Geometry simplifyRadial(Geometry geom, double tolerance) { + return RadialDistance.simplify(geom, tolerance); + } + /** * Add a Z axis on the geometry and configure it's ordinates. * diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/RadialDistance.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/RadialDistance.java new file mode 100644 index 0000000000..611627d90c --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/RadialDistance.java @@ -0,0 +1,80 @@ +/* + * 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 org.apache.sis.geometries.Geometry; + + +/** + * Simplification of geometries removing the points too close to the previous point kept. + * This is faster than the {@linkplain DouglasPeucker Douglas-Peucker} algorithm but keeps more points, + * so it is often used to remove the noise of dense lines before the Douglas-Peucker algorithm. + * + * <ul> + * <li>The first and last points of each line are kept.</li> + * <li>The geometry types and attributes are handled as described in {@link DouglasPeucker}: + * rings keep at least the four points of a triangle, points having a non-finite coordinate are kept, etc.</li> + * <li>When no point is removed, the given geometry is returned unchanged.</li> + * </ul> + * + * @author Johann Sorel (Geomatys) + * + * @see <a href="https://psimpl.sourceforge.net/radial-distance.html">Radial distance simplification</a> + */ +public final class RadialDistance { + + private RadialDistance() { + } + + /** + * Simplifies the given geometry. + * + * @param geometry the geometry to simplify. + * @param tolerance minimal distance between two consecutive points of the simplified geometry. + * If not strictly positive, the geometry is returned unchanged. + * @return the simplified geometry, of the same type as the given geometry. + */ + public static Geometry simplify(final Geometry geometry, final double tolerance) { + if (!(tolerance > 0)) { + return geometry; + } + final double tolerance2 = tolerance * tolerance; + return DouglasPeucker.simplify(geometry, (coordinates, dimension, first, last, keep) -> { + keep[first] = true; + keep[last] = true; + int previous = first; + for (int i = first + 1; i < last; i++) { + if (distanceSquared(coordinates, dimension, previous, i) >= tolerance2) { + keep[i] = true; + previous = i; + } + } + }); + } + + /** + * Returns the square of the distance between the points at the given indices. + */ + private static double distanceSquared(final double[] coordinates, final int dimension, final int a, final int b) { + double sum = 0; + for (int k = 0; k < dimension; k++) { + final double d = coordinates[b * dimension + k] - coordinates[a * dimension + k]; + sum += d * d; + } + return sum; + } +} diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RadialDistanceTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RadialDistanceTest.java new file mode 100644 index 0000000000..c04a67722b --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RadialDistanceTest.java @@ -0,0 +1,88 @@ +/* + * 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 org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.apache.sis.geometries.DataPointsType; +import org.apache.sis.geometries.Geometry; +import org.apache.sis.geometries.GeometryFactory; +import org.apache.sis.geometries.curve.LineString; +import org.apache.sis.geometries.curve.LinearRing; +import org.apache.sis.geometries.internal.shared.ArrayDataPoints; +import org.apache.sis.maths.NDArrays; +import org.apache.sis.referencing.CommonCRS; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertSame; +import org.junit.jupiter.api.Test; + + +/** + * Tests {@link RadialDistance}. + * + * @author Johann Sorel (Geomatys) + */ +public class RadialDistanceTest { + + private static final double TOLERANCE = 1E-9; + + private static final CoordinateReferenceSystem CRS_2D = CommonCRS.WGS84.normalizedGeographic(); + + private static ArrayDataPoints points(double... coords) { + return new ArrayDataPoints(NDArrays.of(CRS_2D, coords)); + } + + private static double[] positions(Geometry geom) { + return geom.getDataPoints().getAttributeArray(DataPointsType.ATT_POSITION).toArrayDouble(); + } + + /** + * The points closer than the tolerance to the previous point kept are removed. The last point is always kept. + */ + @Test + public void testLineString() { + final LineString line = GeometryFactory.DEFAULT.createLineString(points(0, 0, 0.5, 0, 1, 0, 1.2, 0, 3, 0, 3.1, 0, 5, 0)); + final Geometry result = RadialDistance.simplify(line, 1); + assertInstanceOf(LineString.class, result); + assertArrayEquals(new double[] {0, 0, 1, 0, 3, 0, 5, 0}, positions(result), TOLERANCE); + assertArrayEquals(positions(result), positions(new GeometryProcessor().simplifyRadial(line, 1)), TOLERANCE); + } + + /** + * The geometry is returned unchanged when no point is removed or when the tolerance is not strictly positive. + */ + @Test + public void testUnchanged() { + final LineString line = GeometryFactory.DEFAULT.createLineString(points(0, 0, 2, 0, 4, 0)); + assertSame(line, RadialDistance.simplify(line, 1)); + assertSame(line, RadialDistance.simplify(line, 0)); + assertSame(line, RadialDistance.simplify(line, Double.NaN)); + } + + /** + * A ring smaller than the tolerance keeps the four points of a triangle. + */ + @Test + public void testRingCollapse() { + final LinearRing ring = GeometryFactory.DEFAULT.createLinearRing(points(0, 0, 0.1, 0, 0.1, 0.1, 0, 0.1, 0, 0)); + final Geometry result = RadialDistance.simplify(ring, 5); + assertInstanceOf(LinearRing.class, result); + assertArrayEquals(new double[] {0, 0, 0.1, 0, 0.1, 0.1, 0, 0}, positions(result), TOLERANCE); + } +}
