Skip to content
All work

Personal project · Open data · 2026

Bus Tracking

A live map of TransMilenio’s trunk and dual buses in Bogotá: pick a route and see where each of its buses is between stations. Built on the open GTFS and GTFS Realtime data the operator publishes.

The live map with route H20 selected: its path drawn in gold across Bogotá from Portal 80 to Portal Usme, a navy panel with the route code, destination and bus count, and the route strip along the bottom with its stations in order and each bus placed between two of them.
Bus Tracking architectureA Fastify API on Render polls the TransMilenio GTFS Realtime feed every 30 seconds, keeps only trunk buses, derives their heading and holds the snapshot in memory; the browser asks it for /vehicles every 15 seconds. The browser loads the static Next.js site and the route and station GeoJSON from Cloudflare Pages and the map tiles from OpenFreeMap. A daily GitHub Action downloads GTFS.zip, rebuilds the data and triggers new deploys of the API and the site.GTFS-RT · 30 s/vehicles · 15 stilesHTML · GeoJSONGTFS.zipredeployrebuildTransMilenioGTFS · GTFS-RTOpenFreeMapvector tilesAPIFastify · Rendertrunk only · heading · cacheBrowserNext.js · MapLibreGitHub Actionsdaily · 05:00 BogotáCloudflare Pagesstatic exportThe only server I runDaily data build
Architecture sketch — Simplified view of the deployed architecture. Scroll sideways to see the full diagram.

My role

Solo project, end to end: data preprocessing, API, interface design, map frontend, tests, CI and deployment.

Problem

TransMilenio publishes bus positions as a GTFS Realtime protobuf feed, but a browser cannot read it: the feed sends no CORS headers and answers with Cross-Origin-Resource-Policy: same-origin. It carries 4,000+ vehicles, most of them not trunk buses, and no heading or speed. Meanwhile the people who need it are passengers checking their phone on the way to the station, who want their route’s buses in seconds.

Approach

One small Fastify API is the only server: it polls the feed every 30 seconds, keeps the trunk and dual buses, derives each heading from consecutive positions and serves the snapshot from memory. The frontend is a static Next.js export on Cloudflare Pages that draws the buses with MapLibre over OpenFreeMap tiles and asks for /vehicles every 15 seconds. Routes and stations come from the static GTFS, preprocessed once a day into light JSON and GeoJSON. The interface reads like a transit diagram rather than a generic map app: choosing a route draws it in a single gold ink and lays its stations out in order on a strip, with every bus placed between two of them.

Technical highlights

  • The route strip: the daily preprocessor picks a representative trip for each route (the one with the longest shape) and stores its trunk stations in order, which covers 141 of 144 routes. The browser projects every bus onto that station sequence to place it between two stations.
  • Honest about the data: there are no arrival times, because the feed has no per-stop times and its stop and trip IDs have been unreliable since November 2026. Instead, the interface shows how old the latest report is, and older reports fade.
  • The API turns about 180 KB of protobuf with 4,000+ vehicles into about 20 KB gzipped with only the trunk buses.
  • Heading is computed between consecutive positions and only updated after a move of about 15 m, so GPS noise does not spin a stopped bus. Buses glide between readings unless the visitor prefers reduced motion.
  • Spanish and English interface, and route search by code or destination that ignores accents. The palette avoids TransMilenio’s colors: trunk and dual buses differ by shape (filled or ring), never by color alone.
  • A daily GitHub Action downloads the static GTFS (about 121 MB), streams its 578 MB stop_times file, simplifies route shapes with Douglas-Peucker and commits only when the data changed; Render and Cloudflare Pages redeploy from that commit.
  • Built for free tiers: the map detects a cold start and says the server is waking up, backs off on errors and stops polling while the tab is hidden. /health reports stale feeds and route IDs missing from the static GTFS.
  • The rate limit is keyed on Cloudflare’s CF-Connecting-IP, so a forged X-Forwarded-For cannot bypass it, and CORS only admits the site’s own origins. CI checks formatting, types, Vitest tests against a recorded feed and the build on every pull request and push to main.

Stack

Next.js · TypeScript · Fastify · MapLibre GL · GTFS Realtime · Cloudflare Pages · Render · GitHub Actions · Vitest · Turborepo

Outcome

Live at bus-tracking.dieramirezma.dev on free tiers (Render, Cloudflare Pages, OpenFreeMap). The API sleeps after 15 minutes without traffic, so the first visit can take about a minute. Data © TRANSMILENIO S.A.