/** Splits the input geometry into two LineStrings at the given point. */ public static P2<LineString> splitGeometryAtPoint(Geometry geometry, Coordinate nearestPoint) { // An index in JTS can actually refer to any point along the line. It is NOT an array index. LocationIndexedLine line = new LocationIndexedLine(geometry); LinearLocation l = line.indexOf(nearestPoint); LineString beginning = (LineString) line.extractLine(line.getStartIndex(), l); LineString ending = (LineString) line.extractLine(l, line.getEndIndex()); return new P2<LineString>(beginning, ending); }
/** Splits the input geometry into two LineStrings at a fraction of the distance covered. */ public static P2<LineString> splitGeometryAtFraction(Geometry geometry, double fraction) { LineString empty = new LineString(null, gf); Coordinate[] coordinates = geometry.getCoordinates(); CoordinateSequence sequence = gf.getCoordinateSequenceFactory().create(coordinates); LineString total = new LineString(sequence, gf); if (coordinates.length < 2) return new P2<LineString>(empty, empty); if (fraction <= 0) return new P2<LineString>(empty, total); if (fraction >= 1) return new P2<LineString>(total, empty); double totalDistance = total.getLength(); double requestedDistance = totalDistance * fraction; // An index in JTS can actually refer to any point along the line. It is NOT an array index. LocationIndexedLine line = new LocationIndexedLine(geometry); LinearLocation l = LengthLocationMap.getLocation(geometry, requestedDistance); LineString beginning = (LineString) line.extractLine(line.getStartIndex(), l); LineString ending = (LineString) line.extractLine(l, line.getEndIndex()); return new P2<LineString>(beginning, ending); }