Calculate Area of GeoSeries in GeoPandas

  • Share this:

Code introduction


This function calculates the area of each geometry in a GeoPandas GeoSeries object.


Technology Stack : GeoPandas, NumPy

Code Type : Function

Code Difficulty : Intermediate


                
                    
def calculate_area(geometry):
    """
    Calculate the area of a GeoPandas GeoSeries object.
    """
    from geopandas import GeoSeries
    import numpy as np
    
    # Check if the input is a GeoSeries
    if not isinstance(geometry, GeoSeries):
        raise ValueError("Input must be a GeoSeries")
    
    # Calculate the area of each geometry in the GeoSeries
    area_series = geometry.area
    
    # Return the area as a numpy array
    return np.array(area_series)