web-dev-qa-db-ja.com

GeoPandasからの過度の機能が機能しない

私は単にgeopandasを使用して、2つのポリゴン領域の和集合と交差を取得したいと考えています。私は定義します:

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'

こちら の指示に従っています。

9
Rotail

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を使用していると思います)。

長い説明

私は同じエラーに直面し、何が問題なのかを理解するために多くの時間を費やしました。この質問への答え pythonでのlibspatialindexおよびRtree 問題の解決方法に関するヒントを教えてください。

以下のコード(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 packagertree.

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

少なくとも私にとっては問題は解決されました。

15
pedro regis