Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.