pandas GeoPandas 的过度功能不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53546775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overly Function from GeoPandas Not Working
提问by Rotail
I simply want to use geopandas
to get a union and intersection of two polygonal areas. I define:
我只是想用来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]})
I try the following to get the union
:
我尝试以下方法来获得union
:
res_union = gpd.overlay(df1, df2, how='union')
and it fails with the following ERROR:
它失败并显示以下错误:
AttributeError: 'NoneType' object has no attribute 'intersection'
I am following the instructions here.
我正在按照此处的说明操作。
回答by pedro regis
Despite I don't know the OP's operational system I think that I figured out how to solve the problem, at least for GNU/Linux systems (I'm not able to test in other systems).
尽管我不知道 OP 的操作系统,但我认为我找到了解决问题的方法,至少对于 GNU/Linux 系统(我无法在其他系统中进行测试)。
Direct explanation
直接说明
To be able to use the overlay
function you need more than just install geopandas
, you need install rtree
, but rtree
is a wrapper to the C library libspatialindex. So to use rtree
library you need to install libspatialindex
C library.
为了能够使用overlay
您不仅geopandas
需要安装的功能,您还需要安装rtree
,但它rtree
是 C 库libspatialindex的包装器。所以要使用rtree
库,你需要安装libspatialindex
C 库。
To install libspatialindex
open a terminal end type:
要安装libspatialindex
开放式终端类型:
sudo apt-get update && apt-get install -y libspatialindex-dev
sudo apt-get update && apt-get install -y libspatialindex-dev
Note: actually you only need the sudo apt-get install libspatialindex-dev
, but it is good practice update the system, and the -y flag is just to don't stop the installation process to ask for continue with the installation or not.
注意:实际上您只需要sudo apt-get install libspatialindex-dev
,但最好更新系统, -y 标志只是为了不要停止安装过程以要求继续安装。
Now it should solve your problem. Note: make sure you have rtree
installed in your system, you can do this using pip3 freeze
(I am supposing that you use python3
).
现在它应该可以解决您的问题。注意:确保您已rtree
在系统中安装,您可以使用pip3 freeze
(我假设您使用python3
)来执行此操作。
Long explanation
长解释
I faced the same error, and spent a lot of time to figure out what was the problem. The answer to this question libspatialindex and Rtree on pythongive to me a tip on how to solve the problem.
我遇到了同样的错误,并花了很多时间找出问题所在。这个问题的答案libspatialindex 和 Python 上的 Rtree给了我一个关于如何解决问题的提示。
Consider the bellow code (the OP's code example) and save named as script.py
:
考虑波纹管代码(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')
Consider the following requirements.txt
:
请考虑以下事项requirements.txt
:
Shapely==1.6.4.post2
descartes==1.1.0
geopandas==0.4.0
matplotlib==3.0.2
If you try to only install the libraries in the requirements.txt
and run scrip.py
, and not install rtree
library according to the requirements.txt
, it is going to show the following error message:
如果您尝试只安装requirements.txt
和 run 中的库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'
The last line of the error message
错误信息的最后一行
AttributeError: 'NoneType' object has no attribute 'intersection'
AttributeError: 'NoneType' object has no attribute 'intersection'
is not so useful. But if you look to the first line:
不是很有用。但是如果你看第一行:
/usr/local/lib/python3.6/site-packages/geopandas/base.py:76: UserWarning: Cannot generate spatial index: Missing package
rtree.
/usr/local/lib/python3.6/site-packages/geopandas/base.py:76: UserWarning: Cannot generate spatial index: Missing package
树.
it is complaining about the rtree
library.
它在抱怨rtree
图书馆。
So lets install rtree
and see what happens. requirements.txt
now is updated to:
所以让我们安装rtree
看看会发生什么。requirements.txt
现在更新为:
Shapely==1.6.4.post2
descartes==1.1.0
geopandas==0.4.0
matplotlib==3.0.2
rtree==0.8.3
Runing again the script.py
I get the following error:
再次运行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
The last line complain about libspatialindex_c
, so as explained in the first part of my answer, the "Direct explanation", just run the bellow code to install libspatialindex
and the script.py
should work.
最后一行抱怨libspatialindex_c
,所以我的回答,“直接的解释”的第一部分所解释的,只需运行娄代码安装libspatialindex
和script.py
应该工作。
sudo apt-get update && apt-get install -y libspatialindex-dev
sudo apt-get update && apt-get install -y libspatialindex-dev
At least for me the problem is solved.
至少对我来说,问题解决了。