refactor(geo): define shared distance contract

This commit is contained in:
onionviolet committed 2026-09-16 08:48:31 -04:00
1 parent a65d9d85f1
commit 9c638fa290
22 files changed
+267 -195

No files matched your search

+3 -16
View File
@@ -1,6 +1,7 @@
import { createOverpassFeatureSource } from './sources/overpassFeatures.js';
import { applicationServices } from './services/application.js';
import * as Cesium from 'cesium';
import { greatCircleKm } from './geoDistance.js';
import {
viewportBias,
placesNearViewRecovery,
@@ -1103,18 +1104,6 @@ export const PLACE_ANCHOR_OFFSET_RATIO = 0.15;
*/
const PLACE_FALLBACK_HALF_SPAN_KM = 20;
/** Great-circle distance in km (small enough here that the spherical model is fine). */
function greatCircleKm(lat1, lng1, lat2, lng2) {
const dLat = Cesium.Math.toRadians(lat2 - lat1);
const dLng = Cesium.Math.toRadians(lng2 - lng1);
const a =
Math.sin(dLat / 2) ** 2 +
Math.cos(Cesium.Math.toRadians(lat1)) *
Math.cos(Cesium.Math.toRadians(lat2)) *
Math.sin(dLng / 2) ** 2;
return 6371 * 2 * Math.asin(Math.min(1, Math.sqrt(a)));
}
/**
* Sanity-gate a geocode viewport before framing it. Pure — exported for unit tests.
*
@@ -1149,10 +1138,8 @@ export function placeFramingViewport(
return viewport;
const offsetKm = greatCircleKm(
anchorLat,
anchorLng,
metrics.centerLat,
metrics.centerLng,
{ lat: anchorLat, lon: anchorLng },
{ lat: metrics.centerLat, lon: metrics.centerLng },
);
if (offsetKm <= metrics.spanKm * PLACE_ANCHOR_OFFSET_RATIO) return viewport;