Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VWSimplifier coordinate aliasing #1107

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.locationtech.jts.simplify;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.CoordinateArrays;
import org.locationtech.jts.geom.CoordinateList;
import org.locationtech.jts.geom.Triangle;

Expand Down Expand Up @@ -51,9 +52,9 @@ public Coordinate[] simplify()
Coordinate[] simp = vwLine.getCoordinates();
// ensure computed value is a valid line
if (simp.length < 2) {
return new Coordinate[] { simp[0], new Coordinate(simp[0]) };
return new Coordinate[] { simp[0].copy(), simp[0].copy() };
}
return simp;
return CoordinateArrays.copyDeep(simp);
}

private double simplifyVertex(VWLineSimplifier.VWVertex vwLine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@

package org.locationtech.jts.simplify;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.CoordinateFilter;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

import junit.framework.TestCase;
import test.jts.GeometryTestCase;


/**
* @version 1.7
*/
public class VWSimplifierTest
extends TestCase
extends GeometryTestCase
{
public VWSimplifierTest(String name) {
super(name);
Expand Down Expand Up @@ -67,6 +69,11 @@ public void testPolygonSpikeInHole() throws Exception {
.test();
}

public void testNoAlias() {
Geometry geom = read("LINESTRING (1 1, 3 6, 6 5, 8 6, 9 1)");
Geometry result = VWSimplifier.simplify(geom, 2);
checkNoAlias(geom, result);
}

}

Expand Down
17 changes: 17 additions & 0 deletions modules/core/src/test/java/test/jts/GeometryTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public abstract class GeometryTestCase extends TestCase{

private static final String CHECK_EQUAL_FAIL = "FAIL - Expected = %s -- Actual = %s\n";
private static final String CHECK_EQUAL_FAIL_MSG = "FAIL - %s: Expected = %s -- Actual = %s\n";
private static final String CHECK_NO_AlIAS_FAIL = "FAIL - geometries have aliased coordinates\n";

final GeometryFactory geomFactory;

Expand Down Expand Up @@ -225,6 +226,22 @@ protected void checkEqualXY(String message, Coordinate expected, Coordinate actu
assertEquals(message + " Y", expected.getY(), actual.getY(), tolerance);
}

protected void checkNoAlias(Geometry geom, Geometry geom2) {
Geometry geom2Copy = geom2.copy();
geom.apply(new CoordinateFilter() {

@Override
public void filter(Coordinate coord) {
coord.x = coord.x + 1;
}

});
boolean equal = geom2.equalsExact(geom2Copy);
if (! equal) {
System.out.println(CHECK_NO_AlIAS_FAIL);
fail();
}
}

/**
* Reads a {@link Geometry} from a WKT string using a custom {@link GeometryFactory}.
Expand Down
Loading