ImportError: Spatial indexes require either `rtree` or `pygeos` in geopanda but rtree is installed

Viewed 10737

I am trying to clip spatial data in python but when I run my code...

europe = gpd.clip(worldmap, europe_bound_gdf)

... I get the error:

(ImportError: Spatial indexes require either rtreeorpygeos`.)

When I try to install rtree using:

pip3 install rtree

I'm told:

Requirement already satisfied: rtree in /Users/joshuajones/.pyenv/versions/3.9.4/lib/python3.9/site-packages (0.9.7`)

So why is my code not working?

4 Answers

I had the same issue and this solved it for me:

pip uninstall rtree
sudo apt install libspatialindex-dev
pip install rtree

Found the answer here.

GStav's answer worked for me, but as a Mac user my steps were slightly different:

pip uninstall rtree
brew install spatialindex
pip install rtree

In my case, the only thing that helped:

install and import condalab, run twice.

!pip install -q condacolab -q

import condacolab

condacolab.install()

install geopandas, this could take long

!conda install geopandas

import geopandas as gpd

From the documentation here, you can set geopandas to use pygeos.
Note: it will only work if the GeoDataFrames you use are created (e.g. reading a file with read_file) after changing this value. So do a fresh import of geopandas and run these two lines after that and next, load the geopandas dataframes again.

import pygeos
geopandas.options.use_pygeos = True
Related