Get the vertices on a LineString either side of a Point

前端 未结 2 1338
北恋
北恋 2021-01-15 02:46

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

How can I find the vertices of t

2条回答
  •  我在风中等你
    2021-01-15 03:23

    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:]
    

提交回复
热议问题