计算重叠区域
calculate overlapping areas
我按照示例 here 成功创建了凸包。但是我有一个问题是如何计算下图中每个凸包之间的共享区域:
谢谢!
这是一个获取曼哈顿和布朗克斯交集的例子。如果你想合并自治市镇,你可以在 .overlay() 之前使用 pd.concat() 。
import geopandas as gpd
nybb_path = gpd.datasets.get_path('nybb')
boros = gpd.read_file(nybb_path)
boros.set_index('BoroCode', inplace=True)
boros.sort_index(inplace=True)
boros['geometry'] = boros['geometry'].convex_hull
print(boros)
BoroName Shape_Leng Shape_Area geometry
BoroCode
1 Manhattan 359299.096471 6.364715e+08 POLYGON ((977855.445 188082.322, 971830.134 19...
2 Bronx 464392.991824 1.186925e+09 POLYGON ((1017949.978 225426.885, 1015563.562 ...
3 Brooklyn 741080.523166 1.937479e+09 POLYGON ((988872.821 146772.032, 983670.606 14...
4 Queens 896344.047763 3.045213e+09 POLYGON ((1000721.532 136681.776, 994611.996 2...
5 Staten Island 330470.010332 1.623820e+09 POLYGON ((915517.688 120121.881, 915467.035 12...
manhattan_gdf = boros.iloc[0:1, :]
bronx_gdf = boros.iloc[1:2, :]
manhattan_bronx_intersecetion_polygon = gpd.overlay(manhattan_gdf, bronx_gdf,
how='intersection')
#SPCS83 New York Long Island zone (US Survey feet)
print(manhattan_bronx_intersecetion_polygon.geometry[0].area)
164559574.89341027
ax = manhattan_bronx_intersecetion_polygon.plot(figsize=(6,6))
boros.plot(ax=ax, facecolor='none', edgecolor='k');
这是您在评论中要求的循环解决方案。
import geopandas as gpd
nybb_path = gpd.datasets.get_path('nybb')
boros = gpd.read_file(nybb_path)
boros.set_index('BoroCode', inplace=True)
boros.sort_index(inplace=True)
boros['geometry'] = boros['geometry'].convex_hull
intersection_polygons_list = []
for idx, row in boros.iterrows():
main_boro_gdf = boros.iloc[idx-1:idx, :]
print('\n' + 'main boro:', main_boro_gdf['BoroName'].values.tolist()[:])
other_boro_list = boros.index.tolist()
other_boro_list.remove(idx)
other_boro_gdf = boros[boros.index.isin(other_boro_list)]
print('other boros:',other_boro_gdf['BoroName'].values.tolist()[:])
intersection_polygons = gpd.overlay(main_boro_gdf, other_boro_gdf, how='intersection')
intersection_polygons['intersection_area'] = intersection_polygons.geometry.area
print('intersecton area sum:', intersection_polygons['intersection_area'].sum())
intersection_polygons_list.append(intersection_polygons)
输出:
main boro: ['Manhattan']
other boros: ['Bronx', 'Brooklyn', 'Queens', 'Staten Island']
intersecton area sum: 279710750.6116526
main boro: ['Bronx']
other boros: ['Manhattan', 'Brooklyn', 'Queens', 'Staten Island']
intersecton area sum: 216638786.2669542
main boro: ['Brooklyn']
other boros: ['Manhattan', 'Bronx', 'Queens', 'Staten Island']
intersecton area sum: 1506573115.3550038
main boro: ['Queens']
other boros: ['Manhattan', 'Bronx', 'Brooklyn', 'Staten Island']
intersecton area sum: 1560297426.3563197
main boro: ['Staten Island']
other boros: ['Manhattan', 'Bronx', 'Brooklyn', 'Queens']
intersecton area sum: 0.0
您可以使用 intersection_polygons_list 索引值进行绘图。例如,这里是布朗克斯的重叠区域:
intersection_polygons_list[1].plot()
我按照示例 here 成功创建了凸包。但是我有一个问题是如何计算下图中每个凸包之间的共享区域:
谢谢!
这是一个获取曼哈顿和布朗克斯交集的例子。如果你想合并自治市镇,你可以在 .overlay() 之前使用 pd.concat() 。
import geopandas as gpd
nybb_path = gpd.datasets.get_path('nybb')
boros = gpd.read_file(nybb_path)
boros.set_index('BoroCode', inplace=True)
boros.sort_index(inplace=True)
boros['geometry'] = boros['geometry'].convex_hull
print(boros)
BoroName Shape_Leng Shape_Area geometry
BoroCode
1 Manhattan 359299.096471 6.364715e+08 POLYGON ((977855.445 188082.322, 971830.134 19...
2 Bronx 464392.991824 1.186925e+09 POLYGON ((1017949.978 225426.885, 1015563.562 ...
3 Brooklyn 741080.523166 1.937479e+09 POLYGON ((988872.821 146772.032, 983670.606 14...
4 Queens 896344.047763 3.045213e+09 POLYGON ((1000721.532 136681.776, 994611.996 2...
5 Staten Island 330470.010332 1.623820e+09 POLYGON ((915517.688 120121.881, 915467.035 12...
manhattan_gdf = boros.iloc[0:1, :]
bronx_gdf = boros.iloc[1:2, :]
manhattan_bronx_intersecetion_polygon = gpd.overlay(manhattan_gdf, bronx_gdf,
how='intersection')
#SPCS83 New York Long Island zone (US Survey feet)
print(manhattan_bronx_intersecetion_polygon.geometry[0].area)
164559574.89341027
ax = manhattan_bronx_intersecetion_polygon.plot(figsize=(6,6))
boros.plot(ax=ax, facecolor='none', edgecolor='k');
这是您在评论中要求的循环解决方案。
import geopandas as gpd
nybb_path = gpd.datasets.get_path('nybb')
boros = gpd.read_file(nybb_path)
boros.set_index('BoroCode', inplace=True)
boros.sort_index(inplace=True)
boros['geometry'] = boros['geometry'].convex_hull
intersection_polygons_list = []
for idx, row in boros.iterrows():
main_boro_gdf = boros.iloc[idx-1:idx, :]
print('\n' + 'main boro:', main_boro_gdf['BoroName'].values.tolist()[:])
other_boro_list = boros.index.tolist()
other_boro_list.remove(idx)
other_boro_gdf = boros[boros.index.isin(other_boro_list)]
print('other boros:',other_boro_gdf['BoroName'].values.tolist()[:])
intersection_polygons = gpd.overlay(main_boro_gdf, other_boro_gdf, how='intersection')
intersection_polygons['intersection_area'] = intersection_polygons.geometry.area
print('intersecton area sum:', intersection_polygons['intersection_area'].sum())
intersection_polygons_list.append(intersection_polygons)
输出:
main boro: ['Manhattan']
other boros: ['Bronx', 'Brooklyn', 'Queens', 'Staten Island']
intersecton area sum: 279710750.6116526
main boro: ['Bronx']
other boros: ['Manhattan', 'Brooklyn', 'Queens', 'Staten Island']
intersecton area sum: 216638786.2669542
main boro: ['Brooklyn']
other boros: ['Manhattan', 'Bronx', 'Queens', 'Staten Island']
intersecton area sum: 1506573115.3550038
main boro: ['Queens']
other boros: ['Manhattan', 'Bronx', 'Brooklyn', 'Staten Island']
intersecton area sum: 1560297426.3563197
main boro: ['Staten Island']
other boros: ['Manhattan', 'Bronx', 'Brooklyn', 'Queens']
intersecton area sum: 0.0
您可以使用 intersection_polygons_list 索引值进行绘图。例如,这里是布朗克斯的重叠区域:
intersection_polygons_list[1].plot()