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

前端 未结 9 525
生来不讨喜
生来不讨喜 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 19:30

    Here is a solution that uses basemap, instead of pyproj and shapely, for the coordinate conversion. The idea is the same as suggested by @sgillies though. NOTE that I've added the 5th point so that the path is a closed loop.

    import numpy
    from mpl_toolkits.basemap import Basemap
    
    coordinates=numpy.array([
    [-102.05, 41.0], 
    [-102.05, 37.0], 
    [-109.05, 37.0], 
    [-109.05, 41.0],
    [-102.05, 41.0]])
    
    lats=coordinates[:,1]
    lons=coordinates[:,0]
    
    lat1=numpy.min(lats)
    lat2=numpy.max(lats)
    lon1=numpy.min(lons)
    lon2=numpy.max(lons)
    
    bmap=Basemap(projection='cea',llcrnrlat=lat1,llcrnrlon=lon1,urcrnrlat=lat2,urcrnrlon=lon2)
    xs,ys=bmap(lons,lats)
    
    area=numpy.abs(0.5*numpy.sum(ys[:-1]*numpy.diff(xs)-xs[:-1]*numpy.diff(ys)))
    area=area/1e6
    
    print area
    

    The result is 268993.609651 in km^2.

提交回复
热议问题