Class LineStringSnapper


  • public class LineStringSnapper
    extends java.lang.Object
    Snaps the vertices and segments of a 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.

    Version:
    1.7
    • Field Detail

      • snapTolerance

        private double snapTolerance
      • 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 given LineString 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 snap
        snapTolerance - 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 snap
        snapPts - the points to snap to
      • 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 snapped
        snapPts - 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 to
        srcCoords - the source segment coordinates
        Returns:
        the index of the snapped segment or -1 if no segment snaps to the snap point