Getting counts of intersections between geometries in GeoPandas

邮差的信 提交于 2019-12-01 07:36:30

You want a spatial join: geopandas.tools.sjoin().

There's an example in this Jupyter Notebook — look at the section called Spatial join. This is counting a set of points (midpoints) into a set of polygons (bins). Both geometries define a GeoDataFrame.

At the time of writing, tools.sjoin() is not in the current release of geopandas. I couldn't get geopandas.tools to build in any of their branches, but I fixed it — for me anyway — in my fork. My fix is an open PR.

I don't know about a built-in tool to do this but I'm not an expert. At the same time it's easily done with some pandas magic:

import geopandas as gpd
from shapely.geometry import *

p1 = Point(.5,.5)
p2 = Point(.5,1)
p3 = Point(1,1)

poly = Polygon([(0,0), (0,2), (2,2), (2,0)])

df1 = gpd.GeoSeries([p1,p2,p3])
df2 = gpd.GeoDataFrame([poly,p3], columns=['geometries'])

f = lambda x:np.sum(df1.intersects(x))
df2['geometries'].apply(f)

Should return:

0    3
1    1
Name: geometries, dtype: int64
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!