Polygon area calculation using Latitude and Longitude generated from Cartesian space and a world file

后端 未结 7 1927
孤街浪徒
孤街浪徒 2020-11-27 22:16

Given a series of GPS coordinate pairs, I need to calculate the area of a polygon (n-gon). This is relatively small (not larger than 50,000 sqft). The geocodes are created

7条回答
  •  死守一世寂寞
    2020-11-27 22:31

    Adapted RiskyPathak's snippet to Ruby

    def deg2rad(input)
      input * Math::PI / 180.0
    end
    
    def polygone_area(coordinates)
      return 0.0 unless coordinates.size > 2
    
      area = 0.0
      coor_p = coordinates.first
      coordinates[1..-1].each{ |coor|
        area += deg2rad(coor[1] - coor_p[1]) * (2 + Math.sin(deg2rad(coor_p[0])) + Math.sin(deg2rad(coor[0])))
        coor_p = coor
      }
    
      (area * 6378137 * 6378137 / 2.0).abs # 6378137 Earth's radius in meters
    end
    

提交回复
热议问题