diff --git a/js/format/Gpx.js b/js/format/Gpx.js index 26fff45..5064943 100644 --- a/js/format/Gpx.js +++ b/js/format/Gpx.js @@ -3,6 +3,7 @@ BR.Gpx = { if (!geoJson?.features) return ''; const trkNameTransform = { + comment: '', trk: function (trk, feature, coordsList) { // name as first tag, by using assign and in this order return Object.assign( @@ -26,7 +27,7 @@ BR.Gpx = { transform: gpxTransform, }); const statsComment = BR.Gpx._statsComment(geoJson); - gpx = '' + statsComment + gpx; + gpx = '' + statsComment + gpxTransform.comment + gpx; gpx = BR.Gpx.pretty(gpx); return gpx; }, diff --git a/js/format/VoiceHints.js b/js/format/VoiceHints.js index e932460..9cd5566 100644 --- a/js/format/VoiceHints.js +++ b/js/format/VoiceHints.js @@ -68,6 +68,7 @@ getGpxTransform: function () { const transform = { + comment: '', trk: function (trk, feature, coordsList) { const properties = this._getTrk(); @@ -86,6 +87,25 @@ 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) { let command = BR.VoiceHints.commands[id]; if (id === 13) { @@ -114,25 +134,12 @@ }, _addWaypoints: function (gpx) { - for (const values of this.voicehints) { - 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; - } - + this._loopHints((hint, cmd, coord) => { const properties = this._getWpt(hint, cmd, coord); const wpt = this._createWpt(coord, properties); gpx.wpt.push(wpt); - } + }); }, _createWpt: function (coord, properties) { @@ -214,14 +221,50 @@ }, }); + BR.CommentVoiceHints = BR.VoiceHints.extend({ + _addToTransform: function (transform) { + let comment = ` + + + +`; + + transform.comment = comment; + }, + }); + BR.voiceHints = function (geoJson, turnInstructionMode, transportMode) { switch (turnInstructionMode) { case 2: return new BR.LocusVoiceHints(geoJson, turnInstructionMode, transportMode); + case 4: + return new BR.CommentVoiceHints(geoJson, turnInstructionMode, transportMode); case 5: return new BR.GpsiesVoiceHints(geoJson, turnInstructionMode, transportMode); default: - console.error('unhandled turnInstructionMode: ' + mode); + console.error('unhandled turnInstructionMode: ' + turnInstructionMode); return new BR.VoiceHints(geoJson, turnInstructionMode, transportMode); } }; diff --git a/tests/format/Gpx.test.js b/tests/format/Gpx.test.js index e6ea31b..f3a99ba 100644 --- a/tests/format/Gpx.test.js +++ b/tests/format/Gpx.test.js @@ -51,6 +51,14 @@ describe('voice hints', () => { 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', () => { const brouterGpx = read('5-gpsies.gpx'); const gpx = BR.Gpx.format(geoJson, 5); diff --git a/tests/format/data/4-comment.gpx b/tests/format/data/4-comment.gpx new file mode 100644 index 0000000..47d6ecf --- /dev/null +++ b/tests/format/data/4-comment.gpx @@ -0,0 +1,26 @@ + + + + + + + + 4-comment + + 101.5 + 101.5 + 101.5 + 101.5 + 101.75 + 103.5 + 99.75 + + + diff --git a/tests/format/data/track.json b/tests/format/data/track.json index fd1c324..8cf5a2c 100644 --- a/tests/format/data/track.json +++ b/tests/format/data/track.json @@ -13,8 +13,8 @@ "total-energy": "4412", "cost": "533", "voicehints": [ - [1,5,0,140.0,24.90994644165039], - [5,2,0,90.0,9.614852905273438] + [1,5,0,140.0,24.90994644165039," 6(90)6 (0)6 (-89)2"], + [5,2,0,90.0,9.614852905273438," 6(-89)6 (0)6 (89)6"] ], "messages": [ ["Longitude", "Latitude", "Elevation", "Distance", "CostPerKm", "ElevCost", "TurnCost", "NodeCost", "InitialCost", "WayTags", "NodeTags"],