-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGeo3x3.min.js
1 lines (1 loc) · 1.39 KB
/
Geo3x3.min.js
1
class Geo3x3{static encode(a,b,c){if(1>c||!c||1e3<=c)return null;if("string"==typeof a&&(a=parseFloat(a)),"string"==typeof b&&(b=parseFloat(b)),isNaN(a)||isNaN(b)||-90>a||90<a||-180>b||180<b)return null;let d="E";0>b&&(d="W",b+=180),a+=90;let e=180;for(let f=1;f<c;f++){e/=3;const c=Math.floor(b/e),f=Math.floor(a/e);d+=c+3*f+1,b-=c*e,a-=f*e}return d}static decode(a){if(!a||"string"!=typeof a||!a.length)return null;let b=!1,d=0;const e=a.charAt(0);if("W"==e)b=!0,d=1;else if("E"==e)d=1;else return null;let c=180,f=0,g=0,h=1;for(let b,e=d;e<a.length&&(b="0123456789".indexOf(a.charAt(e)),0!=b);e++){if(0>b)return null;c/=3,b--,g+=b%3*c,f+=Math.floor(b/3)*c,h++}return f+=c/2,g+=c/2,f-=90,b&&(g-=180),{lat:f,lng:g,level:h,unit:c}}static getCoords(a){const b=this.decode(a),c=b.lng,d=b.lat,e=b.unit/2;return[{lat:d-e,lng:c-e},{lat:d-e,lng:c+e},{lat:d+e,lng:c+e},{lat:d+e,lng:c-e}]}static getMeshSize(a){const b=this.getCoords(a),c=[,,,,];for(let d=0;d<c.length;d++)c[d]=this.ll2xy(b[d].lat,b[d].lng);const d=c[1].x-c[0].x,e=c[2].y-c[1].y;return{x:d,y:e}}static ll2xy(a,b){const c=this.RPI_EARTH*b,d=this.RPI_EARTH*Math.log(Math.tan((90+a)*Math.PI/360))/(Math.PI/180);return{x:c,y:d}}static xy2ll(a,b){const c=a/this.RPI_EARTH;let d=b/this.RPI_EARTH;return d=180/Math.PI*(2*Math.atan(Math.exp(d*Math.PI/180))-Math.PI/2),{lat:d,lng:c}}}Geo3x3.R2_EARTH=12756274,Geo3x3.RPI_EARTH=Geo3x3.R2_EARTH*Math.PI/2/180;export{Geo3x3};