building-use-map/src/geo.ts
HangerThem 4f9c11039b refactor: split into TypeScript modules built with Vite
- Move the single-file app into src/ as typed ES modules
- Vite for dev server and minified production build (bun run dev / build)
- oxfmt/oxlint config; rename useName -> ruianUseName (hooks lint false positive)
- Fix horizontal overflow in the sidebar (range input default margin)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 11:32:26 +01:00

25 lines
905 B
TypeScript

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
}