2017-11-11 22:35:41 +01:00
|
|
|
'use strict'
|
|
|
|
|
2019-02-05 19:07:19 +01:00
|
|
|
const {parse} = require('qs')
|
|
|
|
|
2017-12-11 19:25:29 +01:00
|
|
|
const POI = 'P'
|
|
|
|
const STATION = 'S'
|
|
|
|
const ADDRESS = 'A'
|
2017-11-11 22:35:41 +01:00
|
|
|
|
2018-10-15 17:31:28 +02:00
|
|
|
const leadingZeros = /^0+/
|
|
|
|
|
2017-11-11 22:35:41 +01:00
|
|
|
// todo: what is s.rRefL?
|
2019-10-20 00:19:11 +02:00
|
|
|
const parseLocation = (ctx, l) => {
|
|
|
|
const {profile, opt} = ctx
|
|
|
|
|
2019-02-07 17:46:49 +01:00
|
|
|
const lid = parse(l.lid, {delimiter: '@'})
|
|
|
|
const res = {
|
|
|
|
type: 'location',
|
|
|
|
id: (l.extId || lid.L || '').replace(leadingZeros, '') || null
|
|
|
|
}
|
|
|
|
|
2017-12-11 19:25:29 +01:00
|
|
|
if (l.crd) {
|
|
|
|
res.latitude = l.crd.y / 1000000
|
|
|
|
res.longitude = l.crd.x / 1000000
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
2017-11-11 23:56:09 +01:00
|
|
|
|
2017-12-11 19:25:29 +01:00
|
|
|
if (l.type === STATION) {
|
2018-07-10 23:32:34 +02:00
|
|
|
const stop = {
|
|
|
|
type: l.isMainMast ? 'station' : 'stop',
|
2019-02-07 17:46:49 +01:00
|
|
|
id: res.id,
|
2020-02-27 15:49:41 +01:00
|
|
|
name: l.name || lid.O ? profile.parseStationName(ctx, l.name || lid.O) : null,
|
2019-10-28 17:42:33 +01:00
|
|
|
location: 'number' === typeof res.latitude ? res : null // todo: remove `.id`
|
2017-12-11 19:25:29 +01:00
|
|
|
}
|
2018-01-26 16:25:13 +01:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
if ('pCls' in l) stop.products = profile.parseProductsBitmask(ctx, l.pCls)
|
2019-06-30 13:21:25 +02:00
|
|
|
if ('meta' in l) stop.isMeta = !!l.meta
|
2018-01-26 16:25:13 +01:00
|
|
|
|
2019-08-23 16:06:21 +02:00
|
|
|
if (opt.linesOfStops && Array.isArray(l.lines)) {
|
|
|
|
stop.lines = l.lines
|
2018-01-26 16:25:13 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 00:28:47 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-10 23:32:34 +02:00
|
|
|
return stop
|
2017-12-11 19:25:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 19:53:26 +01:00
|
|
|
if (l.type === ADDRESS) res.address = l.name
|
2017-12-11 19:25:29 +01:00
|
|
|
else res.name = l.name
|
2019-02-07 17:46:49 +01:00
|
|
|
if (l.type === POI) res.poi = true
|
2017-11-11 23:56:09 +01:00
|
|
|
|
2017-11-12 01:23:34 +01:00
|
|
|
return res
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = parseLocation
|