注意:我也问了here同样的问题,但到目前为止还没有得到任何反馈。
是否有一种方法可以使用Geopandas轻松计算重叠多边形,与ArcGIS Pro Count Overlapping Features的工作方式相同?
到目前为止,我的方法是使用aggfunc='count'
进行联合覆盖,然后使用dissolve,但由于某些原因,我得到的结果并不正确。
我在一个地理数据框架中有3个重叠的多边形:
然后我做覆盖:
union = gpd.overlay(gdf, gdf, how='union')
结果我只得到了9个多边形,虽然我应该得到10个(这是QGIS或ArcGIS中的union
将返回的结果):
我的方法有什么问题吗?计算单个地理数据帧中重叠多边形的最佳方法是什么?
完整的代码在下面。它返回9个多边形。根据我对union/intersect
操作的理解,它应该产生10个多边形。3个多边形的交点只计算两次,而不是三次...在QGIS中,对同一组多边形执行并集操作会产生10个多边形。
import pandas as pd
import matplotlib as plt
import geopandas as gpd
from shapely import wkt
data = {'name': ['polygon A', 'polygon B', 'polygon C'],
'id': [1, 2, 3],
'geom': ['MULTIPOLYGON (((36.00000 11.00000, 36.00000 12.00000, 37.00000 12.00000, 37.00000 11.00000, 36.00000 11.00000)))', 'MULTIPOLYGON (((36.50000 11.50000, 37.50000 11.50000, 37.50000 11.00000, 36.50000 11.00000, 36.50000 11.50000)))', 'MULTIPOLYGON (((36.61799 10.80580, 36.61570 11.19321, 36.86327 11.29637, 37.34925 10.91813, 37.00540 10.71182, 36.61799 10.80580)))']
}
df = pd.DataFrame (data, columns = ['name','id','geom'])
df['geom'] = df['geom'].apply(wkt.loads)
gdf = gpd.GeoDataFrame(df, geometry='geom')
gdf.plot(alpha=0.5, column='id')
union = gpd.overlay(gdf, gdf, how='union')
len(union)
预期结果:
发布于 2021-02-24 08:48:37
这应该工作,如果你想要的计数和输出的ArcGIS专业计数重叠功能工具。如果要包含原始多边形,可以注释掉/删除此行: other_poly_list.remove(idx)。
intersection_polygons_list = []
for idx, row in gdf.iterrows():
main_poly_gdf = gdf.iloc[idx:idx+1, :]
print('\n' + 'main poly:', main_poly_gdf['id'].tolist()[:])
other_poly_list = gdf.index.tolist()
other_poly_list.remove(idx)
other_poly_gdf = gdf[gdf.index.isin(other_poly_list)]
print('other polys:', other_poly_gdf['id'].values.tolist()[:])
intersection_polygons = gpd.overlay(main_poly_gdf, other_poly_gdf, how='intersection')
intersection_polygons_list.append(intersection_polygons)
intersections_gdf = pd.concat(intersection_polygons_list)
intersections_gdf_gb = intersections_gdf.groupby('name_1').agg({'name_2':'size'})
https://stackoverflow.com/questions/66341143
复制相似问题