See the tests/benchmarks.py file for a comparison of various query methods and how much acceleration can be obtained from using Rtree.
There are a few simple things that will improve performance.
Use stream loading¶
This will substantially (orders of magnitude in many cases) improve
insert() by allowing the data to
>>> def generator_function(somedata): ... for i, obj in enumerate(somedata): ... yield (i, (obj.xmin, obj.ymin, obj.xmax, obj.ymax), obj) >>> r = index.Index(generator_function(somedata))
After bulk loading the index, you can then insert additional records into
the index using
dumps to use the highest pickle protocol¶
>>> import cPickle, rtree >>> class FastRtree(rtree.Rtree): ... def dumps(self, obj): ... return cPickle.dumps(obj, -1) >>> r = FastRtree()
>>> objs = r.intersection((xmin, ymin, xmax, ymax), objects="raw")
Adjust index properties¶
rtree.index.Property appropriate to your index.
Limit dimensionality to the amount you need¶
Don’t use more dimensions than you actually need. If you only need 2, only use two. Otherwise, you will waste lots of storage and add that many more floating point comparisons for each query, search, and insert operation of the index.