From 2a241375db97270a443791a677a30132ce8bcd86 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sat, 7 Mar 2020 00:28:47 +0100 Subject: [PATCH] parseLocation: parse foreign stop IDs from `I` hints `rRefL` seems to be used by older HAFAS endpoints, e.g. `ver: 1.11`. see also #5, #90, #131 --- parse/common.js | 9 +++++++-- parse/hint.js | 18 ++++++++++++++++++ parse/location.js | 21 +++++++++++++++++++++ test/fixtures/insa-stop.js | 3 +++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/parse/common.js b/parse/common.js index ddafdf90..d92b09e6 100644 --- a/parse/common.js +++ b/parse/common.js @@ -8,7 +8,7 @@ const parseCommonData = (_ctx) => { const matches = findInTree(res, [ '**.oprX', '**.icoX', '**.prodX', '**.pRefL', '**.locX', '**.ani.fLocX', '**.ani.tLocX', '**.fLocX', '**.tLocX', - '**.remX', '**.himX', '**.polyG.polyXL' + '**.remX', '**.himX', '**.polyG.polyXL', '**.rRefL', ]); const common = {} @@ -77,11 +77,16 @@ const parseCommonData = (_ctx) => { } common.hints = [] - if (opt.remarks && Array.isArray(c.remL)) { + if (Array.isArray(c.remL)) { common.hints = c.remL.map(hint => profile.parseHint(ctx, hint)) matches['**.remX'].forEach(([idx, parents]) => { if ('number' === typeof idx) parents[0].hint = common.hints[idx] }) + matches['**.rRefL'].forEach(([idxs, parents]) => { + parents[0].hints = idxs + .filter(idx => !!common.hints[idx]) + .map(idx => common.hints[idx]) + }) } common.warnings = [] if (opt.remarks && Array.isArray(c.himL)) { diff --git a/parse/hint.js b/parse/hint.js index a13259bd..6bd309d2 100644 --- a/parse/hint.js +++ b/parse/hint.js @@ -4,6 +4,15 @@ const codesByIcon = Object.assign(Object.create(null), { cancel: 'cancelled' }) +const linkTypesByCode = Object.assign(Object.create(null), { + // IFOPT-based DHID + // https://wiki.openstreetmap.org/wiki/DE:Key:ref:IFOPT + // https://trid.trb.org/view/1435440 + IF: 'stop-dhid', + // todo: `{type: 'I',code: 'TD',icoX: 1,txtN: '8010224'}` + // todo: `{type: 'I',code: 'TE',icoX: 1,txtN: '8024001'}` +}) + // todo: pass in tag list from hint reference, e.g.: // "tagL": [ // "RES_JNY_H3" // H3 = level 3 heading? shown on overview @@ -15,6 +24,15 @@ const codesByIcon = Object.assign(Object.create(null), { const parseHint = (ctx, h) => { // todo: C + if (h.type === 'I' && h.code && h.txtN) { + if (h.code in linkTypesByCode) { + return {type: linkTypesByCode[h.code], text: h.txtN} + } + if (h.code === 'TW' && h.txtN[0] === '#') { + return {type: 'foreign-id', text: h.txtN.slice(1)} + } + } + const text = h.txtN && h.txtN.trim() || '' const icon = h.icon || null const code = h.code || (icon && icon.type && codesByIcon[icon.type]) || null diff --git a/parse/location.js b/parse/location.js index bb9d439b..e533cc64 100644 --- a/parse/location.js +++ b/parse/location.js @@ -38,6 +38,27 @@ const parseLocation = (ctx, l) => { stop.lines = l.lines } + const hints = l.hints || [] + const byType = type => hints.find(h => h.type === type) + + const dhid = (byType('stop-dhid') || {}).text + if (dhid) { + if (!stop.ids) stop.ids = {} + stop.ids.dhid = dhid + } + + const otherIds = hints + .filter(h => h.type === 'foreign-id') + .filter(h => 'string' === typeof h.text && h.text.includes(':')) + .map(({text}) => { + const i = text.indexOf(':') + return [text.slice(0, i), text.slice(i + 1)] + }) + if (otherIds.length > 0) { + if (!stop.ids) stop.ids = {} + for (const [src, id] of otherIds) stop.ids[src] = id + } + return stop } diff --git a/test/fixtures/insa-stop.js b/test/fixtures/insa-stop.js index fccb7c53..a92684a4 100644 --- a/test/fixtures/insa-stop.js +++ b/test/fixtures/insa-stop.js @@ -3,6 +3,9 @@ module.exports = { type: 'stop', id: '7341', + ids: { + dhid: 'de:15003:7341', + }, name: 'Magdeburg, S-Bahnhof SKET Industriepark', location: { type: 'location',