gpt4 book ai didi

python - 多边形中的 geopandas 点

转载 作者:太空狗 更新时间:2023-10-29 21:25:26 26 4
gpt4 key购买 nike

我有一个多边形的 GeoDataFrame (~30) 和一个点的 GeoDataFrame (~10k)

如果点存在于多边形中,我希望在我的点的 GeoDataFrame 中使用简单的 bool 值 True/False 创建 30 个新列(具有适当的多边形名称)。

举个例子,Polygons的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

关于python - 多边形中的 geopandas 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48097742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com