Many public transit systems publish real-time GTFS data, but vehicle co-ordinates are often missing (especially for underground trains that can’t log GPS positions). For example, New York’s MTA feeds contain VehiclePosition messages with only stop ID and status.

A common workaround for live transit maps is to interpolate vehicle positions in the UI based on arrival times. This got me thinking: what if we had a server that polls upstream feeds, calculates vehicle co-ordinates, and generates valid GTFS-RT responses with estimated latitude and longitude?

Map of lower Manhattan with live train positions

I created gtfs-position-interpolator: a lightweight Go API with a single /positions endpoint. The demo UI visualises live train positions on the NYC Subway.

Interpo-what?

Interpolation is just a fancy word for estimating missing values between known data points. If we know when a train left stop A, when it’s expected to arrive at stop B, and the distance between stops A and B, then we can make an informed prediction about its current position.

Static GTFS schedules typically include route polylines and station co-ordinates. GTFS-RT feeds provide predicted arrival times and vehicle status. It’s already possible to interpolate vehicle positions. This project simply enables clients to consume them as if they were provided directly in the first-party feed.

How it works

The server interpolates on a per-vehicle basis. When a client is actively polling /positions, the server in turn polls the upstream transit system protobuf feeds every 5 seconds. On the MTA Subway, there’s a new snapshot at least every 15 seconds (more often for the 1–7 lines) and we want to pick that up as soon as possible.

Static GTFS CSVs: daily TripUpdate feeds: every 5 seconds stops.txt stop_id, stop_lat, stop_lon trips.txt trip_id, route_id, shape_id shapes.txt shape_id, shape_pt_sequence, shape_pt_lat, shape_pt_lon stop_times.txt trip_id, stop_id, stop_sequence, arrival_time, departure_time TripDescriptor trip_id, start_date, route_id StopTimeUpdate stop_id, stop_sequence, arrival.time, departure.time VehiclePosition, if any stop_id, current_status, timestamp Route data Shapes, stop co-ordinates,median run time between stops Trip and vehicle data Last stop, next stop, ETA Tracker Calculate per-vehicle positionand speed by comparing updates /positions VehiclePosition feed:lat/lon, bearing, stop_id,current_status

My first idea was for /positions to be a pure function of the upstream feed, but it turned out to be necessary to keep some state in the tracker:

  1. Departures: Each snapshot provides a vehicle’s next stop and ETA, but we also need to know when it leaves a stop.
  2. Previous snapshot: Comparing each snapshot to the previous one helps identify status changes and filter glitches (like a feed briefly reporting a vehicle back at a stop it just left).
  3. Prediction bias: Comparing predicted arrival times to when vehicles are reported at a stop gives us a per-feed offset to correct future ETAs.

Accelerate, cruise, brake

To make the movement more realistic than linear interpolation, I assume trains will spend 15 seconds accelerating to/braking from cruise speed. This seems plausible at least for NYC: the R160 Subway car accelerates at 2.5mph/s, so it would reach ~37mph in 15 seconds (its max speed is 55mph). In practice we don’t cap the cruise speed, so a train can sprint to catch up with the upstream feed when predictions are wrong.

Timed to reach stop B based on GTFS-RT prediction Arrival slips: ease to slower cruise Speed Time Ramp Ramp Prediction changes Predicted arrival

When the predicted arrival time for the next stop moves earlier or later, the cruise speed adjusts from the current estimated position to compensate. This avoids backwards motion and sudden jumps in position.

Accuracy

It’s hard to measure if the results are correct without standing on a station platform. Even that would only tell us each end of one leg was correctly timed. In most cases we simply have no reliable data about what a train is doing between stops.

If we trust the arrival times and vehicle statuses from the upstream feeds, this approach will at least reliably place each train on the correct segment of track, with a self-correcting position estimate.

Time Predicted position vs. upstream feed
82% Train positions are consistent with the new status.
15% The upstream feed says a train has reached a stop when we still have it pulling in.
3% We place a train at a stop before the feed says it’s arrived.
<1% We override the feed because it has briefly put a train back at a stop it just left.

I recorded 24 hours of NYC Subway GTFS-RT data that can be replayed to test changes to the algorithm. It ranges from ~110 active trains at 3:15am to 520 in the morning rush hour at 8:30am. The table above summarises position consistency across a full day.

Data wrangling

A curious finding was that the 1–7 and L trains have an “in transit” status that agrees with the upcoming stops in TripUpdate messages >98% of the time, while other lines keep returning “stopped at” status until trains arrive at the next stop. Previous stops linger in some feeds but not others. It’s likely there are two or three different underlying systems generating these feeds in slightly different ways.

The next step to tune location predictions is to capture a GTFS-RT feed with reliable GPS data and measure the delta between observed and predicted positions. However, we’ve seen that every transit system has its own data quirks, so it’s unclear if tweaks from these findings will generalise well.

Made at RC

This is one of many small projects I worked on at the Recurse Center. If you found this post interesting, check out my regular updates.

Want to become a better programmer? Join the Recurse Center!