You can download this code by clicking the button below.
This code is now available for download.
This function calculates the area of each polygon in a GeoDataFrame and returns a dictionary with polygon index as keys and their area as values.
Technology Stack : GeoPandas, Shapely
Code Type : Custom function
Code Difficulty : Intermediate
def calculate_area_of_polygons(geo_data):
"""
Calculate the area of each polygon in a GeoDataFrame.
:param geo_data: GeoDataFrame containing Polygon geometries.
:return: Dictionary with polygon index as keys and their area as values.
"""
# Ensure the GeoDataFrame contains Polygon geometries
if not all(isinstance(g, shapely.geometry.Polygon) for g in geo_data.geometry):
raise ValueError("GeoDataFrame must contain Polygon geometries.")
# Calculate the area for each polygon
areas = geo_data.geometry.area
# Return a dictionary with polygon index as keys and their area as values
return {index: area for index, area in zip(geo_data.index, areas)}