6

Pickling Rtree does not appear straightforward as it is a ctypes wrapper. This comment on SO seconds that assumption.

But, in a (much) older @sgillies post (the author of this lib), in the comments section, he suggests that it is indeed doable.

Yet, when I recreate these steps locally, the results suggest otherwise:

>>> idx = rtree.index.Index()
>>> idx.insert(10, (1,2,3,4))
>>> list(idx.intersection((0,0,5,5)))
# [10]
>>> f = open('foo.p', 'wb')
>>> pickle.dump(idx, f)
>>> a = pickle.load( open( "foo.p", "rb" ) )
>>> a.get_bounds()
# [1.7976931348623157e+308, 1.7976931348623157e+308, -1.7976931348623157e+308, -1.7976931348623157e+308]]
>>> list(a.intersection((0,0,5,5)))
# []

Question: Is there an operation I am failing to perform correctly to enable pickling of the spatial index? If spatial indexing is possible, what would be the correct way to go about it?

Anecdotally, I was able to pass a GeoPandas.GeoDataFrame.sindex successfully through a pickling process (pickling performed by Dask.distributed). I'm aware it uses cloudpickle or pickle (dep. on situation) but from the GeoPandas side, sindex's SpatialIndex class seems to simply be a wrapper around rtree.index.Index. I have yet to more deeply dive into understanding why this was the case, but wanted to check here first to see if others had insight.

kuanb
  • 1,618
  • 2
  • 20
  • 42

1 Answers1

0

It looks like you can write an index to a file, so it should be possible to store it as a string: https://gis.stackexchange.com/questions/254781/saving-python-rtree-spatial-index-to-file/254785

You could also try Dill, which I believe is capable of 'pickling' more kinds of objects, or at least saving the state of the interpreter: https://pypi.python.org/pypi/dill

user1478842
  • 135
  • 4
  • 10