support gpx routes by converting them to tracks

This commit is contained in:
vcoppe
2024-07-15 19:06:13 +02:00
parent a85beee1f0
commit 1d33607266
3 changed files with 71 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
import { ramerDouglasPeucker } from "./simplify"; 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"; import { immerable, isDraft, original, freeze } from "immer";
function cloneJSON<T>(obj: T): T { function cloneJSON<T>(obj: T): T {
@@ -115,6 +115,7 @@ export class GPXFile extends GPXTreeNode<Track>{
metadata: Metadata; metadata: Metadata;
wpt: Waypoint[]; wpt: Waypoint[];
trk: Track[]; trk: Track[];
rte: RouteType[];
constructor(gpx?: GPXFileType & { _data?: any } | GPXFile) { constructor(gpx?: GPXFileType & { _data?: any } | GPXFile) {
super(); super();
@@ -123,6 +124,9 @@ export class GPXFile extends GPXTreeNode<Track>{
this.metadata = gpx.metadata; this.metadata = gpx.metadata;
this.wpt = gpx.wpt ? gpx.wpt.map((waypoint) => new Waypoint(waypoint)) : []; this.wpt = gpx.wpt ? gpx.wpt.map((waypoint) => new Waypoint(waypoint)) : [];
this.trk = gpx.trk ? gpx.trk.map((track) => new Track(track)) : []; 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')) { if (gpx.hasOwnProperty('_data')) {
this._data = gpx._data; this._data = gpx._data;
} }
@@ -188,6 +192,7 @@ export class GPXFile extends GPXTreeNode<Track>{
metadata: cloneJSON(this.metadata), metadata: cloneJSON(this.metadata),
wpt: this.wpt.map((waypoint) => waypoint.clone()), wpt: this.wpt.map((waypoint) => waypoint.clone()),
trk: this.trk.map((track) => track.clone()), trk: this.trk.map((track) => track.clone()),
rte: [],
_data: cloneJSON(this._data), _data: cloneJSON(this._data),
}); });
} }
@@ -204,7 +209,8 @@ export class GPXFile extends GPXTreeNode<Track>{
attributes: cloneJSON(this.attributes), attributes: cloneJSON(this.attributes),
metadata: cloneJSON(this.metadata), metadata: cloneJSON(this.metadata),
wpt: this.wpt, 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[] color: string[]
opacity: number[], opacity: number[],
weight: number[], weight: number[],
}; };
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;
}

View File

@@ -8,7 +8,7 @@ export function parseGPX(gpxData: string): GPXFile {
attributeNamePrefix: "", attributeNamePrefix: "",
attributesGroupName: 'attributes', attributesGroupName: 'attributes',
isArray: (name: string) => { 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) { attributeValueProcessor(attrName, attrValue, jPath) {
if (attrName === 'lat' || attrName === 'lon') { if (attrName === 'lat' || attrName === 'lon') {

View File

@@ -3,6 +3,7 @@ export type GPXFileType = {
metadata: Metadata; metadata: Metadata;
wpt: WaypointType[]; wpt: WaypointType[];
trk: TrackType[]; trk: TrackType[];
rte: RouteType[];
}; };
export type GPXFileAttributes = { export type GPXFileAttributes = {
@@ -38,6 +39,11 @@ export type WaypointType = {
link?: Link; link?: Link;
sym?: string; sym?: string;
type?: string; type?: string;
extensions?: WaypointExtensions;
};
export type WaypointExtensions = {
'gpxx:RoutePointExtension'?: RoutePointExtension;
}; };
export type Coordinates = { export type Coordinates = {
@@ -99,4 +105,23 @@ export type Author = {
name?: string; name?: string;
email?: string; email?: string;
link?: Link; link?: Link;
}; };
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;
}