First version of the new importer (stiil needs to convert non timed tracks into routes).
This commit is contained in:
1 parent
102a2feddc
commit
90fb0e6944
4 files changed
+194
-138
No files matched your search
+133
-88
@@ -1,7 +1,9 @@
|
||||
import { cloneDeep } from 'lodash';
|
||||
import { PureComponent } from 'react';
|
||||
import { $DEVCOMP } from 'solid-js';
|
||||
import { Point, Rectangle } from '../components/map/types';
|
||||
import { lat2tile, lon2tile, rectanglesIntersect } from '../lib/geo';
|
||||
import { findStartTime } from '../lib/gpx';
|
||||
import getUri, { intToGpxId, intToTrkptId } from '../lib/ids';
|
||||
import { get, getDocsByType, getFamily, put, putAll } from './lib';
|
||||
|
||||
@@ -61,7 +63,13 @@ export const existsGpx = async (id: IdGpx) => {
|
||||
}
|
||||
};
|
||||
|
||||
const prune = (id: any, object: any, docs: any[]) => {
|
||||
const prune = (
|
||||
id: any,
|
||||
object: any,
|
||||
previousGpx: Gpx | null,
|
||||
extensions: any,
|
||||
docs: any[]
|
||||
) => {
|
||||
if (typeof object === 'object') {
|
||||
for (const key in object) {
|
||||
if (
|
||||
@@ -74,6 +82,10 @@ const prune = (id: any, object: any, docs: any[]) => {
|
||||
) {
|
||||
const subObjects = object[key];
|
||||
let previousId = 0;
|
||||
let nbPreviousObjects = 0;
|
||||
if (previousGpx !== null && key in previousGpx) {
|
||||
nbPreviousObjects = previousGpx[key].length;
|
||||
}
|
||||
for (const index in subObjects) {
|
||||
const subId = { ...id };
|
||||
if (key === 'trkpt') {
|
||||
@@ -85,7 +97,7 @@ const prune = (id: any, object: any, docs: any[]) => {
|
||||
subId[key] = id;
|
||||
previousId = id;
|
||||
} else {
|
||||
subId[key] = index;
|
||||
subId[key] = index + nbPreviousObjects;
|
||||
}
|
||||
// console.log({
|
||||
// caller: 'prune',
|
||||
@@ -95,113 +107,146 @@ const prune = (id: any, object: any, docs: any[]) => {
|
||||
// object: object[key][index],
|
||||
// time: object[key][index].time,
|
||||
// });
|
||||
subObjects[index].extensions = {
|
||||
...extensions,
|
||||
...subObjects[index].extensions,
|
||||
};
|
||||
docs.push({
|
||||
_id: getUri(key, subId),
|
||||
type: key,
|
||||
doc: subObjects[index],
|
||||
});
|
||||
prune(subId, subObjects[index], docs);
|
||||
prune(subId, subObjects[index], previousGpx, extensions, docs);
|
||||
}
|
||||
object[key] = undefined;
|
||||
} else prune(id, object[key], docs);
|
||||
} else prune(id, object[key], previousGpx, extensions, docs);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const extensionsFromObject = (
|
||||
object: any,
|
||||
extensions = {
|
||||
viewport: { topLeft: <Point>{}, bottomRight: <Point>{} },
|
||||
bbox: {
|
||||
minLon: <number | undefined>undefined,
|
||||
minLat: <number | undefined>undefined,
|
||||
maxLon: <number | undefined>undefined,
|
||||
maxLat: <number | undefined>undefined,
|
||||
},
|
||||
}
|
||||
) => {
|
||||
if (typeof object === 'object') {
|
||||
if ('$' in object) {
|
||||
const attributes = object.$;
|
||||
if ('lat' in attributes) {
|
||||
const lat = +attributes.lat;
|
||||
if (
|
||||
extensions.bbox.minLat === undefined ||
|
||||
lat < extensions.bbox.minLat
|
||||
) {
|
||||
extensions.bbox.minLat = lat;
|
||||
}
|
||||
if (
|
||||
extensions.bbox.maxLat === undefined ||
|
||||
lat > extensions.bbox.maxLat
|
||||
) {
|
||||
extensions.bbox.maxLat = lat;
|
||||
}
|
||||
}
|
||||
if ('lon' in attributes) {
|
||||
const lon = +attributes.lon;
|
||||
if (
|
||||
extensions.bbox.minLon === undefined ||
|
||||
lon < extensions.bbox.minLon
|
||||
) {
|
||||
extensions.bbox.minLon = lon;
|
||||
}
|
||||
if (
|
||||
extensions.bbox.maxLon === undefined ||
|
||||
lon > extensions.bbox.maxLon
|
||||
) {
|
||||
extensions.bbox.maxLon = lon;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const key in object) {
|
||||
extensionsFromObject(object[key], extensions);
|
||||
}
|
||||
}
|
||||
return extensions;
|
||||
};
|
||||
// const extensionsFromObject = (
|
||||
// object: any,
|
||||
// extensions = {
|
||||
// viewport: { topLeft: <Point>{}, bottomRight: <Point>{} },
|
||||
// bbox: {
|
||||
// minLon: <number | undefined>undefined,
|
||||
// minLat: <number | undefined>undefined,
|
||||
// maxLon: <number | undefined>undefined,
|
||||
// maxLat: <number | undefined>undefined,
|
||||
// },
|
||||
// }
|
||||
// ) => {
|
||||
// if (typeof object === 'object') {
|
||||
// if ('$' in object) {
|
||||
// const attributes = object.$;
|
||||
// if ('lat' in attributes) {
|
||||
// const lat = +attributes.lat;
|
||||
// if (
|
||||
// extensions.bbox.minLat === undefined ||
|
||||
// lat < extensions.bbox.minLat
|
||||
// ) {
|
||||
// extensions.bbox.minLat = lat;
|
||||
// }
|
||||
// if (
|
||||
// extensions.bbox.maxLat === undefined ||
|
||||
// lat > extensions.bbox.maxLat
|
||||
// ) {
|
||||
// extensions.bbox.maxLat = lat;
|
||||
// }
|
||||
// }
|
||||
// if ('lon' in attributes) {
|
||||
// const lon = +attributes.lon;
|
||||
// if (
|
||||
// extensions.bbox.minLon === undefined ||
|
||||
// lon < extensions.bbox.minLon
|
||||
// ) {
|
||||
// extensions.bbox.minLon = lon;
|
||||
// }
|
||||
// if (
|
||||
// extensions.bbox.maxLon === undefined ||
|
||||
// lon > extensions.bbox.maxLon
|
||||
// ) {
|
||||
// extensions.bbox.maxLon = lon;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// for (const key in object) {
|
||||
// extensionsFromObject(object[key], extensions);
|
||||
// }
|
||||
// }
|
||||
// return extensions;
|
||||
// };
|
||||
|
||||
const extensionsFromGpx = (gpx: Gpx) => {
|
||||
const extensions = { ...gpx.extensions, ...extensionsFromObject(gpx) };
|
||||
gpx.extensions = undefined;
|
||||
if (
|
||||
extensions.bbox.maxLat !== undefined &&
|
||||
extensions.bbox.minLon !== undefined
|
||||
) {
|
||||
extensions.viewport.topLeft = {
|
||||
x: lon2tile(extensions.bbox.minLon, 0),
|
||||
y: lat2tile(extensions.bbox.maxLat, 0),
|
||||
};
|
||||
}
|
||||
if (
|
||||
extensions.bbox.minLat !== undefined &&
|
||||
extensions.bbox.maxLon !== undefined
|
||||
) {
|
||||
extensions.viewport.bottomRight = {
|
||||
x: lon2tile(extensions.bbox.maxLon, 0),
|
||||
y: lat2tile(extensions.bbox.minLat, 0),
|
||||
};
|
||||
}
|
||||
// const extensionsFromGpx = (gpx: Gpx) => {
|
||||
// const extensions = { ...gpx.extensions, ...extensionsFromObject(gpx) };
|
||||
// gpx.extensions = undefined;
|
||||
// if (
|
||||
// extensions.bbox.maxLat !== undefined &&
|
||||
// extensions.bbox.minLon !== undefined
|
||||
// ) {
|
||||
// extensions.viewport.topLeft = {
|
||||
// x: lon2tile(extensions.bbox.minLon, 0),
|
||||
// y: lat2tile(extensions.bbox.maxLat, 0),
|
||||
// };
|
||||
// }
|
||||
// if (
|
||||
// extensions.bbox.minLat !== undefined &&
|
||||
// extensions.bbox.maxLon !== undefined
|
||||
// ) {
|
||||
// extensions.viewport.bottomRight = {
|
||||
// x: lon2tile(extensions.bbox.maxLon, 0),
|
||||
// y: lat2tile(extensions.bbox.minLat, 0),
|
||||
// };
|
||||
// }
|
||||
|
||||
return extensions;
|
||||
};
|
||||
// return extensions;
|
||||
// };
|
||||
|
||||
export const pruneAndSaveImportedGpx = async (params: any) => {
|
||||
console.log({ caller: 'pruneAndSaveImportedGpx', params });
|
||||
const { id, gpx, extensions } = params;
|
||||
let docs: any[] = [
|
||||
{ _id: getUri('gpx', id), type: 'gpx', doc: gpx },
|
||||
{
|
||||
_id: getUri('extensions', id),
|
||||
type: 'extensions',
|
||||
doc: { ...extensions, ...extensionsFromGpx(gpx) },
|
||||
const { id, gpx, tech } = params;
|
||||
let gpxId: IdGpx;
|
||||
let docs: any[] = [];
|
||||
const extensions = {
|
||||
...tech,
|
||||
...gpx.extensions,
|
||||
gpx: {
|
||||
creator: gpx?.$?.creator,
|
||||
metadata: gpx.metadata,
|
||||
extensions: gpx.extensions,
|
||||
},
|
||||
];
|
||||
prune(id, gpx, docs);
|
||||
};
|
||||
let previousGpx: Gpx | null = null;
|
||||
if (id === 'new') {
|
||||
const currentDateTime = new Date().toISOString();
|
||||
const startTime = new Date(findStartTime(gpx, currentDateTime));
|
||||
if (gpx.metadata === undefined) {
|
||||
gpx.metadata = {};
|
||||
}
|
||||
if (gpx.metadata.time === undefined) {
|
||||
gpx.metadata.time = startTime.toISOString();
|
||||
}
|
||||
gpxId = { gpx: intToGpxId(startTime.valueOf()) };
|
||||
docs = [
|
||||
{ _id: getUri('gpx', gpxId), type: 'gpx', doc: gpx },
|
||||
{
|
||||
_id: getUri('extensions', gpxId),
|
||||
type: 'extensions',
|
||||
doc: {},
|
||||
},
|
||||
];
|
||||
} else {
|
||||
gpxId = getUri('gpx', id);
|
||||
previousGpx = (await getGpx(id)) ?? null;
|
||||
}
|
||||
prune(gpxId, gpx, previousGpx, extensions, docs);
|
||||
console.log({ caller: 'pruneAndSaveImportedGpx / pruned', docs });
|
||||
try {
|
||||
const result = await putAll(docs);
|
||||
console.log(JSON.stringify(result));
|
||||
if (id !== 'new') {
|
||||
put(id, 'gpx', (doc) => doc, {});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`error: ${err}`);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user