db-vendo-client/parse/location.js

66 lines
1.2 KiB
JavaScript
Raw Normal View History

import {parse} from 'qs';
2019-02-05 19:07:19 +01:00
2024-12-07 16:16:31 +00:00
const POI = 'POI';
const STATION = 'ST';
const ADDRESS = 'ADR';
2017-11-11 22:35:41 +01:00
const leadingZeros = /^0+/;
const parseLocation = (ctx, l) => {
2024-12-08 21:42:57 +00:00
const {profile} = ctx;
2024-12-07 22:46:04 +00:00
if (!l) {
return null;
}
2024-12-07 16:16:31 +00:00
const lid = parse(l.id, {delimiter: '@'});
const res = {
type: 'location',
2024-12-10 17:51:20 +00:00
id: (l.extId || lid.L || l.evaNumber || l.evaNo || '').replace(leadingZeros, '') || null,
};
2024-12-07 16:16:31 +00:00
if (l.lat && l.lon) {
res.latitude = l.lat;
res.longitude = l.lon;
} 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
}
2024-12-10 17:51:20 +00:00
if (l.type === STATION || l.extId || l.evaNumber || l.evaNo) {
2018-07-10 23:32:34 +02:00
const stop = {
2024-12-07 16:16:31 +00:00
type: 'stop', // TODO station
id: res.id,
2024-12-07 16:16:31 +00:00
name: l.name,
location: 'number' === typeof res.latitude
? res
: null, // todo: remove `.id`
};
2024-12-07 16:16:31 +00:00
// TODO subStops
2024-12-07 16:16:31 +00:00
if ('products' in l) {
stop.products = profile.parseProductsBitmask(ctx, l.products);
}
2024-12-08 21:42:57 +00:00
// TODO isMeta
// TODO station
// TODO entrances, lines, transitAuthority, dhid, ids
return stop;
}
if (l.type === ADDRESS) {
res.address = l.name;
} else {
res.name = l.name;
}
if (l.type === POI) {
res.poi = true;
}
return res;
};
2017-11-11 22:35:41 +01:00
2022-05-07 16:17:37 +02:00
export {
2024-12-07 16:16:31 +00:00
parseLocation,
};