fix(map): a distance between near antipodal points is half the globe instead of NaN

haversineKm in src/utils/geo.ts answered NaN for some near antipodal pairs,
for example 47.3812, -10.8827 and -47.3812001, 169.1173. Rounding pushed the
haversine term a hair past 1, and asin of a value above 1 is NaN, which then
spread into route sums and range checks.

Cause: unlike the Studio legKm and the roadtrip haversineKm in
@trek/shared, the shared client helper had no clamp before Math.asin.

Fix: clamp with Math.min(1, ...) before Math.asin. With the clamp the helper
runs exactly the operations of Studio legKm in the same order (2 * 6371 is
12742), so Studio now uses haversineKm and legKm goes, with its cases carried
into the new geo tests. The roadtrip variant multiplies its terms in another
order and gives different last bits, so it stays as it is.

Red test: src/utils/geo.test.ts FE-UTIL-GEO-002 failed on the unfixed code
with "AssertionError: expected NaN to be close to 20015.086796020572,
received difference is NaN, but expected 0.0005".

Removed tests: src/components/Studio/legKm.test.ts went with legKm. Its four
cases (zero for one point, a degree of latitude about 111.19 km, the same
both ways between 8700 and 8900 km, 0,0 to 0,180 equal to 12742 * PI / 2) now
run against haversineKm as FE-UTIL-GEO-001, 003 and 004.
This commit is contained in:
Maurice
2026-10-09 17:52:35 +02:00
parent 7659f2741e
commit 0246e5c317
6 changed files with 42 additions and 43 deletions
@@ -8,7 +8,7 @@ import {
type LucideIcon,
} from 'lucide-react'
import { fontStack } from './bookFonts'
import { legKm } from './legKm'
import { haversineKm } from '../../utils/geo'
import { brightness } from './folioColour'
import { COUNTRY_SHAPES, countryParts, countryWorldPath, projectMercator, unprojectMercator } from './countryShapes'
import { projectOntoTiles, tileView, usableStaticUrl } from './mapTiles'
@@ -380,7 +380,7 @@ function MapView({ el, frameStyle, big = false }: {
const d = Math.hypot(dx, dy)
if (d < 1e-6) return { k: 0, nx: 0, ny: 0 }
const u = Math.min(1, Math.max(0, Math.log(legKm(a, b) / 150) / Math.log(10)))
const u = Math.min(1, Math.max(0, Math.log(haversineKm(a, b) / 150) / Math.log(10)))
const ramp = u * u * (3 - 2 * u)
// How much of the leg runs east-west, which is where a bow is truthful.
const eastWest = 0.35 + 0.65 * Math.abs(dx / d)
@@ -1,24 +0,0 @@
import { describe, expect, it } from 'vitest';
import { legKm } from './legKm';
describe('legKm', () => {
it('is zero between a point and itself', () => {
expect(legKm({ lat: 48.1, lng: 11.6 }, { lat: 48.1, lng: 11.6 })).toBe(0);
});
it('measures a degree of latitude as about 111 km', () => {
expect(legKm({ lat: 0, lng: 0 }, { lat: 1, lng: 0 })).toBeCloseTo(111.19, 1);
});
it('is the same both ways', () => {
const a = { lat: 35.68, lng: 139.69 };
const b = { lat: 34.05, lng: -118.24 };
expect(legKm(a, b)).toBeCloseTo(legKm(b, a), 9);
expect(legKm(a, b)).toBeGreaterThan(8700);
expect(legKm(a, b)).toBeLessThan(8900);
});
it('stays defined for antipodal points', () => {
expect(legKm({ lat: 0, lng: 0 }, { lat: 0, lng: 180 })).toBeCloseTo((12742 * Math.PI) / 2, 3);
});
});
-14
View File
@@ -1,14 +0,0 @@
/**
* Kilometres between two coordinates, along the great circle.
*
* Shared by the map element, which ramps how far a leg bows by it, and by the
* road fetch, which refuses to route a leg past its ceiling. The clamp keeps
* `asin` defined when rounding pushes two near antipodal points a hair past 1.
*/
export function legKm(a: { lat: number; lng: number }, b: { lat: number; lng: number }): number {
const r = (d: number) => (d * Math.PI) / 180;
const dLat = r(b.lat - a.lat);
const dLng = r(b.lng - a.lng);
const h = Math.sin(dLat / 2) ** 2 + Math.cos(r(a.lat)) * Math.cos(r(b.lat)) * Math.sin(dLng / 2) ** 2;
return 12742 * Math.asin(Math.min(1, Math.sqrt(h)));
}
+2 -2
View File
@@ -1,5 +1,5 @@
import { calculateRouteWithLegs } from '../Map/RouteCalculator'
import { legKm } from './legKm'
import { haversineKm } from '../../utils/geo'
/**
* The roads between a book's stops.
@@ -99,7 +99,7 @@ export async function fetchRoads(
const a = stops[i]
const b = stops[i + 1]
if (legKm(a, b) > ROAD_CEILING_KM) {
if (haversineKm(a, b) > ROAD_CEILING_KM) {
opts.onProgress?.(i + 1, legs)
continue
}
+35
View File
@@ -0,0 +1,35 @@
import { describe, expect, it } from 'vitest';
import { haversineKm, withinDayTripRange } from './geo';
// FE-UTIL-GEO-001 to FE-UTIL-GEO-005
describe('haversineKm', () => {
it('FE-UTIL-GEO-001: measures a degree of latitude and nothing between a point and itself', () => {
expect(haversineKm({ lat: 48.1, lng: 11.6 }, { lat: 48.1, lng: 11.6 })).toBe(0);
expect(haversineKm({ lat: 0, lng: 0 }, { lat: 1, lng: 0 })).toBeCloseTo(111.19, 1);
});
it('FE-UTIL-GEO-002: a near antipodal pair is half the globe, not NaN', () => {
// Rounding pushes the haversine term a hair past 1 for this pair, where asin is undefined.
const km = haversineKm({ lat: 47.3812, lng: -10.8827 }, { lat: -47.3812001, lng: 169.1173 });
expect(km).toBeCloseTo(6371 * Math.PI, 3);
});
it('FE-UTIL-GEO-003: is the same both ways', () => {
const a = { lat: 35.68, lng: 139.69 };
const b = { lat: 34.05, lng: -118.24 };
expect(haversineKm(a, b)).toBeCloseTo(haversineKm(b, a), 9);
expect(haversineKm(a, b)).toBeGreaterThan(8700);
expect(haversineKm(a, b)).toBeLessThan(8900);
});
it('FE-UTIL-GEO-004: an exact antipodal pair is half the globe', () => {
expect(haversineKm({ lat: 0, lng: 0 }, { lat: 0, lng: 180 })).toBeCloseTo(6371 * Math.PI, 3);
});
it('FE-UTIL-GEO-005: a stop counts as a day trip up to 150 km away', () => {
expect(withinDayTripRange({ lat: 0, lng: 0 }, { lat: 1, lng: 0 })).toBe(true);
expect(withinDayTripRange({ lat: 0, lng: 0 }, { lat: 2, lng: 0 })).toBe(false);
});
});
+3 -1
View File
@@ -4,6 +4,8 @@
* Lifted out of useTransportRoutes so the day-route builder can share the one
* implementation instead of adding a fourth copy. The map overlays keep their own
* `[lat, lng]`-tuple variants — different call shape, and they only sum arc lengths.
* The clamp keeps `asin` defined when rounding pushes two near antipodal points a hair
* past 1, which would otherwise answer NaN.
*/
export function haversineKm(a: { lat: number; lng: number }, b: { lat: number; lng: number }): number {
const R = 6371
@@ -12,7 +14,7 @@ export function haversineKm(a: { lat: number; lng: number }, b: { lat: number; l
const la1 = a.lat * Math.PI / 180
const la2 = b.lat * Math.PI / 180
const h = Math.sin(dLat / 2) ** 2 + Math.cos(la1) * Math.cos(la2) * Math.sin(dLng / 2) ** 2
return 2 * R * Math.asin(Math.sqrt(h))
return 2 * R * Math.asin(Math.min(1, Math.sqrt(h)))
}
/**