private static LineString [ ] splitLineStringWithPoint ( LineString line , Point pointToSplit , double tolerance ) { Coordinate [ ] coords = line . getCoordinates ( ) ; Coordinate firstCoord = coords [ 0 ] ; Coordinate lastCoord = coords [ coords . length - 1 ] ; Coordinate coordToSplit = pointToSplit . getCoordinate ( ) ; if ( ( coordToSplit . distance ( firstCoord ) <= PRECISION ) || ( coordToSplit . distance ( lastCoord ) <= PRECISION ) ) { return new LineString [ ] { line } ; } else { ArrayList < Coordinate > firstLine = new ArrayList < Coordinate > ( ) ; firstLine . add ( coords [ 0 ] ) ; ArrayList < Coordinate > secondLine = new ArrayList < Coordinate > ( ) ; GeometryLocation geometryLocation = EditUtilities . getVertexToSnap ( line , pointToSplit , tolerance ) ; if ( geometryLocation != null ) { int segmentIndex = geometryLocation . getSegmentIndex ( ) ; Coordinate coord = geometryLocation . getCoordinate ( ) ; int index = - 1 ; for ( int i = 1 ; i < coords . length ; i ++ ) { index = i - 1 ; if ( index < segmentIndex ) { firstLine . add ( coords [ i ] ) ; } else if ( index == segmentIndex ) { coord . z = CoordinateUtils . interpolate ( coords [ i - 1 ] , coords [ i ] , coord ) ; firstLine . add ( coord ) ; secondLine . add ( coord ) ; if ( ! coord . equals2D ( coords [ i ] ) ) { secondLine . add ( coords [ i ] ) ; } } else { secondLine . add ( coords [ i ] ) ; } } LineString lineString1 = FACTORY . createLineString ( firstLine . toArray ( new Coordinate [ 0 ] ) ) ; LineString lineString2 = FACTORY . createLineString ( secondLine . toArray ( new Coordinate [ 0 ] ) ) ; return new LineString [ ] { lineString1 , lineString2 } ; } } return null ; }
Splits a LineString using a Point with a distance tolerance .