From d81d189cdf42b9da7eca0f8a2a10490742066128 Mon Sep 17 00:00:00 2001 From: vcoppe Date: Fri, 19 Jul 2024 13:18:38 +0200 Subject: [PATCH] elevation tool test --- gpx/src/gpx.ts | 29 +++++++++ .../src/lib/components/toolbar/Toolbar.svelte | 7 ++- .../components/toolbar/ToolbarItemMenu.svelte | 3 + .../components/toolbar/tools/Elevation.svelte | 33 ++++++++++ website/src/lib/db.ts | 62 ++++++++++++++++++- website/src/lib/stores.ts | 1 + website/src/lib/utils.ts | 25 +++++++- website/src/locales/en.json | 6 ++ 8 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 website/src/lib/components/toolbar/tools/Elevation.svelte diff --git a/gpx/src/gpx.ts b/gpx/src/gpx.ts index 957420fa..6d695d44 100644 --- a/gpx/src/gpx.ts +++ b/gpx/src/gpx.ts @@ -21,6 +21,7 @@ export abstract class GPXTreeElement> { abstract getEndTimestamp(): Date | undefined; abstract getStatistics(): GPXStatistics; abstract getSegments(): TrackSegment[]; + abstract getTrackPoints(): TrackPoint[]; abstract toGeoJSON(): GeoJSON.Feature | GeoJSON.Feature[] | GeoJSON.FeatureCollection | GeoJSON.FeatureCollection[]; @@ -66,6 +67,10 @@ abstract class GPXTreeNode> extends GPXTreeElement return this.children.flatMap((child) => child.getSegments()); } + getTrackPoints(): TrackPoint[] { + return this.children.flatMap((child) => child.getTrackPoints()); + } + // Producers _reverse(originalNextTimestamp?: Date, newPreviousTimestamp?: Date) { let og = getOriginal(this); @@ -315,6 +320,26 @@ export class GPXFile extends GPXTreeNode{ }); } + addElevation(callback: (Coordinates) => number, trackIndices?: number[], segmentIndices?: number[], waypointIndices?: number[]) { + let og = getOriginal(this); // Read as much as possible from the original object because it is faster + this.trk.forEach((track, trackIndex) => { + if (trackIndices === undefined || trackIndices.includes(trackIndex)) { + track.trkseg.forEach((segment, segmentIndex) => { + if (segmentIndices === undefined || segmentIndices.includes(segmentIndex)) { + segment.trkpt.forEach((point, pointIndex) => { + point.ele = callback(og.trk[trackIndex].trkseg[segmentIndex].trkpt[pointIndex].attributes); + }); + } + }); + } + }); + this.wpt.forEach((waypoint, waypointIndex) => { + if (waypointIndices === undefined || waypointIndices.includes(waypointIndex)) { + waypoint.ele = callback(og.wpt[waypointIndex].attributes); + } + }); + } + setStyle(style: LineStyleExtension) { this.trk.forEach((track) => { track.setStyle(style); @@ -753,6 +778,10 @@ export class TrackSegment extends GPXTreeLeaf { return [this]; } + getTrackPoints(): TrackPoint[] { + return this.trkpt; + } + toGeoJSON(): GeoJSON.Feature { return { type: "Feature", diff --git a/website/src/lib/components/toolbar/Toolbar.svelte b/website/src/lib/components/toolbar/Toolbar.svelte index 459e543b..6674b30c 100644 --- a/website/src/lib/components/toolbar/Toolbar.svelte +++ b/website/src/lib/components/toolbar/Toolbar.svelte @@ -9,7 +9,8 @@ Ungroup, MapPin, Filter, - Scissors + Scissors, + MountainSnow } from 'lucide-svelte'; import { _ } from 'svelte-i18n'; @@ -45,6 +46,10 @@ {$_('toolbar.extract.tooltip')} + + + {$_('toolbar.elevation.tooltip')} + {$_('toolbar.reduce.tooltip')} diff --git a/website/src/lib/components/toolbar/ToolbarItemMenu.svelte b/website/src/lib/components/toolbar/ToolbarItemMenu.svelte index 2598f53c..80bba269 100644 --- a/website/src/lib/components/toolbar/ToolbarItemMenu.svelte +++ b/website/src/lib/components/toolbar/ToolbarItemMenu.svelte @@ -9,6 +9,7 @@ import Time from '$lib/components/toolbar/tools/Time.svelte'; import Merge from '$lib/components/toolbar/tools/Merge.svelte'; import Extract from '$lib/components/toolbar/tools/Extract.svelte'; + import Elevation from '$lib/components/toolbar/tools/Elevation.svelte'; import Clean from '$lib/components/toolbar/tools/Clean.svelte'; import Reduce from '$lib/components/toolbar/tools/Reduce.svelte'; import RoutingControlPopup from '$lib/components/toolbar/tools/routing/RoutingControlPopup.svelte'; @@ -48,6 +49,8 @@