mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2025-08-31 23:53:25 +00:00
convert to simple gpx types before export to allow extra fields
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
@@ -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,
|
||||
|
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user