2016-12-24 19 views
0

我有以下的设置 - 它的rtree建立在两点:Python rtree最近 - 它究竟做了什么?

from collections import defaultdict 
from math import sqrt 
import rtree.index 

points = [(5, 4), (3, 1), (6, 3), (2, 8), (7, 8), (8, 1), (2, 3), (0, 4), (3, 7), (6, 4)] 

idx = rtree.index.Rtree() 

for i, p in enumerate(points): 
    idx.insert(i, p+p, p) 

现在我试图找到某些点一定距离内的所有点:

max_distance=5 
p = (6,4) 
list(idx.nearest(p, 5, objects='raw')) 

我收到

[(6, 4), (6, 3), (5, 4), (8, 1), (2, 3), (7, 8)] 

问题是 - 为什么(3, 1)未包含在列表中?距离是〜4.24所以它应该包括在内,对吗?

+0

你为什么在'p + p'插入? –

回答

1

最接近的方法“返回k个最近的对象到给定的坐标”,也就是说,它将返回与其距离无关的最近的对象。

对象的距离不是最接近函数的参数,如Rtree documentation所述。第二个参数是你想要的结果数目。在你的情况下,它返回六个值,因为点(6,3),(5,4)与点(6,4)具有相同的距离(1)。

要获得一定距离内的物体,您应该使用the intersection method