Calculating Areas Of Polygons Inside Other Polygons With Geopandas
I have two GeoSeries: df1 = gpd.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]), Polygon([(1.5,1.5), (4,2), (4,4), (2,4)]), Polyg
Solution 1:
You use geopandas.overlay
:
gpd.overlay(df1, df2, how="intersection")
Solution 2:
Here is a working code using your data. Read the comments provided with the code for more information.
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
Output text, showing calculated areas of the intersected polygons:
01.000011.800050.250090.900020.350060.562531.000041.8500
dtype: float64
And, the resulting plot.
Post a Comment for "Calculating Areas Of Polygons Inside Other Polygons With Geopandas"