16

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?

JoshuaAJones
  • 316
  • 1
  • 2
  • 7

4 Answers4

15

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
  • 1,066
  • 12
  • 20
6

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
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Dan Sadler
  • 165
  • 2
  • 4
3

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
Ihor Konovalenko
  • 1,298
  • 2
  • 16
  • 21
katze
  • 31
  • 1
1

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
Lyrax
  • 331
  • 2
  • 6