3.3.0 • Published 4 years ago

find-hafas-leg-in-another-hafas v3.3.0

Weekly downloads
4
License
ISC
Repository
github
Last release
4 years ago

find-hafas-leg-in-another-hafas

Find a journey leg from one HAFAS endpoint in the data of another HAFAS endpoint.

npm version build status ISC-licensed chat with me on Gitter support me on Patreon

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:

sncf.com showing TGV 6631 bahn.de showing TGV 6631

bahn.de showing IC 2029 sncf.com showing IC 2029

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

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.