Toblerity / rtree

Rtree: spatial index for Python GIS
https://rtree.readthedocs.io
MIT License
627 stars 123 forks source link

About RTree Data Structure Persistent Storage #284

Closed RuoCJ closed 1 year ago

RuoCJ commented 1 year ago

I wanted to persist the built Rtree in my code, but what was stored was an empty pickle file. The code is as follows:

import rtree
import pandas as pd
import pickle
spatial_index = rtree.index.Index()

df = pd.read_csv("block_loc", sep='\t')

for i, row in df.iterrows():
    id = int(row[0])
    longitude = float(row[1])
    latitude = float(row[2])
    print(id, longitude, latitude)
    spatial_index.insert(id, (longitude, latitude))
print(spatial_index)
with open("spatial_index.pickle", "wb") as f:
    pickle.dump(spatial_index, f)

Rtree object:rtree.index.Index(bounds=[-150, 38, 150, 40], size=689274)

Objects read from pickle file:rtree.index.Index(bounds=[1.7976931348623157e+308, 1.7976931348623157e+308, -1.7976931348623157e+308, -1.7976931348623157e+308], size=0)

Why is this

adamjstewart commented 1 year ago

See #87, pickling is currently broken, fixes would be very appreciated.

RuoCJ commented 1 year ago

See #87, pickling is currently broken, fixes would be very appreciated.

Excuse me, do you want me to try fixing this bug?

adamjstewart commented 1 year ago

If you can that would be great. I tried in #197 but couldn't come up with a satisfying solution.