(coords, target, tolerance, snap=True)
return np.vstack([coords, extrapolation])
Extends a line geometry to snap on the target within a tolerance.
Extends a line geometry to snap on the target within a tolerance.
Extends a line geometry to snap on the target within a tolerance .
def _extend_line(coords, target, tolerance, snap=True):
    """
    Extends a line geometry to snap on the target within a tolerance.
    """
    if snap:
        extrapolation = _get_extrapolated_line(
            coords[-4:] if len(coords.shape) == 1 else coords[-2:].flatten(),
            tolerance,
        )
        int_idx = target.sindex.query(extrapolation, predicate="intersects")
        intersection = pygeos.intersection(
            target.iloc[int_idx].geometry.values.data, extrapolation
        )
        if intersection.size > 0:
            if len(intersection) > 1:
                distances = {}
                ix = 0
                for p in intersection:
                    distance = pygeos.distance(p, pygeos.points(coords[-1]))
                    distances[ix] = distance
                    ix = ix + 1
                minimal = min(distances.items(), key=operator.itemgetter(1))[0]
                new_point_coords = pygeos.get_coordinates(intersection[minimal])

            else:
                new_point_coords = pygeos.get_coordinates(intersection[0])
            coo = np.append(coords, new_point_coords)
            new = np.reshape(coo, (int(len(coo) / 2), 2))

            return new
        return coords

    extrapolation = _get_extrapolated_line(
        coords[-4:] if len(coords.shape) == 1 else coords[-2:].flatten(),
        tolerance,
        point=True,
    )
    return np.vstack([coords, extrapolation])
def _extend_line ( coords , target , tolerance , snap = True ) : if snap : extrapolation = _get_extrapolated_line ( coords [ - 4 : ] if len ( coords . shape ) == 1 else coords [ - 2 : ] . flatten ( ) , tolerance , ) int_idx = target . sindex . query ( extrapolation , predicate = "intersects" ) intersection = pygeos . intersection ( target . iloc [ int_idx ] . geometry . values . data , extrapolation ) if intersection . size > 0 : if len ( intersection ) > 1 : distances = { } ix = 0 for p in intersection : distance = pygeos . distance ( p , pygeos . points ( coords [ - 1 ] ) ) distances [ ix ] = distance ix = ix + 1 minimal = min ( distances . items ( ) , key = operator . itemgetter ( 1 ) ) [ 0 ] new_point_coords = pygeos . get_coordinates ( intersection [ minimal ] ) else : new_point_coords = pygeos . get_coordinates ( intersection [ 0 ] ) coo = np . append ( coords , new_point_coords ) new = np . reshape ( coo , ( int ( len ( coo ) / 2 ) , 2 ) ) return new return coords extrapolation = _get_extrapolated_line ( coords [ - 4 : ] if len ( coords . shape ) == 1 else coords [ - 2 : ] . flatten ( ) , tolerance , point = True , ) return np . vstack ( [ coords , extrapolation ] )