2014-01-28 76 views

回答

2

找到線段在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:] 
相關問題