Tokyo train delays map with MapLibre GL JS
Tokyo's trains are famous for running on time. When they don't, the railway hands out delay certificates for riders to show at work, and Japan's transport ministry, MLIT, counts them for 46 of the area's busiest lines. Press Play to run a made-up month of weekday mornings, with trains that turn red and slow down on each line's late mornings. Point at a line for its numbers.
JR East's Saikyo Line, from Osaki through Shinjuku to Omiya, handed out certificates on 99% of weekday mornings, so it ran late almost every day. JR East's other lines into central Tokyo did on 58% to 94% of them, and Tokyo Metro's Tozai and Chiyoda lines on about 80%. Most of the private railways' lines did on fewer than a third, and Tokyu's Oimachi Line on 7%, about once a month. The average of all 46 was half.
The month on the map is made up. Each line runs late on as many of its 20 mornings as MLIT counted from April 2024 to March 2025, and the later the delay, the slower its trains go, but MLIT gives a count for the year, not the days, so which mornings those are is picked at random each time the page loads. The list under the map ranks the lines by how often they ran late. Point at one to find it on the map, or click it to zoom to it.
Most of those late mornings were only a little late. On 6 in 10 of them, the worst delay was 10 minutes or less, and on 1 in 20 it was more than 30. MLIT puts nearly two thirds of the short delays down to passengers, mostly boarding that takes too long, doors opened again, and people taken ill, and most of the rest to a delay on another line whose trains run through.
Each railway sets its own rules for when it hands out certificates, and they cover weekday mornings only, until 9 or 10 a.m. A certificate is for the worst delay anywhere on the line that morning, so this is how often a line had a late train, not how often any one train was late. MLIT counts these 46 lines, not every line in Tokyo, and publishes the count only every few years: this one, out in April 2026, is the first since the year to March 2019. Its lines are drawn here as far as MLIT's own map draws them. Some, such as the Odakyu and Marunouchi lines, include their branches.
Build it yourself
A Node.js script downloads the track of Tokyo's 13 subway lines from OpenStreetMap and adds MLIT's count to each, and a page colors them by it. It colors the subway lines by how often they ran late, without the moving trains or the list. It was tested with MapLibre GL JS 6.15 in Chromium and Node.js 22.
What you need
- Node.js 18 or later.
- A browser key with the Map tiles product, made as API keys shows. Add
http://localhost:*to its allowed origins, so it works from your computer while you build. Map tiles are free, and signing up takes a minute, with no credit card. OpenStreetMap needs no key.
Collect the lines
In OpenStreetMap, each train service is a route relation: a list of the stretches of track it runs on and the stops it makes. Make a folder for the map, and in it a file called lines.mjs:
import { writeFile } from "node:fs/promises";
// Each subway line's OpenStreetMap route relation, one way along the line, and on how many of every 20 weekday mornings
// it handed out delay certificates from April 2024 to March 2025, from MLIT's report.
const lines = [
["Ginza", 443281, 1.9],
["Marunouchi", 8015932, 8.3],
["Hibiya", 443272, 7.3],
["Tozai", 5371620, 16.0],
["Chiyoda", 21780070, 16.6],
["Yurakucho", 443269, 13.8],
["Hanzomon", 443279, 9.2],
["Namboku", 7730199, 11.0],
["Fukutoshin", 5375678, 15.1],
["Asakusa", 3302734, 5.2],
["Mita", 443286, 2.7],
["Shinjuku", 8019858, 7.2],
["Oedo", 8019883, 6.5],
];
const features = [];
for (const [name, relation, days] of lines) {
const response = await fetch(`https://api.openstreetmap.org/api/0.6/relation/${relation}/full.json`, {
headers: { "User-Agent": "tokyo-delays-map" },
});
if (!response.ok) throw new Error(`OpenStreetMap returned ${response.status} for ${name}`);
const { elements } = await response.json();
const nodes = new Map(elements.filter(({ type }) => type === "node").map(({ id, lon, lat }) => [id, [lon, lat]]));
const ways = new Map(elements.filter(({ type }) => type === "way").map(({ id, nodes }) => [id, nodes]));
const route = elements.find(({ type, id }) => type === "relation" && id === relation);
const tracks = route.members
.filter(({ type, role }) => type === "way" && role === "")
.map(({ ref }) => ways.get(ref).map((node) => nodes.get(node)));
const late = Math.round(days * 5);
features.push({ type: "Feature", properties: { name, late }, geometry: { type: "MultiLineString", coordinates: tracks } });
}
await writeFile("lines.json", JSON.stringify({ type: "FeatureCollection", features }));
console.log(`${features.length} lines`);
- The OpenStreetMap API's
full.jsongives a relation with all of its ways and their points. A route's track is its ways with no role. Its stops and platforms have roles, so they're left out. - Each way becomes one part of the line. They don't need joining end to end to be drawn.
- MLIT gives late mornings in every 20 weekdays, so five times that is the share of weekday mornings a line ran late, as a percentage.
- The OpenStreetMap API is for editing the map, so ask it for a few relations at a time, not thousands.
Run it:
node lines.mjs
It prints 13 lines, and writes lines.json next to it.
Make the page
In the same folder, make index.html, with your browser key in place of mapricot_pk_…:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tokyo subway delays</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@6.15.0/dist/maplibre-gl.css">
<script type="importmap">
{
"imports": {
"maplibre-gl": "https://cdn.jsdelivr.net/npm/maplibre-gl@6.15.0/+esm"
}
}
</script>
<style>
body { margin: 0; font-family: system-ui, sans-serif; }
#map { height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
import * as maplibregl from "maplibre-gl";
const key = "mapricot_pk_…";
const map = new maplibregl.Map({
container: "map",
style: `https://api.mapricot.com/tiles/v1/styles/light.json?key=${key}`,
center: [139.75, 35.69],
zoom: 11,
});
await map.once("load");
map.addSource("lines", { type: "geojson", data: "lines.json", attribution: "Delays: MLIT" });
map.addLayer({
id: "lines",
type: "line",
source: "lines",
layout: { "line-cap": "round", "line-join": "round", "line-sort-key": ["get", "late"] },
paint: {
"line-color": ["interpolate", ["linear"], ["get", "late"], 0, "#ffc078", 35, "#fd7e14", 70, "#c2410c", 100, "#7c2d12"],
"line-width": 4,
},
});
const popup = new maplibregl.Popup({ closeButton: false, closeOnClick: false });
map.on("mousemove", "lines", ({ features: [line], lngLat }) => {
popup.setLngLat(lngLat).setText(`${line.properties.name}: late on ${line.properties.late}% of weekday mornings`).addTo(map);
});
map.on("mouseleave", "lines", () => popup.remove());
</script>
</body>
</html>
line-colorruns from pale orange for a line that's hardly ever late to dark brown for one that's late every morning.line-sort-keydraws the later lines on top, where two lines share a street.
Open it
Serve the folder with npx serve or python3 -m http.server, and open the address it prints. Opened straight from the file, the map stays blank, as Open the page explains. You should see the subway across central Tokyo, darkest on the Tozai Line, running east from Nakano under Otemachi to Nishi-Funabashi, and on the Chiyoda Line, from Yoyogi-Uehara to Ayase. Point at the pale line from Shibuya to Asakusa, and it's the Ginza Line, late on 10% of weekday mornings.
MLIT's report has the same numbers for 33 more lines, run by JR East and Tokyo's private railways. Their route relations are in OpenStreetMap too, as route=train.
Data
- Delays: Ministry of Land, Infrastructure, Transport and Tourism (MLIT), 東京圏の鉄道路線の遅延「見える化」(令和6年度), table 1-1, April 2024 to March 2025
- Tracks: OpenStreetMap contributors (ODbL), from the route relations of each line
- Map: OpenStreetMap contributors (ODbL), in Mapricot's map tiles