How to calculate the area of a polygon on the earth's surface using python?

前端 未结 9 521
生来不讨喜
生来不讨喜 2020-11-29 19:09

The title basically says it all. I need to calculate the area inside a polygon on the Earth\'s surface using Python. Calculating area enclosed by arbitrary polygon on Earth&

9条回答
  •  长情又很酷
    2020-11-29 19:40

    Let's say you have a representation of the state of Colorado in GeoJSON format

    {"type": "Polygon", 
     "coordinates": [[
       [-102.05, 41.0], 
       [-102.05, 37.0], 
       [-109.05, 37.0], 
       [-109.05, 41.0]
     ]]}
    

    All coordinates are longitude, latitude. You can use pyproj to project the coordinates and Shapely to find the area of any projected polygon:

    co = {"type": "Polygon", "coordinates": [
        [(-102.05, 41.0),
         (-102.05, 37.0),
         (-109.05, 37.0),
         (-109.05, 41.0)]]}
    lon, lat = zip(*co['coordinates'][0])
    from pyproj import Proj
    pa = Proj("+proj=aea +lat_1=37.0 +lat_2=41.0 +lat_0=39.0 +lon_0=-106.55")
    

    That's an equal area projection centered on and bracketing the area of interest. Now make new projected GeoJSON representation, turn into a Shapely geometric object, and take the area:

    x, y = pa(lon, lat)
    cop = {"type": "Polygon", "coordinates": [zip(x, y)]}
    from shapely.geometry import shape
    shape(cop).area  # 268952044107.43506
    

    It's a very close approximation to the surveyed area. For more complex features, you'll need to sample along the edges, between the vertices, to get accurate values. All caveats above about datelines, etc, apply. If you're only interested in area, you can translate your feature away from the dateline before projecting.

提交回复
热议问题