เอกสาร MapScale
การแสดงผลข้อมูลบนแผนที่

สร้าง 3D skyline กรุงเทพฯ ด้วย fill-extrusion

ยกตึกจาก OpenStreetMap ขึ้นเป็น 3D ใส่สีตามความสูง คลิกตึกเพื่อไฮไลต์ และเพิ่มกล้องที่หมุนช้า ๆ

ปานกลาง

สิ่งที่คุณจะสร้าง

มุมมอง 3D แบบเอียงของสาทรและสีลมในกรุงเทพฯ ตึกทุกหลังถูกยกขึ้นตามความสูงจริง และใส่สีตั้งแต่เทาสเลตเข้ม (ตึกเตี้ย) ผ่านสีฟ้าไซยาน ไปจนถึงสีม่วงแดง (ตึกสูงที่สุด) กล้องจะหมุนรอบเมืองช้า ๆ จนกว่าคุณจะแตะแผนที่ และเมื่อคลิกตึกจะเปลี่ยนเป็นสีทอง ใช้ archive ประเทศไทยของ MapScale แบบไม่ต้องใช้ key ข้อมูลพื้นฐานของแผนที่เป็น © OpenStreetMap contributors จึงต้องแสดงเครดิตนี้ไว้

สิ่งที่คุณจะสร้าง
cd docs/mapscale/visualizations/3d-skyline
python3 -m http.server 4101
# open http://localhost:4101/

สร้างหน้าเว็บและแผนที่พื้นหลังสีเข้ม

MapLibre อ่าน archive ผ่านโปรโตคอล pmtiles:// ข้อความ attribution ของ source คือสิ่งที่ attribution control ของ MapLibre แสดง จึงต้องใส่เครดิต OpenStreetMap ไว้ที่นั่น style ที่โฮสต์ไว้ไม่มี layer 3D คู่มือนี้จึงสร้างเอง เริ่มจากพื้นหลังสีเข้มที่เรียบ แล้วเพิ่มตึกเมื่อแผนที่โหลดเสร็จ

สร้างหน้าเว็บและแผนที่พื้นหลังสีเข้ม
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<title>3D Bangkok skyline</title>
	<style>
		html,
		body,
		#map {
			height: 100%;
			margin: 0;
		}
	</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>
	<button id="orbit" type="button" hidden style="position:absolute;bottom:16px;left:12px;z-index:1">Resume orbit</button>
	<script>
		maplibregl.addProtocol("pmtiles", new pmtiles.Protocol().tile)

		const map = new maplibregl.Map({
			container: "map",
			center: [100.5286, 13.7226], // Sathorn / Silom
			zoom: 15.5,
			pitch: 60,
			bearing: -20,
			maxPitch: 85,
			style: {
				version: 8,
				glyphs: "https://maps.thaipropertiesnetwork.com/{fontstack}/{range}.pbf",
				sources: {
					ms: {
						type: "vector",
						url: "pmtiles://https://maps.thaipropertiesnetwork.com/thailand-v2.pmtiles",
						maxzoom: 14,
						attribution: "© OpenStreetMap contributors · MapScale",
					},
				},
				layers: [
					{ id: "background", type: "background", paint: { "background-color": "#0a0d12" } },
					{ id: "landuse", type: "fill", source: "ms", "source-layer": "landuse", paint: { "fill-color": "#0e1219" } },
					{ id: "water", type: "fill", source: "ms", "source-layer": "water", paint: { "fill-color": "#0c141f" } },
					{
						id: "roads",
						type: "line",
						source: "ms",
						"source-layer": "transportation",
						filter: ["==", "$type", "LineString"],
						paint: { "line-color": "#1b232e", "line-width": { base: 1.2, stops: [[11, 0.5], [18, 10]] } },
					},
				],
			},
		})
		map.addControl(new maplibregl.NavigationControl({ visualizePitch: true }), "top-right")

		map.on("load", () => {
			// Steps 3 to 5 go here.
		})
	</script>
</body>
</html>

ยกตึกขึ้นเป็น 3D และใส่สีตามความสูง

source layer building ใน archive มี field ที่ใช้ได้เพียงสี่ตัว คือ colour, hide_3d, render_height และ render_min_height (หน่วยเมตร) ใช้ render_height เป็นความสูง ใช้ render_min_height เป็นฐาน ข้าม feature ที่มี hide_3d แล้วส่งความสูงเข้า expression แบบ interpolate เพื่อไล่สี

ยกตึกขึ้นเป็น 3D และใส่สีตามความสูง
const height = ["coalesce", ["get", "render_height"], 0]
const base = ["coalesce", ["get", "render_min_height"], 0]
const visible = ["!=", ["get", "hide_3d"], true]

// metres of height -> colour: dark slate, cyan, magenta
const heightColor = [
	"interpolate",
	["linear"],
	height,
	0, "#1c2431",
	30, "#27506b",
	100, "#1fa6c9",
	200, "#35e0ff",
	320, "#ff3df2",
]

map.addLayer({
	id: "buildings-3d",
	type: "fill-extrusion",
	source: "ms",
	"source-layer": "building",
	minzoom: 13,
	filter: visible,
	paint: {
		"fill-extrusion-height": height,
		"fill-extrusion-base": base,
		"fill-extrusion-color": heightColor,
		"fill-extrusion-vertical-gradient": true,
		"fill-extrusion-opacity": 0.92,
	},
})

คลิกตึกสูงเพื่อไฮไลต์

ใน archive ไม่มี id ของตึก จึงจับคู่ตึกที่คลิกด้วยความสูงและฐาน ใช้สอง layer ที่มี filter ตรงข้ามกัน layer หลักวาดทุกตึกยกเว้นตึกที่เลือก layer ไฮไลต์วาดเฉพาะตึกที่เลือก จึงไม่เกิด z-fighting ข้อจำกัด: ตึกหน้าตาเหมือนกันที่สูงและฐานเท่ากันจะถูกไฮไลต์ไปด้วย ดังนั้นเลือกได้เฉพาะตึกสูง 40 เมตรขึ้นไป (ตึกสูงที่ค่าความสูงแทบไม่ซ้ำกัน)

คลิกตึกสูงเพื่อไฮไลต์
const MIN_PICK = 40
const sameAsPicked = (h, b) => ["all", ["==", height, h], ["==", base, b]]

// Starts matching nothing (-1), drawn in gold.
map.addLayer({
	id: "buildings-3d-highlight",
	type: "fill-extrusion",
	source: "ms",
	"source-layer": "building",
	minzoom: 13,
	filter: ["all", visible, sameAsPicked(-1, -1)],
	paint: {
		"fill-extrusion-height": height,
		"fill-extrusion-base": base,
		"fill-extrusion-color": "#ffd23d",
		"fill-extrusion-vertical-gradient": true,
		"fill-extrusion-opacity": 1,
	},
})

// Swap both filters together so the layers stay complementary.
function highlight(h, b) {
	const hit = sameAsPicked(h, b)
	map.setFilter("buildings-3d", ["all", visible, ["!", hit]])
	map.setFilter("buildings-3d-highlight", ["all", visible, hit])
}

const pickable = ["buildings-3d", "buildings-3d-highlight"]
map.on("click", (e) => {
	const f = map.queryRenderedFeatures(e.point, { layers: pickable })[0]
	if (!f) return
	const h = f.properties.render_height ?? 0
	const b = f.properties.render_min_height ?? 0
	if (h < MIN_PICK) return
	highlight(h, b)
})
map.on("mousemove", (e) => {
	const hit = map.queryRenderedFeatures(e.point, { layers: pickable }).length
	map.getCanvas().style.cursor = hit ? "pointer" : ""
})

เพิ่มการหมุนกล้องช้า ๆ ที่หยุดเมื่อผู้ใช้แตะ

ขยับ bearing ไม่กี่องศาต่อวินาทีด้วย requestAnimationFrame และหยุด loop ทันทีที่ผู้ใช้แตะแผนที่ เพื่อไม่ให้แย่งการควบคุมกล้อง เพิ่มปุ่ม Resume ที่ซ่อนไว้หลัง div ของแผนที่ เช่น <button id="orbit" type="button" hidden>Resume orbit</button>

เพิ่มการหมุนกล้องช้า ๆ ที่หยุดเมื่อผู้ใช้แตะ
let orbiting = false
let last = 0

function spin(t) {
	if (!orbiting) return
	const dt = last ? (t - last) / 1000 : 0
	last = t
	map.setBearing(map.getBearing() + dt * 4) // 4 degrees per second
	requestAnimationFrame(spin)
}
function startOrbit() {
	if (orbiting) return
	orbiting = true
	last = 0
	document.getElementById("orbit").hidden = true
	requestAnimationFrame(spin)
}
function stopOrbit() {
	orbiting = false
	document.getElementById("orbit").hidden = false
}

for (const evt of ["mousedown", "touchstart", "wheel"]) map.on(evt, stopOrbit)
document.getElementById("orbit").addEventListener("click", startOrbit)
startOrbit()

ไปต่อที่ไหนดี

หน้าเว็บฉบับเต็มที่ทดสอบแล้วอยู่ที่ docs/mapscale/visualizations/3d-skyline ใน repository ให้เปิดผ่าน http ตามขั้นตอนแรก ลองเปลี่ยน center ไปย่านอื่น ปรับ stop ของสีใน heightColor หรือลดค่า MIN_PICK เพื่อดูว่าตึกเตี้ยที่หน้าตาเหมือนกันจะติดไฮไลต์พร้อมกันเร็วแค่ไหน แผนที่ที่คุณเผยแพร่ต้องแสดง © OpenStreetMap contributors เสมอ

On this page

ตั้งค่า Analytics