diff --git a/gpx/src/gpx.ts b/gpx/src/gpx.ts index 3eb15ef7..f146bc97 100644 --- a/gpx/src/gpx.ts +++ b/gpx/src/gpx.ts @@ -1,5 +1,5 @@ import { ramerDouglasPeucker } from "./simplify"; -import { Coordinates, GPXFileAttributes, GPXFileType, LineStyleExtension, Link, Metadata, TrackExtensions, TrackPointExtensions, TrackPointType, TrackSegmentType, TrackType, WaypointType } from "./types"; +import { Coordinates, GPXFileAttributes, GPXFileType, LineStyleExtension, Link, Metadata, RouteType, TrackExtensions, TrackPointExtensions, TrackPointType, TrackSegmentType, TrackType, WaypointType } from "./types"; import { immerable, isDraft, original, freeze } from "immer"; function cloneJSON(obj: T): T { @@ -115,6 +115,7 @@ export class GPXFile extends GPXTreeNode{ metadata: Metadata; wpt: Waypoint[]; trk: Track[]; + rte: RouteType[]; constructor(gpx?: GPXFileType & { _data?: any } | GPXFile) { super(); @@ -123,6 +124,9 @@ export class GPXFile extends GPXTreeNode{ this.metadata = gpx.metadata; this.wpt = gpx.wpt ? gpx.wpt.map((waypoint) => new Waypoint(waypoint)) : []; this.trk = gpx.trk ? gpx.trk.map((track) => new Track(track)) : []; + if (gpx.rte && gpx.rte.length > 0) { + this.trk = this.trk.concat(gpx.rte.map((route) => convertRouteToTrack(route))); + } if (gpx.hasOwnProperty('_data')) { this._data = gpx._data; } @@ -188,6 +192,7 @@ export class GPXFile extends GPXTreeNode{ metadata: cloneJSON(this.metadata), wpt: this.wpt.map((waypoint) => waypoint.clone()), trk: this.trk.map((track) => track.clone()), + rte: [], _data: cloneJSON(this._data), }); } @@ -204,7 +209,8 @@ export class GPXFile extends GPXTreeNode{ attributes: cloneJSON(this.attributes), metadata: cloneJSON(this.metadata), wpt: this.wpt, - trk: this.trk.map((track) => track.toTrackType()) + trk: this.trk.map((track) => track.toTrackType()), + rte: [], }; } @@ -1251,4 +1257,39 @@ export type MergedLineStyles = { color: string[] opacity: number[], weight: number[], -}; \ No newline at end of file +}; + +function convertRouteToTrack(route: RouteType): Track { + const track = new Track({ + name: route.name, + cmt: route.cmt, + desc: route.desc, + src: route.src, + link: route.link, + type: route.type, + trkseg: [], + extensions: route.extensions, + }); + + if (route.rtept) { + const segment = new TrackSegment(); + + route.rtept.forEach((rpt) => { + if (rpt.extensions && rpt.extensions['gpxx:RoutePointExtension'] && rpt.extensions['gpxx:RoutePointExtension']["gpxx:rpt"]) { + rpt.extensions['gpxx:RoutePointExtension']["gpxx:rpt"].forEach((rptExtension) => { + segment.trkpt.push(new TrackPoint({ + attributes: rptExtension.attributes, + })); + }); + } else { + segment.trkpt.push(new TrackPoint({ + attributes: rpt.attributes, + })); + } + }); + + track.trkseg.push(segment); + } + + return track; +} \ No newline at end of file diff --git a/gpx/src/io.ts b/gpx/src/io.ts index 02c9f7e0..20fd6c58 100644 --- a/gpx/src/io.ts +++ b/gpx/src/io.ts @@ -8,7 +8,7 @@ export function parseGPX(gpxData: string): GPXFile { attributeNamePrefix: "", attributesGroupName: 'attributes', isArray: (name: string) => { - return name === 'trk' || name === 'trkseg' || name === 'trkpt' || name === 'wpt'; + return name === 'trk' || name === 'trkseg' || name === 'trkpt' || name === 'wpt' || name === 'rte' || name === 'rtept' || name === 'gpxx:rpt'; }, attributeValueProcessor(attrName, attrValue, jPath) { if (attrName === 'lat' || attrName === 'lon') { diff --git a/gpx/src/types.ts b/gpx/src/types.ts index 05033174..a48a307d 100644 --- a/gpx/src/types.ts +++ b/gpx/src/types.ts @@ -3,6 +3,7 @@ export type GPXFileType = { metadata: Metadata; wpt: WaypointType[]; trk: TrackType[]; + rte: RouteType[]; }; export type GPXFileAttributes = { @@ -38,6 +39,11 @@ export type WaypointType = { link?: Link; sym?: string; type?: string; + extensions?: WaypointExtensions; +}; + +export type WaypointExtensions = { + 'gpxx:RoutePointExtension'?: RoutePointExtension; }; export type Coordinates = { @@ -99,4 +105,23 @@ export type Author = { name?: string; email?: string; link?: Link; -}; \ No newline at end of file +}; + +export type RouteType = { + name?: string; + cmt?: string; + desc?: string; + src?: string; + link?: Link; + type?: string; + extensions?: TrackExtensions; + rtept: WaypointType[]; +} + +export type RoutePointExtension = { + 'gpxx:rpt'?: GPXXRoutePoint[]; +} + +export type GPXXRoutePoint = { + attributes: Coordinates; +} \ No newline at end of file