1

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

points, aoi and aoi bounds

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

aoi bounds and points from intersection

2 Answers 2

0

Strange, because with Python 2.7.12 or 3.5.1; Shapely 1.5.16 and rtree 0.8.2, I have no problem

from shapely.geometry import Point
data = [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)]
aoi = data[4].buffer(100000)
from rtree import index
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))
candidates = list(idx.intersection(aoi.bounds))
print("We got {} results".format(len(candidates)))
print("Their indexes are: {}".format(candidates))
print("the Points:",data[3].wkt, data[4].wkt)
We got 2 results
Their indexes are: [4, 3]
the Points: POINT (683523.3673841552 5398102.521964455) POINT (633258.6526692112 5405182.75073655)

What are your versions ?

(edit for 6 char limit)

3
  • Python 2.7.12, Shapely==1.5.17, Rtree==0.8.2. So only Shapely differs. I am on Linux. Will ask around for others to test and find hints what might be wrong. Thank you! Commented Nov 22, 2016 at 17:37
  • tried with shapely 1.5.16 in python 2.7.12 and 3.5.2, same wrong output Commented Nov 22, 2016 at 17:46
  • It is not a problem of shapely. I am on Mac OS X and Linux
    – gene
    Commented Nov 22, 2016 at 18:00
0

It looks like this was a bug that has been fixed in rtree 0.8.3.

https://github.com/Toblerity/rtree/issues/71

$ python2 minimalexample.py 
We got 2 results
Their indexes are: [0, 0]

$ pip2 freeze | grep Rtree
Rtree==0.8.2

$ pip2 install --user rtree==0.8.3
(...)
Installing collected packages: rtree
  Found existing installation: Rtree 0.8.2
    Uninstalling Rtree-0.8.2:
      Successfully uninstalled Rtree-0.8.2
Successfully installed rtree-0.8.3
$ python2 minimalexample.py 

We got 2 results
Their indexes are: [4, 3]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.