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

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

    A bit late perhaps, but here is a different method, using Girard's theorem. It states that the area of a polygon of great circles is R**2 times the sum of the angles between the polygons minus (N-2)*pi where N is number of corners.

    I thought this would be worth posting, since it doesn't rely on any other libraries than numpy, and it is a quite different method than the others. Of course, this only works on a sphere, so there will be some inaccuracy when applying it to the Earth.

    First, I define a function to compute the bearing angle from point 1 along a great circle to point 2:

    import numpy as np
    from numpy import cos, sin, arctan2
    
    d2r = np.pi/180
    
    def greatCircleBearing(lon1, lat1, lon2, lat2):
        dLong = lon1 - lon2
    
        s = cos(d2r*lat2)*sin(d2r*dLong)
        c = cos(d2r*lat1)*sin(d2r*lat2) - sin(lat1*d2r)*cos(d2r*lat2)*cos(d2r*dLong)
    
        return np.arctan2(s, c)
    

    Now I can use this to find the angles, and then the area (In the following, lons and lats should of course be specified, and they should be in the right order. Also, the radius of the sphere should be specified.)

    N = len(lons)
    
    angles = np.empty(N)
    for i in range(N):
    
        phiB1, phiA, phiB2 = np.roll(lats, i)[:3]
        LB1, LA, LB2 = np.roll(lons, i)[:3]
    
        # calculate angle with north (eastward)
        beta1 = greatCircleBearing(LA, phiA, LB1, phiB1)
        beta2 = greatCircleBearing(LA, phiA, LB2, phiB2)
    
        # calculate angle between the polygons and add to angle array
        angles[i] = np.arccos(cos(-beta1)*cos(-beta2) + sin(-beta1)*sin(-beta2))
    
    area = (sum(angles) - (N-2)*np.pi)*R**2
    

    With the Colorado coordinates given in another reply, and with Earth radius 6371 km, I get that the area is 268930758560.74808

提交回复
热议问题