1 |
|
2 | import L from 'leaflet';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var extendStatics = function(d, b) {
|
21 | extendStatics = Object.setPrototypeOf ||
|
22 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
23 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
24 | return extendStatics(d, b);
|
25 | };
|
26 |
|
27 | function __extends(d, b) {
|
28 | extendStatics(d, b);
|
29 | function __() { this.constructor = d; }
|
30 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
31 | }
|
32 |
|
33 | var __assign = function() {
|
34 | __assign = Object.assign || function __assign(t) {
|
35 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
36 | s = arguments[i];
|
37 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
38 | }
|
39 | return t;
|
40 | };
|
41 | return __assign.apply(this, arguments);
|
42 | };
|
43 |
|
44 | function __spreadArrays() {
|
45 | for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
46 | for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
47 | for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
48 | r[k] = a[j];
|
49 | return r;
|
50 | }
|
51 |
|
52 | var GeodesicCore = (function () {
|
53 | function GeodesicCore(options) {
|
54 | this.options = { wrap: true, steps: 3 };
|
55 | this.ellipsoid = {
|
56 | a: 6378137,
|
57 | b: 6356752.3142,
|
58 | f: 1 / 298.257223563
|
59 | };
|
60 | this.options = __assign(__assign({}, this.options), options);
|
61 | }
|
62 | GeodesicCore.prototype.toRadians = function (degree) {
|
63 | return degree * Math.PI / 180;
|
64 | };
|
65 | GeodesicCore.prototype.toDegrees = function (radians) {
|
66 | return radians * 180 / Math.PI;
|
67 | };
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | GeodesicCore.prototype.mod = function (n, p) {
|
76 | var r = n % p;
|
77 | return r < 0 ? r + p : r;
|
78 | };
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 | GeodesicCore.prototype.wrap360 = function (degrees) {
|
85 | if (0 <= degrees && degrees < 360) {
|
86 | return degrees;
|
87 | }
|
88 | else {
|
89 | return this.mod(degrees, 360);
|
90 | }
|
91 | };
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | GeodesicCore.prototype.wrap = function (degrees, max) {
|
99 | if (max === void 0) { max = 360; }
|
100 | if (-max <= degrees && degrees <= max) {
|
101 | return degrees;
|
102 | }
|
103 | else {
|
104 | return this.mod((degrees + max), 2 * max) - max;
|
105 | }
|
106 | };
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | GeodesicCore.prototype.direct = function (start, bearing, distance, maxInterations) {
|
119 | if (maxInterations === void 0) { maxInterations = 100; }
|
120 | var φ1 = this.toRadians(start.lat);
|
121 | var λ1 = this.toRadians(start.lng);
|
122 | var α1 = this.toRadians(bearing);
|
123 | var s = distance;
|
124 | var ε = Number.EPSILON * 1000;
|
125 | var _a = this.ellipsoid, a = _a.a, b = _a.b, f = _a.f;
|
126 | var sinα1 = Math.sin(α1);
|
127 | var cosα1 = Math.cos(α1);
|
128 | var tanU1 = (1 - f) * Math.tan(φ1), cosU1 = 1 / Math.sqrt((1 + tanU1 * tanU1)), sinU1 = tanU1 * cosU1;
|
129 | var σ1 = Math.atan2(tanU1, cosα1);
|
130 | var sinα = cosU1 * sinα1;
|
131 | var cosSqα = 1 - sinα * sinα;
|
132 | var uSq = cosSqα * (a * a - b * b) / (b * b);
|
133 | var A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
|
134 | var B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
|
135 | var σ = s / (b * A), sinσ = null, cosσ = null, Δσ = null;
|
136 | var cos2σₘ = null;
|
137 | var σʹ = null, iterations = 0;
|
138 | do {
|
139 | cos2σₘ = Math.cos(2 * σ1 + σ);
|
140 | sinσ = Math.sin(σ);
|
141 | cosσ = Math.cos(σ);
|
142 | Δσ = B * sinσ * (cos2σₘ + B / 4 * (cosσ * (-1 + 2 * cos2σₘ * cos2σₘ) -
|
143 | B / 6 * cos2σₘ * (-3 + 4 * sinσ * sinσ) * (-3 + 4 * cos2σₘ * cos2σₘ)));
|
144 | σʹ = σ;
|
145 | σ = s / (b * A) + Δσ;
|
146 | } while (Math.abs(σ - σʹ) > ε && ++iterations < maxInterations);
|
147 | if (iterations >= maxInterations) {
|
148 | throw new EvalError("Direct vincenty formula failed to converge after " + maxInterations + " iterations (start=" + start.lat + "/" + start.lng + "; bearing=" + bearing + "; distance=" + distance + ")");
|
149 | }
|
150 | var x = sinU1 * sinσ - cosU1 * cosσ * cosα1;
|
151 | var φ2 = Math.atan2(sinU1 * cosσ + cosU1 * sinσ * cosα1, (1 - f) * Math.sqrt(sinα * sinα + x * x));
|
152 | var λ = Math.atan2(sinσ * sinα1, cosU1 * cosσ - sinU1 * sinσ * cosα1);
|
153 | var C = f / 16 * cosSqα * (4 + f * (4 - 3 * cosSqα));
|
154 | var dL = λ - (1 - C) * f * sinα * (σ + C * sinσ * (cos2σₘ + C * cosσ * (-1 + 2 * cos2σₘ * cos2σₘ)));
|
155 | var λ2 = λ1 + dL;
|
156 | var α2 = Math.atan2(sinα, -x);
|
157 | return {
|
158 | lat: this.toDegrees(φ2),
|
159 | lng: this.toDegrees(λ2),
|
160 | bearing: this.wrap360(this.toDegrees(α2))
|
161 | };
|
162 | };
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | GeodesicCore.prototype.inverse = function (start, dest, maxInterations, mitigateConvergenceError) {
|
173 | if (maxInterations === void 0) { maxInterations = 100; }
|
174 | if (mitigateConvergenceError === void 0) { mitigateConvergenceError = true; }
|
175 | var p1 = start, p2 = dest;
|
176 | var φ1 = this.toRadians(p1.lat), λ1 = this.toRadians(p1.lng);
|
177 | var φ2 = this.toRadians(p2.lat), λ2 = this.toRadians(p2.lng);
|
178 | var π = Math.PI;
|
179 | var ε = Number.EPSILON;
|
180 |
|
181 | var _a = this.ellipsoid, a = _a.a, b = _a.b, f = _a.f;
|
182 | var dL = λ2 - λ1;
|
183 | var tanU1 = (1 - f) * Math.tan(φ1), cosU1 = 1 / Math.sqrt((1 + tanU1 * tanU1)), sinU1 = tanU1 * cosU1;
|
184 | var tanU2 = (1 - f) * Math.tan(φ2), cosU2 = 1 / Math.sqrt((1 + tanU2 * tanU2)), sinU2 = tanU2 * cosU2;
|
185 | var antipodal = Math.abs(dL) > π / 2 || Math.abs(φ2 - φ1) > π / 2;
|
186 | var λ = dL, sinλ = null, cosλ = null;
|
187 | var σ = antipodal ? π : 0, sinσ = 0, cosσ = antipodal ? -1 : 1, sinSqσ = null;
|
188 | var cos2σₘ = 1;
|
189 | var sinα = null, cosSqα = 1;
|
190 | var C = null;
|
191 | var λʹ = null, iterations = 0;
|
192 | do {
|
193 | sinλ = Math.sin(λ);
|
194 | cosλ = Math.cos(λ);
|
195 | sinSqσ = (cosU2 * sinλ) * (cosU2 * sinλ) + (cosU1 * sinU2 - sinU1 * cosU2 * cosλ) * (cosU1 * sinU2 - sinU1 * cosU2 * cosλ);
|
196 | if (Math.abs(sinSqσ) < ε) {
|
197 | break;
|
198 | }
|
199 | sinσ = Math.sqrt(sinSqσ);
|
200 | cosσ = sinU1 * sinU2 + cosU1 * cosU2 * cosλ;
|
201 | σ = Math.atan2(sinσ, cosσ);
|
202 | sinα = cosU1 * cosU2 * sinλ / sinσ;
|
203 | cosSqα = 1 - sinα * sinα;
|
204 | cos2σₘ = (cosSqα !== 0) ? (cosσ - 2 * sinU1 * sinU2 / cosSqα) : 0;
|
205 | C = f / 16 * cosSqα * (4 + f * (4 - 3 * cosSqα));
|
206 | λʹ = λ;
|
207 | λ = dL + (1 - C) * f * sinα * (σ + C * sinσ * (cos2σₘ + C * cosσ * (-1 + 2 * cos2σₘ * cos2σₘ)));
|
208 | var iterationCheck = antipodal ? Math.abs(λ) - π : Math.abs(λ);
|
209 | if (iterationCheck > π) {
|
210 | throw new EvalError('λ > π');
|
211 | }
|
212 | } while (Math.abs(λ - λʹ) > 1e-12 && ++iterations < maxInterations);
|
213 | if (iterations >= maxInterations) {
|
214 | if (mitigateConvergenceError) {
|
215 | return this.inverse(start, new L.LatLng(dest.lat, dest.lng - 0.01), maxInterations, mitigateConvergenceError);
|
216 | }
|
217 | else {
|
218 | throw new EvalError("Inverse vincenty formula failed to converge after " + maxInterations + " iterations (start=" + start.lat + "/" + start.lng + "; dest=" + dest.lat + "/" + dest.lng + ")");
|
219 | }
|
220 | }
|
221 | var uSq = cosSqα * (a * a - b * b) / (b * b);
|
222 | var A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
|
223 | var B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
|
224 | var Δσ = B * sinσ * (cos2σₘ + B / 4 * (cosσ * (-1 + 2 * cos2σₘ * cos2σₘ) -
|
225 | B / 6 * cos2σₘ * (-3 + 4 * sinσ * sinσ) * (-3 + 4 * cos2σₘ * cos2σₘ)));
|
226 | var s = b * A * (σ - Δσ);
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | var α1 = Math.abs(sinSqσ) < ε ? 0 : Math.atan2(cosU2 * sinλ, cosU1 * sinU2 - sinU1 * cosU2 * cosλ);
|
232 | var α2 = Math.abs(sinSqσ) < ε ? π : Math.atan2(cosU1 * sinλ, -sinU1 * cosU2 + cosU1 * sinU2 * cosλ);
|
233 | return {
|
234 | distance: s,
|
235 | initialBearing: Math.abs(s) < ε ? NaN : this.wrap360(this.toDegrees(α1)),
|
236 | finalBearing: Math.abs(s) < ε ? NaN : this.wrap360(this.toDegrees(α2))
|
237 | };
|
238 | };
|
239 | |
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | GeodesicCore.prototype.intersection = function (firstPos, firstBearing, secondPos, secondBearing) {
|
251 | var φ1 = this.toRadians(firstPos.lat);
|
252 | var λ1 = this.toRadians(firstPos.lng);
|
253 | var φ2 = this.toRadians(secondPos.lat);
|
254 | var λ2 = this.toRadians(secondPos.lng);
|
255 | var θ13 = this.toRadians(firstBearing);
|
256 | var θ23 = this.toRadians(secondBearing);
|
257 | var Δφ = φ2 - φ1, Δλ = λ2 - λ1;
|
258 | var π = Math.PI;
|
259 | var ε = Number.EPSILON;
|
260 |
|
261 | var δ12 = 2 * Math.asin(Math.sqrt(Math.sin(Δφ / 2) * Math.sin(Δφ / 2)
|
262 | + Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2)));
|
263 | if (Math.abs(δ12) < ε) {
|
264 | return firstPos;
|
265 | }
|
266 |
|
267 | var cosθa = (Math.sin(φ2) - Math.sin(φ1) * Math.cos(δ12)) / (Math.sin(δ12) * Math.cos(φ1));
|
268 | var cosθb = (Math.sin(φ1) - Math.sin(φ2) * Math.cos(δ12)) / (Math.sin(δ12) * Math.cos(φ2));
|
269 | var θa = Math.acos(Math.min(Math.max(cosθa, -1), 1));
|
270 | var θb = Math.acos(Math.min(Math.max(cosθb, -1), 1));
|
271 | var θ12 = Math.sin(λ2 - λ1) > 0 ? θa : 2 * π - θa;
|
272 | var θ21 = Math.sin(λ2 - λ1) > 0 ? 2 * π - θb : θb;
|
273 | var α1 = θ13 - θ12;
|
274 | var α2 = θ21 - θ23;
|
275 | if (Math.sin(α1) === 0 && Math.sin(α2) === 0) {
|
276 | return null;
|
277 | }
|
278 | if (Math.sin(α1) * Math.sin(α2) < 0) {
|
279 | return null;
|
280 | }
|
281 | var cosα3 = -Math.cos(α1) * Math.cos(α2) + Math.sin(α1) * Math.sin(α2) * Math.cos(δ12);
|
282 | var δ13 = Math.atan2(Math.sin(δ12) * Math.sin(α1) * Math.sin(α2), Math.cos(α2) + Math.cos(α1) * cosα3);
|
283 | var φ3 = Math.asin(Math.min(Math.max(Math.sin(φ1) * Math.cos(δ13) + Math.cos(φ1) * Math.sin(δ13) * Math.cos(θ13), -1), 1));
|
284 | var Δλ13 = Math.atan2(Math.sin(θ13) * Math.sin(δ13) * Math.cos(φ1), Math.cos(δ13) - Math.sin(φ1) * Math.sin(φ3));
|
285 | var λ3 = λ1 + Δλ13;
|
286 | return new L.LatLng(this.toDegrees(φ3), this.toDegrees(λ3));
|
287 | };
|
288 | GeodesicCore.prototype.midpoint = function (start, dest) {
|
289 |
|
290 |
|
291 |
|
292 | var φ1 = this.toRadians(start.lat);
|
293 | var λ1 = this.toRadians(start.lng);
|
294 | var φ2 = this.toRadians(dest.lat);
|
295 | var Δλ = this.toRadians(dest.lng - start.lng);
|
296 |
|
297 | var A = { x: Math.cos(φ1), y: 0, z: Math.sin(φ1) };
|
298 | var B = { x: Math.cos(φ2) * Math.cos(Δλ), y: Math.cos(φ2) * Math.sin(Δλ), z: Math.sin(φ2) };
|
299 |
|
300 | var C = { x: A.x + B.x, y: A.y + B.y, z: A.z + B.z };
|
301 | var φm = Math.atan2(C.z, Math.sqrt(C.x * C.x + C.y * C.y));
|
302 | var λm = λ1 + Math.atan2(C.y, C.x);
|
303 | return new L.LatLng(this.toDegrees(φm), this.toDegrees(λm));
|
304 | };
|
305 | return GeodesicCore;
|
306 | }());
|
307 |
|
308 | var GeodesicGeometry = (function () {
|
309 | function GeodesicGeometry(options) {
|
310 | this.geodesic = new GeodesicCore();
|
311 | this.options = { wrap: true, steps: 3 };
|
312 | this.options = __assign(__assign({}, this.options), options);
|
313 | this.steps = (this.options.steps === undefined) ? 3 : this.options.steps;
|
314 | }
|
315 | GeodesicGeometry.prototype.recursiveMidpoint = function (start, dest, iterations) {
|
316 | var geom = [start, dest];
|
317 | var midpoint = this.geodesic.midpoint(start, dest);
|
318 | if (this.options.wrap) {
|
319 | midpoint.lng = this.geodesic.wrap(midpoint.lng, 180);
|
320 | }
|
321 | if (iterations > 0) {
|
322 | geom.splice.apply(geom, __spreadArrays([0, 1], this.recursiveMidpoint(start, midpoint, iterations - 1)));
|
323 | geom.splice.apply(geom, __spreadArrays([geom.length - 2, 2], this.recursiveMidpoint(midpoint, dest, iterations - 1)));
|
324 | }
|
325 | else {
|
326 | geom.splice(1, 0, midpoint);
|
327 | }
|
328 | return geom;
|
329 | };
|
330 | GeodesicGeometry.prototype.line = function (start, dest) {
|
331 | return this.recursiveMidpoint(start, dest, Math.min(8, this.steps));
|
332 | };
|
333 | GeodesicGeometry.prototype.circle = function (center, radius) {
|
334 | var points = [];
|
335 | for (var i = 0; i < this.steps + 1; i++) {
|
336 | var point = this.geodesic.direct(center, 360 / this.steps * i, radius);
|
337 | points.push(new L.LatLng(point.lat, point.lng));
|
338 | }
|
339 | return points;
|
340 | };
|
341 | GeodesicGeometry.prototype.multiLineString = function (latlngs) {
|
342 | var _this = this;
|
343 | var multiLineString = [];
|
344 | latlngs.forEach(function (linestring) {
|
345 | var segment = [];
|
346 | for (var j = 1; j < linestring.length; j++) {
|
347 | segment.splice.apply(segment, __spreadArrays([segment.length - 1, 1], _this.line(linestring[j - 1], linestring[j])));
|
348 | }
|
349 | multiLineString.push(segment);
|
350 | });
|
351 | return multiLineString;
|
352 | };
|
353 | GeodesicGeometry.prototype.lineString = function (latlngs) {
|
354 | return this.multiLineString([latlngs])[0];
|
355 | };
|
356 | |
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 | GeodesicGeometry.prototype.splitLine = function (startPosition, destPosition) {
|
369 | var antimeridianWest = {
|
370 | point: new L.LatLng(89.9, -180),
|
371 | bearing: 180
|
372 | };
|
373 | var antimeridianEast = {
|
374 | point: new L.LatLng(89.9, 180),
|
375 | bearing: 180
|
376 | };
|
377 |
|
378 | var start = new L.LatLng(startPosition.lat, startPosition.lng);
|
379 | var dest = new L.LatLng(destPosition.lat, destPosition.lng);
|
380 | start.lng = this.geodesic.wrap(start.lng, 360);
|
381 | dest.lng = this.geodesic.wrap(dest.lng, 360);
|
382 | if ((dest.lng - start.lng) > 180) {
|
383 | dest.lng = dest.lng - 360;
|
384 | }
|
385 | else if ((dest.lng - start.lng) < -180) {
|
386 | dest.lng = dest.lng + 360;
|
387 | }
|
388 | var result = [[new L.LatLng(start.lat, this.geodesic.wrap(start.lng, 180)), new L.LatLng(dest.lat, this.geodesic.wrap(dest.lng, 180))]];
|
389 |
|
390 | if ((start.lng >= -180) && (start.lng <= 180)) {
|
391 |
|
392 | if (dest.lng < -180) {
|
393 | var bearing = this.geodesic.inverse(start, dest).initialBearing;
|
394 | var intersection = this.geodesic.intersection(start, bearing, antimeridianWest.point, antimeridianWest.bearing);
|
395 | if (intersection) {
|
396 | result = [[start, intersection], [new L.LatLng(intersection.lat, intersection.lng + 360), new L.LatLng(dest.lat, dest.lng + 360)]];
|
397 | }
|
398 | }
|
399 |
|
400 | else if (dest.lng > 180) {
|
401 | var bearing = this.geodesic.inverse(start, dest).initialBearing;
|
402 | var intersection = this.geodesic.intersection(start, bearing, antimeridianEast.point, antimeridianEast.bearing);
|
403 | if (intersection) {
|
404 | result = [[start, intersection], [new L.LatLng(intersection.lat, intersection.lng - 360), new L.LatLng(dest.lat, dest.lng - 360)]];
|
405 | }
|
406 | }
|
407 | }
|
408 |
|
409 | else if ((dest.lng >= -180) && (dest.lng <= 180)) {
|
410 |
|
411 | if (start.lng < -180) {
|
412 | var bearing = this.geodesic.inverse(start, dest).initialBearing;
|
413 | var intersection = this.geodesic.intersection(start, bearing, antimeridianWest.point, antimeridianWest.bearing);
|
414 | if (intersection) {
|
415 | result = [[new L.LatLng(start.lat, start.lng + 360), new L.LatLng(intersection.lat, intersection.lng + 360)], [intersection, dest]];
|
416 | }
|
417 | }
|
418 |
|
419 | else if (start.lng > 180) {
|
420 | var bearing = this.geodesic.inverse(start, dest).initialBearing;
|
421 | var intersection = this.geodesic.intersection(start, bearing, antimeridianWest.point, antimeridianWest.bearing);
|
422 | if (intersection) {
|
423 | result = [[new L.LatLng(start.lat, start.lng - 360), new L.LatLng(intersection.lat, intersection.lng - 360)], [intersection, dest]];
|
424 | }
|
425 | }
|
426 | }
|
427 | return result;
|
428 | };
|
429 | |
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 | GeodesicGeometry.prototype.splitMultiLineString = function (multilinestring) {
|
440 | var _this = this;
|
441 | var result = [];
|
442 | multilinestring.forEach(function (linestring) {
|
443 | if (linestring.length === 1) {
|
444 | result.push(linestring);
|
445 | }
|
446 | else {
|
447 | var segment = [];
|
448 | for (var j = 1; j < linestring.length; j++) {
|
449 | var split = _this.splitLine(linestring[j - 1], linestring[j]);
|
450 | segment.pop();
|
451 | segment = segment.concat(split[0]);
|
452 | if (split.length > 1) {
|
453 | result.push(segment);
|
454 | segment = split[1];
|
455 | }
|
456 | }
|
457 | result.push(segment);
|
458 | }
|
459 | });
|
460 | return result;
|
461 | };
|
462 | GeodesicGeometry.prototype.distance = function (start, dest) {
|
463 | return this.geodesic.inverse(new L.LatLng(start.lat, this.geodesic.wrap(start.lng, 180)), new L.LatLng(dest.lat, this.geodesic.wrap(dest.lng, 180))).distance;
|
464 | };
|
465 | GeodesicGeometry.prototype.multilineDistance = function (multilinestring) {
|
466 | var _this = this;
|
467 | var dist = [];
|
468 | multilinestring.forEach(function (linestring) {
|
469 | var segmentDistance = 0;
|
470 | for (var j = 1; j < linestring.length; j++) {
|
471 | segmentDistance += _this.distance(linestring[j - 1], linestring[j]);
|
472 | }
|
473 | dist.push(segmentDistance);
|
474 | });
|
475 | return dist;
|
476 | };
|
477 | GeodesicGeometry.prototype.updateStatistics = function (points, vertices) {
|
478 | var stats = {};
|
479 | stats.distanceArray = this.multilineDistance(points);
|
480 | stats.totalDistance = stats.distanceArray.reduce(function (x, y) { return x + y; }, 0);
|
481 | stats.points = 0;
|
482 | points.forEach(function (item) {
|
483 | stats.points += item.reduce(function (x) { return x + 1; }, 0);
|
484 | });
|
485 | stats.vertices = 0;
|
486 | vertices.forEach(function (item) {
|
487 | stats.vertices += item.reduce(function (x) { return x + 1; }, 0);
|
488 | });
|
489 | return stats;
|
490 | };
|
491 | return GeodesicGeometry;
|
492 | }());
|
493 |
|
494 | function instanceOfLatLngLiteral(object) {
|
495 | return ((typeof object === "object")
|
496 | && (object !== null)
|
497 | && ('lat' in object)
|
498 | && ('lng' in object)
|
499 | && (typeof object.lat === "number")
|
500 | && (typeof object.lng === "number"));
|
501 | }
|
502 | function instanceOfLatLngTuple(object) {
|
503 | return ((object instanceof Array)
|
504 | && (typeof object[0] === "number")
|
505 | && (typeof object[1] === "number"));
|
506 | }
|
507 | function instanceOfLatLngExpression(object) {
|
508 | if (object instanceof L.LatLng) {
|
509 | return true;
|
510 | }
|
511 | else if (instanceOfLatLngTuple(object)) {
|
512 | return true;
|
513 | }
|
514 | else if (instanceOfLatLngLiteral(object)) {
|
515 | return true;
|
516 | }
|
517 | else {
|
518 | return false;
|
519 | }
|
520 | }
|
521 | function latlngExpressiontoLatLng(input) {
|
522 | if (input instanceof L.LatLng) {
|
523 | return input;
|
524 | }
|
525 | else if (instanceOfLatLngTuple(input)) {
|
526 | return new L.LatLng(input[0], input[1]);
|
527 | }
|
528 | else if (instanceOfLatLngLiteral(input)) {
|
529 | return new L.LatLng(input.lat, input.lng);
|
530 | }
|
531 | else {
|
532 | throw new Error("L.LatLngExpression expected. Unknown object found.");
|
533 | }
|
534 | }
|
535 | function latlngExpressionArraytoLatLngArray(input) {
|
536 | var latlng = [];
|
537 | var _loop_1 = function (group) {
|
538 |
|
539 | if (instanceOfLatLngExpression(group)) {
|
540 | var sub_1 = [];
|
541 | input.forEach(function (point) {
|
542 | sub_1.push(latlngExpressiontoLatLng(point));
|
543 | });
|
544 | latlng.push(sub_1);
|
545 | return "break";
|
546 | }
|
547 |
|
548 | else if (group instanceof Array) {
|
549 | if (instanceOfLatLngExpression(group[0])) {
|
550 | var sub_2 = [];
|
551 | group.forEach(function (point) {
|
552 | sub_2.push(latlngExpressiontoLatLng(point));
|
553 | });
|
554 | latlng.push(sub_2);
|
555 | }
|
556 | else {
|
557 | throw new Error("L.LatLngExpression[] | L.LatLngExpression[][] expected. Unknown object found.");
|
558 | }
|
559 | }
|
560 | else {
|
561 | throw new Error("L.LatLngExpression[] | L.LatLngExpression[][] expected. Unknown object found.");
|
562 | }
|
563 | };
|
564 | for (var _i = 0, input_1 = input; _i < input_1.length; _i++) {
|
565 | var group = input_1[_i];
|
566 | var state_1 = _loop_1(group);
|
567 | if (state_1 === "break")
|
568 | break;
|
569 | }
|
570 | return latlng;
|
571 | }
|
572 |
|
573 |
|
574 |
|
575 |
|
576 | var GeodesicLine = (function (_super) {
|
577 | __extends(GeodesicLine, _super);
|
578 | function GeodesicLine(latlngs, options) {
|
579 | var _this = _super.call(this, [], options) || this;
|
580 |
|
581 | _this.defaultOptions = { wrap: true, steps: 3 };
|
582 |
|
583 | _this.statistics = {};
|
584 |
|
585 | _this.points = [];
|
586 | L.Util.setOptions(_this, __assign(__assign({}, _this.defaultOptions), options));
|
587 | _this.geom = new GeodesicGeometry(_this.options);
|
588 | if (latlngs !== undefined) {
|
589 | _this.setLatLngs(latlngs);
|
590 | }
|
591 | return _this;
|
592 | }
|
593 |
|
594 | GeodesicLine.prototype.updateGeometry = function () {
|
595 | var geodesic = [];
|
596 | geodesic = this.geom.multiLineString(this.points);
|
597 | if (this.options.wrap) {
|
598 | var split = this.geom.splitMultiLineString(geodesic);
|
599 | _super.prototype.setLatLngs.call(this, split);
|
600 | }
|
601 | else {
|
602 | _super.prototype.setLatLngs.call(this, geodesic);
|
603 | }
|
604 | this.statistics = this.geom.updateStatistics(this.points, geodesic);
|
605 | };
|
606 | |
607 |
|
608 |
|
609 |
|
610 | GeodesicLine.prototype.setLatLngs = function (latlngs) {
|
611 | this.points = latlngExpressionArraytoLatLngArray(latlngs);
|
612 | this.updateGeometry();
|
613 | return this;
|
614 | };
|
615 | |
616 |
|
617 |
|
618 |
|
619 |
|
620 | GeodesicLine.prototype.addLatLng = function (latlng, latlngs) {
|
621 | var point = latlngExpressiontoLatLng(latlng);
|
622 | if (this.points.length === 0) {
|
623 | this.points.push([point]);
|
624 | }
|
625 | else {
|
626 | if (latlngs === undefined) {
|
627 | this.points[this.points.length - 1].push(point);
|
628 | }
|
629 | else {
|
630 | latlngs.push(point);
|
631 | }
|
632 | }
|
633 | this.updateGeometry();
|
634 | return this;
|
635 | };
|
636 | |
637 |
|
638 |
|
639 |
|
640 | GeodesicLine.prototype.fromGeoJson = function (input) {
|
641 | var latlngs = [];
|
642 | var features = [];
|
643 | if (input.type === "FeatureCollection") {
|
644 | features = input.features;
|
645 | }
|
646 | else if (input.type === "Feature") {
|
647 | features = [input];
|
648 | }
|
649 | else if (["MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon"].includes(input.type)) {
|
650 | features = [{
|
651 | type: "Feature",
|
652 | geometry: input,
|
653 | properties: {}
|
654 | }];
|
655 | }
|
656 | else {
|
657 | console.log("[Leaflet.Geodesic] fromGeoJson() - Type \"" + input.type + "\" not supported.");
|
658 | }
|
659 | features.forEach(function (feature) {
|
660 | switch (feature.geometry.type) {
|
661 | case "MultiPoint":
|
662 | case "LineString":
|
663 | latlngs = __spreadArrays(latlngs, [L.GeoJSON.coordsToLatLngs(feature.geometry.coordinates, 0)]);
|
664 | break;
|
665 | case "MultiLineString":
|
666 | case "Polygon":
|
667 | latlngs = __spreadArrays(latlngs, L.GeoJSON.coordsToLatLngs(feature.geometry.coordinates, 1));
|
668 | break;
|
669 | case "MultiPolygon":
|
670 | feature.geometry.coordinates.forEach(function (item) {
|
671 | latlngs = __spreadArrays(latlngs, L.GeoJSON.coordsToLatLngs(item, 1));
|
672 | });
|
673 | break;
|
674 | default:
|
675 | console.log("[Leaflet.Geodesic] fromGeoJson() - Type \"" + feature.geometry.type + "\" not supported.");
|
676 | }
|
677 | });
|
678 | if (latlngs.length) {
|
679 | this.setLatLngs(latlngs);
|
680 | }
|
681 | return this;
|
682 | };
|
683 | |
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 | GeodesicLine.prototype.distance = function (start, dest) {
|
690 | return this.geom.distance(latlngExpressiontoLatLng(start), latlngExpressiontoLatLng(dest));
|
691 | };
|
692 | return GeodesicLine;
|
693 | }(L.Polyline));
|
694 |
|
695 |
|
696 |
|
697 |
|
698 | var GeodesicCircleClass = (function (_super) {
|
699 | __extends(GeodesicCircleClass, _super);
|
700 | function GeodesicCircleClass(center, options) {
|
701 | var _this = _super.call(this, [], options) || this;
|
702 | _this.defaultOptions = { wrap: true, steps: 24, fill: true, noClip: true };
|
703 | _this.statistics = {};
|
704 | L.Util.setOptions(_this, __assign(__assign({}, _this.defaultOptions), options));
|
705 |
|
706 | var extendedOptions = _this.options;
|
707 | _this.radius = (extendedOptions.radius === undefined) ? 1000 * 1000 : extendedOptions.radius;
|
708 | _this.center = (center === undefined) ? new L.LatLng(0, 0) : latlngExpressiontoLatLng(center);
|
709 | _this.geom = new GeodesicGeometry(_this.options);
|
710 |
|
711 | _this.update();
|
712 | return _this;
|
713 | }
|
714 | |
715 |
|
716 |
|
717 | GeodesicCircleClass.prototype.update = function () {
|
718 | var latlngs = this.geom.circle(this.center, this.radius);
|
719 | this.statistics = this.geom.updateStatistics([[this.center]], [latlngs]);
|
720 |
|
721 | this.statistics.totalDistance = this.geom.multilineDistance([latlngs]).reduce(function (x, y) { return x + y; }, 0);
|
722 | this.setLatLngs(latlngs);
|
723 | };
|
724 | |
725 |
|
726 |
|
727 |
|
728 |
|
729 | GeodesicCircleClass.prototype.distanceTo = function (latlng) {
|
730 | var dest = latlngExpressiontoLatLng(latlng);
|
731 | return this.geom.distance(this.center, dest);
|
732 | };
|
733 | |
734 |
|
735 |
|
736 |
|
737 | GeodesicCircleClass.prototype.setLatLng = function (latlng) {
|
738 | this.center = latlngExpressiontoLatLng(latlng);
|
739 | this.update();
|
740 | };
|
741 | |
742 |
|
743 |
|
744 |
|
745 | GeodesicCircleClass.prototype.setRadius = function (radius) {
|
746 | this.radius = radius;
|
747 | this.update();
|
748 | };
|
749 | return GeodesicCircleClass;
|
750 | }(L.Polyline));
|
751 |
|
752 | L.Geodesic = GeodesicLine;
|
753 | L.geodesic = function () {
|
754 | var args = [];
|
755 | for (var _i = 0; _i < arguments.length; _i++) {
|
756 | args[_i] = arguments[_i];
|
757 | }
|
758 | return new (GeodesicLine.bind.apply(GeodesicLine, __spreadArrays([void 0], args)))();
|
759 | };
|
760 | L.GeodesicCircle = GeodesicCircleClass;
|
761 | L.geodesiccircle = function () {
|
762 | var args = [];
|
763 | for (var _i = 0; _i < arguments.length; _i++) {
|
764 | args[_i] = arguments[_i];
|
765 | }
|
766 | return new (GeodesicCircleClass.bind.apply(GeodesicCircleClass, __spreadArrays([void 0], args)))();
|
767 | };
|
768 |
|
769 | export { GeodesicCircleClass, GeodesicLine };
|