Class LineStringSnapper
- java.lang.Object
-
- org.locationtech.jts.operation.overlay.snap.LineStringSnapper
-
public class LineStringSnapper extends java.lang.Object
Snaps the vertices and segments of aLineString
to a set of target snap vertices. A snap distance tolerance is used to control where snapping is performed.The implementation handles empty geometry and empty snap vertex sets.
- Version:
- 1.7
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
allowSnappingToSourceVertices
private boolean
isClosed
private LineSegment
seg
private double
snapTolerance
private Coordinate[]
srcPts
-
Constructor Summary
Constructors Constructor Description LineStringSnapper(Coordinate[] srcPts, double snapTolerance)
Creates a new snapper using the given points as source points to be snapped.LineStringSnapper(LineString srcLine, double snapTolerance)
Creates a new snapper using the points in the givenLineString
as source snap points.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
findSegmentIndexToSnap(Coordinate snapPt, CoordinateList srcCoords)
Finds a src segment which snaps to (is close to) the given snap point.private Coordinate
findSnapForVertex(Coordinate pt, Coordinate[] snapPts)
private static boolean
isClosed(Coordinate[] pts)
void
setAllowSnappingToSourceVertices(boolean allowSnappingToSourceVertices)
private void
snapSegments(CoordinateList srcCoords, Coordinate[] snapPts)
Snap segments of the source to nearby snap vertices.Coordinate[]
snapTo(Coordinate[] snapPts)
Snaps the vertices and segments of the source LineString to the given set of snap vertices.private void
snapVertices(CoordinateList srcCoords, Coordinate[] snapPts)
Snap source vertices to vertices in the target.
-
-
-
Field Detail
-
snapTolerance
private double snapTolerance
-
srcPts
private Coordinate[] srcPts
-
seg
private LineSegment seg
-
allowSnappingToSourceVertices
private boolean allowSnappingToSourceVertices
-
isClosed
private boolean isClosed
-
-
Constructor Detail
-
LineStringSnapper
public LineStringSnapper(LineString srcLine, double snapTolerance)
Creates a new snapper using the points in the givenLineString
as source snap points.- Parameters:
srcLine
- a LineString to snap (may be empty)snapTolerance
- the snap tolerance to use
-
LineStringSnapper
public LineStringSnapper(Coordinate[] srcPts, double snapTolerance)
Creates a new snapper using the given points as source points to be snapped.- Parameters:
srcPts
- the points to snapsnapTolerance
- the snap tolerance to use
-
-
Method Detail
-
setAllowSnappingToSourceVertices
public void setAllowSnappingToSourceVertices(boolean allowSnappingToSourceVertices)
-
isClosed
private static boolean isClosed(Coordinate[] pts)
-
snapTo
public Coordinate[] snapTo(Coordinate[] snapPts)
Snaps the vertices and segments of the source LineString to the given set of snap vertices.- Parameters:
snapPts
- the vertices to snap to- Returns:
- a list of the snapped points
-
snapVertices
private void snapVertices(CoordinateList srcCoords, Coordinate[] snapPts)
Snap source vertices to vertices in the target.- Parameters:
srcCoords
- the points to snapsnapPts
- the points to snap to
-
findSnapForVertex
private Coordinate findSnapForVertex(Coordinate pt, Coordinate[] snapPts)
-
snapSegments
private void snapSegments(CoordinateList srcCoords, Coordinate[] snapPts)
Snap segments of the source to nearby snap vertices. Source segments are "cracked" at a snap vertex. A single input segment may be snapped several times to different snap vertices.For each distinct snap vertex, at most one source segment is snapped to. This prevents "cracking" multiple segments at the same point, which would likely cause topology collapse when being used on polygonal linework.
- Parameters:
srcCoords
- the coordinates of the source linestring to be snappedsnapPts
- the target snap vertices
-
findSegmentIndexToSnap
private int findSegmentIndexToSnap(Coordinate snapPt, CoordinateList srcCoords)
Finds a src segment which snaps to (is close to) the given snap point.Only a single segment is selected for snapping. This prevents multiple segments snapping to the same snap vertex, which would almost certainly cause invalid geometry to be created. (The heuristic approach to snapping used here is really only appropriate when snap pts snap to a unique spot on the src geometry.)
Also, if the snap vertex occurs as a vertex in the src coordinate list, no snapping is performed.
- Parameters:
snapPt
- the point to snap tosrcCoords
- the source segment coordinates- Returns:
- the index of the snapped segment or -1 if no segment snaps to the snap point
-
-