Guides

A rain radar layer for MapLibre

The next three hours of rain over Europe on a MapLibre GL JS map, with a slider to move through time.

MapLibre draws Nimbo’s tiles as a plain raster source. This guide builds a complete page with a time slider: drag it and the rain moves forward, up to three hours ahead.

Before you start

You need a key: get one free, no card. Tiles take it in the URL (?key=). There is nothing to install, as the page loads MapLibre GL JS 4 from a CDN with a free base map style. Any style works.

The complete page

Save it as index.html, put your key in KEY and open it in a browser.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rain radar</title>
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@4.7.1/dist/maplibre-gl.css">
<script src="https://unpkg.com/maplibre-gl@4.7.1/dist/maplibre-gl.js"></script>
<style>
  html, body, #map { height: 100%; margin: 0; }
  #bar { position: absolute; z-index: 1; left: 10px; right: 10px; bottom: 30px; display: flex; gap: 8px;
         align-items: center; padding: 6px 10px; background: #fff; font: 14px sans-serif; }
  #slider { flex: 1; }
</style>
</head>
<body>
<div id="map"></div>
<div id="bar"><input id="slider" type="range" min="0" max="0" value="0"><span id="time"></span></div>
<script>
const BASE = "https://nimboweather.com";
const KEY = "nimbo_...";  // a key in a web page can be copied: regenerate it from your dashboard if that happens

const map = new maplibregl.Map({
  container: "map",
  style: "https://tiles.openfreemap.org/styles/positron",
  center: [8, 48.5],
  zoom: 4,
});
const slider = document.getElementById("slider");
let frames = [];

function show(i) {
  // a hidden layer loads no tiles: only the frame on screen is fetched (and kept once seen)
  frames.forEach((f, j) => map.setLayoutProperty(f.id, "visibility", j === i ? "visible" : "none"));
  document.getElementById("time").textContent =
    new Date(frames[i].time).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
}

async function loadForecast() {
  const res = await fetch(`${BASE}/v1/tiles/index?key=${KEY}`);
  const index = await res.json();
  if (!res.ok) throw new Error(`${index.error}: ${index.message}`);
  for (const f of frames) { map.removeLayer(f.id); map.removeSource(f.id); }
  // one raster source per frame, all from this one index (one forecast)
  frames = index.forecast.map((f, i) => ({ id: `rain-${i}`, time: f.time, url: `${f.tiles.rain}?key=${KEY}` }));
  for (const f of frames) {
    map.addSource(f.id, {
      type: "raster", tiles: [f.url], tileSize: 256, maxzoom: 8,  // tiles stop at zoom 8: MapLibre stretches them above
      attribution: index.attribution.join(", "),
    });
    map.addLayer({ id: f.id, type: "raster", source: f.id, layout: { visibility: "none" },
                   paint: { "raster-opacity": 0.85, "raster-fade-duration": 0 } });
  }
  slider.max = frames.length - 1;
  slider.value = Math.min(Number(slider.value), frames.length - 1);
  show(Number(slider.value));
}

slider.addEventListener("input", () => show(Number(slider.value)));
map.on("load", () => {
  loadForecast();
  setInterval(loadForecast, 10 * 60_000);  // a new forecast every 10 minutes
});
</script>
</body>
</html>

How it works

  1. The tile index (GET /v1/tiles/index) lists the 20 frames of the latest forecast, every 5 minutes to one hour ahead, then every 15 minutes to three hours, each with a ready URL.
  2. One raster source and layer per frame, all from that one index, so every position of the slider shows the same forecast.
  3. Only the frame on screen is visible. MapLibre loads no tiles for a layer whose visibility is none, so the map fetches a frame the first time it is shown, and keeps it.
  4. raster-fade-duration: 0 swaps frames at once instead of fading one into the next.
  5. The page reads a fresh index every 10 minutes, when a new forecast is out, and rebuilds the layers.

Adding a play button

To play the loop instead of dragging, step the slider in a loop. A frame shown for the first time has to load its tiles, a full screen’s worth at once, so the first pass waits longer to stay within the tile limit; frames already seen play quickly. Add this at the end of the script:

const seen = new Set();
const wait = (ms) => new Promise((done) => setTimeout(done, ms));

async function play() {
  for (;;) {
    if (!frames.length) { await wait(500); continue; }
    const i = (Number(slider.value) + 1) % frames.length;
    slider.value = i;
    show(i);
    const first = !seen.has(frames[i].url);
    seen.add(frames[i].url);
    await new Promise((done) => map.once("idle", done));  // this frame’s tiles are in
    await wait(first ? 2000 : 800);  // the tile limit refills at 25 tiles a second
  }
}
play();

What each frame holds

FieldMeaning
timeThe time the frame shows (UTC). toLocaleTimeString shows it in the visitor’s time.
offset_minMinutes after the forecast’s issue_time: 5 to 60 by 5, then 75 to 180 by 15.
tiles.rainRain rate tiles in the default palette. Add &palette= after the key for another (see colours and legend).
tiles.p1Chance of 1 mm/h or more, as blue of varying opacity.

Common pitfalls

  • Opacity 0 still loads. A raster layer at raster-opacity: 0 fetches its tiles. Twenty frames at once is a burst of hundreds of tiles and ends in 429 tile_rate_limited; hide frames with visibility instead.
  • Set maxzoom: 8 on the source. Tiles stop at zoom 8 and MapLibre stretches them above; without it, deeper zooms ask for tiles that do not exist.
  • Remove layers before sources. MapLibre refuses to remove a source a layer still uses, which is why the refresh removes each layer first.
  • Empty tiles outside Europe. A tile wholly outside the forecast grid answers 204 No Content: nothing to draw, and nothing to handle.
  • No retry on 429. A refused tile stays blank until the map asks again. Bursts are 40 tiles, refilled at 25 a second (Pro: 80 and 50); see rate limits.
  • Late radar. When the index says stale: true, show it next to the slider.
  • Your key is in the URLs. Tiles need it there. Regenerate it from your dashboard if it is misused.

Next: Rain forecast in Python