db-vendo-client/parse/location.js

125 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-11-11 22:35:41 +01:00
'use strict'
2019-02-05 19:07:19 +01:00
const {parse} = require('qs')
const get = require('lodash/get')
2019-02-05 19:07:19 +01:00
const POI = 'P'
const STATION = 'S'
const ADDRESS = 'A'
2017-11-11 22:35:41 +01:00
const leadingZeros = /^0+/
2020-03-18 21:35:43 +01:00
// todo: what is l.wt? is it "weight"?
// - `6733` for 8013074 with p/vmt
// - `3933` for 8012092 with p/vmt
// - `2062` for 8010168 with p/vmt
const parseLocation = (ctx, l) => {
const {profile, opt} = ctx
const lid = parse(l.lid, {delimiter: '@'})
const res = {
type: 'location',
id: (l.extId || lid.L || '').replace(leadingZeros, '') || null
}
if (l.crd) {
res.latitude = l.crd.y / 1000000
res.longitude = l.crd.x / 1000000
} else if (('X' in lid) && ('Y' in lid)) {
res.latitude = lid.Y / 1000000
res.longitude = lid.X / 1000000
2017-11-11 22:35:41 +01:00
}
if (l.type === STATION) {
// todo: https://github.com/public-transport/hafas-client/issues/151
const locL = get(ctx.res, ['common', 'locL'], [])
const mMastLocX = 'mMastLocX' in l ? l.mMastLocX : NaN
const subStops = (l.stopLocL || [])
.filter(locX => locX !== mMastLocX)
.map(locX => locL[locX])
.filter(s => !!s)
.map(s => profile.parseLocation(ctx, s))
.filter(stop => !!stop)
2018-07-10 23:32:34 +02:00
const stop = {
type: l.isMainMast || subStops.length > 0 ? 'station' : 'stop',
id: res.id,
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`
}
if (opt.subStops && subStops.length > 0) stop.stops = subStops
2018-01-26 16:25:13 +01: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
if (opt.entrances) {
const entrances = (l.entryLocL || [])
.map(locX => locL[locX])
.filter(l => !!l)
.map(l => profile.parseLocation(ctx, l))
.filter(loc => !!loc)
.map(loc => loc.location)
if (entrances.length > 0) stop.entrances = entrances
}
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
}
const locHints = (l.remarkRefs || [])
.filter(ref => !!ref.hint && Array.isArray(ref.tagL))
.filter(({tagL}) => (
tagL.includes('RES_LOC') ||
tagL.find(t => t.slice(0, 8) === 'RES_LOC_') // e.g. `RES_LOC_H3`
))
.map(ref => ref.hint)
const hints = [
...(l.hints || []),
...locHints,
]
const byType = type => hints.find(h => h.type === type)
const transitAuthority = (byType('transit-authority') || {}).text
if (transitAuthority) stop.transitAuthority = transitAuthority
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)]
})
.filter(([src]) => src !== 'NULL')
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:53:26 +01:00
if (l.type === ADDRESS) res.address = l.name
else res.name = l.name
if (l.type === POI) res.poi = true
2017-11-12 01:23:34 +01:00
return res
2017-11-11 22:35:41 +01:00
}
// We use a "visitied list" to prevent endless recursion.
const seen = Symbol('parseLocation seen items')
const parseLocationWithoutCycles = (ctx, l, ...args) => {
if (ctx[seen] && ctx[seen].includes(l)) return null
const newSeen = ctx[seen] ? [...ctx[seen], l] : [l]
return parseLocation({...ctx, [seen]: newSeen}, l, ...args)
}
module.exports = parseLocationWithoutCycles