Skip to content

Instantly share code, notes, and snippets.

@NTag
Last active November 27, 2019 11:09
Show Gist options
  • Save NTag/7e0b7ae017ca01b51d11a2b12b5c30f2 to your computer and use it in GitHub Desktop.
Save NTag/7e0b7ae017ca01b51d11a2b12b5c30f2 to your computer and use it in GitHub Desktop.
Simple script example to create a GeoJSON of your Trainline trips using Raildar.
// npm install request request-promise
// ./data.json is Trainline Europe data export (ask the support)
// node trainline-data-to-geojson.js > trips.geojson
const fs = require('fs');
const rp = require('request-promise');
const computeGeojson = async () => {
const data = JSON.parse(fs.readFileSync('./data.json'), { encoding: 'utf8' });
const legs = data.pnrs.filter(pnr => pnr.status === 'emitted').map(pnr => pnr.legs.map(leg => ({
dep: leg.departure.public_id,
arr: leg.arrival.public_id,
trip: `${leg.departure.name} ❯ ${leg.arrival.name}`,
date: leg.departure_date,
}))).flat();
// const legsFiltered = legs.slice(0, 20);
const legsFiltered = legs;
const features = [];
let i = 0;
for (const leg of legsFiltered) {
if (i++ % 10 == 0) {
process.stderr.write(`Processing ${i}/${legsFiltered.length}\n`);
}
const { dep, arr, ...properties } = leg;
try {
const feature = await rp(`https://trainmap.ntag.fr/api/route?simplify=1&dep=${dep}&arr=${arr}`, { json: true, timeout: 10000 });
feature.properties = properties;
features.push(feature);
} catch (e) {
process.stderr.write(`Error with ${dep} ❯ ${arr} (${properties.trip}): ${e}\n`);
}
}
const geojson = {
"type": "FeatureCollection",
features,
};
return JSON.stringify(geojson);
};
computeGeojson().then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment