其他分享
首页 > 其他分享> > 两点截取线【shapely】【clipline】

两点截取线【shapely】【clipline】

作者:互联网

from shapely.geometry import Point, LineString
from shapely.ops import snap, split, nearest_points
def clipLine(ls, p1, p2):
    p1 = nearest_points(ls, p1)[0]
    geom = snap(ls, p1, 1)
    parts = split(geom, p1)
    if len(parts) == 1:
        fp = parts
    else:
        fp = parts[0] if parts[0].distance(p2) < parts[1].distance(p2) else parts[1]

    p2 = nearest_points(fp, p2)[0]
    geom = snap(fp, p2, 0.1)
    parts = split(geom, p2)

    if len(parts) == 1:
        res = parts
    else:
        res = parts[0] if parts[0].distance(p1) < parts[1].distance(p1) else parts[1]
    return res


if __name__ == '__main__':

    ls = LineString([(0, 0), (5, 5)])
    p1 = Point((1, 1))
    p2 = Point([3, 3])
    res = clipLine(ls, p1, p2)
    print(res.wkt) # LINESTRING (1 1, 3 3)

 

标签:p2,__,p1,shapely,res,截取,clipline,parts,ls
来源: https://www.cnblogs.com/ddzhen/p/15221621.html