Coordinates of the closest points of two geometries in Shapely

前端 未结 2 816
旧巷少年郎
旧巷少年郎 2020-12-01 03:39

There is a polyline with a list of coordinates of the vertices = [(x1,y1), (x2,y2), (x3,y3),...] and a point(x,y). In Shapely, geometry1.distance(geometry2) re

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 04:23

    The GIS term you are describing is linear referencing, and Shapely has these methods.

    # Length along line that is closest to the point
    print(line.project(p))
    
    # Now combine with interpolated point on line
    np = line.interpolate(line.project(p))
    print(np)  # POINT (5 7)
    

    An alternative method is to use nearest_points:

    from shapely.ops import nearest_points
    np = nearest_points(line, p)[0]
    print(np)  # POINT (5 7)
    

    which provides the same answer as the linear referencing technique does, but can determine the nearest pair of points from more complicated geometry inputs, like two polygons.

提交回复
热议问题