จำนวนจุดความร้อนจากดาวเทียมรายอำเภอ
แสดงจำนวนการตรวจจับจุดความร้อนจากดาวเทียมจริงของอำเภอในเชียงราย ด้วยสี fill แบบ step วงกลมเรืองแสงขนาดตามรากที่สอง ปุ่มเลือกเดือน และลายขีดสำหรับพื้นที่ไม่มีข้อมูล
ปานกลาง
สิ่งที่คุณจะสร้าง
แผนที่จำนวนการตรวจจับจุดความร้อนจากดาวเทียมจริงรายอำเภอในจังหวัดเชียงราย ตั้งแต่ 1 มกราคมถึงประมาณ 20 พฤษภาคม 2024: polygon ที่ระบายสีแบบ step วงกลมเรืองแสงที่พื้นที่ตามจำนวน ปุ่มเลือกเดือน (มกราคมถึงพฤษภาคม หรือยอดรวมห้าเดือน) tooltip เมื่อ hover และลายขีดสำหรับอำเภอที่ไม่มีข้อมูลเผยแพร่ เชียงราย 18 อำเภอมีตัวเลขระดับอำเภอ ส่วนตากเผยแพร่เฉพาะระดับจังหวัด polygon 9 อำเภอของตากจึงถูกแรเงาเป็น 'ไม่มีข้อมูลเผยแพร่ ไม่ใช่ศูนย์' ตัวเลขเป็นข้อมูลเปิดจาก data.go.th ส่งผ่าน API ของ Thai Properties Network และเก็บไว้เป็นไฟล์ JSON เล็ก ๆ ไฟล์เดียว การตรวจจับไม่ใช่ไฟไหม้ที่ยืนยันแล้ว ไม่ใช่ขอบเขตไฟ และไม่ใช่พื้นที่ถูกเผา หน้าเว็บ fetch ไฟล์ จึงต้องเปิดโฟลเดอร์ผ่าน http แผนที่พื้นฐานคือ tiles ของ MapScale © OpenStreetMap contributors
cd docs/mapscale/visualizations/fire-hotspots
python3 -m http.server 4203
# open http://localhost:4203/สร้างหน้าเว็บและแผนที่พื้นฐานขั้นต่ำ
ไฟล์ HTML ไฟล์เดียวที่มีแผนที่เต็มความสูง คำอธิบายที่ระบุข้อควรระวัง แผงสำหรับปุ่มเลือกเดือนและ legend และป้ายอ้างอิงแหล่งที่มา style สีเข้มสร้างเองบน Thailand PMTiles archive สาธารณะ ไม่ต้องใช้ key แผนที่เริ่มต้นที่กรอบพื้นที่รอบเชียงราย
<!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>เข้าใจรูปแบบข้อมูล และอย่าเปลี่ยนช่องว่างเป็นศูนย์
data/districts.json คือ GeoJSON FeatureCollection ของ polygon อำเภอที่ลดรายละเอียดแล้ว แต่ละ feature มี total (จำนวนมกราคมถึงพฤษภาคม 2024) และ months (มกราคมถึงพฤษภาคม) ค่า null หมายถึงไม่มีค่าเผยแพร่ และห้ามถือเป็นศูนย์ เช่น แม่สายไม่มีค่าเดือนเมษายน และอำเภอของตากไม่มีค่าเลย นี่คือ property จริงของสามอำเภอ cx และ cy คือจุดศูนย์กลางที่คำนวณไว้ล่วงหน้าเพื่อวางวงกลม
[
{
"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
}
]คัดลอกค่าที่เลือกลงใน property เดียว
เมื่อสลับช่วงเวลา frame() จะคัดลอกตัวเลขที่เลือกลงใน property v ตัวเดียวของแต่ละ feature และทิ้งอาร์เรย์ months ค่าที่ไม่มีจะไม่มี v กฎเดียวกันนี้ใช้สร้างจุดศูนย์กลาง อำเภอที่ไม่มีข้อมูลจึงไม่มีทั้งสีและวงกลม จากนั้นทุก layer อ่านเฉพาะ v
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()สี fill แบบ step และลายขีดสำหรับไม่มีข้อมูล
step ให้ชั้นสีแบบไม่ต่อเนื่อง: [input, สีฐาน, stop1, สี1, ...] layer แยกต่างหากที่ใช้ filter ['!', ['has', 'v']] วาดลายขีดซึ่งเป็นภาพ fill-pattern ที่วาดบน canvas และลงทะเบียนด้วย map.addImage ลายขีดตั้งใจให้ดูต่างจากสีทุกชั้น เพื่อไม่ให้ 'ไม่มีข้อมูล' ถูกเข้าใจผิดว่าเป็น 'ต่ำ' เพิ่มโค้ดนี้ใน load handler ต่อจากการ 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 },
})วงกลมเรืองแสงตามสัดส่วน
เพื่อให้พื้นที่วงกลมเป็นสัดส่วนกับจำนวน รัศมีจึงถูก interpolate บนรากที่สองของ v ส่วนแสงเรืองคือวงกลมเดียวกันที่ใหญ่กว่าและเบลอ วางไว้ใต้วงกลมแกนกลาง
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,
},
})ปุ่มเลือกเดือน legend และ tooltip
setPeriod เรียก frame() ใหม่ ส่งข้อมูลใหม่เข้าทั้งสอง source และสลับช่วงของชั้นสีด้วย setPaintProperty เพราะสเกลรายเดือนเล็กกว่ายอดรวมห้าเดือน legend ถูกสร้างใหม่จาก stop เดียวกัน โดยแถวลายขีดมีป้าย 'no published data (not zero)' tooltip อ่าน property ของ feature ที่ hover ไม่ใช่พิกเซล
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)รีเฟรชข้อมูลด้วย build-data.mjs
data/build-data.mjs สร้าง districts.json ใหม่จาก API สาธารณะโดยไม่ต้องใช้ key สำหรับแต่ละจังหวัด สคริปต์ดึง polygon อำเภอที่ลดรายละเอียดพร้อมชื่อไทยและอังกฤษ ถาม rankings endpoint ว่าอำเภอใดมีข้อมูลระดับอำเภอ แล้วอ่านอนุกรมรายปีและรายเดือนของปี 2024 ของอำเภอเหล่านั้น ปัดพิกัดเหลือ 3 ตำแหน่งทศนิยม และเก็บเฉพาะชื่อ จำนวน และจุดศูนย์กลาง อำเภอที่ไม่มีข้อมูลใน rankings จะคงเป็น 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=46ข้อจำกัด สัญญาอนุญาต และไปต่อที่ไหนดี
ตัวเลขคือการตรวจจับจากดาวเทียม ไม่ใช่ไฟไหม้ที่ยืนยันแล้ว ไม่ใช่ขอบเขตไฟ และไม่ใช่พื้นที่ถูกเผา ข้อมูลครอบคลุมช่วงเดียวคือ 1 มกราคมถึงประมาณ 20 พฤษภาคม 2024 (ชุดข้อมูลสิ้นสุดในเดือนพฤษภาคม) และเฉพาะอำเภอของเชียงราย บางอำเภอบางเดือนไม่มีค่าเผยแพร่และถูกวาดด้วยลายขีด ไม่ใช่ศูนย์ ตากเผยแพร่เฉพาะระดับจังหวัดจึงถูกแรเงาด้วยเช่นกัน ไม่มีอนุกรมรายปีระดับอำเภอสำหรับทำตัวเลื่อนปี จึงสลับตามเดือนแทน API บันทึกสัญญาอนุญาตเป็น 'Open Data Common' ตรวจสอบสัญญาอนุญาตที่แหล่ง data.go.th ก่อนนำไปเผยแพร่ต่อ NASA FIRMS เป็นชุดข้อมูลคนละชุดและไม่ได้ใช้ที่นี่ ให้เครดิต © OpenStreetMap contributors สำหรับแผนที่พื้นฐาน
<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>