Connected dataStart here: 3 of 4Simulated data on generated geometry
Select a detector on a map and in 3D
A junction on a map: the blocks raised to their height, the roads in their true width, the detectors coloured by their flow, read the same in three.js.
What this example defines: its scene and, where it has them, its simulated source and its styling. The page imports and runs this file; the page's own renderer, panels and controls are not in it. For a complete app to run on your machine, download the quickstart starter.
import { fixtureSource } from "@klorad/api/integration";
import { createScene, type SceneObjectSpec } from "@klorad/api/world";
import type { MapboxStyleFn } from "@klorad/engine-mapbox";
import type { StyleFn } from "@klorad/engine-three";
export const SOURCE_ID = "detectors";
/** A junction on a fictional ring road; positions are metres east and north of it. */
export const JUNCTION = { lat: 40.6315, lon: 22.9655, height: 20 };
const road = (id: string, name: string, path: [number, number][], width: number): SceneObjectSpec => ({ id, name, position: { east: 0, north: 0 }, geometry: { kind: "polyline", path, width }, class: "road" });
const block = (id: string, name: string, east: number, north: number, w: number, d: number, h: number): SceneObjectSpec => ({
id,
name,
position: { east, north },
geometry: { kind: "extrusion", footprint: [[-w / 2, -d / 2], [w / 2, -d / 2], [w / 2, d / 2], [-w / 2, d / 2]], height: h },
class: "building",
});
const detector = (id: string, name: string, east: number, north: number): SceneObjectSpec => ({
id,
name,
position: { east, north },
geometry: { kind: "box", width: 9, depth: 9, height: 5 },
correspondence: "shadow",
binding: { source: SOURCE_ID, entity: id },
class: "detector",
});
/** The ring road, the avenue that crosses it, the blocks beside them and four loop detectors that report the flow. */
export const junctionObjects: SceneObjectSpec[] = [
road("ring", "Ring road", [[-400, -60], [-120, -20], [0, 0], [140, 30], [420, 110]], 22),
road("avenue", "Avenue", [[-30, -380], [-10, -150], [0, 0], [20, 180], [40, 400]], 14),
block("depot", "Bus depot", -150, 90, 90, 50, 9),
block("offices", "Office block", 120, -110, 50, 40, 32),
block("market", "Market hall", 160, 140, 70, 45, 14),
block("school", "School", -170, -150, 60, 35, 12),
detector("loop-ring-w", "Ring road west", -180, -32),
detector("loop-ring-e", "Ring road east", 200, 46),
detector("loop-ave-s", "Avenue south", -14, -170),
detector("loop-ave-n", "Avenue north", 24, 200),
{ id: "sign", name: "Message sign, ring road east", position: { east: 300, north: 90, up: 6 }, geometry: { kind: "marker" }, class: "sign" },
];
export function createJunction() {
const scene = createScene({ name: "Ring road junction", coordinateSystem: { origin: JUNCTION }, staleAfterMs: 5000 });
junctionObjects.forEach((o) => scene.add(o));
return scene;
}
/** Vehicles an hour per lane, rising and falling out of step at each detector. */
export function createDetectors(intervalMs = 1000) {
const wave = (phase: number) => (t: number) => Math.round(900 + 700 * Math.sin(t / 5 + phase));
return fixtureSource({
id: SOURCE_ID,
intervalMs,
channels: ["loop-ring-w", "loop-ring-e", "loop-ave-s", "loop-ave-n"].map((entity, i) => ({ entity, quantity: "flow", unit: "veh/h", value: wave(i * 1.7) })),
});
}
const ROAD = "#2b3641";
const BUILDING = "#5d6b78";
/** Free, busy, congested: the colours of a traffic map. */
export const flowColour = (flow: number) => (flow > 1400 ? "#ff6b5a" : flow > 1000 ? "#f5c451" : "#4fd1a5");
/** The icons the map may draw over a point: SVG markup, white on the point's own colour. */
export const MAP_ICONS = {
sign: '<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#ffffff" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="5" width="18" height="10" rx="1"/><path d="M7 9h10M7 12h6M12 15v4"/></svg>',
};
/** On the map: roads and blocks quiet, each detector in the colour of its flow, grey when it goes quiet; the sign by its icon. */
export const junctionMapStyle: MapboxStyleFn = ({ object, readings, stale, palette }) => {
if (object.class === "road") return { color: ROAD };
if (object.class === "building") return { color: BUILDING };
if (object.class === "sign") return { color: "#a78bfa", icon: "sign" };
const flow = readings.flow?.value;
return { color: stale || typeof flow !== "number" ? palette.stale : flowColour(flow) };
};
/** The same reading in three.js: the same scene, the same colours, a label with the flow. */
export const junctionThreeStyle: StyleFn = ({ object, readings, stale, palette }) => {
if (object.class === "road") return { color: ROAD, opacity: 1 };
if (object.class === "building") return { color: BUILDING, opacity: 0.8 };
if (object.class === "sign") return { color: "#a78bfa" };
const flow = readings.flow?.value;
if (stale || typeof flow !== "number") return { color: palette.stale, label: "no reading" };
return { color: flowColour(flow), glow: 0.4, label: `${flow} veh/h` };
};Anchor40.63150° N 22.96550° E
East, north+0.0 m +0.0 m
Digital ObjectDigital ShadowDigital TwinStale
Mapbox / MapLibre