Skip to content

Instantly share code, notes, and snippets.

@robinkraft
Created December 12, 2014 00:37
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save robinkraft/c6de2f988c9d3f01af3c to your computer and use it in GitHub Desktop.
Save robinkraft/c6de2f988c9d3f01af3c to your computer and use it in GitHub Desktop.
get the area in square meters of a polygon using shapely and pyproj
import pyproj
from shapely.geometry import shape
from shapely.ops import transform
geom = {'type': 'Polygon',
'coordinates': [[[-122., 37.], [-125., 37.],
[-125., 38.], [-122., 38.],
[-122., 37.]]]}
s = shape(geom)
proj = partial(pyproj.transform, pyproj.Proj(init='epsg:4326'),
pyproj.Proj(init='epsg:3857'))
s_new = transform(proj, s)
projected_area = transform(proj, s).area
@klunk386
Copy link

It looks you're missing an import:
from functools import partial

@vishalmishra14
Copy link

vishalmishra14 commented Apr 29, 2018

if you have a list of coordinates than how to use them
here

@MartinThoma
Copy link

@eugedebe
Copy link

from functools import partial

thnks!

@praveenmenezes
Copy link

Thanks for sharing this

@Samshal
Copy link

Samshal commented Aug 12, 2020

Thank you.

Please note that init='epsg:4326' is deprecated. use 'epsg:4326' instead.

@asya-brownie
Copy link

Hello,
when I am trying to calculate the area with the code above without init='epsg:4326' some values are returned as nan, while with it I have an output. Does anyone experience the same kind of problem?

@arminus
Copy link

arminus commented Apr 14, 2021

Hello,
when I am trying to calculate the area with the code above without init='epsg:4326' some values are returned as nan, while with it I have an output. Does anyone experience the same kind of problem?

same here :-O

@Pooja3894
Copy link

import pyproj
from shapely.geometry import shape
from shapely.ops import transform

geom = {'type': 'Polygon',
'coordinates': [[[-122., 37.], [-125., 37.],
[-125., 38.], [-122., 38.],
[-122., 37.]]]}

s = shape(geom)
wgs84 = pyproj.CRS('EPSG:4326')
utm = pyproj.CRS('EPSG:3857')
project = pyproj.Transformer.from_crs(wgs84, utm, always_xy=True).transform
projected_area = transform(project, s).area
print(projected_area )

This worked for me.

@LRR0202
Copy link

LRR0202 commented Jan 5, 2023

How would one use this code to interate over a gdf that contains the polygons in a column?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment