【发布时间】:2018-03-19 01:07:28
【问题描述】:
我的数据:
Points:
ID HN Street
1 5 Examplestreet
2 6 Examplestreet
3 2 Otherstreet
4 2 Anotherstreet
Polygons:
Name
Firstpolygon
Secondpolygon
Otherpolygon
所以我想执行一个(多边形)到多个(点)空间连接,并将连接的值从点 HN 列连接到 Polygonsfile 中的新列“all_HN”。
所以结果应该是这样的:
Name all_HN
Firstpolygon 5,6 -> if the points ID1 and ID2 lie within the same polygon (`"Firstpolygon"`)
Secondpolygon 2 -> point ID3 within the `"Secondpolygon"`
Otherpolygon NULL -> no point within "Otherpolygon"
and so on
我想用 geopandas 解决这个问题。 (我有大约 200.000 个点和 100.000 个多边形) 使用以下代码:
from geopandas import gpd
points = gpd.GeoDataFrame.from_file('MyPointsFile) # or geojson etc
polys = gpd.GeoDataFrame.from_file('MyPolygonsFile.shp')
pointInPoly = gpd.sjoin(points, polys, op='within',how='inner')
现在我想使用类似的东西:
pointInPoly.groupby('index_right')['HN_left'].sum()
但正确的命令不是 sum(),而是将匹配点的所有值连接到一个新列“all_HN”中。
任何人,知道如何解决这个问题吗?使用另一个包而不是 geopandas 也可以。 由于 geopandas 基于 pandas,pandas 解决方案也应该可以工作。 只需将匹配项的所有列附加到相应的多边形表中就足够了。
【问题讨论】:
标签: python pandas concatenation geospatial geopandas