From 0735bfc91372b1faa219a1a58ed558bf70fb0e88 Mon Sep 17 00:00:00 2001 From: vcoppe Date: Fri, 19 Apr 2024 12:17:31 +0200 Subject: [PATCH] convert to simple gpx types before export to allow extra fields --- gpx/src/gpx.ts | 43 +++++++++++++++++++++++++++++++++++++------ gpx/src/io.ts | 4 +++- gpx/test/io.test.ts | 4 ++-- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/gpx/src/gpx.ts b/gpx/src/gpx.ts index 17dda226..8cb2d848 100644 --- a/gpx/src/gpx.ts +++ b/gpx/src/gpx.ts @@ -25,6 +25,8 @@ abstract class GPXTreeElement> { // An abstract class that can be extended to facilitate functions working similarly with Tracks and TrackSegments abstract class GPXTreeNode> extends GPXTreeElement { + statistics: GPXStatistics; + isLeaf(): boolean { return false; } @@ -36,6 +38,8 @@ abstract class GPXTreeNode> extends GPXTreeElement statistics.mergeWith(child.computeStatistics()); } + this.statistics = statistics; + return statistics; } @@ -85,7 +89,6 @@ export class GPXFile extends GPXTreeNode{ metadata: Metadata; wpt: Waypoint[]; trk: Track[]; - statistics: GPXStatistics; constructor(gpx: GPXFileType | GPXFile) { super(); @@ -94,7 +97,7 @@ export class GPXFile extends GPXTreeNode{ this.wpt = gpx.wpt ? gpx.wpt.map((waypoint) => new Waypoint(waypoint)) : []; this.trk = gpx.trk ? gpx.trk.map((track) => new Track(track)) : []; - this.statistics = this.computeStatistics(); + this.computeStatistics(); } getChildren(): Track[] { @@ -111,6 +114,15 @@ export class GPXFile extends GPXTreeNode{ features: this.getChildren().flatMap((child) => child.toGeoJSON()) }; } + + toGPXFileType(): GPXFileType { + return { + attributes: this.attributes, + metadata: this.metadata, + wpt: this.wpt, + trk: this.trk.map((track) => track.toTrackType()) + }; + } }; // A class that represents a Track in a GPX file @@ -140,10 +152,6 @@ export class Track extends GPXTreeNode { return this.trkseg; } - clone(): Track { - return new Track(structuredClone(this)); - } - toGeoJSON(): any { return this.getChildren().map((child) => { let geoJSON = child.toGeoJSON(); @@ -161,6 +169,23 @@ export class Track extends GPXTreeNode { return geoJSON; }); } + + toTrackType(): TrackType { + return { + name: this.name, + cmt: this.cmt, + desc: this.desc, + src: this.src, + link: this.link, + type: this.type, + trkseg: this.trkseg.map((seg) => seg.toTrackSegmentType()), + extensions: this.extensions, + }; + } + + clone(): Track { + return new Track(structuredClone(this)); + } }; // A class that represents a TrackSegment in a GPX file @@ -348,6 +373,12 @@ export class TrackSegment extends GPXTreeLeaf { }; } + toTrackSegmentType(): TrackSegmentType { + return { + trkpt: this.trkpt + }; + } + clone(): TrackSegment { return new TrackSegment(structuredClone(this)); } diff --git a/gpx/src/io.ts b/gpx/src/io.ts index 99d64acc..9778a9f8 100644 --- a/gpx/src/io.ts +++ b/gpx/src/io.ts @@ -56,7 +56,9 @@ export function parseGPX(gpxData: string): GPXFile { } -export function buildGPX(gpx: GPXFile): string { +export function buildGPX(file: GPXFile): string { + const gpx = file.toGPXFileType(); + const builder = new XMLBuilder({ format: true, ignoreAttributes: false, diff --git a/gpx/test/io.test.ts b/gpx/test/io.test.ts index a08281e1..b42a7c3c 100644 --- a/gpx/test/io.test.ts +++ b/gpx/test/io.test.ts @@ -223,9 +223,9 @@ describe("Parsing", () => { expect(track.extensions['gpx_style:line']).toHaveProperty('opacity'); expect(track.extensions['gpx_style:line']).toHaveProperty('weight'); - expect(track.extensions['gpx_style:line'].color).toBe("2d3ee9"); + expect(track.extensions['gpx_style:line'].color).toBe("#2d3ee9"); expect(track.extensions['gpx_style:line'].opacity).toBe(0.5); - expect(track.extensions['gpx_style:line'].weight).toBe(5); + expect(track.extensions['gpx_style:line'].weight).toBe(6); }); });