public class LineStringSnapper
extends java.lang.Object
LineString
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.
Modifier and Type | Field and Description |
---|---|
private boolean |
allowSnappingToSourceVertices |
private boolean |
isClosed |
private LineSegment |
seg |
private double |
snapTolerance |
private Coordinate[] |
srcPts |
Constructor and 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 given
LineString
as source snap points. |
Modifier and Type | Method and 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.
|
private double snapTolerance
private Coordinate[] srcPts
private LineSegment seg
private boolean allowSnappingToSourceVertices
private boolean isClosed
public LineStringSnapper(LineString srcLine, double snapTolerance)
LineString
as source snap points.srcLine
- a LineString to snap (may be empty)snapTolerance
- the snap tolerance to usepublic LineStringSnapper(Coordinate[] srcPts, double snapTolerance)
srcPts
- the points to snapsnapTolerance
- the snap tolerance to usepublic void setAllowSnappingToSourceVertices(boolean allowSnappingToSourceVertices)
private static boolean isClosed(Coordinate[] pts)
public Coordinate[] snapTo(Coordinate[] snapPts)
snapPts
- the vertices to snap toprivate void snapVertices(CoordinateList srcCoords, Coordinate[] snapPts)
srcCoords
- the points to snapsnapPts
- the points to snap toprivate Coordinate findSnapForVertex(Coordinate pt, Coordinate[] snapPts)
private void snapSegments(CoordinateList srcCoords, Coordinate[] snapPts)
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.
srcCoords
- the coordinates of the source linestring to be snappedsnapPts
- the target snap verticesprivate int findSegmentIndexToSnap(Coordinate snapPt, CoordinateList srcCoords)
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.
snapPt
- the point to snap tosrcCoords
- the source segment coordinates