Save, load and edit a scene
Pack a scene into a klorad.scene version 2 package, load only the parts a view needs, and apply edits on revisions with conflicts reported field by field.
A scene travels as a package, klorad.scene version 2: a manifest and the chunks it names.
Objects are grouped into chunks by a square grid around the scene's origin; each chunk carries
its bounds and the SHA-256 of its content, so a reader loads only the chunks its view overlaps
and refuses one that does not match. The manifest also carries the scene's layers, named views,
assets by id, external spaces, provenance, its value policy and, when asked, the latest reading
of every quantity.
import { chunksFrom, createScene, EMPTY_META, packScene, readPackedScene, unpackScene } from "@klorad/api/world";
const scene = createScene({ id: "station", name: "Pumping station", coordinateSystem: { origin: { lat: 40.6264, lon: 22.9484 } } });
scene.add({ id: "house", name: "Pump house", position: { east: 0, north: 0 }, class: "building", layer: "buildings" });
scene.add({
id: "pump-1",
name: "Pump 1",
position: { east: -4, north: 0 },
correspondence: "twin",
binding: { source: "scada", entity: "P1" },
class: "pump",
classifications: { ifc: "IfcPump" },
relations: [{ kind: "inside", to: "house" }],
});
// A manifest and its chunks: objects grouped by a grid of 250 m cells, each chunk hashed.
const packed = await packScene(scene, { ...EMPTY_META, layers: [{ id: "buildings", name: "Buildings" }] });
const json = JSON.stringify(packed);
// Reading accepts any scene document Klorad has written, version 1 snapshots included.
const read = await readPackedScene(JSON.parse(json));
// Load only what a view needs: the chunks that overlap a box, the layers asked for.
const { scene: near, loaded } = await unpackScene(read.manifest, chunksFrom(read.chunks), {
bounds: { west: 22.947, south: 40.625, east: 22.95, north: 40.628 },
layers: ["buildings"],
});
export const loadedNear = { chunks: loaded, objects: near.objects().length };readPackedScene accepts every scene document Klorad has written. A version 1 snapshot (from
snapshotScene) is upgraded in memory when it is read; the stored document is not rewritten,
so a scene saved by an earlier version of the packages still opens.
Classes, layers and relations
Every Scene Object may carry a class (Klorad's own, such as "pump"), the classifications of
other schemes it came with ({ ifc: "IfcPump" }, kept so nothing an import knew is lost), the
layer it belongs to, and relations to other objects ({ kind: "inside", to: "house" }).
A model geometry may name a file of the package as asset:<id>; packing refuses an id the
package does not list.
Edits on revisions
Edits are commands (add, update, remove) sent as a batch with the revision its author last
read. A batch is applied when nothing it touches changed after that revision, field by field:
two people who change different fields of one object both succeed; the second one to change the
same field gets a conflict naming the field and the revision that changed it. A batch is applied
whole or not at all.
import { createScene, findConflicts, planCommands, stampVersion, type ObjectVersion } from "@klorad/api/world";
const scene = createScene({ coordinateSystem: { origin: { lat: 40.6264, lon: 22.9484 } } });
scene.add({ id: "pump-1", name: "Pump 1", position: { east: 0, north: 0 }, properties: { setPoint: 4 } });
let revision = 1;
const versions = new Map<string, ObjectVersion>([["pump-1", { exists: true, fields: { "*": 1 } }]]);
// Two people read revision 1. One renames the pump; the other changes its set point.
for (const batch of [
{ base: 1, commands: [{ type: "update" as const, id: "pump-1", patch: { name: "Booster pump" } }] },
{ base: 1, commands: [{ type: "update" as const, id: "pump-1", patch: { properties: { setPoint: 4.5 } } }] },
{ base: 1, commands: [{ type: "update" as const, id: "pump-1", patch: { name: "Main pump" } }] },
]) {
const conflicts = findConflicts(batch, (id) => versions.get(id));
if (conflicts.length) {
console.warn("conflict", conflicts); // [{ objectId: "pump-1", field: "name", changedAt: 2 }]
continue;
}
const plan = planCommands(batch.commands, scene.get, scene.coordinateSystem); // throws on an invalid batch
revision += 1;
batch.commands.forEach((command, i) => {
versions.set(plan[i]!.id, stampVersion(command, revision, versions.get(plan[i]!.id)));
scene.update(command.id, command.patch);
});
}The scene store of Klorad One applies the same rules on the server, with one writer per scene, a journal by revision and the field revisions in its database. In a browser or a test, the functions above are the whole of it; the Revisions sandbox runs them live.
What a package does not carry
| In | Not in |
|---|---|
| Objects, by chunk, with class, layer and relations | The history of readings (it stays with the store) |
| Layers, named views, assets by id, external spaces, provenance | Connections to sources (connect them again) |
| The latest reading of each quantity, when packed with state | Interaction definitions and Access policies (they are code) |
| The revision it was packed at, the value policy | The field revisions of the store |
Something wrong or unclear? Tell us.