首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Geopandas计算重叠要素

使用Geopandas计算重叠要素
EN

Stack Overflow用户
提问于 2021-02-24 05:09:28
回答 1查看 528关注 0票数 2

注意:我也问了here同样的问题,但到目前为止还没有得到任何反馈。

是否有一种方法可以使用Geopandas轻松计算重叠多边形,与ArcGIS Pro Count Overlapping Features的工作方式相同?

到目前为止,我的方法是使用aggfunc='count'进行联合覆盖,然后使用dissolve,但由于某些原因,我得到的结果并不正确。

我在一个地理数据框架中有3个重叠的多边形:

然后我做覆盖:

代码语言:javascript
运行
复制
union = gpd.overlay(gdf, gdf, how='union')

结果我只得到了9个多边形,虽然我应该得到10个(这是QGIS或ArcGIS中的union将返回的结果):

我的方法有什么问题吗?计算单个地理数据帧中重叠多边形的最佳方法是什么?

完整的代码在下面。它返回9个多边形。根据我对union/intersect操作的理解,它应该产生10个多边形。3个多边形的交点只计算两次,而不是三次...在QGIS中,对同一组多边形执行并集操作会产生10个多边形。

代码语言:javascript
运行
复制
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)

预期结果:

EN

回答 1

Stack Overflow用户

发布于 2021-02-24 08:48:37

这应该工作,如果你想要的计数和输出的ArcGIS专业计数重叠功能工具。如果要包含原始多边形,可以注释掉/删除此行: other_poly_list.remove(idx)。

代码语言:javascript
运行
复制
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)

代码语言:javascript
运行
复制
intersections_gdf_gb = intersections_gdf.groupby('name_1').agg({'name_2':'size'})

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66341143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档