Search code examples
pythonareageodepyproj

"Invalid projection" when calculating area of a polygon


I try to calculate the area of a polygon in km2 and projected in EPSG: 3857, but it doesn't recognize my crs

raise GeodError("Invalid geometry provided.")
pyproj.exceptions.GeodError: Invalid geometry provided.

should I use another one? is the function wrong?

def area(polygon):
        geod = Geod('EPSG: 3857')
        x, y = polygon.exterior.coords.xy
        area, perimeter = geod.geometry_area_perimeter(x,y)
        return area

Solution

  • Geodesic calculations expect geographic data. Here is an example for your data in WGS84/EPSG:4326:

    https://pyproj4.github.io/pyproj/stable/examples.html#creating-geod-class

    from pyproj import Geod, CRS
    geod = Geod(ellps='WGS84')
    # or
    geod = CRS("EPSG:4326").get_geod()