gpt4 book ai didi

python - 使用 GeoPandas 计算其他多边形内的多边形面积

转载 作者:行者123 更新时间:2023-11-30 21:57:49 43 4
gpt4 key购买 nike

我有两个GeoSeries:

df1 = gpd.GeoSeries([Polygon([(0,0),     (2,0),   (2,2), (0,2)]),
Polygon([(1.5,1.5), (4,2), (4,4), (2,4)]),
Polygon([(1,3.5), (3,3.5), (1,2.5)]),
Polygon([(1,0), (3,0), (3,2.5)])])

df2 = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
Polygon([(3,3), (5,3), (5,5), (3,5)]),
Polygon([(1,3), (1,5), (3,5), (2,3)]),
Polygon([(5,1), (3,1), (3,3), (3,5)])])

绘制此图给出:

base = df2.plot()
df1.plot(ax=base, cmap='summer')

enter image description here

如何计算 df1 和 df2 之间的重叠面积?

最佳答案

这是使用您的数据的工作代码。请阅读代码附带的注释以获取更多信息。

import geopandas as gpd
from shapely.geometry import Polygon

# use GeoSeries to prepare data
gs1 = gpd.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
Polygon([(1.5,1.5), (4,2), (4,4), (2,4)]),
Polygon([(1,3.5),(3,3.5),(1,2.5)]),
Polygon([(1,0), (3,0), (3,2.5)])])

gs2 = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
Polygon([(3,3), (5,3), (5,5), (3,5)]),
Polygon([(1,3), (1,5), (3,5),(2,3)]),
Polygon([(5,1), (3,1), (3,3), (3,5)])])

#base = gs2.plot()
#gs1.plot(ax=base, cmap='summer')

# create geoDataFrame from GeoSeries obtained above
df1 = gpd.GeoDataFrame(gs1)
# assign geometry to the geoDataFrame
df1g = df1.rename(columns={0:'geometry'}).set_geometry('geometry')

# similarly, ...
df2 = gpd.GeoDataFrame(gs2)
df2g = df2.rename(columns={0:'geometry'}).set_geometry('geometry')

# perform polygon overlay betw the two geoDataFrames
ov_output = gpd.overlay(df1g, df2g, how="intersection")
ov_output.plot(figsize=(4,4), cmap="Set2")

# Calculating areas of all the resulting polygons
ov_output.geometry.area

输出文本,显示相交多边形的计算面积:

0    1.0000
1 1.8000
5 0.2500
9 0.9000
2 0.3500
6 0.5625
3 1.0000
4 1.8500
dtype: float64

以及由此产生的情节。

enter image description here

关于python - 使用 GeoPandas 计算其他多边形内的多边形面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55131380/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com