Displaying GPX tracks (1st attempt)

This commit is contained in:
vdv committed 2022-11-17 12:55:00 +01:00
1 parent d86d63ca61
commit ef3759079c
11 files changed
+347 -39

No files matched your search

+114 -34
View File
@@ -2,7 +2,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 { get, getDocsByType, put, putAll } from './lib';
import { get, getDocsByType, getFamily, put, putAll } from './lib';
const emptyGpx: Gpx = {
$: {
@@ -88,9 +88,9 @@ const prune = (id: any, object: any, docs: any[]) => {
}
};
const techFromObject = (
const extensionsFromObject = (
object: any,
tech = {
extensions = {
viewport: { topLeft: <Point>{}, bottomRight: <Point>{} },
bbox: {
minLon: <number | undefined>undefined,
@@ -105,56 +105,75 @@ const techFromObject = (
const attributes = object.$;
if ('lat' in attributes) {
const lat = +attributes.lat;
if (tech.bbox.minLat === undefined || lat < tech.bbox.minLat) {
tech.bbox.minLat = lat;
if (
extensions.bbox.minLat === undefined ||
lat < extensions.bbox.minLat
) {
extensions.bbox.minLat = lat;
}
if (tech.bbox.maxLat === undefined || lat > tech.bbox.maxLat) {
tech.bbox.maxLat = lat;
if (
extensions.bbox.maxLat === undefined ||
lat > extensions.bbox.maxLat
) {
extensions.bbox.maxLat = lat;
}
}
if ('lon' in attributes) {
const lon = +attributes.lon;
if (tech.bbox.minLon === undefined || lon < tech.bbox.minLon) {
tech.bbox.minLon = lon;
if (
extensions.bbox.minLon === undefined ||
lon < extensions.bbox.minLon
) {
extensions.bbox.minLon = lon;
}
if (tech.bbox.maxLon === undefined || lon > tech.bbox.minLon) {
tech.bbox.maxLon = lon;
if (
extensions.bbox.maxLon === undefined ||
lon > extensions.bbox.minLon
) {
extensions.bbox.maxLon = lon;
}
}
}
for (const key in object) {
techFromObject(object[key], tech);
extensionsFromObject(object[key], extensions);
}
}
return tech;
return extensions;
};
const techFromGpx = (gpx: Gpx) => {
const tech = techFromObject(gpx);
if (tech.bbox.maxLat !== undefined && tech.bbox.minLon !== undefined) {
tech.viewport.topLeft = {
x: lon2tile(tech.bbox.minLon, 0),
y: lat2tile(tech.bbox.maxLat, 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 (tech.bbox.minLat !== undefined && tech.bbox.maxLon !== undefined) {
tech.viewport.bottomRight = {
x: lon2tile(tech.bbox.maxLon, 0),
y: lat2tile(tech.bbox.minLat, 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 tech;
return extensions;
};
export const pruneAndSaveImportedGpx = async (params: any) => {
const { id, gpx, tech } = params;
const { id, gpx, extensions } = params;
let docs: any[] = [
{ _id: getUri('gpx', id), type: 'gpx', doc: gpx },
{
_id: getUri('tech', id),
type: 'tech',
doc: { ...tech, ...techFromGpx(gpx) },
_id: getUri('extensions', id),
type: 'extensions',
doc: { ...extensions, ...extensionsFromGpx(gpx) },
},
];
prune(id, gpx, docs);
@@ -179,11 +198,72 @@ export const getGpxesForViewport = async (params: any) => {
y: (viewport.bottomRight.y + 1) / 2 ** zoomLevel,
},
};
const techs = await getDocsByType('tech');
console.log(`getGpxesForViewport, techs: ${JSON.stringify(techs)}`);
return techs
.filter((tech: any) => {
return rectanglesIntersect(zoomedViewport, tech.doc.viewport);
const allExtensions = await getDocsByType('extensions');
console.log(
`getGpxesForViewport, allExtensions: ${JSON.stringify(allExtensions)}`
);
return allExtensions
.filter((extensions: any) => {
return rectanglesIntersect(zoomedViewport, extensions.doc.viewport);
})
.map((tech: any) => getUri('tech', tech._id));
.map((extensions: any) =>
getUri('gpx', getUri('extensions', extensions._id))
);
};
const appendToArray = (target: any, key: string, value: any) => {
if (!(key in target)) {
target[key] = <any>[];
}
target[key].push(value);
};
export const getFullGpx = async (params: any) => {
const { id } = params;
const docs = await getFamily(id, { include_docs: true });
let target: any[];
let gpx: Gpx | undefined = undefined;
docs.rows.forEach((row: any) => {
// level 0
if (row.doc.type === 'gpx') {
target = [row.doc.doc];
gpx = row.doc.doc;
}
//level 1
if (
row.doc.type === 'wpt' ||
row.doc.type === 'rte' ||
row.doc.type === 'trk' ||
row.doc.type === 'extensions'
) {
target.splice(1);
appendToArray(target.at(-1), row.doc.type, row.doc.doc);
target.push(row.doc.doc);
}
// level 2
if (row.doc.type === 'rtept' || row.doc.type === 'trkseg') {
target.splice(2);
appendToArray(target.at(-1), row.doc.type, row.doc.doc);
target.push(row.doc.doc);
}
// level 3
if (row.doc.type === 'trkpt') {
appendToArray(target.at(-1), row.doc.type, row.doc.doc);
}
});
return gpx;
};
export const getGpx = async (params: any) => {
const { id } = params;
const docs = await getFamily(id, { include_docs: true });
console.log(`getGpx, uri: ${id} docs: ${JSON.stringify(docs)}`);
return docs.rows.filter(
(row: any) =>
row.doc.type === 'gpx' ||
row.doc.type === 'wpt' ||
row.doc.type === 'rte' ||
row.doc.type === 'trk' ||
row.doc.type === 'extensions'
);
};
+10 -1
View File
@@ -1,6 +1,6 @@
import getUri from '../lib/ids';
import { putNewGpx } from './gpx';
import { put } from './lib';
import { getFamily, put } from './lib';
export const emptyTrk: Trk = {
name: undefined,
@@ -31,3 +31,12 @@ export const putNewTrk = async (id?: IdTrk | IdGpx) => {
);
return finalId as IdTrk;
};
export const getTrk = async (params: any) => {
const { id } = params;
const docs = await getFamily(id, { include_docs: true });
console.log(`getTrk, uri: ${id} docs: ${JSON.stringify(docs)}`);
return docs.rows.filter(
(row: any) => row.doc.type === 'trk' || row.doc.type === 'trkseg'
);
};
+8 -1
View File
@@ -1,5 +1,5 @@
import getUri from '../lib/ids';
import { put } from './lib';
import { getFamily, put } from './lib';
import { putNewTrk } from './trk';
const emptyTrkseg: Trkseg = {
@@ -24,3 +24,10 @@ export const putNewTrkseg = async (id?: IdTrk | IdGpx | IdTrkseg) => {
);
return finalId as IdTrkseg;
};
export const getTrkseg = async (params: any) => {
const { id } = params;
const docs = await getFamily(id, { include_docs: true });
console.log(`getTrkseg, uri: ${id} docs: ${JSON.stringify(docs)}`);
return docs.rows;
};