From 9ca93e4c032f3acde71b7c98d6c00d31814338cb Mon Sep 17 00:00:00 2001 From: Marcus Jaschen Date: Sat, 20 Nov 2021 13:46:14 +0100 Subject: [PATCH] Normalize `surface` and `smoothness` variants The `surface` tag exists in different variants, e.g. `surface`, `cycleway:surface` etc. Previously, the `surface` and `smoothness` tags were only processed for route analysis if they were found in their canonical form in the BRouter server response. With this commit, the variants are normalized down to the main tag name which has the effect that they're included in the route analysis. Fixes #438 --- js/control/TrackAnalysis.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/js/control/TrackAnalysis.js b/js/control/TrackAnalysis.js index 2f230c9..24dd0c4 100644 --- a/js/control/TrackAnalysis.js +++ b/js/control/TrackAnalysis.js @@ -142,7 +142,8 @@ BR.TrackAnalysis = L.Class.extend({ var wayTags = segments[segmentIndex].feature.properties.messages[messageIndex][9].split(' '); for (var wayTagIndex = 0; wayTagIndex < wayTags.length; wayTagIndex++) { var wayTagParts = wayTags[wayTagIndex].split('='); - switch (wayTagParts[0]) { + var tagName = this.normalizeTagName(wayTagParts[0]); + switch (tagName) { case 'highway': var highwayType = wayTagParts[1]; var trackType = ''; @@ -167,10 +168,10 @@ BR.TrackAnalysis = L.Class.extend({ break; case 'surface': case 'smoothness': - if (typeof analysis[wayTagParts[0]][wayTagParts[1]] === 'undefined') { - analysis[wayTagParts[0]][wayTagParts[1]] = { + if (typeof analysis[tagName][wayTagParts[1]] === 'undefined') { + analysis[tagName][wayTagParts[1]] = { formatted_name: i18next.t( - 'sidebar.analysis.data.' + wayTagParts[0] + '.' + wayTagParts[1], + 'sidebar.analysis.data.' + tagName + '.' + wayTagParts[1], wayTagParts[1] ), name: wayTagParts[1], @@ -178,7 +179,7 @@ BR.TrackAnalysis = L.Class.extend({ distance: 0.0, }; } - analysis[wayTagParts[0]][wayTagParts[1]].distance += parseFloat( + analysis[tagName][wayTagParts[1]].distance += parseFloat( segments[segmentIndex].feature.properties.messages[messageIndex][3] ); break; @@ -190,6 +191,31 @@ BR.TrackAnalysis = L.Class.extend({ return this.sortAnalysisData(analysis); }, + /** + * Normalize the tag name. + * + * Motivation: The `surface` tag comes in different variations, + * e.g. `surface`, `cycleway:surface` etc. We're only interested + * in the main tag so all other variations are normalized. + * + * @param {string} tagName + * @returns {string} + */ + normalizeTagName: function (tagName) { + // we assume that a tag belongs to the category `surface`, + // if that string is contained anywhere in the tag name: + if (tagName.indexOf('surface') !== -1) { + return 'surface'; + } + + // the same applies to `smoothness` + if (tagName.indexOf('smoothness') !== -1) { + return 'smoothness'; + } + + return tagName; + }, + /** * Transform analysis data for each type into an array, sort it * by distance descending and convert it back to an object.