Get the vertices on a LineString either side of a

2019-02-25 16:15发布

I have a shapely LineString and have defined a shapely Point which lies along the LineString.

How can I find the vertices of the LineString which lie either side of the point? (split the line in two)

1条回答
Juvenile、少年°
2楼-- · 2019-02-25 16:53

Locate the line segment in the LineString where the point lies. Then split in two groups the vertices of the LineString accordingly. To locate the line segment, simply apply a point / line segment intersection test to each segment.

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:]
查看更多
登录 后发表回答