python – Shapely中两个几何的最近点的坐标
作者:互联网
存在具有顶点坐标列表的折线= [(x1,y1),(x2,y2),(x3,y3),…]和点(x,y).在Shapely中,geometry1.distance(geometry2)返回两个几何之间的最短距离.
>>> from shapely.geometry import LineString, Point
>>> line = LineString([(0, 0), (5, 7), (12, 6)]) # geometry2
>>> list(line.coords)
[(0.0, 0.0), (5.0, 7.0), (12.0, 6.0)]
>>> p = Point(4,8) # geometry1
>>> list(p.coords)
[(4.0, 8.0)]
>>> p.distance(line)
1.4142135623730951
但我还需要找到最接近点(x,y)的线上的点的坐标.在上面的示例中,这是LineString对象上距离Point(4,8)1.4142135623730951单位的点的坐标.方法distance()在计算距离时应该有坐标.有没有办法让它从这个方法返回?
解决方法:
您描述的GIS术语是linear referencing和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)
另一种方法是使用nearest_points
:
from shapely.ops import nearest_points
np = nearest_points(line, p)[0]
print(np) # POINT (5 7)
它提供了与线性参考技术相同的答案,但可以从更复杂的几何输入中确定最近的一对点,如两个多边形.
标签:shapely,python,distance,geos 来源: https://codeday.me/bug/20190926/1822010.html