2024-02-06 22:58:49 +01:00
|
|
|
import _googlePolyline from 'google-polyline';
|
|
|
|
import distance from 'gps-distance';
|
2018-05-16 21:07:05 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const {decode} = _googlePolyline;
|
2018-05-16 21:07:05 +02:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
// todo: what is p.delta?
|
|
|
|
// todo: what is p.type?
|
|
|
|
// todo: what is p.crdEncS?
|
|
|
|
// todo: what is p.crdEncF?
|
|
|
|
const parsePolyline = (ctx, p) => { // p = raw polyline
|
2024-02-06 22:58:49 +01:00
|
|
|
const points = decode(p.crdEncYX);
|
|
|
|
if (points.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-05-16 21:07:05 +02:00
|
|
|
|
2020-01-31 18:07:16 +01:00
|
|
|
const res = points.map(([lat, lon]) => ({
|
2019-10-20 00:19:11 +02:00
|
|
|
type: 'Feature',
|
|
|
|
properties: {},
|
|
|
|
geometry: {
|
|
|
|
type: 'Point',
|
2024-02-06 22:58:49 +01:00
|
|
|
coordinates: [lon, lat],
|
|
|
|
},
|
|
|
|
}));
|
2018-05-16 21:07:05 +02:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
if (Array.isArray(p.ppLocRefL)) {
|
|
|
|
for (let ref of p.ppLocRefL) {
|
2024-02-06 22:58:49 +01:00
|
|
|
const p = res[ref.ppIdx];
|
|
|
|
if (p && ref.location) {
|
|
|
|
p.properties = ref.location;
|
|
|
|
}
|
2018-05-16 21:07:05 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
// Often there is one more point right next to each point at a station.
|
|
|
|
// We filter them here if they are < 5m from each other.
|
|
|
|
for (let i = 1; i < res.length; i++) {
|
2024-02-06 22:58:49 +01:00
|
|
|
const p1 = res[i - 1].geometry.coordinates;
|
|
|
|
const p2 = res[i].geometry.coordinates;
|
|
|
|
const d = distance(p1[1], p1[0], p2[1], p2[0]);
|
|
|
|
if (d >= 0.005) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const l1 = Object.keys(res[i - 1].properties).length;
|
|
|
|
const l2 = Object.keys(res[i].properties).length;
|
|
|
|
if (l1 === 0 && l2 > 0) {
|
|
|
|
res.splice(i - 1, 1);
|
|
|
|
} else if (l2 === 0 && l1 > 0) {
|
|
|
|
res.splice(i, 1);
|
|
|
|
}
|
2018-05-16 21:07:05 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 00:19:11 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'FeatureCollection',
|
2024-02-06 22:58:49 +01:00
|
|
|
features: res,
|
|
|
|
};
|
|
|
|
};
|
2018-05-16 21:07:05 +02:00
|
|
|
|
2022-05-07 16:17:37 +02:00
|
|
|
export {
|
|
|
|
parsePolyline,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|