Discussion:
[Community] rtree nearest()
Redoute
2012-10-24 03:59:38 UTC
Permalink
Hello all,

in some Python scripts I want to query an rtree index like this:

for member in ix.nearest(point, <infinite>):
if check(member): break

Setting num_results to high numbers gets expensive with a large index.
Do you have general advice how to deal with this?

Could nearest() (and maybe intersection() too) be modified, so that the
work is delayed into the generator to allow "open end" queries?

Thank You,
Redoute
Howard Butler
2012-10-25 19:49:54 UTC
Permalink
Post by Redoute
Hello all,
if check(member): break
Setting num_results to high numbers gets expensive with a large index.
Do you have general advice how to deal with this?
Could nearest() (and maybe intersection() too) be modified, so that the
work is delayed into the generator to allow "open end" queries?
Redoute,

This is not currently possible, and it would take some updates to the C API of libspatialindex to support it. In short, the C API constructs an array for all of the results in memory before handing it back to the client (in this case Python).

The base libspatialindex library itself absolutely supports this through it its visitor pattern, however. Presumably there would need to be some sort of callback-based method, much like the way the DataStream stuff in the C API works in the reverse direction, to connect up the Python side to the results visitor.

I'm not sure this helps you too much, but if you're looking to go deep, the direction I have described is probably the way to start trekking.

Howard

http://hobu.biz
http://pointcloud.org
Redoute
2012-10-28 17:47:54 UTC
Permalink
Thank you, Howard, for giving some background. "Going deep" here is
beyond my skills, I think.

I now use a simple wrapper function/generator as workaround. It queries
the index with num_results set to powers of ten. Works for me so far:

def nearestObjects(ix, coord, lenIx):
n1 = min(lenIx, 10)
n2 = 0
while n1 > n2:
buffer = ix.nearest(coord, n1, objects=True)
for i in xrange(n2): buffer.next()
for x in buffer: yield x
n2 = n1
n1 = min(lenIx, 10 * n1)

Redoute
Sean Gillies
2012-10-30 15:50:14 UTC
Permalink
Hi Redoute,

Real generators would be a nice feature. Would you please add it to
https://github.com/Toblerity/rtree/issues? I'm not sure when I'd be
free to tackle it or if I have any C++ skills left, but I don't want
to lose track of the idea.
Post by Redoute
Thank you, Howard, for giving some background. "Going deep" here is
beyond my skills, I think.
I now use a simple wrapper function/generator as workaround. It queries
n1 = min(lenIx, 10)
n2 = 0
buffer = ix.nearest(coord, n1, objects=True)
for i in xrange(n2): buffer.next()
for x in buffer: yield x
n2 = n1
n1 = min(lenIx, 10 * n1)
Redoute
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
--
Sean Gillies
Loading...