Fix distance calculation for GPX files without elevation data

Distance was only stored in the elevation profile array, which was
empty when track points had no <ele> elements. Now computed via
haversine independently and exposed as gpxData.distance.

Tested: berlin-dresden-radweg-2021.gpx (5660 points, no elevation)
now correctly reports 248.8 km.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-04-02 20:19:55 +01:00
parent ed2d04c248
commit d5bcecb345
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9
4 changed files with 8 additions and 11 deletions

View file

@ -23,8 +23,7 @@ export async function createActivity(ownerId: string, input: ActivityInput) {
if (input.gpx) {
try {
const gpxData = await parseGpxAsync(input.gpx);
const profile = gpxData.elevation.profile;
distance = profile.length > 0 ? Math.round(profile[profile.length - 1]!.distance) : null;
distance = gpxData.distance || null;
elevationGain = gpxData.elevation.gain;
elevationLoss = gpxData.elevation.loss;

View file

@ -145,11 +145,7 @@ async function computeRouteStats(gpxString: string) {
try {
const gpxData = await parseGpxAsync(gpxString);
return {
distance: Math.round(
gpxData.elevation.profile.length > 0
? gpxData.elevation.profile[gpxData.elevation.profile.length - 1]!.distance
: 0,
),
distance: gpxData.distance,
elevationGain: gpxData.elevation.gain,
elevationLoss: gpxData.elevation.loss,
};