2024-02-06 22:58:49 +01:00
|
|
|
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
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const leadingZeros = /^0+/;
|
2018-10-15 17:31:28 +02:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
const parseLocation = (ctx, l) => {
|
2024-12-18 00:08:52 +00:00
|
|
|
const {profile, common} = ctx;
|
2019-10-20 00:19:11 +02:00
|
|
|
|
2024-12-07 22:46:04 +00:00
|
|
|
if (!l) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-12-21 23:04:05 +00:00
|
|
|
const lid = parse(l.id || l.locationId, {delimiter: '@'});
|
2019-02-07 17:46:49 +01:00
|
|
|
const res = {
|
|
|
|
type: 'location',
|
2025-02-09 00:46:21 +01:00
|
|
|
id: (l.extId || l.evaNr || lid.L || l.evaNumber || l.evaNo || l.bahnhofsId || '').replace(leadingZeros, '') || null,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|
2024-12-17 19:41:00 +00:00
|
|
|
const name = l.name || lid.O;
|
2019-02-07 17:46:49 +01:00
|
|
|
|
2024-12-21 23:04:05 +00:00
|
|
|
if (l.lat && l.lon || l.coordinates || l.position) {
|
|
|
|
res.latitude = l.lat || l.coordinates?.latitude || l.position?.latitude;
|
|
|
|
res.longitude = l.lon || l.coordinates?.longitude || l.position?.longitude;
|
2024-02-06 22:58:49 +01:00
|
|
|
} 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
|
|
|
}
|
2017-11-11 23:56:09 +01:00
|
|
|
|
2025-01-03 10:57:24 +00:00
|
|
|
// addresses and pois might also have fake evaNr sometimes!
|
2025-02-09 00:46:21 +01:00
|
|
|
if (l.type === STATION || l.extId || l.evaNumber || l.evaNo || lid.A == '1' || l.bahnhofsId) {
|
2024-12-18 00:08:52 +00:00
|
|
|
let stop = {
|
|
|
|
type: 'station',
|
2019-02-07 17:46:49 +01:00
|
|
|
id: res.id,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|
2025-02-09 00:46:21 +01:00
|
|
|
if (name) {
|
|
|
|
stop.name = name;
|
|
|
|
}
|
2025-01-02 14:00:45 +00:00
|
|
|
if ('number' === typeof res.latitude) {
|
|
|
|
stop.location = res; // todo: remove `.id`
|
|
|
|
}
|
2024-12-07 16:16:31 +00:00
|
|
|
// TODO subStops
|
2020-02-15 19:13:43 +00:00
|
|
|
|
2024-12-07 16:16:31 +00:00
|
|
|
if ('products' in l) {
|
2024-12-21 15:26:49 +00:00
|
|
|
stop.products = profile.parseProducts(ctx, l.products);
|
2020-03-07 00:28:47 +01:00
|
|
|
}
|
|
|
|
|
2024-12-18 00:08:52 +00:00
|
|
|
if (common && common.locations && common.locations[stop.id]) {
|
|
|
|
delete stop.type;
|
|
|
|
stop = {
|
|
|
|
...common.locations[stop.id],
|
|
|
|
...stop,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-12-08 21:42:57 +00:00
|
|
|
// TODO isMeta
|
2024-12-18 00:08:52 +00:00
|
|
|
// TODO entrances, lines
|
2024-02-06 22:58:49 +01:00
|
|
|
return stop;
|
2017-12-11 19:25:29 +01:00
|
|
|
}
|
|
|
|
|
2025-02-09 00:46:21 +01:00
|
|
|
if (name && common?.locations?.[name] && res.id === null) {
|
|
|
|
delete res.type;
|
|
|
|
delete res.id;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...common.locations[name],
|
|
|
|
...res,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-12-17 19:41:00 +00:00
|
|
|
res.name = name;
|
2025-01-02 14:00:45 +00:00
|
|
|
if (l.type === ADDRESS || lid.A == '2') {
|
2024-12-17 19:41:00 +00:00
|
|
|
res.address = name;
|
2024-02-06 22:58:49 +01:00
|
|
|
}
|
2025-01-02 14:00:45 +00:00
|
|
|
if (l.type === POI || lid.A == '4') {
|
2024-02-06 22:58:49 +01:00
|
|
|
res.poi = true;
|
|
|
|
}
|
2017-11-11 23:56:09 +01:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
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,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|