我有一个形状优美的 LineString 并定义了一个形状优美的 Point,它位于 LineString 上。
如何找到 LineString 位于点两侧的顶点? (将线一分为二)
请您参考如下方法:
在点所在的LineString中定位线段。然后将 LineString 的顶点相应地分成两组。要定位线段,只需对每个线段应用点/线段相交测试。
from shapely.geometry import Point,LineString
def split(line_string, point):
coords = line_string.coords
j = None
for i in range(len(coords) - 1):
if LineString(coords[i:i + 2]).intersects(point):
j = i
break
assert j is not None
# Make sure to always include the point in the first group
if Point(coords[j + 1:j + 2]).equals(point):
return coords[:j + 2], coords[j + 1:]
else:
return coords[:j + 1], coords[j:]
