UNPKG

12.7 kBJavaScriptView Raw
1/*! Leaflet.Geodesic 2.5.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";n=n&&n.hasOwnProperty("default")?n.default:n;
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 ***************************************************************************** */
17var i=function(t,n){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var i in n)n.hasOwnProperty(i)&&(t[i]=n[i])})(t,n)};function e(t,n){function e(){this.constructor=t}i(t,n),t.prototype=null===n?Object.create(n):(e.prototype=n.prototype,new e)}var s=function(){return(s=Object.assign||function(t){for(var n,i=1,e=arguments.length;i<e;i++)for(var s in n=arguments[i])Object.prototype.hasOwnProperty.call(n,s)&&(t[s]=n[s]);return t}).apply(this,arguments)};function a(){for(var t=0,n=0,i=arguments.length;n<i;n++)t+=arguments[n].length;var e=Array(t),s=0;for(n=0;n<i;n++)for(var a=arguments[n],o=0,r=a.length;o<r;o++,s++)e[s]=a[o];return e}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=s(s({},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.wrap360=function(t){return 0<=t&&t<360?t:(t%360+360)%360},t.prototype.wrap180=function(t){return-180<=t&&t<180?t:((t+180)%360+360)%360-180},t.prototype.direct=function(t,n,i,e){void 0===e&&(e=100);var s=this.toRadians(t.lat),a=this.toRadians(t.lng),o=this.toRadians(n),r=i,h=1e3*Number.EPSILON,c=this.ellipsoid,p=c.a,u=c.b,l=c.f,f=Math.sin(o),g=Math.cos(o),d=(1-l)*Math.tan(s),M=1/Math.sqrt(1+d*d),y=d*M,L=Math.atan2(d,g),v=M*f,m=1-v*v,w=m*(p*p-u*u)/(u*u),b=1+w/16384*(4096+w*(w*(320-175*w)-768)),E=w/1024*(256+w*(w*(74-47*w)-128)),O=r/(u*b),P=null,x=null,R=null,S=null,D=0;do{R=Math.cos(2*L+O),S=O,O=r/(u*b)+E*(P=Math.sin(O))*(R+E/4*((x=Math.cos(O))*(2*R*R-1)-E/6*R*(4*P*P-3)*(4*R*R-3)))}while(Math.abs(O-S)>h&&++D<e);if(D>=e)throw new EvalError("Direct vincenty formula failed to converge after "+e+" iterations (start="+t.lat+"/"+t.lng+"; bearing="+n+"; distance="+i+")");var G=y*P-M*x*g,N=Math.atan2(y*x+M*P*g,(1-l)*Math.sqrt(v*v+G*G)),j=l/16*m*(4+l*(4-3*m)),k=a+(Math.atan2(P*f,M*x-y*P*g)-(1-j)*l*v*(O+j*P*(R+j*x*(2*R*R-1)))),_=Math.atan2(v,-G);return{lat:this.toDegrees(N),lng:this.toDegrees(k),bearing:this.wrap360(this.toDegrees(_))}},t.prototype.inverse=function(t,i,e,s){void 0===e&&(e=100),void 0===s&&(s=!0);var a=t,o=i,r=this.toRadians(a.lat),h=this.toRadians(a.lng),c=this.toRadians(o.lat),p=this.toRadians(o.lng),u=Math.PI,l=Number.EPSILON,f=this.ellipsoid,g=f.a,d=f.b,M=f.f,y=p-h,L=(1-M)*Math.tan(r),v=1/Math.sqrt(1+L*L),m=L*v,w=(1-M)*Math.tan(c),b=1/Math.sqrt(1+w*w),E=w*b,O=Math.abs(y)>u/2||Math.abs(c-r)>u/2,P=y,x=null,R=null,S=O?u:0,D=0,G=O?-1:1,N=null,j=1,k=null,_=1,q=null,I=null,A=0;do{if(N=b*(x=Math.sin(P))*(b*x)+(v*E-m*b*(R=Math.cos(P)))*(v*E-m*b*R),Math.abs(N)<l)break;if(G=m*E+v*b*R,I=P,P=y+(1-(q=M/16*(_=1-(k=v*b*x/(D=Math.sqrt(N)))*k)*(4+M*(4-3*_))))*M*k*((S=Math.atan2(D,G))+q*D*((j=0!==_?G-2*m*E/_:0)+q*G*(2*j*j-1))),(O?Math.abs(P)-u:Math.abs(P))>u)throw new EvalError("λ > π")}while(Math.abs(P-I)>1e-12&&++A<e);if(A>=e){if(s)return this.inverse(t,new n.LatLng(i.lat,i.lng-.01),e,s);throw new EvalError("Inverse vincenty formula failed to converge after "+e+" iterations (start="+t.lat+"/"+t.lng+"; dest="+i.lat+"/"+i.lng+")")}var J=_*(g*g-d*d)/(d*d),T=J/1024*(256+J*(J*(74-47*J)-128)),B=d*(1+J/16384*(4096+J*(J*(320-175*J)-768)))*(S-T*D*(j+T/4*(G*(2*j*j-1)-T/6*j*(4*D*D-3)*(4*j*j-3)))),C=Math.abs(N)<l?0:Math.atan2(b*x,v*E-m*b*R),U=Math.abs(N)<l?u:Math.atan2(v*x,-m*b+v*E*R);return{distance:B,initialBearing:Math.abs(B)<l?NaN:this.wrap360(this.toDegrees(C)),finalBearing:Math.abs(B)<l?NaN:this.wrap360(this.toDegrees(U))}},t.prototype.intersection=function(t,i,e,s){var a=this.toRadians(t.lat),o=this.toRadians(t.lng),r=this.toRadians(e.lat),h=this.toRadians(e.lng),c=this.toRadians(i),p=this.toRadians(s),u=r-a,l=h-o,f=Math.PI,g=Number.EPSILON,d=2*Math.asin(Math.sqrt(Math.sin(u/2)*Math.sin(u/2)+Math.cos(a)*Math.cos(r)*Math.sin(l/2)*Math.sin(l/2)));if(Math.abs(d)<g)return t;var M=(Math.sin(r)-Math.sin(a)*Math.cos(d))/(Math.sin(d)*Math.cos(a)),y=(Math.sin(a)-Math.sin(r)*Math.cos(d))/(Math.sin(d)*Math.cos(r)),L=Math.acos(Math.min(Math.max(M,-1),1)),v=Math.acos(Math.min(Math.max(y,-1),1)),m=c-(Math.sin(h-o)>0?L:2*f-L),w=(Math.sin(h-o)>0?2*f-v:v)-p;if(0===Math.sin(m)&&0===Math.sin(w))return null;if(Math.sin(m)*Math.sin(w)<0)return null;var b=-Math.cos(m)*Math.cos(w)+Math.sin(m)*Math.sin(w)*Math.cos(d),E=Math.atan2(Math.sin(d)*Math.sin(m)*Math.sin(w),Math.cos(w)+Math.cos(m)*b),O=Math.asin(Math.sin(a)*Math.cos(E)+Math.cos(a)*Math.sin(E)*Math.cos(c));if(isNaN(O))return null;var P=o+Math.atan2(Math.sin(c)*Math.sin(E)*Math.cos(a),Math.cos(E)-Math.sin(a)*Math.sin(O));return new n.LatLng(this.toDegrees(O),this.toDegrees(P))},t.prototype.midpoint=function(t,i){var e=this.toRadians(t.lat),s=this.toRadians(t.lng),a=this.toRadians(i.lat),o=this.toRadians(i.lng-t.lng),r=Math.cos(e),h=0,c=Math.sin(e),p={x:r+Math.cos(a)*Math.cos(o),y:h+Math.cos(a)*Math.sin(o),z:c+Math.sin(a)},u=Math.atan2(p.z,Math.sqrt(p.x*p.x+p.y*p.y)),l=s+Math.atan2(p.y,p.x);return new n.LatLng(this.toDegrees(u),this.toDegrees(l))},t}(),r=function(){function t(t){this.geodesic=new o,this.options={wrap:!0,steps:3},this.options=s(s({},this.options),t),this.steps=void 0===this.options.steps?3:this.options.steps}return t.prototype.recursiveMidpoint=function(t,n,i){var e=[t,n],s=this.geodesic.midpoint(t,n);return this.options.wrap&&(s.lng=this.geodesic.wrap180(s.lng)),i>0?(e.splice.apply(e,a([0,1],this.recursiveMidpoint(t,s,i-1))),e.splice.apply(e,a([e.length-2,2],this.recursiveMidpoint(s,n,i-1)))):e.splice(1,0,s),e},t.prototype.line=function(t,n){return this.recursiveMidpoint(t,n,Math.min(8,this.steps))},t.prototype.circle=function(t,i){for(var e=[],s=0;s<this.steps+1;s++){var a=this.geodesic.direct(t,360/this.steps*s,i);e.push(new n.LatLng(a.lat,a.lng))}return e},t.prototype.multiLineString=function(t){var n=this,i=[];return t.forEach((function(t){for(var e=[],s=1;s<t.length;s++)e.splice.apply(e,a([e.length-1,1],n.line(t[s-1],t[s])));i.push(e)})),i},t.prototype.lineString=function(t){return this.multiLineString([t])[0]},t.prototype.splitLine=function(t,i){var e={point:new n.LatLng(89,-180),bearing:180},s={point:new n.LatLng(89,180),bearing:180};t.lng=Math.max(-179.9,t.lng),t.lng=Math.min(179.9,t.lng),i.lng=Math.max(-179.9,i.lng),i.lng=Math.min(179.9,i.lng);var a,o=this.geodesic.inverse(t,i);if((a=o.initialBearing>180?this.geodesic.intersection(t,o.initialBearing,e.point,e.bearing):this.geodesic.intersection(t,o.initialBearing,s.point,s.bearing))&&this.geodesic.inverse(t,a).distance<o.distance)return a.lng<-179.9999?[[t,a],[new n.LatLng(a.lat,a.lng+360),i]]:a.lng>179.9999?[[t,a],[new n.LatLng(a.lat,a.lng-360),i]]:[[t,a],[a,i]];return[[t,i]]},t.prototype.splitMultiLineString=function(t){var n=this,i=[];return t.forEach((function(t){for(var e=[t[0]],s=1;s<t.length;s++){var a=n.splitLine(t[s-1],t[s]);1===a.length?e.push(t[s]):(e.push(a[0][1]),i.push(e),e=a[1])}i.push(e)})),i},t.prototype.distance=function(t,n){return this.geodesic.inverse(t,n).distance},t.prototype.multilineDistance=function(t){var n=this,i=[];return t.forEach((function(t){for(var e=0,s=1;s<t.length;s++)e+=n.distance(t[s-1],t[s]);i.push(e)})),i},t.prototype.updateStatistics=function(t,n){var i={};return i.distanceArray=this.multilineDistance(t),i.totalDistance=i.distanceArray.reduce((function(t,n){return t+n}),0),i.points=0,t.forEach((function(t){i.points+=t.reduce((function(t){return t+1}),0)})),i.vertices=0,n.forEach((function(t){i.vertices+=t.reduce((function(t){return t+1}),0)})),i},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 c(t){return t instanceof Array&&"number"==typeof t[0]&&"number"==typeof t[1]}function p(t){return t instanceof n.LatLng||(!!c(t)||!!h(t))}function u(t){if(t instanceof n.LatLng)return t;if(c(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 l=function(t){function i(i,e){var a=t.call(this,[],e)||this;return a.defaultOptions={wrap:!0,steps:3},a.statistics={},a.points=[],n.Util.setOptions(a,s(s({},a.defaultOptions),e)),a.geom=new r(a.options),void 0!==i&&a.setLatLngs(i),a}return e(i,t),i.prototype.updateGeometry=function(){var n=[];if(this.points.length>0&&this.points[0].length>=2)if(n=this.geom.multiLineString(this.points),this.options.wrap){var i=this.geom.splitMultiLineString(n);t.prototype.setLatLngs.call(this,i)}else t.prototype.setLatLngs.call(this,n);else t.prototype.setLatLngs.call(this,n);this.statistics=this.geom.updateStatistics(this.points,n)},i.prototype.setLatLngs=function(t){return this.points=function(t){for(var n=[],i=function(i){if(p(i)){var e=[];return t.forEach((function(t){e.push(u(t))})),n.push(e),"break"}if(!(i instanceof Array))throw new Error("L.LatLngExpression[] | L.LatLngExpression[][] expected. Unknown object found.");if(!p(i[0]))throw new Error("L.LatLngExpression[] | L.LatLngExpression[][] expected. Unknown object found.");var s=[];i.forEach((function(t){s.push(u(t))})),n.push(s)},e=0,s=t;e<s.length;e++){if("break"===i(s[e]))break}return n}(t),this.updateGeometry(),this},i.prototype.addLatLng=function(t,n){var i=u(t);return 0===this.points.length?this.points.push([i]):void 0===n?this.points[this.points.length-1].push(i):n.push(i),this.updateGeometry(),this},i.prototype.fromGeoJson=function(t){var i=[],e=[];return"FeatureCollection"===t.type?e=t.features:"Feature"===t.type?e=[t]:["MultiPoint","LineString","MultiLineString","Polygon","MultiPolygon"].includes(t.type)?e=[{type:"Feature",geometry:t,properties:{}}]:console.log('[Leaflet.Geodesic] fromGeoJson() - Type "'+t.type+'" not supported.'),e.forEach((function(t){switch(t.geometry.type){case"MultiPoint":case"LineString":i=a(i,[n.GeoJSON.coordsToLatLngs(t.geometry.coordinates,0)]);break;case"MultiLineString":case"Polygon":i=a(i,n.GeoJSON.coordsToLatLngs(t.geometry.coordinates,1));break;case"MultiPolygon":t.geometry.coordinates.forEach((function(t){i=a(i,n.GeoJSON.coordsToLatLngs(t,1))}));break;default:console.log('[Leaflet.Geodesic] fromGeoJson() - Type "'+t.geometry.type+'" not supported.')}})),i.length&&this.setLatLngs(i),this},i.prototype.distance=function(t,n){return this.geom.distance(u(t),u(n))},i}(n.Polyline),f=function(t){function i(i,e){var a=t.call(this,[],e)||this;a.defaultOptions={wrap:!0,steps:24,fill:!0,noClip:!0},a.statistics={},n.Util.setOptions(a,s(s({},a.defaultOptions),e));var o=a.options;return a.radius=void 0===o.radius?1e6:o.radius,a.center=void 0===i?new n.LatLng(0,0):u(i),a.geom=new r(a.options),a.update(),a}return e(i,t),i.prototype.update=function(){var t=this.geom.circle(this.center,this.radius);this.statistics=this.geom.updateStatistics([[this.center]],[t]),this.statistics.totalDistance=this.geom.multilineDistance([t]).reduce((function(t,n){return t+n}),0),this.setLatLngs(t)},i.prototype.distanceTo=function(t){var n=u(t);return this.geom.distance(this.center,n)},i.prototype.setLatLng=function(t){this.center=u(t),this.update()},i.prototype.setRadius=function(t){this.radius=t,this.update()},i}(n.Polyline);n.Geodesic=l,n.geodesic=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return new(l.bind.apply(l,a([void 0],t)))},n.GeodesicCircle=f,n.geodesiccircle=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return new(f.bind.apply(f,a([void 0],t)))},t.GeodesicCircleClass=f,t.GeodesicLine=l,Object.defineProperty(t,"__esModule",{value:!0})}));