nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

index.js (1047B)


      1 const EARTH_RADIUS = 6371008.8;
      2 const RADIANS_PER_DEGREE = Math.PI / 180;
      3 /**
      4  * Calculate the approximate area of the polygon were it projected onto the earth.
      5  *
      6  * Reference:
      7  * Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for Polygons on a Sphere",
      8  * JPL Publication 07-03, Jet Propulsion
      9  * Laboratory, Pasadena, CA, June 2007 https://trs.jpl.nasa.gov/handle/2014/40409
     10  */
     11 function polygonArea(latlngs) {
     12     const latlngsLength = latlngs.length;
     13     if (latlngsLength < 3) {
     14         return 0;
     15     }
     16     let acc = 0;
     17     for (let i = 0; i < latlngsLength; i++) {
     18         let iLow = i - 1;
     19         if (iLow === -1) {
     20             iLow = latlngsLength - 1;
     21         }
     22         let iHigh = i + 1;
     23         if (iHigh === latlngsLength) {
     24             iHigh = 0;
     25         }
     26         acc +=
     27             (latlngs[iHigh].lng - latlngs[iLow].lng) *
     28             RADIANS_PER_DEGREE *
     29             Math.sin(latlngs[i].lat * RADIANS_PER_DEGREE);
     30     }
     31     return Math.abs(((EARTH_RADIUS * EARTH_RADIUS) / 2) * acc);
     32 }
     33 
     34 export {polygonArea};