Changing Gpx and Trkpt indexes so that GPX are sorted by datetime desc and Trkpt use their timestamp as indexes.

This commit is contained in:
Eric van der Vlist 2022-12-10 18:31:51 +01:00
parent be72adce05
commit 86a8c823f1
4 changed files with 25 additions and 10 deletions

View File

@ -21,9 +21,9 @@ const GpxImport: Component = () => {
'load', 'load',
() => { () => {
// this will then display a text file // 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); const gpx = GPX.parse(fileReader.result);
console.log(`gpx: ${JSON.stringify(gpx)}`); console.log({ caller: 'GpxImport / JSON', gpx });
if (gpx) { if (gpx) {
const startTime = new Date(findStartTime(gpx)!); const startTime = new Date(findStartTime(gpx)!);
dispatch({ dispatch({

View File

@ -1,7 +1,7 @@
import { PureComponent } from 'react'; import { PureComponent } from 'react';
import { Point, Rectangle } from '../components/map/types'; import { Point, Rectangle } from '../components/map/types';
import { lat2tile, lon2tile, rectanglesIntersect } from '../lib/geo'; 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'; import { get, getDocsByType, getFamily, put, putAll } from './lib';
const emptyGpx: Gpx = { const emptyGpx: Gpx = {
@ -74,7 +74,18 @@ const prune = (id: any, object: any, docs: any[]) => {
const subObjects = object[key]; const subObjects = object[key];
for (const index in subObjects) { for (const index in subObjects) {
const subId = { ...id }; 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({ docs.push({
_id: getUri(key, subId), _id: getUri(key, subId),
type: key, type: key,
@ -177,7 +188,7 @@ export const pruneAndSaveImportedGpx = async (params: any) => {
}, },
]; ];
prune(id, gpx, docs); prune(id, gpx, docs);
console.log(JSON.stringify(docs)); console.log({ caller: 'pruneAndSaveImportedGpx / pruned', docs });
try { try {
const result = await putAll(docs); const result = await putAll(docs);
console.log(JSON.stringify(result)); console.log(JSON.stringify(result));

View File

@ -1,4 +1,4 @@
import getUri from '../lib/ids'; import getUri, { intToTrkptId } from '../lib/ids';
import { put } from './lib'; import { put } from './lib';
import { putNewTrkseg } from './trkseg'; import { putNewTrkseg } from './trkseg';
@ -35,7 +35,7 @@ export const putNewTrkpt = async (id?: IdTrk | IdGpx | IdTrkseg | IdTrkpt) => {
let finalId = { ...id }; let finalId = { ...id };
if (!('trkpt' in finalId)) { if (!('trkpt' in finalId)) {
const trksegId = await putNewTrkseg(id); const trksegId = await putNewTrkseg(id);
finalId = { ...trksegId, trkpt: 0 }; finalId = { ...trksegId, trkpt: intToTrkptId(Date.now()) };
} }
const uri = getUri('trkpt', finalId); const uri = getUri('trkpt', finalId);
await put( await put(

View File

@ -1,4 +1,4 @@
import route from './docuri'; import route from './docuri';
const integerType = (n: number) => { const integerType = (n: number) => {
return { return {
@ -21,7 +21,7 @@ const coding = {
rtept: integerType(6), rtept: integerType(6),
trk: integerType(6), trk: integerType(6),
trkseg: integerType(6), trkseg: integerType(6),
trkpt: integerType(6), trkpt: integerType(16),
}; };
const routes = { const routes = {
@ -47,5 +47,9 @@ export default uri;
const minDate = -8640000000000000; const minDate = -8640000000000000;
const halfMinDate = minDate / 2; 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;