Satellite hotspot counts by district
Map real satellite hotspot-detection counts for Chiang Rai districts with a step-ramp fill, sqrt-sized glowing circles, month buttons and an honest no-data hatch.
Intermediate
What you'll build
A map of real satellite hotspot-detection counts per district in Chiang Rai, 1 January to about 20 May 2024: a step-ramp polygon fill, glowing circles whose area tracks the count, month buttons (Jan to May, or the five-month total), a hover tooltip and a hatched fill for districts with no published data. 18 Chiang Rai districts have district-level counts; Tak is published only at province level, so its 9 district polygons are hatched as 'no published data, not zero'. The counts are open data from data.go.th delivered by the Thai Properties Network API and vendored as one small JSON file. A detection is not a confirmed fire, perimeter or burned area. Serve the folder over http because the page fetches the file. Basemap tiles are MapScale, © OpenStreetMap contributors.
cd docs/mapscale/visualizations/fire-hotspots
python3 -m http.server 4203
# open http://localhost:4203/Create the page and a minimal basemap
One HTML file with a full-height map, a caption that states the caveat, a panel for month buttons and the legend, and an attribution badge. The dark style is hand-built over the public Thailand PMTiles archive and needs no key. The map starts on a bounding box around Chiang Rai.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Satellite hotspot detections</title>
<style>
html,
body,
#map {
height: 100%;
margin: 0;
}
.top {
position: absolute;
top: 12px;
left: 12px;
right: 12px;
z-index: 1;
max-width: 560px;
padding: 8px 10px;
border-radius: 8px;
background: rgba(10, 13, 18, 0.86);
color: #cfe7ff;
font: 12px/1.4 ui-monospace, monospace;
}
.panel {
position: absolute;
left: 12px;
bottom: 34px;
z-index: 1;
max-width: calc(100% - 24px);
padding: 10px 12px;
border-radius: 8px;
background: rgba(10, 13, 18, 0.88);
color: #cfe7ff;
font: 12px/1.4 ui-monospace, monospace;
}
.months button.active {
background: #ff8a1f;
}
.legend {
display: grid;
grid-template-columns: auto 1fr;
gap: 3px 8px;
align-items: center;
}
.legend i {
display: inline-block;
width: 26px;
height: 12px;
}
.legend .hatch {
background: repeating-linear-gradient(45deg, #4b5563 0 2px, #1c2330 2px 5px);
}
.badge {
position: absolute;
bottom: 12px;
left: 12px;
z-index: 1;
padding: 4px 8px;
border-radius: 6px;
background: rgba(10, 13, 18, 0.8);
color: #cfe7ff;
font: 12px/1.4 ui-monospace, monospace;
}
</style>
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.css" />
<script src="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.js"></script>
<script src="https://unpkg.com/pmtiles@4/dist/pmtiles.js"></script>
</head>
<body>
<div id="map"></div>
<div class="top">
<strong>Satellite hotspot detections by district</strong>
<div>A detection is not a confirmed fire, perimeter or burned area. Districts with no published data are not zero.</div>
<div>Real counts: data.go.th open data (Chiang Rai provincial office, Open Data Common) via Thai Properties Network, 1 Jan to 20 May 2024. Tak has no district-level numbers.</div>
</div>
<div class="panel">
<div class="months" id="months"></div>
<div class="legend" id="legend"></div>
</div>
<div class="badge">
<a href="https://mapscale.io">MapScale</a> tiles · © OpenStreetMap contributors
</div>
<script>
maplibregl.addProtocol("pmtiles", new pmtiles.Protocol().tile)
const map = new maplibregl.Map({
container: "map",
bounds: [[99.3, 19.05], [100.6, 20.5]],
fitBoundsOptions: { padding: { top: 110, bottom: 190, left: 20, right: 20 } },
minZoom: 4,
style: {
version: 8,
glyphs: "https://maps.thaipropertiesnetwork.com/{fontstack}/{range}.pbf",
sources: {
ms: {
type: "vector",
url: "pmtiles://https://maps.thaipropertiesnetwork.com/thailand-v2.pmtiles",
attribution: "© OpenStreetMap contributors · MapScale",
},
},
layers: [
{ id: "background", type: "background", paint: { "background-color": "#0a0d12" } },
{ id: "water", type: "fill", source: "ms", "source-layer": "water", paint: { "fill-color": "#0c141f" } },
{
id: "roads", type: "line", source: "ms", "source-layer": "transportation",
filter: ["in", "class", "motorway", "trunk", "primary"],
paint: { "line-color": "#232d3b", "line-width": { base: 1.2, stops: [[8, 0.7], [18, 13]] } },
},
],
},
})
map.on("load", async () => {
// Steps 3 to 7 go here.
})
</script>
</body>
</html>Know the data shape, and never turn a gap into zero
data/districts.json is a GeoJSON FeatureCollection of simplified district polygons. Each feature carries total (the Jan to May 2024 count) and months (Jan to May). null means no published value and is never treated as zero: Mae Sai has no April value, and Tak districts have no values at all. These are the real properties of three districts. cx and cy are a precomputed centroid used to place the circles.
[
{
"id": 668, "province": "chiang-rai", "th": "พาน", "en": "Phan",
"total": 121, "months": [7, 8, 9, 18, 79],
"cx": 99.751, "cy": 19.56
},
{
"id": 682, "province": "chiang-rai", "th": "แม่สาย", "en": "Mae Sai",
"total": 21, "months": [3, 4, 3, null, 11],
"cx": 99.911, "cy": 20.364
},
{
"id": 735, "province": "tak", "th": "เมืองตาก", "en": "Mueang Tak",
"total": null, "months": [null, null, null, null, null],
"cx": 99.164, "cy": 16.891
}
]Copy the chosen value into one property
To switch period, frame() copies the chosen number into a single v property on each feature and drops the months array; a missing value simply has no v. The same rule builds the centroid points, so a no-data district gets neither a colour nor a circle. Every layer then reads v and nothing else.
const PERIODS = [
{ key: "total", label: "Jan–May 2024", stops: [0, 25, 75, 150, 300, 600] },
{ key: 0, label: "Jan", stops: [0, 5, 10, 20, 40, 100] },
{ key: 1, label: "Feb", stops: [0, 5, 10, 20, 40, 100] },
{ key: 2, label: "Mar", stops: [0, 5, 10, 20, 40, 100] },
{ key: 3, label: "Apr", stops: [0, 5, 10, 20, 40, 100] },
{ key: 4, label: "May (to 20th)", stops: [0, 5, 10, 20, 40, 100] },
]
let data = null
let period = PERIODS[0]
const countFor = (props, p) => (p.key === "total" ? props.total : props.months[p.key])
function frame(p) {
const features = data.features.map((f) => {
const v = countFor(f.properties, p)
const props = { ...f.properties }
props.months = undefined
if (v !== null && v !== undefined) {
props.v = v
}
return { ...f, properties: props }
})
return { type: "FeatureCollection", features }
}
const centroids = (fc) => ({
type: "FeatureCollection",
features: fc.features
.filter((f) => f.properties.v !== undefined)
.map((f) => ({
type: "Feature",
properties: { ...f.properties },
geometry: { type: "Point", coordinates: [f.properties.cx, f.properties.cy] },
})),
})
const districtsRes = await fetch("data/districts.json")
if (!districtsRes.ok) throw new Error("districts request failed")
data = await districtsRes.json()Step-ramp fill and a no-data hatch
step gives discrete classes: [input, base colour, stop1, colour1, ...]. A separate layer filtered with ['!', ['has', 'v']] paints the hatch, a fill-pattern image drawn on a canvas and registered with map.addImage. The hatch is a deliberately different look from every class colour, so 'no data' can never be mistaken for 'low'. Add this inside the load handler after the fetch.
const COLORS = ["#2b1017", "#7a1224", "#d4281c", "#ff7a1a", "#ffc233", "#fff2a0"]
function fillExpr(p) {
const expr = ["step", ["get", "v"], COLORS[0]]
for (let i = 1; i < p.stops.length; i++) {
expr.push(p.stops[i], COLORS[i])
}
return expr
}
function hatchImage() {
const size = 12
const canvas = document.createElement("canvas")
canvas.width = size
canvas.height = size
const ctx = canvas.getContext("2d")
ctx.fillStyle = "#1c2330"
ctx.fillRect(0, 0, size, size)
ctx.strokeStyle = "#5b6676"
ctx.lineWidth = 1.6
ctx.beginPath()
for (const o of [-size, 0, size]) {
ctx.moveTo(o, size)
ctx.lineTo(o + size, 0)
}
ctx.stroke()
return ctx.getImageData(0, 0, size, size)
}
map.addImage("hatch", hatchImage(), { pixelRatio: 2 })
const fc = frame(period)
map.addSource("districts", { type: "geojson", data: fc })
map.addSource("centroids", { type: "geojson", data: centroids(fc) })
map.addLayer({
id: "district-nodata",
type: "fill",
source: "districts",
filter: ["!", ["has", "v"]],
paint: { "fill-pattern": "hatch", "fill-opacity": 0.85 },
})
map.addLayer({
id: "district-fill",
type: "fill",
source: "districts",
filter: ["has", "v"],
paint: { "fill-color": fillExpr(period), "fill-opacity": 0.82 },
})
map.addLayer({
id: "district-line",
type: "line",
source: "districts",
paint: { "line-color": "#0a0d12", "line-width": 1.2 },
})Proportional glowing circles
For circle area to track the count, the radius is interpolated on the square root of v. The glow is the same circle drawn bigger and blurred underneath the core.
const radius = (k) => [
"interpolate",
["linear"],
["sqrt", ["get", "v"]],
0, 3 * k,
10, 9 * k,
20, 15 * k,
30, 22 * k,
]
map.addLayer({
id: "centroid-glow",
type: "circle",
source: "centroids",
paint: {
"circle-radius": radius(2),
"circle-blur": 1,
"circle-color": "#ff5a14",
"circle-opacity": 0.45,
},
})
map.addLayer({
id: "centroid-core",
type: "circle",
source: "centroids",
paint: {
"circle-radius": radius(0.8),
"circle-color": "#ffb23a",
"circle-opacity": 0.8,
"circle-stroke-color": "#fff2a0",
"circle-stroke-width": 1,
},
})Month buttons, legend and tooltip
setPeriod re-runs frame(), pushes new data into both sources and swaps the class breaks with setPaintProperty, because a monthly scale is smaller than the five-month total. The legend is rebuilt from the same stops, with the hatch row labelled 'no published data (not zero)'. The tooltip reads the hovered feature's properties, never pixels.
const monthsEl = document.getElementById("months")
const legendEl = document.getElementById("legend")
function renderLegend(p) {
const rows = [`<h2>Detections, ${p.label}</h2>`]
for (let i = 0; i < p.stops.length; i++) {
const lo = p.stops[i]
const hi = p.stops[i + 1]
const text = i === 0 ? `under ${hi}` : hi === undefined ? `${lo} or more` : `${lo} – ${hi - 1}`
rows.push(`<i style="background:${COLORS[i]}"></i><span>${text}</span>`)
}
rows.push(`<i class="hatch"></i><span>no published data (not zero)</span>`)
legendEl.innerHTML = rows.join("")
}
function setPeriod(p) {
period = p
const fc = frame(p)
map.getSource("districts").setData(fc)
map.getSource("centroids").setData(centroids(fc))
map.setPaintProperty("district-fill", "fill-color", fillExpr(p))
for (const b of monthsEl.children) {
b.classList.toggle("active", b.dataset.key === String(p.key))
}
renderLegend(p)
}
for (const p of PERIODS) {
const b = document.createElement("button")
b.type = "button"
b.dataset.key = String(p.key)
b.textContent = p.label
b.addEventListener("click", () => setPeriod(p))
monthsEl.append(b)
}
const popup = new maplibregl.Popup({ closeButton: false, closeOnClick: false, offset: 8 })
function show(e) {
const p = e.features[0].properties
const value = p.v === undefined ? "no published data (not zero)" : `<b>${p.v}</b> detections`
const where = p.province === "tak" ? "Tak: no district-level data" : period.label
popup.setLngLat(e.lngLat).setHTML(`${p.th} · ${p.en}<br />${where} 2024<br />${value}`).addTo(map)
}
for (const id of ["district-fill", "district-nodata"]) {
map.on("mousemove", id, show)
map.on("mouseleave", id, () => popup.remove())
}
setPeriod(period)Refresh the data with build-data.mjs
data/build-data.mjs rebuilds districts.json from the public API with no key. For each province it fetches simplified district polygons with Thai and English names, asks the rankings endpoint which districts have district-level data, and reads each of those districts' annual and monthly series for 2024. It rounds coordinates to 3 decimals and keeps only the name, counts and a centroid. Districts without ranked data keep null.
node docs/mapscale/visualizations/fire-hotspots/data/build-data.mjs
# the public, read-only endpoints it calls (no key), for province 46 (Chiang Rai) and 51 (Tak):
# GET https://api.thaipropertiesnetwork.com/region-statistics/risk-map?topic=air-quality&parentProvinceId=46®ionType=district
# GET https://api.thaipropertiesnetwork.com/region-statistics/rankings?regionType=district&parentProvinceId=46&metric=fire_hotspot_detections_annual_total
# GET https://api.thaipropertiesnetwork.com/region-statistics/district/<districtId>?provinceId=46Limitations, licence and where to go next
Counts are satellite detections: not confirmed fires, not fire perimeters, not burned area. Coverage is one period, 1 January to about 20 May 2024 (the dataset stops in May), for Chiang Rai districts only. A few district-months have no published value and are drawn hatched, not as zero. Tak is published only at province level, so it is hatched too. There is no per-year district series to put on a year slider, which is why the switcher is by month. The API records the licence as 'Open Data Common'; check the licence at the data.go.th source before redistributing. NASA FIRMS is a different dataset and is not used here. Credit © OpenStreetMap contributors for the basemap.
<div class="top">
<div>A detection is not a confirmed fire, perimeter or burned area. Districts with no published data are not zero.</div>
<div>Real counts: data.go.th open data (Chiang Rai provincial office, Open Data Common) via Thai Properties Network, 1 Jan to 20 May 2024. Tak has no district-level numbers.</div>
</div>
<div class="badge">
<a href="https://mapscale.io">MapScale</a> tiles · © OpenStreetMap contributors
</div>