Skip to content

Commit 85313ef

Browse files
committed
feat: add ST_RemoveRepeatedPoints
1 parent c23554e commit 85313ef

File tree

22 files changed

+597
-0
lines changed

22 files changed

+597
-0
lines changed

common/src/main/java/org/apache/sedona/common/Functions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,14 @@ public static Geometry removePoint(Geometry linestring, int position) {
873873
return null;
874874
}
875875

876+
public static Geometry removeRepeatedPoints(Geometry geom, double tolerance) {
877+
return GeometryDuplicateCoordinateRemover.transform(geom, tolerance);
878+
}
879+
880+
public static Geometry removeRepeatedPoints(Geometry geom) {
881+
return removeRepeatedPoints(geom, 0);
882+
}
883+
876884
public static Geometry setPoint(Geometry linestring, int position, Geometry point) {
877885
if (linestring instanceof LineString) {
878886
List<Coordinate> coordinates = new ArrayList<>(Arrays.asList(linestring.getCoordinates()));
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.sedona.common.utils;
20+
21+
import java.util.Arrays;
22+
import java.util.LinkedHashSet;
23+
import java.util.Objects;
24+
import java.util.Set;
25+
import org.locationtech.jts.geom.*;
26+
27+
public class GeometryDuplicateCoordinateRemover {
28+
29+
public static Coordinate[] removeDuplicates(Coordinate[] coords, int minPoints) {
30+
Coordinate pt;
31+
int numPoint = coords.length;
32+
int totalPointsOut = 1;
33+
34+
double distance = Double.MAX_VALUE;
35+
36+
if (numPoint <= minPoints) return new Coordinate[0];
37+
38+
Coordinate last = coords[0];
39+
int pToIndex = 1;
40+
41+
for (int i = 1; i < numPoint; i++) {
42+
boolean lastPoint = (i == numPoint - 1);
43+
44+
pt = coords[i];
45+
46+
if (numPoint + totalPointsOut > minPoints + i) {
47+
if (TOLERANCE > 0.0) {
48+
distance = pt.distance(last);
49+
if (!lastPoint && distance <= TOLERANCE) {
50+
continue;
51+
}
52+
} else {
53+
if (pt.equals2D(last)) {
54+
continue;
55+
}
56+
}
57+
58+
if (lastPoint && totalPointsOut > 1 && TOLERANCE > 0.0 && distance <= TOLERANCE) {
59+
totalPointsOut--;
60+
pToIndex--;
61+
}
62+
}
63+
64+
coords[pToIndex] = pt;
65+
totalPointsOut++;
66+
pToIndex++;
67+
last = pt;
68+
}
69+
Coordinate[] newCoordinates = newCoordinates = new Coordinate[totalPointsOut];
70+
System.arraycopy(coords, 0, newCoordinates, 0, totalPointsOut);
71+
72+
return newCoordinates;
73+
}
74+
75+
public static Coordinate[] removeDuplicatePointsMultiPoint(
76+
Coordinate[] coords, boolean recursion) {
77+
if (TOLERANCE == 0 || recursion) {
78+
Set<Coordinate> uniqueCoords = new LinkedHashSet<>(Arrays.asList(coords));
79+
return uniqueCoords.toArray(new Coordinate[0]);
80+
}
81+
Coordinate[] deduplicated =
82+
Arrays.stream(removeDuplicatePointsMultiPoint(coords, true))
83+
.sorted()
84+
.toArray(Coordinate[]::new);
85+
86+
for (int i = 0; i < deduplicated.length; i++) {
87+
for (int j = i + 1; j < deduplicated.length; j++) {
88+
if (deduplicated[i] != null
89+
&& deduplicated[j] != null
90+
&& deduplicated[i].distance(deduplicated[j]) < TOLERANCE) {
91+
deduplicated[j] = null;
92+
} else {
93+
break;
94+
}
95+
}
96+
}
97+
98+
return Arrays.stream(deduplicated).filter(Objects::nonNull).toArray(Coordinate[]::new);
99+
}
100+
101+
private static GeometryFactory FACTORY = null;
102+
103+
private static double TOLERANCE = 0;
104+
105+
public static Geometry transform(Geometry geometry, double tolerance) {
106+
107+
TOLERANCE = tolerance;
108+
109+
if (geometry.isEmpty()) return geometry;
110+
111+
FACTORY = geometry.getFactory();
112+
113+
if (geometry.getGeometryType().equals(Geometry.TYPENAME_POINT)) return geometry;
114+
if (geometry.getGeometryType().equals(Geometry.TYPENAME_MULTIPOINT))
115+
return transformMultiPoint((MultiPoint) geometry);
116+
if (geometry.getGeometryType().equals(Geometry.TYPENAME_LINEARRING))
117+
return transformLinearRing((LinearRing) geometry);
118+
if (geometry.getGeometryType().equals(Geometry.TYPENAME_LINESTRING))
119+
return transformLineString((LineString) geometry);
120+
if (geometry.getGeometryType().equals(Geometry.TYPENAME_MULTILINESTRING))
121+
return transformMultiLineString((MultiLineString) geometry);
122+
if (geometry.getGeometryType().equals(Geometry.TYPENAME_POLYGON))
123+
return transformPolygon((Polygon) geometry);
124+
if (geometry.getGeometryType().equals(Geometry.TYPENAME_MULTIPOLYGON))
125+
return transformMultiPolygon((MultiPolygon) geometry);
126+
if (geometry.getGeometryType().equals(Geometry.TYPENAME_GEOMETRYCOLLECTION))
127+
return transformGeometryCollection((GeometryCollection) geometry);
128+
129+
throw new IllegalArgumentException(
130+
"Unknown Geometry subtype: " + geometry.getClass().getName());
131+
}
132+
133+
private static MultiPoint transformMultiPoint(MultiPoint geometry) {
134+
Coordinate[] coords = geometry.getCoordinates();
135+
return FACTORY.createMultiPointFromCoords(removeDuplicatePointsMultiPoint(coords, false));
136+
}
137+
138+
private static LinearRing transformLinearRing(LinearRing geometry) {
139+
Coordinate[] coords = geometry.getCoordinates();
140+
return FACTORY.createLinearRing(removeDuplicates(coords, 4));
141+
}
142+
143+
private static LineString transformLineString(LineString geometry) {
144+
if (geometry.getNumPoints() <= 2) return geometry;
145+
146+
Coordinate[] coords = geometry.getCoordinates();
147+
return FACTORY.createLineString(removeDuplicates(coords, 2));
148+
}
149+
150+
private static MultiLineString transformMultiLineString(MultiLineString geometry) {
151+
LineString[] lineStrings = new LineString[geometry.getNumGeometries()];
152+
for (int i = 0; i < lineStrings.length; i++) {
153+
lineStrings[i] = transformLineString((LineString) geometry.getGeometryN(i));
154+
}
155+
return FACTORY.createMultiLineString(lineStrings);
156+
}
157+
158+
private static Polygon transformPolygon(Polygon geometry) {
159+
LinearRing shell = transformLinearRing(geometry.getExteriorRing());
160+
161+
LinearRing[] holes = new LinearRing[geometry.getNumInteriorRing()];
162+
for (int i = 0; i < holes.length; i++) {
163+
holes[i] = transformLinearRing(geometry.getInteriorRingN(i));
164+
}
165+
return FACTORY.createPolygon(shell, holes);
166+
}
167+
168+
private static MultiPolygon transformMultiPolygon(MultiPolygon geometry) {
169+
Polygon[] polygons = new Polygon[geometry.getNumGeometries()];
170+
for (int i = 0; i < polygons.length; i++) {
171+
polygons[i] = transformPolygon((Polygon) geometry.getGeometryN(i));
172+
}
173+
return FACTORY.createMultiPolygon(polygons);
174+
}
175+
176+
private static GeometryCollection transformGeometryCollection(GeometryCollection geometry) {
177+
Geometry[] geometries = new Geometry[geometry.getNumGeometries()];
178+
for (int i = 0; i < geometries.length; i++) {
179+
geometries[i] = transform(geometry.getGeometryN(i), TOLERANCE);
180+
}
181+
return FACTORY.createGeometryCollection(geometries);
182+
}
183+
}

common/src/test/java/org/apache/sedona/common/FunctionsTest.java

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,126 @@ public void numPointsUnsupported() throws Exception {
16221622
assertEquals(expected, e.getMessage());
16231623
}
16241624

1625+
@Test
1626+
public void removeRepeatedPoints() throws ParseException {
1627+
Geometry geom = Constructors.geomFromWKT("POINT (10 23)", 4321);
1628+
Geometry actualGeom = Functions.removeRepeatedPoints(geom);
1629+
String actual = Functions.asWKT(actualGeom);
1630+
String expected = "POINT (10 23)";
1631+
assertEquals(expected, actual);
1632+
int actualSRID = Functions.getSRID(actualGeom);
1633+
assertEquals(4321, actualSRID);
1634+
1635+
geom = Constructors.geomFromWKT("MULTIPOINT ((1 1), (4 4), (2 2), (3 3))", 1000);
1636+
actualGeom = Functions.removeRepeatedPoints(geom);
1637+
actual = Functions.asWKT(actualGeom);
1638+
expected = "MULTIPOINT ((1 1), (4 4), (2 2), (3 3))";
1639+
assertEquals(expected, actual);
1640+
actualSRID = Functions.getSRID(actualGeom);
1641+
assertEquals(1000, actualSRID);
1642+
1643+
geom =
1644+
Constructors.geomFromWKT("MULTIPOINT (20 20, 10 10, 30 30, 40 40, 20 20, 30 30, 40 40)", 0);
1645+
actual = Functions.asWKT(Functions.removeRepeatedPoints(geom, 20));
1646+
expected = "MULTIPOINT ((10 10), (30 30))";
1647+
assertEquals(expected, actual);
1648+
1649+
geom = Constructors.geomFromWKT("MULTIPOINT ((1 1), (4 4), (2 2), (3 3), (3 3))", 0);
1650+
actual = Functions.asWKT(Functions.removeRepeatedPoints(geom, 2000));
1651+
expected = "MULTIPOINT ((1 1))";
1652+
assertEquals(expected, actual);
1653+
1654+
geom = Constructors.geomFromWKT("LINESTRING (0 0, 0 0, 1 1, 0 0, 1 1, 2 2)", 2000);
1655+
actualGeom = Functions.removeRepeatedPoints(geom);
1656+
actual = Functions.asWKT(actualGeom);
1657+
expected = "LINESTRING (0 0, 1 1, 0 0, 1 1, 2 2)";
1658+
assertEquals(expected, actual);
1659+
actualSRID = Functions.getSRID(actualGeom);
1660+
assertEquals(2000, actualSRID);
1661+
1662+
geom = Constructors.geomFromWKT("LINESTRING (0 0, 0 0, 1 1, 5 5, 1 1, 2 2)", 0);
1663+
actual = Functions.asWKT(Functions.removeRepeatedPoints(geom, 2));
1664+
expected = "LINESTRING (0 0, 5 5, 2 2)";
1665+
assertEquals(expected, actual);
1666+
1667+
actual = Functions.asWKT(Functions.removeRepeatedPoints(geom, 6));
1668+
expected = "LINESTRING (0 0, 2 2)";
1669+
assertEquals(expected, actual);
1670+
1671+
geom =
1672+
Constructors.geomFromWKT("LINESTRING (20 20, 10 10, 30 30, 40 40, 20 20, 30 30, 40 40)", 0);
1673+
actual = Functions.asWKT(Functions.removeRepeatedPoints(geom, 20));
1674+
expected = "LINESTRING (20 20, 40 40, 20 20, 40 40)";
1675+
assertEquals(expected, actual);
1676+
1677+
geom =
1678+
Constructors.geomFromWKT("LINESTRING (10 10, 20 20, 20 20, 30 30, 30 30, 40 40, 40 40)", 0);
1679+
actual = Functions.asWKT(Functions.removeRepeatedPoints(geom, 10000));
1680+
expected = "LINESTRING (10 10, 40 40)";
1681+
assertEquals(expected, actual);
1682+
1683+
geom =
1684+
Constructors.geomFromWKT(
1685+
"MULTILINESTRING ((10 10, 20 20, 20 20, 30 30), (40 40, 50 50, 50 50, 60 60))", 3000);
1686+
actualGeom = Functions.removeRepeatedPoints(geom);
1687+
actual = Functions.asWKT(actualGeom);
1688+
expected = "MULTILINESTRING ((10 10, 20 20, 30 30), (40 40, 50 50, 60 60))";
1689+
assertEquals(expected, actual);
1690+
actualSRID = Functions.getSRID(actualGeom);
1691+
assertEquals(3000, actualSRID);
1692+
1693+
geom =
1694+
Constructors.geomFromWKT(
1695+
"POLYGON ((10 10, 20 20, 20 20, 30 30, 30 30, 40 40, 40 40, 10 10))", 4000);
1696+
actualGeom = Functions.removeRepeatedPoints(geom);
1697+
actual = Functions.asWKT(actualGeom);
1698+
expected = "POLYGON ((10 10, 20 20, 30 30, 40 40, 10 10))";
1699+
assertEquals(expected, actual);
1700+
actualSRID = Functions.getSRID(actualGeom);
1701+
assertEquals(4000, actualSRID);
1702+
1703+
geom =
1704+
Constructors.geomFromWKT(
1705+
"POLYGON ((10 10, 20 20, 20 20, 30 30, 30 30, 40 40, 40 40, 10 10),(15 15, 25 25, 25 25, 35 35, 35 35, 15 15),(25 25, 35 35, 35 35, 45 45, 45 45, 25 25))",
1706+
0);
1707+
actual = Functions.asWKT(Functions.removeRepeatedPoints(geom, 1000));
1708+
expected =
1709+
"POLYGON ((10 10, 40 40, 40 40, 10 10), (15 15, 35 35, 35 35, 15 15), (25 25, 45 45, 45 45, 25 25))";
1710+
assertEquals(expected, actual);
1711+
1712+
geom =
1713+
Constructors.geomFromWKT(
1714+
"MULTIPOLYGON (((10 10, 20 20, 20 20, 30 30, 30 30, 40 40, 40 40, 10 10)),((50 50, 60 60, 60 60, 70 70, 70 70, 80 80, 80 80, 50 50)))",
1715+
5000);
1716+
actualGeom = Functions.removeRepeatedPoints(geom, 1000);
1717+
actual = Functions.asWKT(actualGeom);
1718+
expected = "MULTIPOLYGON (((10 10, 40 40, 40 40, 10 10)), ((50 50, 80 80, 80 80, 50 50)))";
1719+
assertEquals(expected, actual);
1720+
actualSRID = Functions.getSRID(actualGeom);
1721+
assertEquals(5000, actualSRID);
1722+
1723+
geom =
1724+
Constructors.geomFromWKT(
1725+
"MULTIPOLYGON (((10 10, 20 20, 20 20, 30 30, 30 30, 40 40, 40 40, 10 10),(15 15, 25 25, 25 25, 35 35, 35 35, 15 15),(25 25, 35 35, 35 35, 45 45, 45 45, 25 25)),((50 50, 60 60, 60 60, 70 70, 70 70, 80 80, 80 80, 50 50),(55 55, 65 65, 65 65, 75 75, 75 75, 55 55),(65 65, 75 75, 75 75, 85 85, 85 85, 65 65)))",
1726+
0);
1727+
actual = Functions.asWKT(Functions.removeRepeatedPoints(geom, 1000));
1728+
expected =
1729+
"MULTIPOLYGON (((10 10, 40 40, 40 40, 10 10), (15 15, 35 35, 35 35, 15 15), (25 25, 45 45, 45 45, 25 25)), ((50 50, 80 80, 80 80, 50 50), (55 55, 75 75, 75 75, 55 55), (65 65, 85 85, 85 85, 65 65)))";
1730+
assertEquals(expected, actual);
1731+
1732+
geom =
1733+
Constructors.geomFromWKT(
1734+
"GEOMETRYCOLLECTION (POINT (10 10),LINESTRING (20 20, 20 20, 30 30, 30 30),POLYGON ((40 40, 50 50, 50 50, 60 60, 60 60, 70 70, 70 70, 40 40)), MULTIPOINT ((80 80), (90 90), (90 90), (100 100)))",
1735+
6000);
1736+
actualGeom = Functions.removeRepeatedPoints(geom);
1737+
actual = Functions.asWKT(actualGeom);
1738+
expected =
1739+
"GEOMETRYCOLLECTION (POINT (10 10), LINESTRING (20 20, 30 30), POLYGON ((40 40, 50 50, 60 60, 70 70, 40 40)), MULTIPOINT ((80 80), (90 90), (100 100)))";
1740+
assertEquals(expected, actual);
1741+
actualSRID = Functions.getSRID(actualGeom);
1742+
assertEquals(6000, actualSRID);
1743+
}
1744+
16251745
@Test
16261746
public void simplifyVW() throws ParseException {
16271747
Geometry geom = Constructors.geomFromEWKT("LINESTRING(5 2, 3 8, 6 20, 7 25, 10 10)");

docs/api/flink/Function.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,46 @@ Output:
32063206
LINESTRING(0 0, 1 0)
32073207
```
32083208

3209+
## ST_RemoveRepeatedPoints
3210+
3211+
Introduction: This function eliminates consecutive duplicate points within a geometry, preserving endpoints of LineStrings. It operates on (Multi)LineStrings, (Multi)Polygons, and MultiPoints, processing GeometryCollection elements individually. When an optional 'tolerance' value is provided, vertices within that distance are also considered duplicates.
3212+
3213+
Format:
3214+
3215+
`ST_RemoveRepeatedPoints(geom: Geometry, tolerance: Double)`
3216+
3217+
`ST_RemoveRepeatedPoints(geom: Geometry)`
3218+
3219+
Since: `v1.7.0`
3220+
3221+
SQL Example:
3222+
3223+
```sql
3224+
SELECT ST_RemoveRepeatedPoints(
3225+
ST_GeomFromWKT('MULTIPOINT ((20 20), (10 10), (30 30), (40 40), (20 20), (30 30), (40 40))')
3226+
)
3227+
```
3228+
3229+
Output:
3230+
3231+
```
3232+
MULTIPOINT ((20 20), (10 10), (30 30), (40 40))
3233+
```
3234+
3235+
SQL Example:
3236+
3237+
```sql
3238+
SELECT ST_RemoveRepeatedPoints(
3239+
ST_GeomFromWKT('LINESTRING (20 20, 10 10, 30 30, 40 40, 20 20, 30 30, 40 40)'), 20
3240+
)
3241+
```
3242+
3243+
Output:
3244+
3245+
```
3246+
LINESTRING (20 20, 40 40, 20 20, 40 40)
3247+
```
3248+
32093249
## ST_Rotate
32103250

32113251
Introduction: Rotates a geometry by a specified angle in radians counter-clockwise around a given origin point. The origin for rotation can be specified as either a POINT geometry or x and y coordinates. If the origin is not specified, the geometry is rotated around POINT(0 0).

0 commit comments

Comments
 (0)