Created
December 12, 2014 00:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
How would one use this code to interate over a gdf that contains the polygons in a column?
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.
I think worked for me too!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.