简体   繁体   English

如何使用rtree,python在查询框(查询区域)内找到最近的点

[英]how to find nearest point within querybox(query area) using rtree, python

I m new to python, I m using import rtree library.我是 python 新手,我正在使用 import rtree 库。 I m indexin the 2 d dimensional points data into rtree and specifying query box(rectangle).我将二维点数据索引到 rtree 并指定查询框(矩形)。 I should get the points which lie inside the query box.我应该得到位于查询框中的点。 But I m getting the nearest point which is out of the query box.但是我得到了查询框之外的最近点。

   My code is below
   from rtree import index
   idx = index.Index()
   left, bottom, right, top = (0.0, 0.0, 1.0, 1.0)
   idx.insert(0, (1,2))
   idx.insert(1, (8,2))
   idx.insert(2, (6,2))
   idx.insert(3, (1,2))
   idx.insert(4, (2,2))
   idx.insert(5, (3,2))
   idx.insert(6, (7,2))
   idx.insert(7, (1,2))
   idx.insert(8, (8,2))
   idx.insert(9, (6,2))
    query_box = [100,29,144,90]
    list(idx.nearest((query_box)))

but I m getting results which are out of query box.但我得到的结果超出了查询框。

from rtree import index
idx = index.Index()
left, bottom, right, top = (0.0, 0.0, 1.0, 1.0)
idx.insert(0, (1,2))
idx.insert(1, (8,2))
idx.insert(2, (6,2))
idx.insert(3, (1,2))
idx.insert(4, (2,2))
idx.insert(5, (3,2)) 
idx.insert(6, (7,2))
idx.insert(7, (1,2))
idx.insert(8, (8,2))
idx.insert(9, (6,2))  
query_box = [100,29,144,90]   
list(idx.nearest((query_box)))
Out[142]: [1, 8]

In the output, you can see that rtree is returning index 1, 8 which close to the outside of the query box.在输出中,您可以看到 rtree 正在返回靠近查询框外部的索引 1、8。 but I need only the points which lie inside the query box.但我只需要位于查询框中的点。 is their any suggestion to get points which lie inside the query box using rtree.他们是否建议使用 rtree 获取位于查询框中的点。

If you want the intersection, use the method intersection .如果你想要交集,使用方法intersection

The method nearest returns the nearest neighbors, as the name indicates.该方法nearest返回最近的邻居,顾名思义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM