MapScale Docs
Visualizations

3D Bangkok skyline with fill-extrusion

Extrude OpenStreetMap buildings, colour them by height, click a tower to highlight it and add a slow camera orbit.

Intermediate

What you'll build

A pitched 3D view of Sathorn and Silom in Bangkok: every building is extruded to its real height and coloured from dark slate (low-rise) through cyan to magenta (the tallest towers). The camera orbits slowly until you touch the map, and clicking a tower turns it gold. It uses the keyless MapScale Thailand archive, so no API key is needed. The basemap data is © OpenStreetMap contributors, so keep that credit visible.

What you'll build
cd docs/mapscale/visualizations/3d-skyline
python3 -m http.server 4101
# open http://localhost:4101/

Create the page and a dark basemap

MapLibre reads the archive through the pmtiles:// protocol. The source's attribution string is what MapLibre's attribution control shows, so it carries the OpenStreetMap credit. The hosted style has no 3D layer, so this guide builds its own. We start from a quiet dark base and add the buildings on load.

Create the page and a dark basemap
<!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>

Extrude buildings and colour them by height

The archive's building source layer carries only four useful fields: colour, hide_3d, render_height and render_min_height (metres). Use render_height for the extrusion height and render_min_height for the base, skip features flagged hide_3d, and feed the height into an interpolate expression for the colour ramp.

Extrude buildings and colour them by height
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,
	},
})

Click a tower to highlight it

The archive has no building id, so a click is matched by height plus base. Two layers with complementary filters draw the buildings: the base layer draws everything except the pick, the highlight layer draws only the pick, so nothing z-fights. Limitation: look-alike buildings with the same height and base highlight too, so only buildings of 40 m or more (the tall, near-unique ones) are pickable.

Click a tower to highlight it
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" : ""
})

Add a slow orbit that yields to the user

Nudge the bearing a few degrees per second with requestAnimationFrame, and stop the loop completely the moment the user touches the map so the camera is never fought over. Add a hidden Resume button after the map div, for example: <button id="orbit" type="button" hidden>Resume orbit</button>.

Add a slow orbit that yields to the user
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()

Where to go next

The complete, tested page lives in docs/mapscale/visualizations/3d-skyline in the repository. Serve it over http as shown in the first step. Try other districts by changing center, tune the ramp stops in heightColor, or lower MIN_PICK to see how quickly look-alike low-rise blocks start lighting up together. Keep © OpenStreetMap contributors visible on any map you publish.

On this page

Analytics settings