Can I calculate estimated time of arrival for a bus?


#1

Originally asked by Simon:

I’m a passenger on a bus, I want to know how long the bus will take to get where I’m going (and I know Countdown has a 30 minute limitation). I open my smartphone and into my bus app I type ‘ETA LG52XYJ’. The app’s backend, using the legacy URL would post something like:

http:///interfaces/ura/instant_V1?RegistrationNumber=LG52XYJ&ReturnList=LineName,LineID,VehicleID,RegistrationNumber,StopPointName,StopCode1,StopCode2,DestinationText,DestinationName,EstimatedTime

…and get back something like the below which the backend would then sort into either ‘EstimatedTime’ order (with the possibility of stops being slightly out of order) or Stop order (which it already knows & with the possibility of the arrival times being slightly wrong).

[4,”1.0″,1461777201555]
[1,”St Donatts Road”,”47876″,”490012652S”,”172″,”172″,”Brockley Rise”,”Brockley Rise”,16150,”LG52XYJ”,1461778771000]
[1,”Millmark Grove”,”47079″,”490009913S”,”172″,”172″,”Brockley Rise”,”Brockley Rise”,16150,”LG52XYJ”,1461778861000]
[1,”Peckham Park Road”,”51530″,”490010880EN”,”172″,”172″,”Brockley Rise”,”Brockley Rise”,16150,”LG52XYJ”,1461777740000]
[1,”New Cross Bus Garage”,”47366″,”490010204G”,”172″,”172″,”Brockley Rise”,”Brockley Rise”,16150,”LG52XYJ”,1461778279000]
[1,”Malt Street”,”71447″,”490009531E”,”172″,”172″,”Brockley Rise”,”Brockley Rise”,16150,”LG52XYJ”,1461777602000]
[1,”Old Kent Road / Ilderton Road”,”51870″,”490008461E2″,”172″,”172″,”Brockley Rise”,”Brockley Rise”,16150,”LG52XYJ”,1461778059000]
[1,”Dunton Road”,”72792″,”490006266EC”,”172″,”172″,”Brockley Rise”,”Brockley Rise”,16150,”LG52XYJ”,1461777429000]
[1,”Briant Street”,”87898″,”490003246F”,”172″,”172″,”Brockley Rise”,”Brockley Rise”,16150,”LG52XYJ”,1461778146000]

Now, using the unified API, the app’s backend would post https://api.tfl.gov.uk/Mode/bus/Arrivals?count=1 (with credentials) and get back far more data (9MB at 19:00 as I type). It would then have to filter that to find only the bus our user is interested in, sort it and then present it back to the user.

If the Unified API could do something like https://api.tfl.gov.uk/Eta/Bus/vehicleId/LG52XYJ or maybe https://api.tfl.gov.uk/Eta/Tram/vehicleId/2550 if we want to check a tram’s ETA. (Are vehicleIDs unique across modes in the unified API? If so, might rule out the need for /tram/ or /bus/.)Alternatively, https://api.tfl.gov.uk/Mode/bus/Arrivals?vehicleId=LG52XYJ.

I did consider searching for vehicleId under ‘Line’ or ‘StopPoint’ but then we’d need to know what route the vehicle was on before we could search for it.

The output I would hope to see from a query like this would be like the return from https://api.tfl.gov.uk/line/172/arrivals or https://api.tfl.gov.uk/Mode/bus/Arrivals but restricted to the vehicleId we’ve searched for and maybe slimmed down… do I need to know the bearing of the stop, the ‘towards’ information (e.g. “Towards Waterloo, Trafalgar Square or Holborn”) or the direction (inbound/outbound)?

BTW, Are there any plans to remove the legacy Countdown feed any time soon? I hope not – It seems far more flexible in what you can ask it and ask it to present on return (i.e. don’t give me the information I don’t need) than what you can do with the unified API.


#2

Dan Mewett replied:

Hi Simon,

Thanks for providing a very detailed description of your use case and expectations for an arrivals end-point based on vehicleid. I can see that such an interface would provide apps the ability to give customers really useful information on their journey, especially if you are trying to meet someone or catch a transport connection further down the route. On our “Arrivals of things” blog post, other users are also asking for the same functionality, and according to @Tim (see the comments section) we have already got a version of this in our development pipeline, so you can expect something to show up in the API for this very soon and we will let you know when that happens.

Here is a link to the blog article: https://blog.tfl.gov.uk/2015/12/07/unified-api-part-5-aot-arrivals-of-things/

There are plans to make some changes to the web and mobile interfaces of Countdown in the near future (June) but currently no plans to change access or outputs of the existing Countdown API.


#3

The Vehicle Arrivals API is now available, so using a vehicleId (which matches the registration mark for buses at least) you can retrieve the arrival times of the vehicle for the remainder of its route.

The API is structured as /Vehicle/{vehicleId}/Arrivals

For example:

$ curl "https://api.tfl.gov.uk/StopPoint/490006170N1/Arrivals" -s | jq ".[] | .vehicleId"
"LTZ1573"
"LTZ1577"
"LTZ1581"
"LTZ1582"
"LTZ1591"
"LTZ1592"
"LTZ1601"
"LJ09SUA"
"LJ09SUF"

Let’s try the first vehicleId…

$> curl "https://api.tfl.gov.uk/Vehicle/LTZ1573/Arrivals" -s | jq '.[] | .stationName, .expectedArrival'
"Hoxton Station  / Geffrye Museum"
"2017-01-26T09:44:42.6013107Z"
"Pearson Street"
"2017-01-26T09:46:03.6013107Z"
"St Leonard's Hospital"
"2017-01-26T09:46:53.6013107Z"
"Laburnum Street"
"2017-01-26T09:47:43.6013107Z"
"Haggerston Station"
"2017-01-26T09:48:08.6013107Z"
"Middleton Road"
"2017-01-26T09:50:37.6013107Z"
"Kingsland High Street / Dalston Jct"
"2017-01-26T09:51:45.6013107Z"
"Dalston Junction Station"
"2017-01-26T09:51:57.6013107Z"
"Dalston Kingsland Station"
"2017-01-26T09:53:51.6013107Z"
"Rio Cinema"
"2017-01-26T09:55:50.6013107Z"
...etc

Voilá - the 149 bus going North up Kingsland Road as expected


#4

Oh, that’s a helpful endpoint! Previously I’d been doing this by requesting arrivals data for all the stops and then matching them up based on vehicleId. This worked okay for smallish modes (eg tube or river), but was impractical to do for a larger network (like buses). This lets me get the data on demand, rather than having to hold it all in memory. :grinning: