UNPKG

@hamset/maidenhead-locator

Version:

Maidenhead grid locator and WGS84 coordinate calculation and transformation

35 lines (34 loc) 1.75 kB
const a = (n) => String.fromCharCode(65 + n), r = (n) => { if (n.length !== 1 || !/[A-Za-z]/.test(n)) throw new Error("invalid char input."); return n.toUpperCase().charCodeAt(0) - 65; }, d = (n) => typeof n != "string" || n.length % 2 !== 0 ? !1 : /^[A-R]{2}[0-9]{2}[A-Xa-x]{2}$/.test(n), u = (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 l = Math.floor((e + 90) / 10), o = Math.floor((t + 180) / 20), s = Math.floor(e + 90) % 10, h = Math.floor(Math.floor(t + 180) % 20 / 2), i = Math.floor((e - Math.floor(e)) * 60 / 2.5), f = Math.floor((t - Math.floor(t / 2) * 2) * 60 / 5); return `${a(o)}${a(l)}${h}${s}${(a(f) + a(i)).toLowerCase()}`; }, w = (n) => { if (!d(n)) throw new Error("invalid gridLocator"); const e = r(n[0]), t = r(n[1]), l = parseInt(n[2]), o = parseInt(n[3]), s = r(n[4].toUpperCase()), h = r(n[5].toUpperCase()), i = e * 20 - 180 + l * 2 + s * 5 / 60; return { lat: t * 10 - 90 + o + h * 2.5 / 60, lng: i }; }, b = (n) => { if (!d(n)) throw new Error("invalid gridLocator"); let e = r(n[0]) * 20 + parseInt(n[2]) * 2 + r(n[4]) * 5 / 60 - 180, t = r(n[1]) * 10 + parseInt(n[3]) + r(n[5]) * 2.5 / 60 - 90; return [ [t, e], [t + 0.041666666666666664, e + 0.08333333333333333] ]; }; export { u as WGS84ToMaidenhead, b as maidenheadToBoundingBox, w as maidenheadToWGS84, d as validateGridLocator };