forked from manuelbieh/geolib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeolib.min.js
12 lines (12 loc) · 4.73 KB
/
geolib.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/**
* A small library to provide some basic geo functions like distance calculation,
* conversion of decimal coordinates to sexagesimal and vice versa, etc.
* WGS 84 (World Geodetic System 1984)
*
* @author Manuel Bieh
* @url http://www.manuel-bieh.de/
* @version 1.1.4
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
*
*/
;(function(a,b){var c=6378137;var d=/^([0-9]{1,3})°\s*([0-9]{1,3})'\s*(([0-9]{1,3}(\.([0-9]{1,2}))?)"\s*)?([NEOSW]?)$/;var e={decimal:{},sexagesimal:{},distance:0,getDistance:function(a,b,c){c=parseInt(c,10)||1;var d={},f={};d.latitude=e.useDecimal(a.latitude);d.longitude=e.useDecimal(a.longitude);f.latitude=e.useDecimal(b.latitude);f.longitude=e.useDecimal(b.longitude);var g=6378137,h=6356752.314245,i=1/298.257223563;var j=(f.longitude-d.longitude).toRad();var k=Math.atan((1-i)*Math.tan(parseFloat(d.latitude).toRad()));var l=Math.atan((1-i)*Math.tan(parseFloat(f.latitude).toRad()));var m=Math.sin(k),n=Math.cos(k);var o=Math.sin(l),p=Math.cos(l);var q=j,r,s=100;do{var t=Math.sin(q),u=Math.cos(q);var v=Math.sqrt(p*t*p*t+(n*o-m*p*u)*(n*o-m*p*u));if(v==0){return e.distance=0}var w=m*o+n*p*u;var x=Math.atan2(v,w);var y=n*p*t/v;var z=1-y*y;var A=w-2*m*o/z;if(isNaN(A)){A=0}var B=i/16*z*(4+i*(4-3*z));r=q;q=j+(1-B)*i*y*(x+B*v*(A+B*w*(-1+2*A*A)))}while(Math.abs(q-r)>1e-12&&--s>0);if(s==0){return NaN}var C=z*(g*g-h*h)/(h*h);var D=1+C/16384*(4096+C*(-768+C*(320-175*C)));var E=C/1024*(256+C*(-128+C*(74-47*C)));var F=E*v*(A+E/4*(w*(-1+2*A*A)-E/6*A*(-3+4*v*v)*(-3+4*A*A)));var G=h*D*(x-F);G=G.toFixed(3);return e.distance=parseInt(Math.round(G/c)*c,10)},getDistanceSimple:function(a,b,d){d=parseInt(d,10)||1;var f={},g={};f.latitude=parseFloat(e.useDecimal(a.latitude)).toRad();f.longitude=parseFloat(e.useDecimal(a.longitude)).toRad();g.latitude=parseFloat(e.useDecimal(b.latitude)).toRad();g.longitude=parseFloat(e.useDecimal(b.longitude)).toRad();var h=Math.round(Math.acos(Math.sin(g.latitude)*Math.sin(f.latitude)+Math.cos(g.latitude)*Math.cos(f.latitude)*Math.cos(f.longitude-g.longitude))*c);return e.distance=parseInt(Math.round(h/d)*d,10)},getCenter:function(a){var b=function(a){return Math.max.apply(Math,a)};var c=function(a){return Math.min.apply(Math,a)};var d,f,g={lat:[],lng:[]};for(var h in a){g.lat.push(e.useDecimal(a[h].latitude));g.lng.push(e.useDecimal(a[h].longitude))}var i=c(g.lat);var j=c(g.lng);var k=b(g.lat);var l=b(g.lng);d=((i+k)/2).toFixed(6);f=((j+l)/2).toFixed(6);var m=e.convertUnit("km",e.getDistance(i,j,k,l));return{latitude:d,longitude:f,distance:m}},isPointInside:function(a,b){for(var c=false,d=-1,e=b.length,f=e-1;++d<e;f=d){(b[d].longitude<=a.longitude&&a.longitude<b[f].longitude||b[f].longitude<=a.longitude&&a.longitude<b[d].longitude)&&a.latitude<(b[f].latitude-b[d].latitude)*(a.longitude-b[d].longitude)/(b[f].longitude-b[d].longitude)+b[d].latitude&&(c=!c)}return c},isPointInCircle:function(a,b,c){return e.getDistance(a,b)<c},orderByDistance:function(a,b){var c=[];for(var d in b){var f=e.getDistance(a,b[d]);c.push({key:d,latitude:b[d].latitude,longitude:b[d].longitude,distance:f})}return c.sort(function(a,b){return a.distance-b.distance})},findNearest:function(a,b,c){c=c||0;var d=e.orderByDistance(a,b);return d[c]},getPathLength:function(a){var b=0,c;for(var d in a){if(c){b+=e.getDistance(a[d],c)}c=a[d]}return b},convertUnit:function(a,b,c){if(b==0||typeof b=="undefined"){if(e.distance==0){return 0}else{b=e.distance}}a=a||"m";c=c||4;switch(a){case"m":return e.round(b,c);break;case"km":return e.round(b/1e3,c);break;case"cm":return e.round(b*100,c);break;case"mm":return e.round(b*1e3,c);break;case"mi":return e.round(b*(1/1609.344),c);break;case"sm":return e.round(b*(1/1852.216),c);break;case"ft":return e.round(b*(100/30.48),c);break;case"in":return e.round(b*100/2.54,c);break;case"yd":return e.round(b*(1/.9144),c);break}return b},useDecimal:function(a){a=a.toString().replace(/\s*/,"");if(!isNaN(parseFloat(a))&&parseFloat(a).toString()==a){return a}else if(e.isSexagesimal(a)==true){return e.sexagesimal2decimal(a)}else{throw"Unknown format."}},decimal2sexagesimal:function(a){if(a in e.sexagesimal){return e.sexagesimal[a]}var b=a.toString().split(".");var c=b[0];var d=("0."+b[1])*60;var f=d.toString().split(".");d=parseInt(d,10);f=(("0."+f[1])*60).toFixed(2);e.sexagesimal[a]=c+"° "+d+"' "+f+'"';return e.sexagesimal[a]},sexagesimal2decimal:function(a){if(a in e.decimal){return e.decimal[a]}var b=new RegExp(d);var c=b.exec(a);if(c){var f=parseFloat(c[2]/60);var g=parseFloat(c[4]/3600)||0}var h=(parseFloat(c[1])+f+g).toFixed(8);h=c[7]=="S"||c[7]=="W"?h*-1:h;e.decimal[a]=h;return h},isSexagesimal:function(a){return d.test(a)},round:function(a,b){var c=Math.pow(10,b);return Math.round(a*c)/c}};if(typeof Number.prototype.toRad==="undefined"){Number.prototype.toRad=function(){return this*Math.PI/180}}a.geolib=e})(this)