I'm trying to split a Shapely LineString at the nearest point to some other coordinate. I can get the closest point on the line using project
and interpolate
but I am unable to split the line at this point as it is not a vertex.
I need to split the line along the edge, not snapped to the nearest vertex, so that the nearest point becomes a new vertex on the line.
Here's what I've done so far:
from shapely.ops import split
from shapely.geometry import Point, LineString
line = LineString([(0, 0), (5,8)])
point = Point(2,3)
# Find coordinate of closest point on line to point
d = line.project(point)
p = line.interpolate(d)
print(p)
# >>> POINT (1.910112359550562 3.056179775280899)
# Split the line at the point
result = split(line, p)
print(result)
# >>> GEOMETRYCOLLECTION (LINESTRING (0 0, 5 8))
Thanks!