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

+7 -7
View File
@@ -1,8 +1,8 @@
// src/data/trafficBounds.test.mjs
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { greatCircleKm } from '../geoDistance.js';
import {
greatCircleKm,
deriveFetchCenter,
clampBoundsAroundCenter,
} from './trafficBounds.js';
@@ -11,7 +11,7 @@ import {
const NADIR = { lat: 30.2672, lon: -97.7431 };
test('greatCircleKm sanity: Austin -> ~5 km north', () => {
const d = greatCircleKm(NADIR.lat, NADIR.lon, NADIR.lat + 0.045, NADIR.lon);
const d = greatCircleKm(NADIR, { lat: NADIR.lat + 0.045, lon: NADIR.lon });
assert.ok(Math.abs(d - 5.0) < 0.1, `got ${d}`);
});
@@ -41,14 +41,14 @@ test('oblique look within 12 km: uses the hit point verbatim', () => {
test('horizon gaze: far hit is pulled back to 12 km along the bearing', () => {
// Hit ~100 km due east of nadir.
const hit = { lat: NADIR.lat, lon: NADIR.lon + 1.041 };
assert.ok(greatCircleKm(NADIR.lat, NADIR.lon, hit.lat, hit.lon) > 90, 'precondition: far hit');
assert.ok(greatCircleKm(NADIR, hit) > 90, 'precondition: far hit');
const c = deriveFetchCenter({
nadirLat: NADIR.lat, nadirLon: NADIR.lon,
hitLat: hit.lat, hitLon: hit.lon,
maxPullKm: 12,
});
assert.equal(c.source, 'pulled');
const d = greatCircleKm(NADIR.lat, NADIR.lon, c.lat, c.lon);
const d = greatCircleKm(NADIR, c);
assert.ok(Math.abs(d - 12) < 0.05, `pulled distance ${d} km, expected ~12`);
// Due-east bearing: latitude stays ~constant, longitude moves east but well
// short of the hit.
@@ -64,12 +64,12 @@ test('pull cap is honored for other maxPullKm values', () => {
maxPullKm: 5,
});
assert.equal(c.source, 'pulled');
const d = greatCircleKm(NADIR.lat, NADIR.lon, c.lat, c.lon);
const d = greatCircleKm(NADIR, c);
assert.ok(Math.abs(d - 5) < 0.05, `pulled distance ${d} km, expected ~5`);
});
test('pickEllipsoid failure (non-finite hit): falls back to the camera nadir', () => {
for (const [hitLat, hitLon] of [[NaN, NaN], [undefined, undefined], [30.3, undefined]]) {
test('invalid ground hit falls back to the camera nadir', () => {
for (const [hitLat, hitLon] of [[NaN, NaN], [undefined, undefined], [30.3, undefined], [91, -97], [30, 181]]) {
const c = deriveFetchCenter({
nadirLat: NADIR.lat, nadirLon: NADIR.lon,
hitLat, hitLon,