6

I am trying to do an intersection with 2 GeoDataFrame using the gdf.overlay function on Google Colab. Please see the code below

!sudo apt install libspatialindex-dev
!sudo pip3 install rtree
!pip install pygeos

overlap = gpd.overlay(gdf1,gdf2, how='intersection')
overlap.plot(figsize=(10,10), cmap='jet')

and I get the following error message

ImportError: Spatial indexes require either `rtree` or `pygeos`. See installation instructions at https://geopandas.org/install.html

What is causing the issue here?

Arturo Sbr
  • 5,567
  • 4
  • 38
  • 76
  • You used both `pip` and `pip3`. They likely operate on different python installations – Paul H Mar 26 '21 at 18:46
  • 2
    use `conda` to install `rtree` and update your python to `cpython` using `conda-forge`. – BetterCallMe Mar 27 '21 at 05:21
  • 1
    Try using a venv (`python3 -mvenv your-virtual-env`), then avoid (always!) `sudo pip`. pip-installing packages, once you have activated our virtual-env, shoud produce a coherent library ecosystem in a more reliable way. – olepinto Apr 12 '21 at 08:12

1 Answers1

5

I had the same issue with running this on colab, it's because of dependencies, follow the same order as suggested in: https://geopandas.org/getting_started/install.html

For me, error gone when I first installed dependencies:

!pip install pandas fiona shapely pyproj rtree

and then:

!pip install geopandas
Salman
  • 113
  • 2
  • 5
  • 2
    For me, this did not work. – zabop Feb 21 '22 at 12:19
  • 1
    Thank you so much it works fine for me, uninstalling my geopandas first – Jhonny Carvajal Perez Aug 10 '22 at 13:44
  • Note that as mentioned in the [`geopandas.sindex.SpatialIndex.query` doc](https://geopandas.org/en/stable/docs/reference/api/geopandas.sindex.SpatialIndex.query.html#geopandas-sindex-spatialindex-query), if performance is an issue, prefer using [`pygeos`](https://pygeos.readthedocs.io/en/stable/) as a "backend" instead of `rtree`. – jmon12 Nov 24 '22 at 13:43