簡體   English   中英

geopandas指向多邊形

[英]geopandas point in polygon

我有一個GeoDataFrame的多邊形(~30)和一個GeoDataFrame的點(~10k)

我想在我的GeoDataFrame中創建30個新的列(具有適當的多邊形名稱),如果該點存在於多邊形中,則使用簡單的布爾值True / False。

例如,多邊形的GeoDataFrame是這樣的:

id  geometry
foo POLYGON ((-0.18353,51.51022, -0.18421,51.50767, -0.18253,51.50744, -0.1794,51.50914))
bar POLYGON ((-0.17003,51.50739, -0.16904,51.50604, -0.16488,51.50615, -0.1613,51.5091))

Points的GeoDataFrame是這樣的:

counter     points
   1     ((-0.17987,51.50974))
   2     ((-0.16507,51.50925))

預期產量:

counter          points        foo    bar
   1    ((-0.17987,51.50974))  False  False
   1    ((-0.16507,51.50925))  False  False

我可以通過以下方式手動完成:

foo = df_poly.loc[df_poly.id=='foo']
df_points['foo'] = df_points['points'].map(lambda x: True if foo.contains(x).any()==True else False

但鑒於我有30個多邊形,我想知道是否有更好的方法。 感謝任何幫助!

不太清楚你實際擁有什么樣的數據結構。 此外,您的所有預期結果都是假的,因此很難檢查。 假設GeoSeries和GeoDataFrames,我會這樣做:

from shapely.geometry import Point, Polygon
import geopandas

polys = geopandas.GeoSeries({
    'foo': Polygon([(5, 5), (5, 13), (13, 13), (13, 5)]),
    'bar': Polygon([(10, 10), (10, 15), (15, 15), (15, 10)]),
})

_pnts = [Point(3, 3), Point(8, 8), Point(11, 11)]
pnts = geopandas.GeoDataFrame(geometry=_pnts, index=['A', 'B', 'C'])
pnts = pnts.assign(**{key: pnts.within(geom) for key, geom in polys.items()})

print(pnts)

這給了我:

        geometry    bar    foo
A    POINT (3 3)  False  False
B    POINT (8 8)  False   True
C  POINT (11 11)   True   True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM