Skip to content

Commit d751435

Browse files
authored
Fix CoordinateArrays.hasRepeatedOrInvalidPoints to check first point (#1157)
1 parent 371ffe7 commit d751435

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

modules/core/src/main/java/org/locationtech/jts/geom/CoordinateArrays.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,10 @@ public static Coordinate[] removeRepeatedPoints(Coordinate[] coord) {
434434
* @see Coordinate#isValid()
435435
*/
436436
public static boolean hasRepeatedOrInvalidPoints(Coordinate[] coord) {
437-
for (int i = 1; i < coord.length; i++) {
437+
for (int i = 0; i < coord.length; i++) {
438438
if (! coord[i].isValid())
439439
return true;
440-
if (coord[i - 1].equals(coord[i])) {
440+
if (i > 0 && coord[i - 1].equals(coord[i])) {
441441
return true;
442442
}
443443
}

modules/core/src/test/java/org/locationtech/jts/geom/CoordinateArraysTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ public void testOrientCCW() {
184184
checkOrient("POLYGON ((9 7, 5 9, 1 4, 5 4, 4 1, 8 1, 9 7))");
185185
}
186186

187+
public void testRemoveInvalidStartPoint() {
188+
checkRemoveInvalidPoints("LINESTRING (NaN 0, 1 1, 2 2)",
189+
"LINESTRING (1 1, 2 2)");
190+
}
191+
192+
public void testRemoveInvalidPoints() {
193+
checkRemoveInvalidPoints("LINESTRING (Nan 0, 1 1, Nan Nan, 2 2, 3 NaN)",
194+
"LINESTRING (1 1, 2 2)");
195+
}
196+
197+
//================================================
198+
187199
private void checkOrient(String wkt) {
188200
Coordinate[] pts = read(wkt).getCoordinates();
189201
//-- orient CW
@@ -240,4 +252,14 @@ private static Coordinate[] createCircularString(Coordinate center, double radiu
240252
return sequence;
241253
}
242254

255+
private void checkRemoveInvalidPoints(String wkt, String wktExpected) {
256+
Coordinate[] pts = read(wkt).getCoordinates();
257+
258+
assertTrue(CoordinateArrays.hasRepeatedOrInvalidPoints(pts));
259+
260+
Coordinate[] ptsFix = CoordinateArrays.removeRepeatedOrInvalidPoints(pts);
261+
Coordinate[] ptsExpected = read(wktExpected).getCoordinates();
262+
assertTrue(CoordinateArrays.equals(ptsFix, ptsExpected));
263+
}
264+
243265
}

0 commit comments

Comments
 (0)