Format route points as waypoints

This commit is contained in:
Norbert Renner 2021-03-18 18:04:57 +01:00
parent 449a24e5ce
commit 7fc2f6bee5
8 changed files with 380 additions and 19 deletions

View file

@ -69,6 +69,9 @@ BR.Export = L.Class.extend({
_formatTrack: function (format, name, includeWaypoints) {
const track = BR.Export._concatTotalTrack(this.segments);
if (includeWaypoints) {
this._addRouteWaypoints(track);
}
//console.log('GeoJson: ', trackGeoJson);
//console.log('GeoJson: ', JSON.stringify(trackGeoJson, null, 4));
switch (format) {
@ -85,6 +88,24 @@ BR.Export = L.Class.extend({
console.error('Export format not implemented: ' + format);
},
_addRouteWaypoints: function (track) {
const routePoints = [];
for (const [i, latLng] of this.latLngs.entries()) {
let name = 'via' + i;
let type = 'via';
if (i === 0) {
name = 'from';
type = 'from';
} else if (i === this.latLngs.length - 1) {
name = 'to';
type = 'to';
}
const properties = { name, type };
routePoints.push(turf.point([latLng.lng, latLng.lat], properties));
}
track.features.push(...routePoints);
},
_validationMessage: function () {
var trackname = this.trackname;
var replaceRegex = new RegExp('[^' + this.tracknameAllowedChars + ']', 'g');

View file

@ -2,9 +2,34 @@ BR.Gpx = {
format: function (geoJson, turnInstructionMode = 0, transportMode = 'bike') {
if (!geoJson?.features) return '';
const trkNameTransform = {
comment: '',
trk: function (trk, feature, coordsList) {
class GpxTransform {
constructor(voiceHintsTransform) {
this.voiceHintsTransform = voiceHintsTransform;
this.comment = voiceHintsTransform?.comment || '';
if (this.voiceHintsTransform) {
Object.keys(this.voiceHintsTransform).forEach((member) => {
if (!GpxTransform.prototype.hasOwnProperty(member)) {
this[member] = this.voiceHintsTransform[member];
}
});
}
}
wpt(wpt, feature, coord, index) {
// not in use right now, just to be safe in case of future overrides
wpt = (voiceHintsTransform?.wpt && voiceHintsTransform.wpt(wpt, feature, coord, index)) || wpt;
if (feature.properties.name) {
wpt.name = feature.properties.name;
}
if (feature.properties.type) {
wpt.type = feature.properties.type;
}
return wpt;
}
trk(trk, feature, coordsList) {
trk = (voiceHintsTransform?.trk && voiceHintsTransform.trk(trk, feature, coordsList)) || trk;
// name as first tag, by using assign and in this order
return Object.assign(
{
@ -12,14 +37,15 @@ BR.Gpx = {
},
trk
);
},
};
let gpxTransform = trkNameTransform;
}
}
let voiceHintsTransform;
if (turnInstructionMode > 1) {
const voiceHints = BR.voiceHints(geoJson, turnInstructionMode, transportMode);
gpxTransform = voiceHints.getGpxTransform();
voiceHintsTransform = voiceHints.getGpxTransform();
}
const gpxTransform = new GpxTransform(voiceHintsTransform);
let gpx = togpx(geoJson, {
featureTitle: function () {},

View file

@ -48,14 +48,7 @@
comment: '',
trk: function (trk, feature, coordsList) {
const properties = this._getTrk();
return Object.assign(
{
name: feature.properties.name,
},
properties,
trk
);
return Object.assign(properties, trk);
}.bind(this),
};
@ -146,12 +139,14 @@
}
_addWaypoints(gpx) {
const waypoints = [];
this._loopHints((hint, cmd, coord) => {
const properties = this._getWpt(hint, cmd, coord);
const wpt = this._createWpt(coord, properties);
gpx.wpt.push(wpt);
waypoints.push(wpt);
});
gpx.wpt.unshift(...waypoints);
}
_createWpt(coord, properties) {

View file

@ -112,10 +112,10 @@ BR.Diff.adoptGpx = function (gpx, replaceCreator = true) {
gpx = gpx.replace(/(lon|lat)="([-0-9]+.[0-9]+?)0+"/g, '$1="$2"'); // remove trailing zeros
// remove trailing zeros comment-style voicehints
gpx = gpx.replace(/;\s*([-0-9]+.[0-9]+?)0+;/g, (match, p1) => `;${p1.padStart(10)};`);
gpx = gpx.replace(/>([-0-9]+?\.\d*0+)<\//g, (match, p1) => `>${+p1}</`); // remove trailing zeros
gpx = gpx.replace('</gpx>\n', '</gpx>');
// added
gpx = gpx.replace(/>([-.0-9]+?0+)<\//g, (match, p1) => `>${+p1}</`); // remove trailing zeros
// trunc bc. float precision diffs
gpx = gpx.replace(/(rteTime|rteSpeed)>([^<]*)<\//g, (match, p1, p2) => `${p1}>${(+p2).toFixed(3)}</`);
gpx = gpx.replace(/\n?\s*<\/extensions>\n?\s*<extensions>/, ''); // ignore (invalid) double tag