Shapely中两个几何的最近点的坐标 [英] Coordinates of the closest points of two geometries in Shapely

查看:621
本文介绍了Shapely中两个几何的最近点的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一条折线,其中折线的顶点列表为[[x1,y1),(x2,y2),(x3,y3),...]和一个点(x,y).在Shapely中,geometry1.distance(geometry2)返回两个几何之间的最短距离.

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) returns the shortest distance between the two geometries.

>>> 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()应具有坐标.有什么办法可以从此方法返回它吗?

But I also need to find the coordinate of the point on the line that is closest to the point(x,y). In the example above, this is the coordinate of the point on the LineString object that is 1.4142135623730951 unit distant from Point(4,8). The method distance() should have the coordinates when calculating the distance. Is there any way to get it returned from this method?

推荐答案

您正在描述的GIS术语是 linear引用,并且形状上有这些方法

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)


另一种方法是使用 nearest_points :


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.

这篇关于Shapely中两个几何的最近点的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆