UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 2.97 kB
const t=16,n=8,o=4,e=2,r=1,c=new Float64Array(2),f=new Float64Array(2),l=-90,u=90,h=-180,s=180,i="0123456789bcdefghjkmnpqrstuvwxyz";function a(t){return t<=57?t-48:t<=104?t-88:t<=107?t-89:t<=110?t-90:t-91}function g(t){return i[t]}function A(t){return(t[0]+t[1])/2}function d(t,n,o){return t[0]=n,t[1]=o,t}function M(t,n){const o=A(t),e=n,r=!n;t[0]=r*t[0]+e*o,t[1]=r*o+e*t[1]}function w(t,n){const o=n>A(t);return M(t,o),o}function y(i,g){const w=d(c,l,u),y=d(f,h,s);for(let c=0;c<g.length;c++){const f=a(g.charCodeAt(c));c%2==0?(M(y,!!(t&f)),M(y,!!(o&f)),M(y,!!(r&f)),M(w,!!(n&f)),M(w,!!(e&f))):(M(w,!!(t&f)),M(w,!!(o&f)),M(w,!!(r&f)),M(y,!!(n&f)),M(y,!!(e&f)))}return i[0]=A(w),i[1]=A(y),i}function C(t,n){let o=0,e=0,r=30,c=30;for(let f=0;f<n.length;f++){const t=a(n.charCodeAt(f)),l=f%2==0;r-=l?3:2,c-=l?2:3,o|=F(t,l)<<r,e|=j(t,l)<<c}return{geohashX:o,geohashY:e}}function X(t,n){let o=-90,e=90,r=-180,c=180;for(let f=0;f<n;f++){const n=Math.ceil((f+1)/2),l=Math.floor((f+1)/2),u=1-f%2,h=30-(3*n+2*l),s=30-(2*n+3*l),i=3*u+2*(1-u),a=2*u+3*(1-u),g=3*u+7*(1-u)<<s,A=(7*u+3*(1-u)<<h&t.geohashX)>>h,d=(g&t.geohashY)>>s;for(let t=i-1;t>=0;t--){const n=(r+c)/2,o=A&1<<t?1:0;r=(1-o)*r+o*n,c=(1-o)*n+o*c}for(let t=a-1;t>=0;t--){const n=(o+e)/2,r=d&1<<t?1:0;o=(1-r)*o+r*n,e=(1-r)*n+r*e}}return[r,o,c,e]}function Y(t,n,o,e){e%2&&(e+=1);let r=0,c=0,f=-90,l=90,u=-180,h=180;for(let s=0;s<e/2;s++){for(let t=0;t<5;t++){const n=(u+h)/2,e=o>n?1:0;r|=e<<29-(t+5*s),u=(1-e)*u+e*n,h=(1-e)*n+e*h}for(let t=0;t<5;t++){const o=(f+l)/2,e=n>o?1:0;c|=e<<29-(t+5*s),f=(1-e)*f+e*o,l=(1-e)*o+e*l}}t.geohashX=r,t.geohashY=c}function b(t,n,o,e,r){r%2&&(r+=1);let c=0,f=0,l=-90,u=90,h=-180,s=180;for(let i=0;i<r/2;i++){for(let t=0;t<5;t++){const n=(h+s)/2,o=e>n?1:0;c|=o<<29-(t+5*i),h=(1-o)*h+o*n,s=(1-o)*n+o*s}for(let t=0;t<5;t++){const n=(l+u)/2,e=o>n?1:0;f|=e<<29-(t+5*i),l=(1-e)*l+e*n,u=(1-e)*n+e*u}}t[2*n]=c,t[2*n+1]=f}function p(t,n,o){let e="";const r=d(c,-90,90),l=d(f,-180,180);for(let c=0;c<o;c++){let o=0;!(c%2)?(o|=w(l,n)<<4,o|=w(r,t)<<3,o|=w(l,n)<<2,o|=w(r,t)<<1,o|=w(l,n)<<0):(o|=w(r,t)<<4,o|=w(l,n)<<3,o|=w(r,t)<<2,o|=w(l,n)<<1,o|=w(r,t)<<0),e+=g(o)}return e}function x(t,n,c){return c?t&r|(n&r)<<1|(t&e)<<1|(n&e)<<2|(t&o)<<2:n&r|(t&r)<<1|(n&e)<<1|(t&e)<<2|(n&o)<<2}function F(c,f){return f?r&c|(o&c)>>1|(t&c)>>2:(e&c)>>1|(n&c)>>2}function j(c,f){return f?(e&c)>>1|(n&c)>>2:r&c|(o&c)>>1|(t&c)>>2}function k(c,f,l){const u=!((c.length-1)%2),h=c.substring(0,c.length-1),s=a(c.charCodeAt(c.length-1));let i=0,A=0,d=0,M=0;u?(i=8,A=4,d=r&s|(o&s)>>1|(t&s)>>2,M=(e&s)>>1|(n&s)>>2):(i=4,A=8,M=r&s|(o&s)>>1|(t&s)>>2,d=(e&s)>>1|(n&s)>>2);const w=d+f,y=M+l,C=Math.floor(w/i),X=Math.floor(y/A),Y=g(x(w-C*i,y-X*A,u));return c.length>1&&(C||X)?k(h,C,X)+Y:h+Y}export{C as convertGeohash32ToXY,a as decodeBase32Char,y as decodeGeohash,X as decodeGeohashXY,g as encodeBase32Char,p as encodeGeohash,k as getRelativeGeohash,b as setGeohashBuf,Y as setGeohashXY,F as unpackXBits,j as unpackYBits};