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:
parent
ed2d04c248
commit
d5bcecb345
4 changed files with 8 additions and 11 deletions
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -46,9 +46,9 @@ function parseGpxWithParser(parser: DOMParser, xml: string): GpxData {
|
|||
const name = doc.querySelector("metadata > name")?.textContent ?? undefined;
|
||||
const waypoints = parseWaypoints(doc);
|
||||
const tracks = parseTracks(doc);
|
||||
const elevation = computeElevation(tracks);
|
||||
const { totalDistance, ...elevation } = computeElevation(tracks);
|
||||
|
||||
return { name, waypoints, tracks, elevation };
|
||||
return { name, waypoints, tracks, distance: totalDistance, elevation };
|
||||
}
|
||||
|
||||
function parseWaypoints(doc: Document): Waypoint[] {
|
||||
|
|
@ -85,7 +85,7 @@ function parseTracks(doc: Document): TrackPoint[][] {
|
|||
return tracks;
|
||||
}
|
||||
|
||||
function computeElevation(tracks: TrackPoint[][]): GpxData["elevation"] {
|
||||
function computeElevation(tracks: TrackPoint[][]): GpxData["elevation"] & { totalDistance: number } {
|
||||
let gain = 0;
|
||||
let loss = 0;
|
||||
const profile: ElevationProfile[] = [];
|
||||
|
|
@ -112,7 +112,7 @@ function computeElevation(tracks: TrackPoint[][]): GpxData["elevation"] {
|
|||
}
|
||||
}
|
||||
|
||||
return { gain: Math.round(gain), loss: Math.round(loss), profile };
|
||||
return { totalDistance: Math.round(totalDistance), gain: Math.round(gain), loss: Math.round(loss), profile };
|
||||
}
|
||||
|
||||
/** Haversine distance between two points in meters */
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ export interface GpxData {
|
|||
name?: string;
|
||||
waypoints: Waypoint[];
|
||||
tracks: TrackPoint[][];
|
||||
/** Total distance in meters (haversine, works with or without elevation data) */
|
||||
distance: number;
|
||||
elevation: {
|
||||
gain: number;
|
||||
loss: number;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue