Skip to content

Commit

Permalink
Fix linestring split accuracy problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Kontinuation committed Aug 7, 2024
1 parent aef523b commit 60e3f58
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,12 @@ private MultiLineString splitLinesByPoints(Geometry lines, Geometry points) {
}

private MultiLineString splitLinesByLines(Geometry inputLines, Geometry blade) {
// compute the intersection of inputLines and blade
// and pass back to splitLines to handle as points
Geometry intersectionWithBlade = inputLines.intersection(blade);

if (intersectionWithBlade.isEmpty()) {
// blade and inputLines are disjoint so just return the input as a multilinestring
return (MultiLineString) ensureMultiGeometryOfDimensionN(inputLines, 1);
} else if (intersectionWithBlade.getDimension() != 0) {
logger.warn("Colinear sections detected between source and blade geometry. Returned null.");
return null;
Geometry diff = inputLines.difference(blade);
if (diff instanceof MultiLineString) {
return (MultiLineString) diff;
} else {
return geometryFactory.createMultiLineString(new LineString[] {(LineString) inputLines});
}

return splitLines(inputLines, intersectionWithBlade);
}

private MultiPolygon splitPolygonsByLines(Geometry polygons, Geometry blade) {
Expand Down Expand Up @@ -197,6 +190,7 @@ private void splitLineStringAtCoordinates(
Iterator<Coordinate> coordIterator =
getIteratorForSegmentDirection(pointCoords, lineBuilder.getLastCoordinate(), endCoord);

// line.getPrecisionModel()
applyCoordsToLineSegment(lineBuilder, coordIterator, endCoord);
}

Expand Down
37 changes: 37 additions & 0 deletions common/src/test/java/org/apache/sedona/common/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.geotools.referencing.operation.projection.ProjectionException;
import org.junit.Test;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.geom.prep.PreparedGeometry;
import org.locationtech.jts.geom.prep.PreparedGeometryFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;
Expand Down Expand Up @@ -305,6 +307,41 @@ public void splitLineStringByPolygon() {
assertEquals(actualResult, expectedResult);
}

@Test
public void splitLineStringFpPrecisionIssue() {
LineString lineString =
GEOMETRY_FACTORY.createLineString(
coordArray(
-8.961173822708158, -3.93776773106963, -8.08908227533288, -3.8845245068873444));
Polygon polygon =
GEOMETRY_FACTORY.createPolygon(
coordArray(
-6.318936372442209, -6.44985859539768,
-8.669092633645995, -3.0659222341103956,
-6.264600073171498, -3.075347218794894,
-5.3654318906014495, -3.1019726170919877,
-5.488002156793005, -5.892626167859213,
-6.318936372442209, -6.44985859539768));

Geometry result = Functions.split(lineString, polygon);
assertEquals(2, result.getNumGeometries());
assertEquals(lineString.getLength(), result.getLength(), 1e-6);
}

@Test
public void tempTest() {
LineString lineString =
GEOMETRY_FACTORY.createLineString(
coordArray(
-8.961173822708158, -3.93776773106963, -8.08908227533288, -3.8845245068873444));
Point point =
GEOMETRY_FACTORY.createPoint(new Coordinate(-8.100103048843774, -3.885197350829553));
PreparedGeometryFactory factory = new PreparedGeometryFactory();
PreparedGeometry prepLineString = factory.create(lineString);
boolean intersects = prepLineString.intersects(point);
System.out.println(intersects);
}

@Test
public void splitPolygonByLineString() {
Polygon polygon =
Expand Down

0 comments on commit 60e3f58

Please sign in to comment.