Format comment style voice hints

This commit is contained in:
Norbert Renner 2021-02-25 13:47:26 +01:00
parent e34e6a4cb0
commit ea564006e9
5 changed files with 97 additions and 19 deletions

View file

@ -3,6 +3,7 @@ BR.Gpx = {
if (!geoJson?.features) return ''; if (!geoJson?.features) return '';
const trkNameTransform = { const trkNameTransform = {
comment: '',
trk: function (trk, feature, coordsList) { trk: function (trk, feature, coordsList) {
// name as first tag, by using assign and in this order // name as first tag, by using assign and in this order
return Object.assign( return Object.assign(
@ -26,7 +27,7 @@ BR.Gpx = {
transform: gpxTransform, transform: gpxTransform,
}); });
const statsComment = BR.Gpx._statsComment(geoJson); const statsComment = BR.Gpx._statsComment(geoJson);
gpx = '<?xml version="1.0" encoding="UTF-8"?>' + statsComment + gpx; gpx = '<?xml version="1.0" encoding="UTF-8"?>' + statsComment + gpxTransform.comment + gpx;
gpx = BR.Gpx.pretty(gpx); gpx = BR.Gpx.pretty(gpx);
return gpx; return gpx;
}, },

View file

@ -68,6 +68,7 @@
getGpxTransform: function () { getGpxTransform: function () {
const transform = { const transform = {
comment: '',
trk: function (trk, feature, coordsList) { trk: function (trk, feature, coordsList) {
const properties = this._getTrk(); const properties = this._getTrk();
@ -86,6 +87,25 @@
return transform; return transform;
}, },
_loopHints: function (hintCallback) {
for (const values of this.voicehints) {
const [indexInTrack, commandId, exitNumber, distance, time, geometry] = values;
const hint = { indexInTrack, commandId, exitNumber, distance, time, geometry };
if (time > 0) {
hint.speed = distance / time;
}
const coord = this.track.geometry.coordinates[indexInTrack];
const cmd = this.getCommand(commandId, exitNumber);
if (!cmd) {
console.error(`no voicehint command for id: ${commandId} (${values})`);
continue;
}
hintCallback(hint, cmd, coord);
}
},
getCommand: function (id, exitNumber) { getCommand: function (id, exitNumber) {
let command = BR.VoiceHints.commands[id]; let command = BR.VoiceHints.commands[id];
if (id === 13) { if (id === 13) {
@ -114,25 +134,12 @@
}, },
_addWaypoints: function (gpx) { _addWaypoints: function (gpx) {
for (const values of this.voicehints) { this._loopHints((hint, cmd, coord) => {
const [indexInTrack, commandId, exitNumber, distance, time] = values;
const hint = { indexInTrack, commandId, exitNumber, distance, time };
if (time > 0) {
hint.speed = distance / time;
}
const coord = this.track.geometry.coordinates[indexInTrack];
const cmd = this.getCommand(commandId, exitNumber);
if (!cmd) {
console.error(`no voicehint command for id: ${commandId} (${values})`);
continue;
}
const properties = this._getWpt(hint, cmd, coord); const properties = this._getWpt(hint, cmd, coord);
const wpt = this._createWpt(coord, properties); const wpt = this._createWpt(coord, properties);
gpx.wpt.push(wpt); gpx.wpt.push(wpt);
} });
}, },
_createWpt: function (coord, properties) { _createWpt: function (coord, properties) {
@ -214,14 +221,50 @@
}, },
}); });
BR.CommentVoiceHints = BR.VoiceHints.extend({
_addToTransform: function (transform) {
let comment = `
<!-- $transport-mode$${this.transportMode}$ -->
<!-- cmd idx lon lat d2next geometry -->
<!-- $turn-instruction-start$
`;
this._loopHints((hint, cmd, coord) => {
const pad = (obj = '', len) => {
return new String(obj).padStart(len) + ';';
};
let turn = '';
turn += pad(cmd.name, 6);
turn += pad(hint.indexInTrack, 6);
turn += pad(coord[0], 10);
turn += pad(coord[1], 10);
turn += pad(hint.distance, 6);
turn += hint.geometry;
comment += `
$turn$${turn}$
`;
});
comment += `
$turn-instruction-end$ -->
`;
transform.comment = comment;
},
});
BR.voiceHints = function (geoJson, turnInstructionMode, transportMode) { BR.voiceHints = function (geoJson, turnInstructionMode, transportMode) {
switch (turnInstructionMode) { switch (turnInstructionMode) {
case 2: case 2:
return new BR.LocusVoiceHints(geoJson, turnInstructionMode, transportMode); return new BR.LocusVoiceHints(geoJson, turnInstructionMode, transportMode);
case 4:
return new BR.CommentVoiceHints(geoJson, turnInstructionMode, transportMode);
case 5: case 5:
return new BR.GpsiesVoiceHints(geoJson, turnInstructionMode, transportMode); return new BR.GpsiesVoiceHints(geoJson, turnInstructionMode, transportMode);
default: default:
console.error('unhandled turnInstructionMode: ' + mode); console.error('unhandled turnInstructionMode: ' + turnInstructionMode);
return new BR.VoiceHints(geoJson, turnInstructionMode, transportMode); return new BR.VoiceHints(geoJson, turnInstructionMode, transportMode);
} }
}; };

View file

@ -51,6 +51,14 @@ describe('voice hints', () => {
expect(gpx).toEqual(brouterGpx); expect(gpx).toEqual(brouterGpx);
}); });
test('4-comment', () => {
let brouterGpx = read('4-comment.gpx');
brouterGpx = brouterGpx.replace(/;\s*([-0-9]+.[0-9]+?)0+;/g, (match, p1) => `;${p1.padStart(10)};`); // remove trailing zeros
const gpx = BR.Gpx.format(geoJson, 4);
expect(gpx).toEqual(brouterGpx);
});
test('5-gpsies', () => { test('5-gpsies', () => {
const brouterGpx = read('5-gpsies.gpx'); const brouterGpx = read('5-gpsies.gpx');
const gpx = BR.Gpx.format(geoJson, 5); const gpx = BR.Gpx.format(geoJson, 5);

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- track-length = 319 filtered ascend = 2 plain-ascend = -1 cost=533 energy=.0kwh time=44s -->
<!-- $transport-mode$bike$ -->
<!-- cmd idx lon lat d2next geometry -->
<!-- $turn-instruction-start$
$turn$ TR; 1; 8.468340; 49.488794; 140; 6(90)6 (0)6 (-89)2$
$turn$ TL; 5; 8.469971; 49.488151; 90; 6(-89)6 (0)6 (89)6$
$turn-instruction-end$ -->
<gpx
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
creator="BRouter-1.6.1" version="1.1">
<trk>
<name>4-comment</name>
<trkseg>
<trkpt lon="8.467714" lat="49.488115"><ele>101.5</ele></trkpt>
<trkpt lon="8.468340" lat="49.488794"><ele>101.5</ele></trkpt>
<trkpt lon="8.468586" lat="49.488698"><ele>101.5</ele></trkpt>
<trkpt lon="8.468743" lat="49.488636"><ele>101.5</ele></trkpt>
<trkpt lon="8.469161" lat="49.488473"><ele>101.75</ele></trkpt>
<trkpt lon="8.469971" lat="49.488151"><ele>103.5</ele></trkpt>
<trkpt lon="8.470610" lat="49.488842"><ele>99.75</ele></trkpt>
</trkseg>
</trk>
</gpx>

View file

@ -13,8 +13,8 @@
"total-energy": "4412", "total-energy": "4412",
"cost": "533", "cost": "533",
"voicehints": [ "voicehints": [
[1,5,0,140.0,24.90994644165039], [1,5,0,140.0,24.90994644165039," 6(90)6 (0)6 (-89)2"],
[5,2,0,90.0,9.614852905273438] [5,2,0,90.0,9.614852905273438," 6(-89)6 (0)6 (89)6"]
], ],
"messages": [ "messages": [
["Longitude", "Latitude", "Elevation", "Distance", "CostPerKm", "ElevCost", "TurnCost", "NodeCost", "InitialCost", "WayTags", "NodeTags"], ["Longitude", "Latitude", "Elevation", "Distance", "CostPerKm", "ElevCost", "TurnCost", "NodeCost", "InitialCost", "WayTags", "NodeTags"],