【GIS】【shapely】【trimLine】

from shapely.geometry import Point, LineString
from shapely.ops import split

def trimLine(line, p1, p2):
# 根据p1,p2截取line上的一段
# 这里要求p1, p2在线line上
gs = split(line, p1)
gs = [split(_, p2) for _ in gs if _.intersects(p2)][0]
res = [_ for _ in gs if _.intersects(p1)][0]

return res


if __name__ == '__main__':

arr = [[0, 0], [10, 0]]
brr = [3, 0]
crr = [7, 0]
line = LineString(arr)
p1 = Point(brr)
p2 = Point(crr)

res = trimLine(line, p1, p2)
原文地址:https://www.cnblogs.com/ddzhen/p/14700419.html