public-transport / hafas-client

JavaScript client for HAFAS public transport APIs.
ISC License
277 stars 53 forks source link

Filter lines by closest position from defined location #234

Closed raikm closed 3 years ago

raikm commented 3 years ago

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!

derhuerst commented 3 years ago

Hey,

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

raikm commented 3 years ago

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.

derhuerst commented 3 years ago

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

raikm commented 3 years ago

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

derhuerst commented 3 years ago

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


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