@hamset/maidenhead-locator
Version:
Maidenhead grid locator and WGS84 coordinate calculation and transformation
2 lines (1 loc) • 1.67 kB
JavaScript
(function(a,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(a=typeof globalThis<"u"?globalThis:a||self,o(a.lib={}))})(this,function(a){"use strict";const o=n=>String.fromCharCode(65+n),l=n=>{if(n.length!==1||!/[A-Za-z]/.test(n))throw new Error("invalid char input.");return n.toUpperCase().charCodeAt(0)-65},r=n=>typeof n!="string"||n.length%2!==0?!1:/^[A-R]{2}[0-9]{2}[A-Xa-x]{2}$/.test(n),b=n=>{if(typeof n!="object"&&!Array.isArray(n))throw new Error("invalid coord. it should be [number, number] or {lat: number, lng: number}");const{lat:e,lng:t}=Array.isArray(n)?{lat:n[0],lng:n[1]}:n;if(e<-90||e>90)throw new Error("invalid lat. it should be between -90 and 90");if(t<-180||t>180)throw new Error("invalid lng. it should be between -180 and 180");const i=Math.floor((e+90)/10),s=Math.floor((t+180)/20),d=Math.floor(e+90)%10,f=Math.floor(Math.floor(t+180)%20/2),h=Math.floor((e-Math.floor(e))*60/2.5),u=Math.floor((t-Math.floor(t/2)*2)*60/5);return`${o(s)}${o(i)}${f}${d}${(o(u)+o(h)).toLowerCase()}`},c=n=>{if(!r(n))throw new Error("invalid gridLocator");const e=l(n[0]),t=l(n[1]),i=parseInt(n[2]),s=parseInt(n[3]),d=l(n[4].toUpperCase()),f=l(n[5].toUpperCase()),h=e*20-180+i*2+d*5/60;return{lat:t*10-90+s+f*2.5/60,lng:h}},p=n=>{if(!r(n))throw new Error("invalid gridLocator");let e=l(n[0])*20+parseInt(n[2])*2+l(n[4])*5/60-180,t=l(n[1])*10+parseInt(n[3])+l(n[5])*2.5/60-90;return[[t,e],[t+.041666666666666664,e+.08333333333333333]]};a.WGS84ToMaidenhead=b,a.maidenheadToBoundingBox=p,a.maidenheadToWGS84=c,a.validateGridLocator=r,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});