ingestor-metrolink
Version:
Simple script to get Metrolink tram station locations from the NaPTAN national transport dataset.
46 lines (38 loc) • 1.38 kB
JavaScript
const { join } = require('path')
const { toJson } = require('xml2json')
const { readFile, writeFile } = require('fs')
const { ezThunk } = require('ez-promise')
const filename = join(__dirname + '/greater-manchester-transport-db.xml')
const read = ezThunk(readFile)
const write = ezThunk(writeFile)
module.exports = (outpath, inpath = filename) => read(inpath)
.then(buffer => buffer.toString('utf8'))
.then(toJson)
.then(JSON.parse)
.then(filterMetrolinkStationsOnly)
.then(extractMetrolinkLocationData)
.then(JSON.stringify)
.then(saveResult(outpath))
.then(onSuccess(outpath))
.catch(onError)
function filterMetrolinkStationsOnly ({ NaPTAN: { StopPoints: { StopPoint } } }) {
return StopPoint.filter(({ StopClassification: { StopType } }) => StopType === 'TMU')
}
function extractMetrolinkLocationData (stations) {
return stations.map(({ Status: status, Descriptor: { CommonName: name, Street: street }, Place: { Location: { Translation: { Longitude: lng, Latitude: lat } } } }) => ({
name, status, street, location: { lat, lng }
}))
}
function saveResult (outpath) {
return result => {
return write(outpath, result, 'utf8')
}
}
function onSuccess (outpath) {
return () => {
console.log('Metrolink JSON output saved to \'%s\'', outpath)
}
}
function onError (error) {
console.log('Error saving Metrolink JSON:\n%s', error)
}