How Choo predicts when a train will pass your bridge
There’s no public GPS feed for British trains. Here’s how Choo uses open rail data, track geometry and live signalling berths to time trains past any spot on the network.
If you’ve ever stood on a footbridge with a small child waiting for a train, you’ll know the experience of trying to figure out whether it’s worth waiting for the next one. Departure boards don’t help that much: they tell you when a train leaves a station, but maybe you’re not at a station. Maybe you’re on a bridge halfway between two, or on a footpath beside the line, or at the end of a platform where the fast trains run straight through.
I built Choo to answer that question. It’s an iOS app that monitors the railways in Great Britain. You put a pin where you’re standing, and it counts down to the next train past it. When it’s close, the countdown runs to the second.
The tricky part is that there’s no live GPS map of British trains. Operators don’t publish live positions, so nobody outside the railway can simply look up where a train is right now. What is published, openly, is the timetable, live reports of trains reaching and leaving places on it, and the signalling’s record of each train moving from one section of line to the next. Everything Choo shows is worked out from those.
This post explains how it all fits together. I’m focusing on National Rail trains; Choo also covers the Tube, Overground, DLR and Elizabeth line from Transport for London’s data, but that’s a story for another time.
1. A train that stops at both ends
Let’s start with the easiest case. You’re standing beside the line somewhere between two stations, and a train is going to stop at both of them:
Suppose the train leaves Tilehurst at 11:15:00 and arrives at Pangbourne at 11:17:00. If you’re a quarter of the way along, a sensible first guess is a quarter of the way through the journey: 11:15:30. Everything else in this post is about refining this guess, and making it more accurate.
Distance along the track
How to measure distance between the stations? As the crow flies is no good: railways curve, and a spot on the outside of a bend can look much nearer one station than it really is along the line.
So first we need the shape of the railway. That comes from OpenStreetMap, whose volunteer-mapped railway lines are detailed and well kept. Choo uses about 32,000 km of them across Great Britain: the main lines, freight branches, the Tube and light rail. With those, “how far along” can be measured along the rails.
Trains don’t go at a steady speed
A quarter-of-the-way guess assumes a constant speed. In reality, a train leaving Tilehurst is still picking up speed as it passes a spot just outside the station, and one approaching Pangbourne is braking. A steady-speed guess will be too early just after Tilehurst, and too late just before Pangbourne.
Choo models the run the way trains typically go: pulling away, speeding up to line speed, then braking for the next stop. Different trains have different figures: an electric unit accelerates faster than a diesel, and a heavy freight train much more slowly than either. The running time between the two stops still comes from the timetable; the model just decides how that time is spread along the way.
OpenStreetMap also records speed limits for most of Great Britain’s main lines, which Choo factors into the calculations.
2. Knowing when trains leave and arrive
Step 1 assumes we already know when the train leaves one station and reaches the next, but where do we get that data from? There are several open datasets we can use:
- Darwin, from National Rail Enquiries, holds each day’s passenger trains and powers station departure boards.
- Network Rail’s SCHEDULE has every train: passenger, freight, engineering. That’s about 730,000 schedules, plus short-notice trains (mostly freight) planned on the day.
Because trains don’t always run to time, Choo also listens to two live feeds: Darwin (for forecasts, platforms and cancellations) and Network Rail’s TRUST (which reports when each train actually arrives, leaves or passes each place in its timetable).
Both feeds give times to the minute, but we can do better than that. To get to the second, we need the signalling.
3. Non-stop trains and missing stations
Not every train stops at both stations. Many trains run straight through. Luckily, schedules don’t just list stops, they also list timing points: stations and other points that a train passes. A pass time works just as well as a departure time.
Choo looks at every leg of a train’s journey, from one timing point to the next, and checks whether it runs past your spot. But this doesn’t solve everything: a train’s timetable doesn’t list every station it goes through. A fast train can have long gaps with no timing points listed in its timetable, meaning our estimate gets less accurate.
4. The signalling
To get times to the second, Choo uses the railway’s signalling. I will confess, until I made Choo, I didn’t really understand what signalling was, other than something that failed regularly on certain Tube lines. So here’s a primer.
Trains take a very long time to stop, and a driver can’t rely on seeing the train in front, so the railway keeps trains apart by never letting two of them onto the same stretch of line.
A main line is divided into sections, each with a signal at its entrance. Only one train is allowed in a section at a time. Equipment in the track detects which sections have a train in them, and the signal behind each train turns red, so the next train waits until it has moved on.
Because stopping takes so long, a driver needs to know about a red signal well before reaching it. Many British main lines therefore use four-aspect signals, which give two signals’ warning:
- Red: the next section is occupied. Stop.
- Single yellow: the next signal is red, so be ready to stop.
- Double yellow: the next signal is a single yellow.
- Green: clear for at least three sections.
Behind each train is a trail of red, yellow, double yellow, then green.
Signallers watch all this on a diagram of the track, with each train shown as a label in the section it’s in. The label is the train’s four-character “train reporting code” (more commonly called its headcode), such as 2U44.
The labels are kept up to date by the train describer signalling system. It keeps a numbered slot for each section of line, called a berth, and stores the headcode of each train in each berth. When a train passes a signal and moves into the next section, the describer moves the train’s headcode on to the next berth.
What Choo gets from it
Network Rail publishes every one of those steps as open data, in its TD feed, to the second. A message looks like this:
[{
"CA_MSG": {
"area_id": "D1",
"from": "1758",
"to": "1752",
"descr": "2U44",
"time": "1793186020000"
}
}]
It says that in signalling area D1, train 2U44 has just moved from berth 1758 to berth 1752.
You can watch it happen live on OpenTrainTimes, which shows diagrams much like the signallers’ own from the same feed.
Where are the berths?
There’s a slight problem though. The message doesn’t say where berth 1752 is. A berth is just a number, and there’s no complete dataset of the berth locations. Volunteers on OpenStreetMap have mapped some of the signals, which Choo can match to berths, but coverage is far from complete.
But we do know where some of them are: Network Rail’s SMART data documents which berth steps mean a train has reached or left a timing point. So by using this lookup, Choo can see, in real time and to the second, when a train reaches or leaves most timing points.
Timing points can be many minutes apart. In between, the train describer still reports every step from berth to berth. We don’t know where those berths are, but it is still useful data: we can watch the data and record how long trains usually take to get from each berth to the next station.
Every time a train is sighted at a timing point, the Choo server looks back at the berths it entered over the previous ten minutes, and notes how long it took from each one (separately for stopping and non-stop trains). Over time, the server learns the typical patterns for the berths.
This lets us forecast when a train will pass its upcoming timing points.
Extra checkpoints for fast trains
Remember the fast train with long gaps in its timetable, from Step 3? The signalling still sees it pass the stations in between, so Choo can use them as extra checkpoints.
These timings also help Choo refine an approximation of where the signals sit along the track (with help from OpenStreetMap where volunteers have mapped them). That adjusts the speed model from Step 1 to match how trains really run, and in a train’s last few minutes, the countdown is timed from the last signal it passed before reaching you.
5. What kind of train?
Knowing when a train is coming is half the fun. The other half is knowing what it is. Using the PTAC feed, Choo shows which units make up each train, with a photo from Wikipedia, its spotter nickname, the unit numbers and how many carriages it has.
Special trains
The headcode from Step 4 also picks out the trains worth looking out for. Its first digit marks freight (4, 6, 7 and 8) and lone locomotives (0), and a 3 in autumn is usually a leaf-clearing train. A Z as the second character usually means a charter (sometimes a steam train!), and a Q a test train, such as the yellow ones that measure the track.
You can ask Choo to tell you when one is coming. The Choo server keeps watch over your saved spots and sends a notification before a special train passes, saying what it is and where it’s going, so you have time to get to your spot. I used this a couple of days after releasing Choo to see my first steam train passing under my local bridge!
Credits and licences
Choo’s predictions are built from open data:
- Darwin (Push Port and timetable): National Rail Enquiries, via the Rail Data Marketplace. Powered by National Rail Enquiries.
- Network Rail’s open data SCHEDULE, VSTP, TRUST, TD, SMART and CORPUS: contains information of Network Rail Infrastructure Limited, licensed under the Open Government Licence v3.0.
- NaPTAN: Department for Transport, Open Government Licence v3.0.
- BPLAN and PTAC: via the Rail Data Marketplace, under its terms.
- TfL Unified API: Powered by TfL Open Data. Contains OS data © Crown copyright and database rights 2016, and Geomni UK Map data © and database rights 2019.
- OpenStreetMap: © OpenStreetMap contributors, under the Open Database Licence (ODbL).
- Wikipedia: train photos and text, each under its own licence (text under CC BY-SA 4.0).
Thanks to the Open Rail Data community, whose wiki is invaluable.