Home

Awesome

hafas-osm-line-colours

Match legs or departures/arrivals found with hafas-client with OpenStreetMap transit line colours from datasets generated using osm-transit-lines.

npm version Build Status Greenkeeper badge dependency status license chat on gitter

Installation

npm install hafas-osm-line-colours

Usage

// the actual methods are wrapped in this creator method because we only want the search tree to be created once, not every time we actually search for a line
// takes a couple of seconds to create the client, depending on the size of your line dataset, but can then handle up to 250.000 queries per second
// note that the data will be stored in-memory, so double-check your hardware before loading a dataset that covers the entire planet
const createTransitLineColourClient = require('hafas-osm-line-colours')
const hafas = require('db-hafas')('user-agent')
const osmTransitLines = require('osm-transit-lines')

const main = async () => {
	const berlinBbox = { south: 52.3418234221, north: 52.6697240587, west: 13.0882097323, east: 13.7606105539 }
	const berlinTransitLines = await osmTransitLines(berlinBbox, { wikidata: true }) // see `osm-transit-lines` docs

	const { legLineColour, departureOrArrivalLineColour } = createTransitLineColourClient(berlinTransitLines) // methods exposed by this module

	// departures/arrivals
	const virchowKlinikumBerlin = '000730855'
	const [departure] = await hafas.departures(virchowKlinikumBerlin) // next train is a tram 50
	const departureLineColour = departureOrArrivalLineColour(departure) // { backgroundColour: '#36ab94', textColour: null } (colour of tram 50), null if no matching background colour was found
	const [arrival] = await hafas.arrivals(virchowKlinikumBerlin) // next train is a tram M13
	const arrivalLineColour = departureOrArrivalLineColour(arrival) // { backgroundColour: '#00cc00', textColour: null } (colour of tram M13), null if no matching background colour was found

	const zehlendorf = '008089098'
	const mexikoplatz = '008089023'
	const [journey] = await hafas.journeys(zehlendorf, mexikoplatz) // only leg is an S1 train
	const lineColour = legLineColour(journey.legs[0]) // { backgroundColour: '#d474ae', textColour: null } (colour of S1), null if no matching background colour was found
}

Since the lines list returned by osm-transit-lines tends to be quite large, the module also exposes two helper methods that allow you to transform the lines list into a more memory-efficient array (that can also be stored) from which you can create a line colour client as well:

const { linesToPoints, createLineColourClientFromPoints } = require('hafas-osm-line-colours')

const main = async () => {
	const berlinBbox = { south: 52.3418234221, north: 52.6697240587, west: 13.0882097323, east: 13.7606105539 }
	const berlinTransitLines = await osmTransitLines(berlinBbox, { wikidata: true }) // see `osm-transit-lines` docs

	const berlinTransitLinePoints = linesToPoints(berlinTransitLines)
	const { legLineColour, departureOrArrivalLineColour } = createLineColourClientFromPoints(berlinTransitLines)
}

Contributing

If you found a bug or want to propose a feature, feel free to visit the issues page.