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
+9
View File
@@ -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 },
+6 -7
View File
@@ -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,
}
}