I would like to use a pandas way to iterate over a geodataframe to compute an equivalent to a cumsum but with union of polygon. I have a series of polygons, the first keeps the same geometry, the second is the union between the first and the second, the third the union between the 3 first ect.
I was thinking about using rolling tools but the windows seems to be constant. Do you have any idea how to deal with it using pandas ?
Thanks.
Reproductible example :
import shapely
import geopandas as gpd
p0 = shapely.geometry.Point([0, 0]).buffer(1)
p1 = shapely.geometry.Point([1, 1]).buffer(1)
p2 = shapely.geometry.Point([1, 0]).buffer(1)
gdf = gpd.GeoDataFrame({'name' : ['p0', 'p1', 'p2'], 'geometry' : [p0, p1, p2]}, crs = 'epsg:4326')
# what i would like
gdf['evolving_area'] = [shapely.unary_union([p0]).area,
shapely.unary_union([p0, p1]).area,
shapely.unary_union([p0, p1, p2]).area]
gdf['evolving_area']
One option would be to perform an expanding
window calculation :
gdf["evolving_area"] = list(
map(lambda p: p.unary_union.area, gdf["geometry"].expanding())
)
Output :
name geometry evolving_area
0 p0 POLYGON ((1.00000 0.00000, 0.99518 -0.09802, 0... 3.136548
1 p1 POLYGON ((2.00000 1.00000, 1.99518 0.90198, 1.... 5.704823
2 p2 POLYGON ((2.00000 0.00000, 1.99518 -0.09802, 1... 6.832249