From 86a8c823f17cafcf9ad5a7718621bfce1f1270d1 Mon Sep 17 00:00:00 2001 From: evlist Date: Sat, 10 Dec 2022 18:31:51 +0100 Subject: [PATCH] Changing Gpx and Trkpt indexes so that GPX are sorted by datetime desc and Trkpt use their timestamp as indexes. --- src/components/gpx-import/GpxImport.tsx | 4 ++-- src/db/gpx.ts | 17 ++++++++++++++--- src/db/trkpt.ts | 4 ++-- src/lib/ids.ts | 10 +++++++--- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/components/gpx-import/GpxImport.tsx b/src/components/gpx-import/GpxImport.tsx index 5876a31..c95df42 100644 --- a/src/components/gpx-import/GpxImport.tsx +++ b/src/components/gpx-import/GpxImport.tsx @@ -21,9 +21,9 @@ const GpxImport: Component = () => { 'load', () => { // this will then display a text file - console.log(fileReader.result); + console.log({ caller: 'GpxImport / XML', result: fileReader.result }); const gpx = GPX.parse(fileReader.result); - console.log(`gpx: ${JSON.stringify(gpx)}`); + console.log({ caller: 'GpxImport / JSON', gpx }); if (gpx) { const startTime = new Date(findStartTime(gpx)!); dispatch({ diff --git a/src/db/gpx.ts b/src/db/gpx.ts index b4e649c..8bcbab3 100644 --- a/src/db/gpx.ts +++ b/src/db/gpx.ts @@ -1,7 +1,7 @@ import { PureComponent } from 'react'; import { Point, Rectangle } from '../components/map/types'; import { lat2tile, lon2tile, rectanglesIntersect } from '../lib/geo'; -import getUri, { intToGpxId } from '../lib/ids'; +import getUri, { intToGpxId, intToTrkptId } from '../lib/ids'; import { get, getDocsByType, getFamily, put, putAll } from './lib'; const emptyGpx: Gpx = { @@ -74,7 +74,18 @@ const prune = (id: any, object: any, docs: any[]) => { const subObjects = object[key]; for (const index in subObjects) { const subId = { ...id }; - subId[key] = index; + subId[key] = + key === 'trkpt' + ? intToTrkptId(new Date(object[key][index].time).valueOf()) + : index; + // console.log({ + // caller: 'prune', + // id, + // subId, + // key, + // object: object[key][index], + // time: object[key][index].time, + // }); docs.push({ _id: getUri(key, subId), type: key, @@ -177,7 +188,7 @@ export const pruneAndSaveImportedGpx = async (params: any) => { }, ]; prune(id, gpx, docs); - console.log(JSON.stringify(docs)); + console.log({ caller: 'pruneAndSaveImportedGpx / pruned', docs }); try { const result = await putAll(docs); console.log(JSON.stringify(result)); diff --git a/src/db/trkpt.ts b/src/db/trkpt.ts index 484be6a..e1babee 100644 --- a/src/db/trkpt.ts +++ b/src/db/trkpt.ts @@ -1,4 +1,4 @@ -import getUri from '../lib/ids'; +import getUri, { intToTrkptId } from '../lib/ids'; import { put } from './lib'; import { putNewTrkseg } from './trkseg'; @@ -35,7 +35,7 @@ export const putNewTrkpt = async (id?: IdTrk | IdGpx | IdTrkseg | IdTrkpt) => { let finalId = { ...id }; if (!('trkpt' in finalId)) { const trksegId = await putNewTrkseg(id); - finalId = { ...trksegId, trkpt: 0 }; + finalId = { ...trksegId, trkpt: intToTrkptId(Date.now()) }; } const uri = getUri('trkpt', finalId); await put( diff --git a/src/lib/ids.ts b/src/lib/ids.ts index 4db603f..1849d8d 100644 --- a/src/lib/ids.ts +++ b/src/lib/ids.ts @@ -1,4 +1,4 @@ -import route from './docuri'; +import route from './docuri'; const integerType = (n: number) => { return { @@ -21,7 +21,7 @@ const coding = { rtept: integerType(6), trk: integerType(6), trkseg: integerType(6), - trkpt: integerType(6), + trkpt: integerType(16), }; const routes = { @@ -47,5 +47,9 @@ export default uri; const minDate = -8640000000000000; const halfMinDate = minDate / 2; +const maxDate = 8640000000000000; +const halfMaxDate = maxDate / 2; -export const intToGpxId = (i: number) => Math.round(i / 2) - halfMinDate; +export const intToGpxId = (i: number) => halfMaxDate - Math.round(i / 2); + +export const intToTrkptId = (i: number) => Math.round(i / 2) - halfMinDate;