fix(tours): elevation model jitter no longer counts as ascent

Ascent and descent were the raw sum of every height difference, so a flat walk across Venice (highest point 16 m) read 121 m of climbing. Heights now count once they move 5 m from the last one that counted; the server's stored figures and the planner's live analysis share the one function.
This commit is contained in:
Maurice
2026-10-25 10:22:03 +01:00
parent e3eec8d6d1
commit 26611b46e4
4 changed files with 68 additions and 19 deletions
+19 -1
View File
@@ -1,5 +1,5 @@
import { haversineMetres } from '../geo/haversine';
import { computeTourMetrics } from './tours.metrics';
import { computeTourMetrics, elevationGainLoss } from './tours.metrics';
import { describe, expect, it } from 'vitest';
@@ -35,6 +35,24 @@ describe('computeTourMetrics', () => {
});
});
describe('elevationGainLoss', () => {
it('leaves out jitter that turns back before it reaches the noise floor', () => {
expect(elevationGainLoss([2, 6, 1, 5, 0, 4, 2])).toEqual({ gain: 0, loss: 0 });
});
it('counts a climb made of small steps in full once it passes the floor', () => {
expect(elevationGainLoss([100, 102, 104, 106, 108, 110, 112, 105])).toEqual({ gain: 12, loss: 7 });
});
it('keeps large moves exactly as they are', () => {
expect(elevationGainLoss([1000, 1100, 1050])).toEqual({ gain: 100, loss: 50 });
});
it('reads nothing into an empty run', () => {
expect(elevationGainLoss([])).toEqual({ gain: 0, loss: 0 });
});
});
describe('haversineMetres', () => {
it('stays finite for antipodal points', () => {
expect(haversineMetres(0, 0, 0, 180)).toBeCloseTo(Math.PI * 6371000, 0);
+34 -11
View File
@@ -6,14 +6,43 @@ export type GeometryPoint = [number, number] | [number, number, number];
export interface TourMetrics {
/** Sum of segment haversine distances, in km. */
distanceKm: number;
/** Sum of positive elevation deltas, in metres. Null when the track carries no elevation. */
/** Ascent in metres, jitter under ELEVATION_NOISE_M left out. Null when the track carries no elevation. */
elevationGainM: number | null;
/** Sum of negative elevation deltas (as a positive number), in metres. Null when no elevation. */
/** Descent in metres, counted the same way. Null when no elevation. */
elevationLossM: number | null;
/** Whether every point in the geometry carried a 3rd (elevation) value. */
hasElevation: boolean;
}
/**
* How far, in metres, a height has to move from the last one that counted before
* it counts as climbing or descending. Elevation models jitter a few metres from
* one point to the next, and summed as it came, that jitter was the whole ascent
* of a flat city walk: 121 m across Venice, whose highest point is 16 m. Measured
* this way a real climb still adds up in full; only moves smaller than this that
* turn back are left out.
*/
const ELEVATION_NOISE_M = 5;
/**
* Ascent and descent over a run of heights, in metres, with the noise above
* filtered out. The server's stored tour figures and the client's live analysis
* both count with this, so a tour reads the same in the list and in its detail.
*/
export function elevationGainLoss(heights: readonly number[]): { gain: number; loss: number } {
let gain = 0;
let loss = 0;
let ref = heights[0];
for (const height of heights) {
const delta = height - ref;
if (delta >= ELEVATION_NOISE_M) gain += delta;
else if (delta <= -ELEVATION_NOISE_M) loss -= delta;
else continue;
ref = height;
}
return { gain, loss };
}
/**
* Derives distance/elevation from a parsed `route_geometry` array. Never throws:
* a track with fewer than 2 points, or with missing/mixed elevation, still
@@ -30,23 +59,17 @@ export function computeTourMetrics(points: readonly GeometryPoint[]): TourMetric
const hasElevation = points.every((p) => p.length === 3 && Number.isFinite(p[2]));
let distanceKm = 0;
let gain = 0;
let loss = 0;
let prev = first;
for (const point of rest) {
distanceKm += haversineMetres(prev[0], prev[1], point[0], point[1]) / 1000;
if (hasElevation) {
const delta = (point[2] as number) - (prev[2] as number);
if (delta > 0) gain += delta;
else loss += -delta;
}
prev = point;
}
const elevation = hasElevation ? elevationGainLoss(points.map((point) => point[2] as number)) : null;
return {
distanceKm,
elevationGainM: hasElevation ? gain : null,
elevationLossM: hasElevation ? loss : null,
elevationGainM: elevation ? elevation.gain : null,
elevationLossM: elevation ? elevation.loss : null,
hasElevation,
};
}