UNPKG

13.9 kBJavaScriptView Raw
1/*! Leaflet.Geodesic 2.6.0 - (c) Henry Thasler - https://github.com/henrythasler/Leaflet.Geodesic */
2!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("leaflet")):"function"==typeof define&&define.amd?define(["exports","leaflet"],n):n(((t=t||self).L=t.L||{},t.L.geodesic={}),t.L)}(this,(function(t,n){"use strict";
3/*! *****************************************************************************
4 Copyright (c) Microsoft Corporation. All rights reserved.
5 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
6 this file except in compliance with the License. You may obtain a copy of the
7 License at http://www.apache.org/licenses/LICENSE-2.0
8
9 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10 KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
11 WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
12 MERCHANTABLITY OR NON-INFRINGEMENT.
13
14 See the Apache Version 2.0 License for specific language governing permissions
15 and limitations under the License.
16 ***************************************************************************** */var e=function(t,n){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var e in n)n.hasOwnProperty(e)&&(t[e]=n[e])})(t,n)};function i(t,n){function i(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(i.prototype=n.prototype,new i)}var a=function(){return(a=Object.assign||function(t){for(var n,e=1,i=arguments.length;e<i;e++)for(var a in n=arguments[e])Object.prototype.hasOwnProperty.call(n,a)&&(t[a]=n[a]);return t}).apply(this,arguments)};function s(){for(var t=0,n=0,e=arguments.length;n<e;n++)t+=arguments[n].length;var i=Array(t),a=0;for(n=0;n<e;n++)for(var s=arguments[n],o=0,r=s.length;o<r;o++,a++)i[a]=s[o];return i}var o=function(){function t(t){this.options={wrap:!0,steps:3},this.ellipsoid={a:6378137,b:6356752.3142,f:1/298.257223563},this.options=a(a({},this.options),t)}return t.prototype.toRadians=function(t){return t*Math.PI/180},t.prototype.toDegrees=function(t){return 180*t/Math.PI},t.prototype.mod=function(t,n){var e=t%n;return e<0?e+n:e},t.prototype.wrap360=function(t){return 0<=t&&t<360?t:this.mod(t,360)},t.prototype.wrap=function(t,n){return void 0===n&&(n=360),-n<=t&&t<=n?t:this.mod(t+n,2*n)-n},t.prototype.direct=function(t,n,e,i){void 0===i&&(i=100);var a=this.toRadians(t.lat),s=this.toRadians(t.lng),o=this.toRadians(n),r=e,h=1e3*Number.EPSILON,l=this.ellipsoid,p=l.a,c=l.b,u=l.f,g=Math.sin(o),f=Math.cos(o),d=(1-u)*Math.tan(a),L=1/Math.sqrt(1+d*d),M=d*L,v=Math.atan2(d,f),y=L*g,w=1-y*y,m=w*(p*p-c*c)/(c*c),b=1+m/16384*(4096+m*(m*(320-175*m)-768)),S=m/1024*(256+m*(m*(74-47*m)-128)),O=r/(c*b),P=null,R=null,D=null,E=null,x=0;do{D=Math.cos(2*v+O),E=O,O=r/(c*b)+S*(P=Math.sin(O))*(D+S/4*((R=Math.cos(O))*(2*D*D-1)-S/6*D*(4*P*P-3)*(4*D*D-3)))}while(Math.abs(O-E)>h&&++x<i);if(x>=i)throw new EvalError("Direct vincenty formula failed to converge after "+i+" iterations \n (start="+t.lat+"/"+t.lng+"; bearing="+n+"; distance="+e+")");var G=M*P-L*R*f,N=Math.atan2(M*R+L*P*f,(1-u)*Math.sqrt(y*y+G*G)),_=u/16*w*(4+u*(4-3*w)),j=s+(Math.atan2(P*g,L*R-M*P*f)-(1-_)*u*y*(O+_*P*(D+_*R*(2*D*D-1)))),q=Math.atan2(y,-G);return{lat:this.toDegrees(N),lng:this.toDegrees(j),bearing:this.wrap360(this.toDegrees(q))}},t.prototype.inverse=function(t,e,i,a){void 0===i&&(i=100),void 0===a&&(a=!0);var s=t,o=e,r=this.toRadians(s.lat),h=this.toRadians(s.lng),l=this.toRadians(o.lat),p=this.toRadians(o.lng),c=Math.PI,u=Number.EPSILON,g=this.ellipsoid,f=g.a,d=g.b,L=g.f,M=p-h,v=(1-L)*Math.tan(r),y=1/Math.sqrt(1+v*v),w=v*y,m=(1-L)*Math.tan(l),b=1/Math.sqrt(1+m*m),S=m*b,O=Math.abs(M)>c/2||Math.abs(l-r)>c/2,P=M,R=null,D=null,E=O?c:0,x=0,G=O?-1:1,N=null,_=1,j=null,q=1,I=null,C=null,k=0;do{if(N=b*(R=Math.sin(P))*(b*R)+(y*S-w*b*(D=Math.cos(P)))*(y*S-w*b*D),Math.abs(N)<u)break;if(G=w*S+y*b*D,C=P,P=M+(1-(I=L/16*(q=1-(j=y*b*R/(x=Math.sqrt(N)))*j)*(4+L*(4-3*q))))*L*j*((E=Math.atan2(x,G))+I*x*((_=0!==q?G-2*w*S/q:0)+I*G*(2*_*_-1))),(O?Math.abs(P)-c:Math.abs(P))>c)throw new EvalError("λ > π")}while(Math.abs(P-C)>1e-12&&++k<i);if(k>=i){if(a)return this.inverse(t,new n.LatLng(e.lat,e.lng-.01),i,a);throw new EvalError("Inverse vincenty formula failed to converge after "+i+" iterations \n (start="+t.lat+"/"+t.lng+"; dest="+e.lat+"/"+e.lng+")")}var B=q*(f*f-d*d)/(d*d),J=B/1024*(256+B*(B*(74-47*B)-128)),T=d*(1+B/16384*(4096+B*(B*(320-175*B)-768)))*(E-J*x*(_+J/4*(G*(2*_*_-1)-J/6*_*(4*x*x-3)*(4*_*_-3)))),A=Math.abs(N)<u?0:Math.atan2(b*R,y*S-w*b*D),U=Math.abs(N)<u?c:Math.atan2(y*R,-w*b+y*S*D);return{distance:T,initialBearing:Math.abs(T)<u?NaN:this.wrap360(this.toDegrees(A)),finalBearing:Math.abs(T)<u?NaN:this.wrap360(this.toDegrees(U))}},t.prototype.intersection=function(t,e,i,a){var s=this.toRadians(t.lat),o=this.toRadians(t.lng),r=this.toRadians(i.lat),h=this.toRadians(i.lng),l=this.toRadians(e),p=this.toRadians(a),c=r-s,u=h-o,g=Math.PI,f=Number.EPSILON,d=2*Math.asin(Math.sqrt(Math.sin(c/2)*Math.sin(c/2)+Math.cos(s)*Math.cos(r)*Math.sin(u/2)*Math.sin(u/2)));if(Math.abs(d)<f)return t;var L=(Math.sin(r)-Math.sin(s)*Math.cos(d))/(Math.sin(d)*Math.cos(s)),M=(Math.sin(s)-Math.sin(r)*Math.cos(d))/(Math.sin(d)*Math.cos(r)),v=Math.acos(Math.min(Math.max(L,-1),1)),y=Math.acos(Math.min(Math.max(M,-1),1)),w=l-(Math.sin(h-o)>0?v:2*g-v),m=(Math.sin(h-o)>0?2*g-y:y)-p;if(0===Math.sin(w)&&0===Math.sin(m))return null;if(Math.sin(w)*Math.sin(m)<0)return null;var b=-Math.cos(w)*Math.cos(m)+Math.sin(w)*Math.sin(m)*Math.cos(d),S=Math.atan2(Math.sin(d)*Math.sin(w)*Math.sin(m),Math.cos(m)+Math.cos(w)*b),O=Math.asin(Math.min(Math.max(Math.sin(s)*Math.cos(S)+Math.cos(s)*Math.sin(S)*Math.cos(l),-1),1)),P=o+Math.atan2(Math.sin(l)*Math.sin(S)*Math.cos(s),Math.cos(S)-Math.sin(s)*Math.sin(O));return new n.LatLng(this.toDegrees(O),this.toDegrees(P))},t.prototype.midpoint=function(t,e){var i=this.toRadians(t.lat),a=this.toRadians(t.lng),s=this.toRadians(e.lat),o=this.toRadians(e.lng-t.lng),r=Math.cos(i),h=0,l=Math.sin(i),p={x:r+Math.cos(s)*Math.cos(o),y:h+Math.cos(s)*Math.sin(o),z:l+Math.sin(s)},c=Math.atan2(p.z,Math.sqrt(p.x*p.x+p.y*p.y)),u=a+Math.atan2(p.y,p.x);return new n.LatLng(this.toDegrees(c),this.toDegrees(u))},t}(),r=function(){function t(t){this.geodesic=new o,this.steps=t&&void 0!==t.steps?t.steps:3}return t.prototype.recursiveMidpoint=function(t,n,e){var i=[t,n],a=this.geodesic.midpoint(t,n);return e>0?(i.splice.apply(i,s([0,1],this.recursiveMidpoint(t,a,e-1))),i.splice.apply(i,s([i.length-2,2],this.recursiveMidpoint(a,n,e-1)))):i.splice(1,0,a),i},t.prototype.line=function(t,n){return this.recursiveMidpoint(t,n,Math.min(8,this.steps))},t.prototype.multiLineString=function(t){for(var n=[],e=0,i=t;e<i.length;e++){for(var a=i[e],o=[],r=1;r<a.length;r++)o.splice.apply(o,s([o.length-1,1],this.line(a[r-1],a[r])));n.push(o)}return n},t.prototype.lineString=function(t){return this.multiLineString([t])[0]},t.prototype.splitLine=function(t,e){var i={point:new n.LatLng(89.9,-180.0000001),bearing:180},a={point:new n.LatLng(89.9,180.0000001),bearing:180},s=new n.LatLng(t.lat,t.lng),o=new n.LatLng(e.lat,e.lng);s.lng=this.geodesic.wrap(s.lng,360),o.lng=this.geodesic.wrap(o.lng,360),o.lng-s.lng>180?o.lng=o.lng-360:o.lng-s.lng<-180&&(o.lng=o.lng+360);var r=[[new n.LatLng(s.lat,this.geodesic.wrap(s.lng,180)),new n.LatLng(o.lat,this.geodesic.wrap(o.lng,180))]];if(s.lng>=-180&&s.lng<=180){if(o.lng<-180){var h=this.geodesic.inverse(s,o).initialBearing;(l=this.geodesic.intersection(s,h,i.point,i.bearing))&&(r=[[s,l],[new n.LatLng(l.lat,l.lng+360),new n.LatLng(o.lat,o.lng+360)]])}else if(o.lng>180){h=this.geodesic.inverse(s,o).initialBearing;(l=this.geodesic.intersection(s,h,a.point,a.bearing))&&(r=[[s,l],[new n.LatLng(l.lat,l.lng-360),new n.LatLng(o.lat,o.lng-360)]])}}else if(o.lng>=-180&&o.lng<=180)if(s.lng<-180){h=this.geodesic.inverse(s,o).initialBearing;(l=this.geodesic.intersection(s,h,i.point,i.bearing))&&(r=[[new n.LatLng(s.lat,s.lng+360),new n.LatLng(l.lat,l.lng+360)],[l,o]])}else if(s.lng>180){var l;h=this.geodesic.inverse(s,o).initialBearing;(l=this.geodesic.intersection(s,h,i.point,i.bearing))&&(r=[[new n.LatLng(s.lat,s.lng-360),new n.LatLng(l.lat,l.lng-360)],[l,o]])}return r},t.prototype.splitMultiLineString=function(t){for(var n=[],e=0,i=t;e<i.length;e++){var a=i[e];if(1!==a.length){for(var s=[],o=1;o<a.length;o++){var r=this.splitLine(a[o-1],a[o]);s.pop(),s=s.concat(r[0]),r.length>1&&(n.push(s),s=r[1])}n.push(s)}else n.push(a)}return n},t.prototype.wrapMultiLineString=function(t){for(var e=[],i=0,a=t;i<a.length;i++){for(var s=[],o=null,r=0,h=a[i];r<h.length;r++){var l=h[r];if(null===o)s.push(new n.LatLng(l.lat,l.lng)),o=new n.LatLng(l.lat,l.lng);else{var p=Math.round((l.lng-o.lng)/360);s.push(new n.LatLng(l.lat,l.lng-360*p)),o=new n.LatLng(l.lat,l.lng-360*p)}}e.push(s)}return e},t.prototype.circle=function(t,e){for(var i=[],a=0;a<this.steps;a++){var s=this.geodesic.direct(t,360/this.steps*a,e);i.push(new n.LatLng(s.lat,s.lng))}return i.push(new n.LatLng(i[0].lat,i[0].lng)),i},t.prototype.splitCircle=function(t){var n=this.splitMultiLineString([t]);return 3===n.length&&(n[2]=s(n[2],n[0]),n.shift()),n},t.prototype.distance=function(t,e){return this.geodesic.inverse(new n.LatLng(t.lat,this.geodesic.wrap(t.lng,180)),new n.LatLng(e.lat,this.geodesic.wrap(e.lng,180))).distance},t.prototype.multilineDistance=function(t){for(var n=[],e=0,i=t;e<i.length;e++){for(var a=i[e],s=0,o=1;o<a.length;o++)s+=this.distance(a[o-1],a[o]);n.push(s)}return n},t.prototype.updateStatistics=function(t,n){var e={};e.distanceArray=this.multilineDistance(t),e.totalDistance=e.distanceArray.reduce((function(t,n){return t+n}),0),e.points=0;for(var i=0,a=t;i<a.length;i++){var s=a[i];e.points+=s.reduce((function(t){return t+1}),0)}e.vertices=0;for(var o=0,r=n;o<r.length;o++){s=r[o];e.vertices+=s.reduce((function(t){return t+1}),0)}return e},t}();function h(t){return"object"==typeof t&&null!==t&&"lat"in t&&"lng"in t&&"number"==typeof t.lat&&"number"==typeof t.lng}function l(t){return t instanceof Array&&"number"==typeof t[0]&&"number"==typeof t[1]}function p(t){return t instanceof n.LatLng||l(t)||h(t)}function c(t){if(t instanceof n.LatLng)return t;if(l(t))return new n.LatLng(t[0],t[1]);if(h(t))return new n.LatLng(t.lat,t.lng);throw new Error("L.LatLngExpression expected. Unknown object found.")}var u=function(t){function e(e,i){var s=t.call(this,[],i)||this;return s.defaultOptions={wrap:!0,steps:3},s.statistics={},s.points=[],n.Util.setOptions(s,a(a({},s.defaultOptions),i)),s.geom=new r(s.options),void 0!==e&&s.setLatLngs(e),s}return i(e,t),e.prototype.updateGeometry=function(){var n;if(n=this.geom.multiLineString(this.points),this.statistics=this.geom.updateStatistics(this.points,n),this.options.wrap){var e=this.geom.splitMultiLineString(n);t.prototype.setLatLngs.call(this,e)}else t.prototype.setLatLngs.call(this,this.geom.wrapMultiLineString(n))},e.prototype.setLatLngs=function(t){return this.points=function(t){for(var n=[],e=0,i=p(t[0])?[t]:t;e<i.length;e++){for(var a=[],s=0,o=i[e];s<o.length;s++){var r=o[s];if(!p(r))throw new Error("L.LatLngExpression[] | L.LatLngExpression[][] expected. Unknown object found.");a.push(c(r))}n.push(a)}return n}(t),this.updateGeometry(),this},e.prototype.addLatLng=function(t,n){var e=c(t);return 0===this.points.length?this.points.push([e]):void 0===n?this.points[this.points.length-1].push(e):n.push(e),this.updateGeometry(),this},e.prototype.fromGeoJson=function(t){var e=[],i=[];return"FeatureCollection"===t.type?i=t.features:"Feature"===t.type?i=[t]:["MultiPoint","LineString","MultiLineString","Polygon","MultiPolygon"].includes(t.type)?i=[{type:"Feature",geometry:t,properties:{}}]:console.log('[Leaflet.Geodesic] fromGeoJson() - Type "'+t.type+'" not supported.'),i.forEach((function(t){switch(t.geometry.type){case"MultiPoint":case"LineString":e=s(e,[n.GeoJSON.coordsToLatLngs(t.geometry.coordinates,0)]);break;case"MultiLineString":case"Polygon":e=s(e,n.GeoJSON.coordsToLatLngs(t.geometry.coordinates,1));break;case"MultiPolygon":t.geometry.coordinates.forEach((function(t){e=s(e,n.GeoJSON.coordsToLatLngs(t,1))}));break;default:console.log('[Leaflet.Geodesic] fromGeoJson() - Type "'+t.geometry.type+'" not supported.')}})),e.length&&this.setLatLngs(e),this},e.prototype.distance=function(t,n){return this.geom.distance(c(t),c(n))},e}(n.Polyline),g=function(t){function e(e,i){var s=t.call(this,[],i)||this;s.defaultOptions={wrap:!0,steps:24,fill:!0,noClip:!0},s.statistics={},n.Util.setOptions(s,a(a({},s.defaultOptions),i));var o=s.options;return s.radius=void 0===o.radius?1e6:o.radius,s.center=void 0===e?new n.LatLng(0,0):c(e),s.geom=new r(s.options),s.update(),s}return i(e,t),e.prototype.update=function(){var n=this.geom.circle(this.center,this.radius);if(this.statistics=this.geom.updateStatistics([[this.center]],[n]),this.statistics.totalDistance=this.geom.multilineDistance([n]).reduce((function(t,n){return t+n}),0),this.options.wrap){var e=this.geom.splitCircle(n);t.prototype.setLatLngs.call(this,e)}else t.prototype.setLatLngs.call(this,n)},e.prototype.distanceTo=function(t){var n=c(t);return this.geom.distance(this.center,n)},e.prototype.setLatLng=function(t,n){this.center=c(t),this.radius=n||this.radius,this.update()},e.prototype.setRadius=function(t,n){this.radius=t,this.center=n?c(n):this.center,this.update()},e}(n.Polyline);void 0!==window.L&&(window.L.Geodesic=u,window.L.geodesic=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return new(u.bind.apply(u,s([void 0],t)))},window.L.GeodesicCircle=g,window.L.geodesiccircle=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return new(g.bind.apply(g,s([void 0],t)))}),t.GeodesicCircleClass=g,t.GeodesicLine=u,Object.defineProperty(t,"__esModule",{value:!0})}));