3.3.0 • Published 4 years ago
find-hafas-leg-in-another-hafas v3.3.0
find-hafas-leg-in-another-hafas
Find a journey leg from one HAFAS endpoint in the data of another HAFAS endpoint.
Public transport providers in Europe often have data about vehicles run by other companies, but almost always it is outdated or imprecise. Consider these examples:
Let's always get the data about a vehicles from the company that actually run it! Given a leg
from an endpoint A
, you can use this library to fetch more up-to-date data about it from another endpoint B
.
Why linked open transit data? explains more about this idea.
Installation
npm install find-hafas-leg-in-another-hafas
Usage
const createDbHafas = require('db-hafas')
const createVbbHafas = require('vbb-hafas')
const createFindLeg = require('find-hafas-leg-in-another-hafas')
const createMergeLegs = require('find-hafas-leg-in-another-hafas/merge')
// Note that, for legs to be matched reliably, you need a more
// sophisticated normalization function. Use e.g.
// - https://github.com/derhuerst/tokenize-db-station-name
// - https://github.com/derhuerst/tokenize-vbb-station-name
const normalizeName = name => str.toLowerCase().replace(/\s/g, '')
// These names should be URL-safe & stable, they will used to compute
// IDs to be matched against other IDs.
const dbName = 'db'
const db = createDbHafas('find-db-hafas-leg-in-another-hafas example')
const vbbName = 'vbb'
const vbb = createVbbHafas('find-db-hafas-leg-in-another-hafas example')
const findLegInAnother = createFindLeg({
clientName: dbName,
hafas: db,
normalizeStopName: normalizeName,
normalizeLineName: normalizeName
}, {
clientName: vbbName,
hafas: vbb,
normalizeStopName: normalizeName,
normalizeLineName: normalizeName
})
const mergeLegs = createMergeLegs({
clientName: dbName,
normalizeStopName: normalizeName
}, {
clientName: vbbName,
normalizeStopName: normalizeName
})
const potsdamerPlatz = '8011118'
const südkreuz = '8011113'
const res = await db.journeys(potsdamerPlatz, südkreuz, {
results: 1, stopovers: true, tickets: false
})
const [journey] = res.journeys
const dbLeg = journey.legs.find(leg => leg.line) // find non-walking leg
console.log('DB leg', dbLeg)
const vbbLeg = findLegInAnother(dbLeg)
console.log('equivalent VBB leg', leg)
const mergedLeg = mergeLegs(dbLeg, vbbLeg)
console.log('mergedLeg', mergedLeg)
Related
- Why linked open transit data?
pan-european-routing
– Get public transport data across Europe, using multiple HAFAS endpoints.stable-public-transport-ids
– Get stable IDs for public transport stations, etc.
Contributing
If you have a question or need support using find-hafas-leg-in-another-hafas
, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, refer to the issues page.