refine gpx class hierarchy

This commit is contained in:
vcoppe
2024-04-18 17:11:23 +02:00
parent 7ca0b3bae3
commit 15785eb387
2 changed files with 44 additions and 11 deletions

View File

@@ -1,4 +1,11 @@
import { Coordinates, GPXFileAttributes, GPXFileType, Link, Metadata, TrackExtensions, TrackPointExtensions, TrackPointType, TrackSegmentType, TrackType, Waypoint } from "./types"; import { Coordinates, GPXFileAttributes, GPXFileType, Link, Metadata, TrackExtensions, TrackPointExtensions, TrackPointType, TrackSegmentType, TrackType, WaypointType } from "./types";
function cloneJSON<T>(obj: T): T {
if (obj === null || typeof obj !== 'object') {
return null;
}
return JSON.parse(JSON.stringify(obj));
}
// An abstract class that groups functions that need to be computed recursively in the GPX file hierarchy // An abstract class that groups functions that need to be computed recursively in the GPX file hierarchy
abstract class GPXTreeElement<T extends GPXTreeElement<any>> { abstract class GPXTreeElement<T extends GPXTreeElement<any>> {
@@ -69,9 +76,9 @@ export class GPXFile extends GPXTreeNode<Track>{
constructor(gpx: GPXFileType | GPXFile) { constructor(gpx: GPXFileType | GPXFile) {
super(); super();
this.attributes = gpx.attributes; this.attributes = cloneJSON(gpx.attributes);
this.metadata = gpx.metadata; this.metadata = cloneJSON(gpx.metadata);
this.wpt = gpx.wpt; this.wpt = gpx.wpt.map((waypoint) => new Waypoint(waypoint));
this.trk = gpx.trk.map((track) => new Track(track)); this.trk = gpx.trk.map((track) => new Track(track));
} }
@@ -108,10 +115,10 @@ export class Track extends GPXTreeNode<TrackSegment> {
this.cmt = track.cmt; this.cmt = track.cmt;
this.desc = track.desc; this.desc = track.desc;
this.src = track.src; this.src = track.src;
this.link = track.link; this.link = cloneJSON(track.link);
this.type = track.type; this.type = track.type;
this.trkseg = track.trkseg.map((seg) => new TrackSegment(seg)); this.trkseg = track.trkseg.map((seg) => new TrackSegment(seg));
this.extensions = track.extensions; this.extensions = cloneJSON(track.extensions);
} }
getChildren(): TrackSegment[] { getChildren(): TrackSegment[] {
@@ -200,11 +207,37 @@ export class TrackPoint {
extensions?: TrackPointExtensions; extensions?: TrackPointExtensions;
constructor(point: TrackPointType | TrackPoint) { constructor(point: TrackPointType | TrackPoint) {
this.attributes = point.attributes; this.attributes = cloneJSON(point.attributes);
this.ele = point.ele; this.ele = point.ele;
if (point.time) { if (point.time) {
this.time = new Date(point.time.getTime()); this.time = new Date(point.time.getTime());
} }
this.extensions = point.extensions; this.extensions = cloneJSON(point.extensions);
} }
}; };
export class Waypoint {
attributes: Coordinates;
ele?: number;
time?: Date;
name?: string;
cmt?: string;
desc?: string;
link?: Link;
sym?: string;
type?: string;
constructor(waypoint: WaypointType | Waypoint) {
this.attributes = cloneJSON(waypoint.attributes);
this.ele = waypoint.ele;
if (waypoint.time) {
this.time = new Date(waypoint.time.getTime());
}
this.name = waypoint.name;
this.cmt = waypoint.cmt;
this.desc = waypoint.desc;
this.link = cloneJSON(waypoint.link);
this.sym = waypoint.sym;
this.type = waypoint.type;
}
}

View File

@@ -1,7 +1,7 @@
export type GPXFileType = { export type GPXFileType = {
attributes: GPXFileAttributes; attributes: GPXFileAttributes;
metadata: Metadata; metadata: Metadata;
wpt: Waypoint[]; wpt: WaypointType[];
trk: TrackType[]; trk: TrackType[];
}; };
@@ -28,7 +28,7 @@ export type LinkAttributes = {
href: string; href: string;
}; };
export type Waypoint = { export type WaypointType = {
attributes: Coordinates; attributes: Coordinates;
ele?: number; ele?: number;
time?: Date; time?: Date;