mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2025-08-31 23:53:25 +00:00
support gpx routes by converting them to tracks
This commit is contained in:
@@ -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<T>(obj: T): T {
|
||||
@@ -115,6 +115,7 @@ export class GPXFile extends GPXTreeNode<Track>{
|
||||
metadata: Metadata;
|
||||
wpt: Waypoint[];
|
||||
trk: Track[];
|
||||
rte: RouteType[];
|
||||
|
||||
constructor(gpx?: GPXFileType & { _data?: any } | GPXFile) {
|
||||
super();
|
||||
@@ -123,6 +124,9 @@ export class GPXFile extends GPXTreeNode<Track>{
|
||||
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<Track>{
|
||||
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<Track>{
|
||||
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[],
|
||||
};
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
@@ -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') {
|
||||
|
@@ -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;
|
||||
};
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user