python - GeoPandas 的过度功能不起作用
问题描述
我只是想用来geopandas
获得两个多边形区域的联合和交集。我定义:
import geopandas as gpd
from shapely.geometry import Polygon
polys1 = gpd.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
Polygon([(2,2), (4,2), (4,4), (2,4)])])
polys2 = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
Polygon([(3,3), (5,3), (5,5), (3,5)])])
df1 = gpd.GeoDataFrame({'geometry': polys1, 'df1':[1,2]})
df2 = gpd.GeoDataFrame({'geometry': polys2, 'df2':[1,2]})
我尝试以下方法来获得union
:
res_union = gpd.overlay(df1, df2, how='union')
它失败并出现以下错误:
AttributeError: 'NoneType' object has no attribute 'intersection'
我正在按照这里的说明进行操作。
解决方案
尽管我不知道 OP 的操作系统,但我认为我已经找到了解决问题的方法,至少对于 GNU/Linux 系统(我无法在其他系统中进行测试)。
直接解释
为了能够使用overlay
您需要的功能不仅仅是安装geopandas
,您需要安装rtree
,但rtree
它是 C 库libspatialindex的包装器。因此,要使用rtree
库,您需要安装libspatialindex
C 库。
要安装libspatialindex
开放式终端类型:
sudo apt-get update && apt-get install -y libspatialindex-dev
注意:实际上您只需要sudo apt-get install libspatialindex-dev
,但最好更新系统,并且 -y 标志只是为了不停止安装过程以询问是否继续安装。
现在它应该可以解决您的问题。注意:确保您已rtree
在系统中安装,您可以使用pip3 freeze
(我假设您使用python3
)来执行此操作。
长解释
我遇到了同样的错误,并花了很多时间来找出问题所在。这个问题的答案libspatialindex and Rtree on python给了我一个关于如何解决问题的提示。
考虑下面的代码(OP的代码示例)并保存命名为script.py
:
import geopandas as gpd
from shapely.geometry import Polygon
polys1 = gpd.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
Polygon([(2,2), (4,2), (4,4), (2,4)])])
polys2 = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
Polygon([(3,3), (5,3), (5,5), (3,5)])])
df1 = gpd.GeoDataFrame({'geometry': polys1, 'df1':[1,2]})
df2 = gpd.GeoDataFrame({'geometry': polys2, 'df2':[1,2]})
res_union = gpd.overlay(df1, df2, how='union')
考虑以下内容requirements.txt
:
Shapely==1.6.4.post2
descartes==1.1.0
geopandas==0.4.0
matplotlib==3.0.2
如果您尝试只安装requirements.txt
并运行中的库scrip.py
,而不是rtree
根据安装库requirements.txt
,它将显示以下错误消息:
/usr/local/lib/python3.6/site-packages/geopandas/base.py:76: UserWarning: Cannot generate spatial index: Missing package `rtree`.
warn("Cannot generate spatial index: Missing package `rtree`.")
Traceback (most recent call last):
File "script.py", line 17, in <module>
res_union = gpd.overlay(df1, df2, how='union')
File "/usr/local/lib/python3.6/site-packages/geopandas/tools/overlay.py", line 371, in overlay
result = _overlay_union(df1, df2)
File "/usr/local/lib/python3.6/site-packages/geopandas/tools/overlay.py", line 298, in _overlay_union
dfinter = _overlay_intersection(df1, df2)
File "/usr/local/lib/python3.6/site-packages/geopandas/tools/overlay.py", line 212, in _overlay_intersection
sidx = bbox.apply(lambda x: list(spatial_index.intersection(x)))
File "/usr/local/lib/python3.6/site-packages/pandas/core/series.py", line 3194, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas/_libs/src/inference.pyx", line 1472, in pandas._libs.lib.map_infer
File "/usr/local/lib/python3.6/site-packages/geopandas/tools/overlay.py", line 212, in <lambda>
sidx = bbox.apply(lambda x: list(spatial_index.intersection(x)))
AttributeError: 'NoneType' object has no attribute 'intersection'
错误信息的最后一行
AttributeError: 'NoneType' object has no attribute 'intersection'
不是那么有用。但是,如果您查看第一行:
/usr/local/lib/python3.6/site-packages/geopandas/base.py:76: UserWarning: Cannot generate spatial index: Missing package
rtree.
它在抱怨rtree
图书馆。
因此,让我们安装rtree
并看看会发生什么。requirements.txt
现在更新为:
Shapely==1.6.4.post2
descartes==1.1.0
geopandas==0.4.0
matplotlib==3.0.2
rtree==0.8.3
再次运行script.py
我得到以下错误:
Traceback (most recent call last):
File "script.py", line 3, in <module>
import geopandas as gpd
File "/usr/local/lib/python3.6/site-packages/geopandas/__init__.py", line 1, in <module>
from geopandas.geoseries import GeoSeries
File "/usr/local/lib/python3.6/site-packages/geopandas/geoseries.py", line 12, in <module>
from geopandas.base import GeoPandasBase, _series_unary_op, _CoordinateIndexer
File "/usr/local/lib/python3.6/site-packages/geopandas/base.py", line 14, in <module>
from rtree.core import RTreeError
File "/usr/local/lib/python3.6/site-packages/rtree/__init__.py", line 1, in <module>
from .index import Rtree
File "/usr/local/lib/python3.6/site-packages/rtree/index.py", line 5, in <module>
from . import core
File "/usr/local/lib/python3.6/site-packages/rtree/core.py", line 125, in <module>
raise OSError("Could not find libspatialindex_c library file")
OSError: Could not find libspatialindex_c library file
最后一行抱怨libspatialindex_c
,所以正如我的答案的第一部分“直接解释”中所解释的那样,只需运行下面的代码来安装libspatialindex
并且script.py
应该可以工作。
sudo apt-get update && apt-get install -y libspatialindex-dev
至少对我来说问题解决了。
推荐阅读
- sublimetext3 - 我在 sublime text 3 上找不到像 notepad++ 这样的 html 标签建议框
- python - 使用python将文件上传到共享点时出错
- ruby - 你能支持我在 Ruby 的 while 循环中使用序数吗?
- google-cloud-platform - 为什么我的云运行部署挂在“部署...”步骤?
- javascript - HTML 下载文件按钮仅保存为 HTML 文档
- c# - 什么是 CloudConfigurationManager.GetSetting(
, 错误的);? - r - 导出格式表
- gcc - 如何指示 mingw64-gcc 使用特定的 mingw-w64-headers/crt
- c++ - 虚函数上的 std::is_trivially_copyable_v
- webpack - 错误:在无服务器框架的 lambda 部署问题中找不到模块“/bin”