From 26611b46e4ca96b2bc502a8a25fe9ce776297ba7 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sun, 25 Oct 2026 10:22:03 +0100 Subject: [PATCH] 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. --- client/src/utils/routeGeometry.test.ts | 9 ++++++ client/src/utils/routeGeometry.ts | 13 ++++---- shared/src/tours/tours.metrics.test.ts | 20 +++++++++++- shared/src/tours/tours.metrics.ts | 45 +++++++++++++++++++------- 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/client/src/utils/routeGeometry.test.ts b/client/src/utils/routeGeometry.test.ts index a2346a5e0..e7ab19c26 100644 --- a/client/src/utils/routeGeometry.test.ts +++ b/client/src/utils/routeGeometry.test.ts @@ -27,6 +27,15 @@ describe('analyzeRouteGeometry', () => { expect(analysis!.distanceIndexedProfileSamples[2].distanceMeters).toBeCloseTo(3335.85, 1) }) + it('leaves the elevation model jitter of a flat walk out of its ascent', () => { + const analysis = analyzeRouteGeometry(JSON.stringify([ + [45.44, 12.32, 2], [45.441, 12.32, 9], [45.442, 12.32, 1], [45.443, 12.32, 4], [45.444, 12.32, 3], + ])) + expect(analysis!.maxEle).toBe(9) + expect(analysis!.gain).toBe(7) + expect(analysis!.loss).toBe(8) + }) + it('maps route endpoints and interpolates focus between prepared samples', () => { const samples = [ { distanceMeters: 0, elevationMeters: 100, lat: 10, lng: 20 }, diff --git a/client/src/utils/routeGeometry.ts b/client/src/utils/routeGeometry.ts index 13c8066e8..fcc911383 100644 --- a/client/src/utils/routeGeometry.ts +++ b/client/src/utils/routeGeometry.ts @@ -1,3 +1,5 @@ +import { elevationGainLoss } from '@trek/shared' + export interface DistanceIndexedProfileSample { distanceMeters: number elevationMeters: number @@ -109,8 +111,6 @@ export function analyzeRouteGeometry(routeGeometry: string | null | undefined): let distanceMeters = 0 let minEle = hasElevation ? points[0][2]! : null let maxEle = minEle - let gain = hasElevation ? 0 : null - let loss = hasElevation ? 0 : null const distanceIndexedProfileSamples: DistanceIndexedProfileSample[] = hasElevation ? [{ distanceMeters: 0, elevationMeters: points[0][2]!, lat: points[0][0], lng: points[0][1] }] : [] @@ -122,19 +122,18 @@ export function analyzeRouteGeometry(routeGeometry: string | null | undefined): const elevation = points[index][2]! minEle = Math.min(minEle!, elevation) maxEle = Math.max(maxEle!, elevation) - const delta = elevation - points[index - 1][2]! - if (delta > 0) gain! += delta - else loss! += Math.abs(delta) distanceIndexedProfileSamples.push({ distanceMeters, elevationMeters: elevation, lat: points[index][0], lng: points[index][1] }) } + // Ascent and descent with the elevation model's jitter left out, as the server counts them. + const climb = hasElevation ? elevationGainLoss(points.map(point => point[2]!)) : null return { routeCoordinates: points.map(([lat, lng]) => [lat, lng]), distanceKm: distanceMeters / 1000, minEle, maxEle, - gain, - loss, + gain: climb ? climb.gain : null, + loss: climb ? climb.loss : null, distanceIndexedProfileSamples, } } diff --git a/shared/src/tours/tours.metrics.test.ts b/shared/src/tours/tours.metrics.test.ts index 6d1410f83..f6b725b7c 100644 --- a/shared/src/tours/tours.metrics.test.ts +++ b/shared/src/tours/tours.metrics.test.ts @@ -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); diff --git a/shared/src/tours/tours.metrics.ts b/shared/src/tours/tours.metrics.ts index 027a6f4a6..89fbbdb4b 100644 --- a/shared/src/tours/tours.metrics.ts +++ b/shared/src/tours/tours.metrics.ts @@ -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, }; }