如何按自定义区域汇总?

时间:2018-08-01 07:37:00

标签: pandas shapely geopandas

我有一个geojson文件,它定义了区域(如多边形)。当我使用df_areas将其加载为geopandas时,它看起来像这样:

                                        id                                           geometry
0     a9d04068-7220-433d-bbcf-50c51c1ee243  POLYGON ((13.43715051671244 52.48283068240888,...
1     9fd4ec97-db2c-4a40-b0ff-b54976bf4784  POLYGON ((13.4383267580672 52.4822756653842, 1...
2     43c2ad0a-51c0-45ff-aab3-51dbfb1119e9  POLYGON ((13.44054177168731 52.48185471774156,...
3     5cd2aa14-5265-432b-a13e-aa99d1fb94ca  POLYGON ((13.43429163342489 52.48417966481776,...
4     1e6c7de6-aa7d-4828-97d8-393738fd6df4  POLYGON ((13.43611174444709 52.48332083376235,...
5     409982b2-d638-4682-b9d1-76e2a9786103  POLYGON ((13.4383267580672 52.48283068240888, ...

然后我有一个df_data,其中有一个列geometry,其中shapely.geometry.Point作为值。我想得到一个热图,其中每个单元格是df_areas中定义的区域之一,而值是df_data.geometry中该区域中的点数。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

解决方案是使用geopandas.sjoin

要求:

$ sudo apt-get install libspatialindex-dev
$ pip3 install rtree --user

要获取加入的数据集:

# gdf contains Points in the 'geometry' column
# world contains Polygons in the 'geometry' column
df_joined = gpd.sjoin(gdf, world, op='within')