convert to simple gpx types before export to allow extra fields

This commit is contained in:
vcoppe
2024-04-19 12:17:31 +02:00
parent 219400aa87
commit 0735bfc913
3 changed files with 42 additions and 9 deletions

View File

@@ -25,6 +25,8 @@ abstract class GPXTreeElement<T extends GPXTreeElement<any>> {
// An abstract class that can be extended to facilitate functions working similarly with Tracks and TrackSegments
abstract class GPXTreeNode<T extends GPXTreeElement<any>> extends GPXTreeElement<T> {
statistics: GPXStatistics;
isLeaf(): boolean {
return false;
}
@@ -36,6 +38,8 @@ abstract class GPXTreeNode<T extends GPXTreeElement<any>> extends GPXTreeElement
statistics.mergeWith(child.computeStatistics());
}
this.statistics = statistics;
return statistics;
}
@@ -85,7 +89,6 @@ export class GPXFile extends GPXTreeNode<Track>{
metadata: Metadata;
wpt: Waypoint[];
trk: Track[];
statistics: GPXStatistics;
constructor(gpx: GPXFileType | GPXFile) {
super();
@@ -94,7 +97,7 @@ export class GPXFile extends GPXTreeNode<Track>{
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<Track>{
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<TrackSegment> {
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<TrackSegment> {
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));
}