fix: keep antimeridian traffic requests in range

This commit is contained in:
Sameh Khamis committed 2026-09-20 17:11:27 -07:00
1 parent 2f16e34cad
commit 62abcfdd16
4 files changed
+68 -34

No files matched your search

+13 -25
View File
@@ -109,30 +109,18 @@ test('span clamp is idempotent on already-clamped bounds (loadRoadsForBounds re-
assert.deepEqual(twice, once);
});
test('span clamp handles bounds crossing the antimeridian', () => {
const bounds = {
south: -0.02,
north: 0.02,
west: 179.98,
east: -179.98,
};
test('span clamp keeps antimeridian bounds monotonic and request-safe', () => {
for (const centerLon of [179.99, -179.99]) {
const result = clampBoundsAroundCenter({
south: -0.02,
north: 0.02,
west: 179.98,
east: -179.98,
}, { lat: 0, lon: centerLon }, 0.05);
const center = {
lat: 0,
lon: 179.99,
};
const result = clampBoundsAroundCenter(bounds, center, 0.05);
// west/east are deliberately left unnormalized (may exceed 180) so that
// west <= east always holds for downstream consumers (getBoundsCenter,
// boundsOverlap) that assume a monotonic box. See #392 review.
assert.ok(result.west <= result.east, `west/east not monotonic: ${result.west}, ${result.east}`);
assert.ok(Math.abs(result.east - result.west - 0.04) < 1e-12);
// The plain-mean center a caller like getBoundsCenter computes must land
// back on the antimeridian near the requested center, not ~180 degrees
// away on the opposite side of the planet.
const center2 = (result.west + result.east) / 2;
assert.ok(Math.abs(center2 - center.lon) < 1e-9, `center drifted: ${center2}`);
assert.ok(result.west >= -180 && result.east <= 180);
assert.ok(result.west <= result.east, `west/east not monotonic: ${result.west}, ${result.east}`);
assert.ok(Math.abs(result.east - result.west - 0.04) < 1e-12);
assert.ok(result.west <= centerLon && centerLon <= result.east);
}
});