DEV Community

Cover image for Timing Porto's buses when the timetable is wrong
Oh Life! Studios
Oh Life! Studios

Posted on

Timing Porto's buses when the timetable is wrong

I'm Dan, a Brit moving to Porto, and I've been building Portudo, a free map of the city's buses, Metro and trains: https://portudo.app/go/devto

Portudo on two phones: the map of Porto with its buses, and a journey from Aliados to Casa da Música

This post is about the hardest part so far: saying when a bus will really come, when the timetable is wrong and the bus doesn't say.

The data Porto gives away

Porto is generous with its transport data. Porto Digital runs two public services, open to anyone, with no keys:

  • an OpenTripPlanner with a GraphQL API, for stops, lines, departures, journeys and fares across STCP's buses, the Metro and CP's trains;
  • a live GTFS-Realtime stream of STCP's bus positions, over MQTT on a WebSocket.

Portudo is React, TypeScript and MapLibre, with OpenFreeMap tiles, on a Cloudflare Worker. It reads the live stream with a small MQTT client of its own that takes only the fields it uses.

Most buses know when they'll come. Some don't.

STCP sends the planner a live time for most of its buses. On the day I traced it, 86 of the 100 buses on the road had one, each within about 12 seconds.

For the rest, the planner gives the timetable's time, however late the bus is. And once that time has passed, the run drops off the stop's list. So a bus ten minutes behind in traffic disappears from your stop while it's still on its way, and you're told the next one is in twenty minutes when the late one is two streets away.

But every one of those buses is in the live stream. We know where it is, just not when it will reach you.

Where a bus is says how late it is

If you know where a bus is on its run, and when the timetable had it there, you know how late it is. Add that to the timetable's times for the stops ahead and you have live times for buses that send none.

Two things make it harder than it sounds. STCP's buses don't wait for their times, so once a bus is past its second stop it can be early as well as late. And a run can pass the same place twice, out and back along one street, so a position can mean two moments. A bus only counts as past your stop if it's past from every place on its run where it could be; otherwise it's timed from where it surely is.

On 14 real runs, the times worked out from where each bus was came within 13 seconds of STCP's own live times for the same buses. The timetable was two minutes out.

Straight lines lie

My first version measured a bus's progress along straight lines between its stops. Streets aren't straight. A 305 that had gone round the corner from Carmo, 123 metres on along the street, was only 25 metres along the straight line, so it still showed as due. A 504 waiting at the Rotunda da Boavista was as near the stretch after its stop as the one before, and dropped off Boavista Cemitério's list a minute before it came.

So now each bus is measured along its line on the real roads. The buses' reports sit close to those lines: half within 2.2 metres, 99% within 16. Replaying seven recorded runs from one day, 136,732 times the app looked at a bus for a stop, the roads removed 139 looks at buses that had already gone, removed none that were still to come, and gave 156 more a time from their bus.

One more fix: the planner forgets late buses. Portudo also asks for the runs due in the last 40 minutes, so a bus whose time has gone stays on the list, timed by where it is, until it comes. One morning's traffic had buses running 21 to 31 minutes late.

An idea that didn't survive real buses

A bus that hasn't started its run yet is usually finishing the one before, and STCP's trip ids name the duty. So I tried timing the next run from that bus: as late at the end of its current trip as it is now, plus two and a half minutes to turn round.

Replayed on recorded runs, it looked worth it. Then I checked it against the next hour of real buses: for those runs, the bus came more than a minute before the time shown 52% of the time. Buses make up time at the end of a run and turn round quicker than that. With no turnaround and half the lateness it was still a minute early 11% of the time; with a quarter, it was no better than the timetable. It came out.

Making buses glide, not jump

STCP's reports arrive about every 10 seconds, but the GPS fixes in them are 6 to 20 seconds apart and 10 to 30 seconds old. Draw each fix as it lands and the buses stutter and jump.

So Portudo plays each bus back about 35 seconds behind real time, like a video with a buffer. Between two fixes it moves at the speed it really went, eases into speed changes, and slows gently if the next fix is late. It travels along its line on the roads, so it stays on the street you see. On an 8-minute recording of the live feed, that cut stop-starts from 1,143 to 87, and sudden jumps in speed from 6,265 to none.

Drawing costs too, especially on phones. The buses are drawn again only as often as one near the screen could move a tenth of a pixel: every frame from a street to most of the city, five times a second zoomed right out. When every bus in view is waiting at a stop or a light, the loop rests until the first is due to set off, which it knows because it plays back behind the reports.

No accounts

There's no sign-up. Saved stops, lines and places live in your browser's own storage, and where a browser can promise to keep them without a pop-up, Portudo asks it to. Open tabs take turns writing, so two can't overwrite each other, and every release keeps reading the older formats, so nothing saved is lost in an update.

Try it

Portudo is free, in English and Portuguese, and opens in any phone's browser: https://portudo.app/go/devto

The buses are the live part; Metro and CP trains show their timetables. If you've timed buses better in your own city, I'd love to hear how.

Written with AI help: I designed Portudo and built it with AI coding tools, and an AI assistant helped me write this post. The transport data is Porto Digital's open data.

Top comments (2)

Collapse
 
userxnom profile image
Abdullah Naseer •

The 13-second check on 14 real runs is the detail that makes this convincing — most live-map posts never validate against the operator's own times. And the "straight lines lie" fix is exactly the kind of thing you only learn by shipping: measuring progress along the real road instead of the crow-flies line between stops.

One question: the estimate works from where the bus is versus where the timetable had it — but STCP buses don't wait for their times, so a bus can be early as well as late. Does the position-based estimate hold up for early buses too, or does it get less reliable when a bus is running ahead of schedule?

Collapse
 
ohlifestudios profile image
Oh Life! Studios •

Thanks, Abdullah, good question. It does work for early buses, with one exception. Once a bus is past the second stop of its run, being ahead of where the timetable had it means it's early, and its stops ahead move earlier by the same amount. Before that, a bus ahead of its time is one waiting at its first stop to leave on time, so there it only ever counts as late.

Counting early buses made a real difference. Over an hour of checks (5,830 looks at stops), timing buses as never early had them come more than a minute before the time shown in 31% of looks. Letting them be early brought that down to 21%, the same as STCP's own live times. One 200 reached Cordoaria four minutes ahead of its timetable.