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

前端 未结 9 507
生来不讨喜
生来不讨喜 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:52

    Here is a Python 3 implementation where the function would take a list of tuple-pairs of lats and longs and would return the area enclosed in the projected polygon.It uses pyproj to project the coordinates and then Shapely to find the area of any projected polygon

    def calc_area(lis_lats_lons):
    
    import numpy as np
    from pyproj import Proj
    from shapely.geometry import shape
    
    
    lons, lats = zip(*lis_lats_lons)
    ll = list(set(lats))[::-1]
    var = []
    for i in range(len(ll)):
        var.append('lat_' + str(i+1))
    st = ""
    for v, l in zip(var,ll):
        st = st + str(v) + "=" + str(l) +" "+ "+"
    st = st +"lat_0="+ str(np.mean(ll)) + " "+ "+" + "lon_0" +"=" + str(np.mean(lons))
    tx = "+proj=aea +" + st
    pa = Proj(tx)
    
    x, y = pa(lons, lats)
    cop = {"type": "Polygon", "coordinates": [zip(x, y)]}
    
    return shape(cop).area 
    

    For a sample set of lats/longs, it gives an area value close to the surveyed approximation value

    calc_area(lis_lats_lons = [(-102.05, 41.0),
     (-102.05, 37.0),
     (-109.05, 37.0),
     (-109.05, 41.0)])
    

    Which outputs an area of 268952044107.4342 Sq. Mts.

    0 讨论(0)
  • 2020-11-29 19:56

    The easiest way to do this (in my opinion), is to project things into (a very simple) equal-area projection and use one of the usual planar techniques for calculating area.

    First off, I'm going to assume that a spherical earth is close enough for your purposes, if you're asking this question. If not, then you need to reproject your data using an appropriate ellipsoid, in which case you're going to want to use an actual projection library (everything uses proj4 behind the scenes, these days) such as the python bindings to GDAL/OGR or (the much more friendly) pyproj.

    However, if you're okay with a spherical earth, it quite simple to do this without any specialized libraries.

    The simplest equal-area projection to calculate is a sinusoidal projection. Basically, you just multiply the latitude by the length of one degree of latitude, and the longitude by the length of a degree of latitude and the cosine of the latitude.

    def reproject(latitude, longitude):
        """Returns the x & y coordinates in meters using a sinusoidal projection"""
        from math import pi, cos, radians
        earth_radius = 6371009 # in meters
        lat_dist = pi * earth_radius / 180.0
    
        y = [lat * lat_dist for lat in latitude]
        x = [long * lat_dist * cos(radians(lat)) 
                    for lat, long in zip(latitude, longitude)]
        return x, y
    

    Okay... Now all we have to do is to calculate the area of an arbitrary polygon in a plane.

    There are a number of ways to do this. I'm going to use what is probably the most common one here.

    def area_of_polygon(x, y):
        """Calculates the area of an arbitrary polygon given its verticies"""
        area = 0.0
        for i in range(-1, len(x)-1):
            area += x[i] * (y[i+1] - y[i-1])
        return abs(area) / 2.0
    

    Hopefully that will point you in the right direction, anyway...

    0 讨论(0)
  • 2020-11-29 19:57

    According to Yellows' assertion, direct integral is more precise.

    But Yellows use an earth radius = 6378 137m, which is the WGS-84 ellipsoid, semi-major axis, while Sulkeh use 6371 000 m.

    Using a radius = 6378 137 m in the Sulkeh' method, gives 269533625893 square meters.

    Assuming that the true value of Colorado area (from the US Census Bureau) is 269601367661 square meters then the variation from the ground truth of Sulkeh' method is : -0,025%, better than -0.07 with the Line integral method.

    So Sulkeh' proposal seems to be the more precise so far.

    In order to be able to make a numerical comparison of the solutions, with the assumption of a spherical Earth, all calculations must use the same terrestrial radius.

    0 讨论(0)
提交回复
热议问题