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

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

我正在尝试在 python 中剪辑空间数据,但是当我 运行 我的代码时...

europe = gpd.clip(worldmap, europe_bound_gdf)

...我收到错误:

(ImportError: Spatial indexes require either rtreeorpygeos`.)

当我尝试使用以下方式安装 rtree 时:

pip3 install rtree

我被告知:

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

为什么我的代码不起作用?

我遇到了同样的问题,这为我解决了:

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

找到答案here

就我而言,唯一有帮助的是:

安装并导入 condalab,运行 两次。

!pip install -q condacolab -q

import condacolab

condacolab.install()

安装 geopandas,这可能需要很长时间

!conda install geopandas

import geopandas as gpd

对我有用,但作为 Mac 用户,我的步骤略有不同:

pip uninstall rtree
brew install spatialindex
pip install rtree

根据文档 here,您可以将 geopandas 设置为使用 pygeos。
注意:只有在更改此值后创建了您使用的 GeoDataFrames(例如,使用 read_file 读取文件)时,它才会起作用。因此,重新导入 geopandas 和 运行 这两行之后,再次加载 geopandas 数据帧。

import pygeos
geopandas.options.use_pygeos = True