TransWikia.com

Save GeoPandas spatial index to disk

Geographic Information Systems Asked on November 16, 2021

I’d like to save the R-tree spatial index generated by GeoPandas to disk.
GeoPandas automatically generates an index for each shape (points in this case) in the dataframe.
I’m attempting to create a new index using the R-trees built in built-in disk serialization, but not sure how to build it up to match the original dataframe.
I’ve made a hack by simply iterating over the number of shapes and adding bounds… this seems to work for my purposes but am quite sure this is not the correct approach.

Apologies for not taking the time to fully understanding how R-trees work.
I borrowed from this post Saving python Rtree spatial index to file? and found this discussion https://github.com/geopandas/geopandas/issues/426

import numpy as np, pandas as pd, geopandas as gpd
from shapely.geometry import Point
from rtree.index import Index

lats = np.arange(10, 20, 1)
lons = np.arange(60, 80, 2)
df        = pd.DataFrame({'lat': lats, 'lon': lons})
df['geo'] = df.apply(lambda df: Point(df['lon'], df['lat']), axis=1)
gdf       = gpd.GeoDataFrame(df, geometry='geo', crs='EPSG:4326')
sidx      = gdf.sindex
bounds    = sidx.bounds

idx       = Index('path')
for sid in range(sidx.get_size()):
    idx.insert(1, sidx.bounds)
idx.close()

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP