在Shapely中查找路径起点的坐标距离

时间:2015-03-02 08:18:07

标签: python geometry distance shapely

我有一个表示路线的坐标列表(lat / lon)。 给定一定半径和另一个坐标,我需要检查坐标是否在路线中(在任何点的给定半径内)以及它与路线起点的距离。

我看着Shapely,它看起来是一个很好的解决方案。

我开始创建StringLine

from shapely.geometry import LineString
route = LineString[(x, y), (x1, y1), ...]

然后检查点是否在路线附近我添加了一个缓冲区并检查 交叉路口

from shapely.geometry import Point
p = Point(x, y)
r = 0.5
intersection = p.buffer(r).intersection(route)
if intersection.is_empty: 
    print "Point not on route"
else: 
    # Calculate P distance from the begning of route

我卡住计算距离。我想在p分割路线并测量前半部分的长度,但我得到的交叉口结果是HeterogeneousGeometrySequence,我不知道我能做些什么。

我相信我找到了解决方案:

if p.buffer(r).intersects(route):
 return route.project(p)

1 个答案:

答案 0 :(得分:2)

不是缓冲一个昂贵且不完美的几何体(因为缓冲需要许多段和many other options),只要看看该点是否在距离阈值内:

if route.distance(p) <= r:
    return route.project(p)

此外,您现在可能已经意识到您的距离单位是度数。如果你想要线性距离,比如米,你需要使用不同的库来使它更加复杂。