/usr/lib/nodejs/geographiclib/src/PolygonArea.js is in node-geographiclib 1.49-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | /*
* PolygonArea.js
* Transcription of PolygonArea.[ch]pp into JavaScript.
*
* See the documentation for the C++ class. The conversion is a literal
* conversion from C++.
*
* The algorithms are derived in
*
* Charles F. F. Karney,
* Algorithms for geodesics, J. Geodesy 87, 43-55 (2013);
* https://doi.org/10.1007/s00190-012-0578-z
* Addenda: https://geographiclib.sourceforge.io/geod-addenda.html
*
* Copyright (c) Charles Karney (2011-2017) <charles@karney.com> and licensed
* under the MIT/X11 License. For more information, see
* https://geographiclib.sourceforge.io/
*/
// Load AFTER GeographicLib/Math.js and GeographicLib/Geodesic.js
(function(
/**
* @exports GeographicLib/PolygonArea
* @description Compute the area of geodesic polygons via the
* {@link module:GeographicLib/PolygonArea.PolygonArea PolygonArea}
* class.
*/
p, g, m, a) {
var transit, transitdirect;
transit = function(lon1, lon2) {
// Return 1 or -1 if crossing prime meridian in east or west direction.
// Otherwise return zero.
var lon12, cross;
// Compute lon12 the same way as Geodesic::Inverse.
lon1 = m.AngNormalize(lon1);
lon2 = m.AngNormalize(lon2);
lon12 = m.AngDiff(lon1, lon2).s;
cross = lon1 <= 0 && lon2 > 0 && lon12 > 0 ? 1 :
(lon2 <= 0 && lon1 > 0 && lon12 < 0 ? -1 : 0);
return cross;
};
// an alternate version of transit to deal with longitudes in the direct
// problem.
transitdirect = function(lon1, lon2) {
// We want to compute exactly
// int(floor(lon2 / 360)) - int(floor(lon1 / 360))
// Since we only need the parity of the result we can use std::remquo but
// this is buggy with g++ 4.8.3 and requires C++11. So instead we do
lon1 = lon1 % 720.0; lon2 = lon2 % 720.0;
return ( ((lon2 >= 0 && lon2 < 360) || lon2 < -360 ? 0 : 1) -
((lon1 >= 0 && lon1 < 360) || lon1 < -360 ? 0 : 1) );
};
/**
* @class
* @property {number} a the equatorial radius (meters).
* @property {number} f the flattening.
* @property {bool} polyline whether the PolygonArea object describes a
* polyline or a polygon.
* @property {number} num the number of vertices so far.
* @property {number} lat the current latitude (degrees).
* @property {number} lon the current longitude (degrees).
* @summary Initialize a PolygonArea object.
* @classdesc Computes the area and perimeter of a geodesic polygon.
* This object is usually instantiated by
* {@link module:GeographicLib/Geodesic.Geodesic#Polygon Geodesic.Polygon}.
* @param {object} geod a {@link module:GeographicLib/Geodesic.Geodesic
* Geodesic} object.
* @param {bool} [polyline = false] if true the new PolygonArea object
* describes a polyline instead of a polygon.
*/
p.PolygonArea = function(geod, polyline) {
this._geod = geod;
this.a = this._geod.a;
this.f = this._geod.f;
this._area0 = 4 * Math.PI * geod._c2;
this.polyline = !polyline ? false : polyline;
this._mask = g.LATITUDE | g.LONGITUDE | g.DISTANCE |
(this.polyline ? g.NONE : g.AREA | g.LONG_UNROLL);
if (!this.polyline)
this._areasum = new a.Accumulator(0);
this._perimetersum = new a.Accumulator(0);
this.Clear();
};
/**
* @summary Clear the PolygonArea object, setting the number of vertices to
* 0.
*/
p.PolygonArea.prototype.Clear = function() {
this.num = 0;
this._crossings = 0;
if (!this.polyline)
this._areasum.Set(0);
this._perimetersum.Set(0);
this._lat0 = this._lon0 = this.lat = this.lon = Number.NaN;
};
/**
* @summary Add the next vertex to the polygon.
* @param {number} lat the latitude of the point (degrees).
* @param {number} lon the longitude of the point (degrees).
* @description This adds an edge from the current vertex to the new vertex.
*/
p.PolygonArea.prototype.AddPoint = function(lat, lon) {
var t;
if (this.num === 0) {
this._lat0 = this.lat = lat;
this._lon0 = this.lon = lon;
} else {
t = this._geod.Inverse(this.lat, this.lon, lat, lon, this._mask);
this._perimetersum.Add(t.s12);
if (!this.polyline) {
this._areasum.Add(t.S12);
this._crossings += transit(this.lon, lon);
}
this.lat = lat;
this.lon = lon;
}
++this.num;
};
/**
* @summary Add the next edge to the polygon.
* @param {number} azi the azimuth at the current the point (degrees).
* @param {number} s the length of the edge (meters).
* @description This specifies the new vertex in terms of the edge from the
* current vertex.
*/
p.PolygonArea.prototype.AddEdge = function(azi, s) {
var t;
if (this.num) {
t = this._geod.Direct(this.lat, this.lon, azi, s, this._mask);
this._perimetersum.Add(s);
if (!this.polyline) {
this._areasum.Add(t.S12);
this._crossings += transitdirect(this.lon, t.lon2);
}
this.lat = t.lat2;
this.lon = t.lon2;
}
++this.num;
};
/**
* @summary Compute the perimeter and area of the polygon.
* @param {bool} reverse if true then clockwise (instead of
* counter-clockwise) traversal counts as a positive area.
* @param {bool} sign if true then return a signed result for the area if the
* polygon is traversed in the "wrong" direction instead of returning the
* area for the rest of the earth.
* @returns {object} r where r.number is the number of vertices, r.perimeter
* is the perimeter (meters), and r.area (only returned if polyline is
* false) is the area (meters<sup>2</sup>).
* @description If the object is a polygon (and not a polygon), the perimeter
* includes the length of a final edge connecting the current point to the
* initial point. If the object is a polyline, then area is nan. More
* points can be added to the polygon after this call.
*/
p.PolygonArea.prototype.Compute = function(reverse, sign) {
var vals = {number: this.num}, t, tempsum, crossings;
if (this.num < 2) {
vals.perimeter = 0;
if (!this.polyline)
vals.area = 0;
return vals;
}
if (this.polyline) {
vals.perimeter = this._perimetersum.Sum();
return vals;
}
t = this._geod.Inverse(this.lat, this.lon, this._lat0, this._lon0,
this._mask);
vals.perimeter = this._perimetersum.Sum(t.s12);
tempsum = new a.Accumulator(this._areasum);
tempsum.Add(t.S12);
crossings = this._crossings + transit(this.lon, this._lon0);
if (crossings & 1)
tempsum.Add( (tempsum.Sum() < 0 ? 1 : -1) * this._area0/2 );
// area is with the clockwise sense. If !reverse convert to
// counter-clockwise convention.
if (!reverse)
tempsum.Negate();
// If sign put area in (-area0/2, area0/2], else put area in [0, area0)
if (sign) {
if (tempsum.Sum() > this._area0/2)
tempsum.Add( -this._area0 );
else if (tempsum.Sum() <= -this._area0/2)
tempsum.Add( +this._area0 );
} else {
if (tempsum.Sum() >= this._area0)
tempsum.Add( -this._area0 );
else if (tempsum < 0)
tempsum.Add( -this._area0 );
}
vals.area = tempsum.Sum();
return vals;
};
/**
* @summary Compute the perimeter and area of the polygon with a tentative
* new vertex.
* @param {number} lat the latitude of the point (degrees).
* @param {number} lon the longitude of the point (degrees).
* @param {bool} reverse if true then clockwise (instead of
* counter-clockwise) traversal counts as a positive area.
* @param {bool} sign if true then return a signed result for the area if the
* polygon is traversed in the "wrong" direction instead of returning the
* @returns {object} r where r.number is the number of vertices, r.perimeter
* is the perimeter (meters), and r.area (only returned if polyline is
* false) is the area (meters<sup>2</sup>).
* @description A new vertex is *not* added to the polygon.
*/
p.PolygonArea.prototype.TestPoint = function(lat, lon, reverse, sign) {
var vals = {number: this.num + 1}, t, tempsum, crossings, i;
if (this.num === 0) {
vals.perimeter = 0;
if (!this.polyline)
vals.area = 0;
return vals;
}
vals.perimeter = this._perimetersum.Sum();
tempsum = this.polyline ? 0 : this._areasum.Sum();
crossings = this._crossings;
for (i = 0; i < (this.polyline ? 1 : 2); ++i) {
t = this._geod.Inverse(
i === 0 ? this.lat : lat, i === 0 ? this.lon : lon,
i !== 0 ? this._lat0 : lat, i !== 0 ? this._lon0 : lon,
this._mask);
vals.perimeter += t.s12;
if (!this.polyline) {
tempsum += t.S12;
crossings += transit(i === 0 ? this.lon : lon,
i !== 0 ? this._lon0 : lon);
}
}
if (this.polyline)
return vals;
if (crossings & 1)
tempsum += (tempsum < 0 ? 1 : -1) * this._area0/2;
// area is with the clockwise sense. If !reverse convert to
// counter-clockwise convention.
if (!reverse)
tempsum *= -1;
// If sign put area in (-area0/2, area0/2], else put area in [0, area0)
if (sign) {
if (tempsum > this._area0/2)
tempsum -= this._area0;
else if (tempsum <= -this._area0/2)
tempsum += this._area0;
} else {
if (tempsum >= this._area0)
tempsum -= this._area0;
else if (tempsum < 0)
tempsum += this._area0;
}
vals.area = tempsum;
return vals;
};
/**
* @summary Compute the perimeter and area of the polygon with a tentative
* new edge.
* @param {number} azi the azimuth of the edge (degrees).
* @param {number} s the length of the edge (meters).
* @param {bool} reverse if true then clockwise (instead of
* counter-clockwise) traversal counts as a positive area.
* @param {bool} sign if true then return a signed result for the area if the
* polygon is traversed in the "wrong" direction instead of returning the
* @returns {object} r where r.number is the number of vertices, r.perimeter
* is the perimeter (meters), and r.area (only returned if polyline is
* false) is the area (meters<sup>2</sup>).
* @description A new vertex is *not* added to the polygon.
*/
p.PolygonArea.prototype.TestEdge = function(azi, s, reverse, sign) {
var vals = {number: this.num ? this.num + 1 : 0}, t, tempsum, crossings;
if (this.num === 0)
return vals;
vals.perimeter = this._perimetersum.Sum() + s;
if (this.polyline)
return vals;
tempsum = this._areasum.Sum();
crossings = this._crossings;
t = this._geod.Direct(this.lat, this.lon, azi, s, this._mask);
tempsum += t.S12;
crossings += transitdirect(this.lon, t.lon2);
t = this._geod.Inverse(t.lat2, t.lon2, this._lat0, this._lon0, this._mask);
vals.perimeter += t.s12;
tempsum += t.S12;
crossings += transit(t.lon2, this._lon0);
if (crossings & 1)
tempsum += (tempsum < 0 ? 1 : -1) * this._area0/2;
// area is with the clockwise sense. If !reverse convert to
// counter-clockwise convention.
if (!reverse)
tempsum *= -1;
// If sign put area in (-area0/2, area0/2], else put area in [0, area0)
if (sign) {
if (tempsum > this._area0/2)
tempsum -= this._area0;
else if (tempsum <= -this._area0/2)
tempsum += this._area0;
} else {
if (tempsum >= this._area0)
tempsum -= this._area0;
else if (tempsum < 0)
tempsum += this._area0;
}
vals.area = tempsum;
return vals;
};
})(GeographicLib.PolygonArea, GeographicLib.Geodesic,
GeographicLib.Math, GeographicLib.Accumulator);
|