import type { LatLon } from './types' /* ================= geometry helpers ================= */ export const R = 6378137, D2R = Math.PI / 180 /** Web Mercator metres → [lon, lat] */ export const m2ll = (x: number, y: number): [number, number] => [ x / R / D2R, (2 * Math.atan(Math.exp(y / R)) - Math.PI / 2) / D2R, ] /* ================= location parsing ================= */ export function parseLoc(text: string): LatLon | null { text = (text || '').trim() const m = text.match(/[?&]x=(-?\d+(?:\.\d+)?)/), n = text.match(/[?&]y=(-?\d+(?:\.\d+)?)/) if (m && n) return { lon: +m[1], lat: +n[1] } const nums = (text.match(/-?\d+(?:\.\d+)?/g) || []).map(Number) if (nums.length >= 2) { const [a, b] = nums if (a >= 48 && a <= 52 && b >= 12 && b <= 19) return { lat: a, lon: b } if (b >= 48 && b <= 52 && a >= 12 && a <= 19) return { lat: b, lon: a } } return null }