rtree icon indicating copy to clipboard operation
rtree copied to clipboard

Stream / generate nearest neighbors

Open dfuhry opened this issue 8 months ago • 3 comments

I am storing polygons / linestrings and I would like to stream (generate) nearest neighbors. This requires first iterating through nearest bounding boxes in order, with true-distance comparisons between the geometries they contain, and at some point cutting off that search.

Right now with the nearest() method I can get a fixed number of nearest neighbor bounding boxes to my query bounding box via the method's second parameter:

https://rtree.readthedocs.io/en/latest/tutorial.html#nearest-neighbors

But I cannot stream bounding boxes out with a generator. If I pass the length of the dataset as the second parameter then I can iterate through that list, but this is very slow for large datasets because it returns all boxes in the dataset sorted by distance, whereas in practice I likely need only the first few. (However the exact number I need depends on exact distance checks between the contained geometries, so I don't exactly know it beforehand.)

I'm not sure whether the inability to generate / yield a list of nearest-neighbor bounding boxes is a libspatialindex limitation, or only one of this library.

dfuhry avatar May 02 '25 11:05 dfuhry