geohex
Version:
Hexagonal geocoding system, library for converting geographic coordinates to hexagonal grid cell and vice versa
1 lines • 3.65 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Geohex=e():t.Geohex=e()}(this,(()=>(()=>{"use strict";var t={d:(e,o)=>{for(var n in o)t.o(o,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:o[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{default:()=>M,getCellByCode:()=>s,getCellByLocation:()=>u,getCellByXY:()=>f,getXYByCode:()=>g,getXYByLocation:()=>y});const o=20037508.34,n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";function r(t){return o/Math.pow(3,t+3)}function l(t,e,o){const n=Math.pow(3,o+2),r=Math.abs(t-e);if(r===n&&t>e)return{x:e,y:t};if(r>n){const o=r-n,l=Math.floor(o/2),a=o-l;if(t>e)return{x:e+a+l,y:t-l-a};if(e>t)return{x:e-a-l,y:t+l+a}}return{x:t,y:e}}function a(t,e){return{x:t*o/180,y:Math.log(Math.tan((90+e)*Math.PI/360))/(Math.PI/180)*o/180}}function h(t,e){return{lon:t/o*180,lat:180/Math.PI*(2*Math.atan(Math.exp(e/o*Math.PI))-Math.PI/2)}}class c{constructor(t,e,o,n,r){this.lat=t,this.lon=e,this.x=o,this.y=n,this.code=r}getZoomLevel(){return this.code.length-2}getHexSize(){return r(this.getZoomLevel())}getHexCoords(){const{lat:t,lon:e}=this,{x:o,y:n}=a(e,t),r=Math.tan(Math.PI*(60/180)),l=this.getHexSize(),c=h(o,n+r*l).lat,i=h(o,n-r*l).lat,s=h(o-2*l,n).lon,u=h(o+2*l,n).lon,f=h(o-1*l,n).lon,g=h(o+1*l,n).lon;return[{lat:t,lon:s},{lat:c,lon:f},{lat:c,lon:g},{lat:t,lon:u},{lat:i,lon:g},{lat:i,lon:f}]}}const i=Math.tan(Math.PI*(30/180));function s(t){const{x:e,y:o}=g(t);return f(e,o,t.length-2)}function u(t,e,o){const{x:n,y:r}=y(t,e,o);return f(n,r,o)}function f(t,e,o){const l=r(o),a=6*l*i,s=(i*t*(6*l)+e*a)/2,u=(s-e*a)/i,f=Math.pow(3,o+2),g=Math.abs(t-e);let{lon:y,lat:M}=h(u,s),d=t,p=e;return g===f&&(t>e&&(d=e,p=t),y=-180),new c(M,y,t,e,function(t,e,o,r){const l=[],a=[];let h="",c="",i="";for(let n=0;n<=r+2;n++){const h=Math.pow(3,r+2-n);t>=Math.ceil(h/2)?(l[n]=2,t-=h):t<=-Math.ceil(h/2)?(l[n]=0,t+=h):l[n]=1,e>=Math.ceil(h/2)?(a[n]=2,e-=h):e<=-Math.ceil(h/2)?(a[n]=0,e+=h):a[n]=1,2===n&&(-180===o||o>=0)&&(2===l[0]&&1===a[0]&&l[1]===a[1]&&l[2]===a[2]?(l[0]=1,a[0]=2):1===l[0]&&0===a[0]&&l[1]===a[1]&&l[2]===a[2]&&(l[0]=0,a[0]=1))}for(let t=0;t<l.length;t++)c+=""+l[t]+a[t],i+=parseInt(c,3),h+=i,c="",i="";const s=h.substring(0,3),u=h.substring(3),f=Math.floor(s/30),g=s%30;return n.charAt(f)+n.charAt(g)+u}(d,p,y,o))}function g(t){const e=t.length-2;let o=""+(30*n.indexOf(t.charAt(0))+n.indexOf(t.charAt(1)))+t.substring(2);o.charAt(0).match(/[15]/)&&o.charAt(1).match(/[^125]/)&&o.charAt(2).match(/[^125]/)&&(5===o.charAt(0)?o="7"+o.substring(1,o.length):1===o.charAt(0)&&(o="3"+o.substring(1,o.length)));let r=o.length;for(let t=0;t<e+3-r;t++)o="0"+o,r++;let a="";for(let t=0;t<r;t++){const e=parseInt(o.charAt(t)).toString(3);e?1===e.length&&(a+="0"):a+="00",a+=e}const h=[],c=[];for(let t=0;t<a.length/2;t++)h[t]=parseInt(a.charAt(2*t)),c[t]=parseInt(a.charAt(2*t+1));let i=0,s=0;for(let t=0;t<=e+2;t++){const o=Math.pow(3,e+2-t);0===h[t]?i-=o:2===h[t]&&(i+=o),0===c[t]?s-=o:2===c[t]&&(s+=o)}return l(i,s,e)}function y(t,e,o){const n=r(o),h=6*n,c=6*n*i,{x:s,y:u}=a(e,t),f=(s+u/i)/h,g=(u-i*s)/c,y=Math.floor(f),M=Math.floor(g),d=f-y,p=g-M;let x=Math.round(f),b=Math.round(g);return p>1-d?p<2*d&&p>.5*d&&(x=y+1,b=M+1):p<1-d&&p>2*d-1&&p<.5*d+.5&&(x=y,b=M),l(x,b,o)}const M={getCellByCode:s,getCellByLocation:u,getCellByXY:f,getXYByCode:g,getXYByLocation:y};return e})()));