17

I have polygons from coordinates in (python shapely) that looks like this

POLYGON ((24.8085317 46.8512821, 24.7986952 46.8574619, 24.8088238 46.8664741, 24.8155239 46.8576335, 24.8085317 46.8512821))

I would like to calculate the area of this polygon in km^2. What would be the best way to do this in Python?

  • 1
    You may look at stackoverflow.com/questions/23697374/… – SIslam Dec 26 '14 at 9:12
  • If you're getting the following error implementing one of the above solutions, is because the lat1 and lat2 should be lat_1 and lat_2: pyproj.exceptions.CRSError: Invalid projection: +proj=aea +lat1=37.843975868971484 +lat2=37.844325658890924 +type=crs: (Internal Proj Error: proj_create: Error -21: conic lat_1 = -lat_2) – Ramtin Kermani Apr 17 '19 at 0:18
24

It wasn't readily apparent to me how to use @sgillies answer, so here is a more verbose version:

import pyproj    
import shapely
import shapely.ops as ops
from shapely.geometry.polygon import Polygon
from functools import partial


geom = Polygon([(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)])
geom_area = ops.transform(
    partial(
        pyproj.transform,
        pyproj.Proj(init='EPSG:4326'),
        pyproj.Proj(
            proj='aea',
            lat1=geom.bounds[1],
            lat2=geom.bounds[3])),
    geom)

# Print the area in m^2
print geom_area.area 
| improve this answer | |
  • 1
    The resulting value is not exactly the same as the one obtained in geojson.io. Why? – astrojuanlu Jul 6 '17 at 19:58
16

It looks like your coordinates are longitude and latitude, yes? Use Shapely's shapely.ops.transform function to transform the polygon to projected equal area coordinates and then take the area.

python
import pyproj
from functools import partial

geom_aea = transform(
partial(
    pyproj.transform,
    pyproj.Proj(init='EPSG:4326'),
    pyproj.Proj(
        proj='aea',
        lat1=geom.bounds[1],
        lat2=geom.bounds[3])),
geom)

print(geom_aea.area)
# Output in m^2: 1083461.9234313113 
| improve this answer | |
  • 1
    You should probably indicate that partial is not a built-in; pyproj will have to be imported and possibly installed, etc. – kingledion Apr 26 '18 at 18:27
  • I noticed pyproj.Proj(proj='aea', lat1=lat1, lat2=lat2) resulted in CRSError: Invalid projection: +proj=aea +lat1=5.0 +lat2=6.0 +type=crs. Changing lat{1,2} into lat_{1,2} as implied by proj4 documentation fixed it: pyproj.Proj(proj='aea', lat1=lat1, lat2=lat2). Is this Answer accurate, or should it be updated? – Herbert Mar 18 '19 at 15:00
  • 1
    I needed to use lat_1 and lat_2 instead of lat1 and lat2. I suspect this applies post PROJ 6.0.0 – oortCloud Apr 26 '19 at 1:58
4

The above answers seem to be correct, EXCEPT that at some point recently, the lat1 and lat2 parameters in the pyproj code were renamed with underscores: lat_1 and lat_2 (see https://stackoverflow.com/a/55259718/1538758). I don't have enough rep to comment, so I'm making a new answer (sorry not sorry)

import pyproj    
import shapely
import shapely.ops as ops
from shapely.geometry.polygon import Polygon
from functools import partial


geom = Polygon([(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)])
geom_area = ops.transform(
    partial(
        pyproj.transform,
        pyproj.Proj(init='EPSG:4326'),
        pyproj.Proj(
            proj='aea',
            lat_1=geom.bounds[1],
            lat_2=geom.bounds[3])),
    geom)

# Print the area in m^2
print geom_area.area 
| improve this answer | |
1

I've stumbled across "area" which seems simpler to use:

https://pypi.org/project/area/

For example:

from area import area

obj = {'type':'Polygon','coordinates':[[[24.8085317,46.8512821], [24.7986952,46.8574619], [24.8088238,46.8664741], [24.8155239,46.8576335], [24.8085317,46.8512821]]]}

area_m2 = area(obj)

area_km2 = area_m2 / 1e+6
print ('area m2:' + str(area_m2))
print ('area km2:' + str(area_km2))

... returns:

area m2:1082979.880942425

area km2:1.082979880942425

| improve this answer | |
1

Calculate a geodesic area, which is very accurate and only requires an ellipsoid (not a projection). This can be done with pyproj 2.3.0 or later.

from pyproj import Geod
from shapely import wkt

# specify a named ellipsoid
geod = Geod(ellps="WGS84")

poly = wkt.loads('''\
POLYGON ((24.8085317 46.8512821, 24.7986952 46.8574619,
24.8088238 46.8664741, 24.8155239 46.8576335, 24.8085317 46.8512821))''')

area = abs(geod.geometry_area_perimeter(poly)[0])

print('# Geodesic area: {:12.3f} m^2'.format(area))
print('#                {:12.3f} km^2'.format(area/1e6))

# Geodesic area:  1083466.869 m^2
#                       1.083 km^2

abs() is used to return only positive areas. A negative area may be returned depending on the winding direction of the polygon.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.