From 3d2db842357cb6d802829171b5b506b07b696ac8 Mon Sep 17 00:00:00 2001 From: evlist Date: Tue, 13 Dec 2022 15:17:50 +0100 Subject: [PATCH] Converting tracks without timestamps to routes during import. --- src/db/gpx.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/db/gpx.ts b/src/db/gpx.ts index f8267b9..7e7e50e 100644 --- a/src/db/gpx.ts +++ b/src/db/gpx.ts @@ -202,6 +202,39 @@ const prune = ( // return extensions; // }; +const hasMissingTimestamps = (trk: Trk) => { + for (const trkseg of trk.trkseg!) { + for (const trkpt of trkseg.trkpt!) { + if (trkpt.time === undefined) { + return true; + } + } + } + return false; +}; + +const convertTrkToRteWhenNeeded = (gpx: Gpx) => { + if (gpx.trk === undefined) { + return; + } + const newTrks: Trk[] = []; + for (const trk of gpx.trk) { + if (hasMissingTimestamps(trk)) { + const rte = { ...trk, trkseg: undefined, rtept: [] }; + for (const trkseg of trk.trkseg!) { + rte.rtept = rte.rtept.concat(trkseg.trkpt); + } + if (gpx.rte === undefined) { + gpx.rte = []; + } + gpx.rte.push(rte); + } else { + newTrks.push(trk); + } + } + gpx.trk = newTrks; +}; + export const pruneAndSaveImportedGpx = async (params: any) => { console.log({ caller: 'pruneAndSaveImportedGpx', params }); const { id, gpx, tech } = params; @@ -216,6 +249,7 @@ export const pruneAndSaveImportedGpx = async (params: any) => { extensions: gpx.extensions, }, }; + convertTrkToRteWhenNeeded(gpx); let previousGpx: Gpx | null = null; if (id === 'new') { const currentDateTime = new Date().toISOString();