public-transport / hafas-client

JavaScript client for HAFAS public transport APIs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Filter lines by closest position from defined location

raikm opened this issue · comments

commented

Hi Jannis,

is there any workaround already existing that I can only show the nearest line dep. from multiple station requests?

Use Case: I would like to display a live tracking dashboard of the nearest departures near my home and if I have for example "U Eberswalderstr." and "Zionskirchplatz" as requested stations and I get from both the M1, I would like to see only the M1 Depature time near my home.

Thanks in advance!

Hey,

I don't fully understand your use case yet. Do you want to query departures at multiple stations, but remove duplicate "runs"/trips?

commented

Yes! I want to remove duplicate "runs"/trips according to the distance of a location (long, lat). So if I live for example at Zionskirchplatz but I request data from Zionskirchplatz and Eberswalderstr I would like to see the M1 only from Zionskirchplatz.

But you don't want to identify all M1 trips as duplicates, you don't just want to remove the exact same trip?

commented

Yes, I don't want to see for example M1 in 3min (= Zionskirchplatz) AND M1 in 5min (Eberswalderstraße). I just want to see M1 in 3min (= Zionskirchplatz) since Zionskirchplatz is near my home.

The Dashboard should show me all trips near my home in the next 10min for example from different stations near my home and it would be confusing if there is a M1 in 3 and 5 minutes.

I hope the description is not to confusing :D

You can deduplicate using .tripId. With the BVG profile, you could do it like this:

const createClient = require('hafas-client')
const bvgProfile = require('hafas-client/p/bvg')
const uniqBy = require('lodash/uniqBy')
const sortBy = require('lodash/sortBy')

const uEberswalderStr = '900000110006'
const zionskirchplatz = '900000100042'

const client = createClient(bvgProfile, 'raikm-departure-board')

const rawDeps = [
	...(await client.departures(uEberswalderStr)),
	...(await client.departures(zionskirchplatz)),
]
const deduplicatedDeps = uniqBy(rawDeps, dep => dep.tripId)
const departures = sortBy(deduplicatedDeps, dep => Date.parse(dep.cancelled ? dep.plannedWhen : dep.when))

If you want more departures than the default (10 minutes), you can

  • increase the limit by passing options to client.departures(), or
  • use hafas-collect-departures-at to collect until your logic decides to stop.

Does that answer you question? Please re-open if it doesn't!