5

I would like to save an Rtree spatial index to file. I tried using pickle.dump, but when I loaded the .p file using pickle.load, the bounds were way off. Here's an example:

import rtree, pickle
from rtree import index
iidx=index.Index()
iidx.add(0,(1,2,3,4))
f=open('rtree2.p','wb')
pickle.dump(iidx,f)
f.close()

When I try opening the file:

f=open('rtree2.p','rb')
uudx=pickle.load(f)
f.close()
uudx.get_bounds()

I get:

[1.7976931348623157e+308, 1.7976931348623157e+308, -1.7976931348623157e+308, -1.7976931348623157e+308]
  • The Rtree documentation appears to use dumps and loads, which writes the index as a string – user3433489 Sep 8 '17 at 20:58
  • 1
    I think the dumps and loads are just for serializing Python objects for storage in the index. I don't think you can pickle the index object since it's just a ctypes wrapper around libspatialindex. – Marc Pfister Sep 8 '17 at 22:00
  • @marc-pfister I did this on a test index, and the shape of the index seems to have changed. The original index I used had two index nodes each with leaves. I "saved" this to file using your insert method, and reloaded it, but the new index has just one index node with all the leaves in that. – user122616 Jun 18 '18 at 14:04
7

Is the built-in disk serialization an acceptable solution?

Make an index and flush it to disk:

from rtree.index import Index
idx = Index('filename')
idx.insert(0, (1, 2, 3, 4))
idx.get_bounds()
# [1.0, 2.0, 3.0, 4.0]
idx.close()

Load the index at a later point in time:

from rtree.index import Index
idx = Index('filename')
idx.get_bounds()
# [1.0, 2.0, 3.0, 4.0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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