I have trouble getting anything useful out of a rtree (Rtree==0.8.2) intersection. Instead of returning the indices/IDs that I passed into the index on creation, I get zeros.
I have some data:
for element in data:
print(element)
POINT (497582.829135131 5419474.690534886) POINT (565856.4271351419 5988338.580648975) POINT (539734.212266123 5614400.468566706) POINT (683523.3673841552 5398102.521964455) POINT (633258.6526692112 5405182.75073655) POINT (353675.3945317736 5747638.443753068) POINT (480313.0993177936 5324783.150275591) POINT (476802.8443284882 5390481.26880122) POINT (392880.6204936574 5829710.142526797) POINT (483370.4752331344 5792650.901288186)
I have an area of interest that spans over some of those points:
aoi = data[4].buffer(100000)
# GeometryCollection((aoi, box(*aoi.bounds), MultiPoint(data))) # image below
I create an index based on rtree's stream load example and the documentation on the __init__ method:
def generator_function(geometries):
for i, geometry in enumerate(geometries):
# None instead of objects as I don't want to store the objects in the index
yield (i, geometry.bounds, None)
idx = index.Index(generator_function(data))
When I query the index with the aoi's bounds, I get two hits but their IDs are both "0".
candidates = list(idx.intersection(aoi.bounds))
print("We got {} results".format(len(candidates)))
print("Their indexes are: {}".format(candidates))
We got 2 results Their indexes are: [0, 0]
My problem: I expected to get the IDs as I passed them from the generator function. Why do I not get those?
If I fill the index step by step instead, I get the result I expected. I see no real logical difference in what should end up in the index though.
idx = index.Index()
for i, element in enumerate(data):
idx.insert(i, element.bounds)
candidates = list(idx.intersection(aoi.bounds))
print("We got {} results".format(len(candidates)))
print("Their indexes are: {}".format(candidates))
We got 2 results Their indexes are: [3, 4]
# GeometryCollection((box(*aoi.bounds), MultiPoint([data[i] for i in candidates]))) # Image below