Extract start/end from track when GPX has no waypoints

The planner's GPX import only used <wpt> elements, which many GPX
files don't have. Now falls back to the first and last track point,
giving the planner two endpoints to route between.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-04-02 20:40:11 +01:00
parent 5f3d4ae846
commit 79942bb99e
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9

View file

@ -36,7 +36,31 @@ export async function loader({ request }: Route.LoaderArgs) {
try {
const gpx = decodeURIComponent(gpxEncoded);
const gpxData = await parseGpxAsync(gpx);
initializeSessionWithWaypoints(session.id, gpxData.waypoints);
// Use explicit waypoints if present, otherwise extract from track segments
let waypoints = gpxData.waypoints;
if (waypoints.length === 0 && gpxData.tracks.length > 0) {
const extracted: Array<{ lat: number; lon: number }> = [];
for (const seg of gpxData.tracks) {
if (seg.length === 0) continue;
const first = seg[0]!;
// Deduplicate: skip if same as previous waypoint
const prev = extracted[extracted.length - 1];
if (!prev || prev.lat !== first.lat || prev.lon !== first.lon) {
extracted.push({ lat: first.lat, lon: first.lon });
}
}
// Add the end of the last segment
const lastSeg = gpxData.tracks[gpxData.tracks.length - 1]!;
if (lastSeg.length > 0) {
const last = lastSeg[lastSeg.length - 1]!;
const prev = extracted[extracted.length - 1];
if (!prev || prev.lat !== last.lat || prev.lon !== last.lon) {
extracted.push({ lat: last.lat, lon: last.lon });
}
}
waypoints = extracted;
}
initializeSessionWithWaypoints(session.id, waypoints);
} catch {
// Continue with empty session if GPX is invalid
}