GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Solar and storage design / globalgrid2050 / solar-design-studio/202609270439-site-world/world/layers

Canopy and 1 more file

A JavaScript module in the layers folder of GlobalGrid2050 that sets index sha (`setIndexSha`), loads hedges (`loadHedges`) and calculates planar distances, covering canopy tiles, hedges and LiDAR terrain, using SHA-256.

399 lines of JavaScript from globalgrid2050, in 2 files. Topic: Solar and storage design. Section 154 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. Canopy ingest · globalgrid2050 / solar-design-studio/202609270439-site-world/world/layers/canopy.mjs lines 1–217
  2. Contours ingest · globalgrid2050 / solar-design-studio/202609270439-site-world/world/layers/contours.mjs lines 1–182

Canopy

A JavaScript module in the layers folder of GlobalGrid2050 that sets index sha (`setIndexSha`), loads hedges (`loadHedges`) and calculates planar distances, covering canopy tiles, hedges and LiDAR terrain, using SHA-256.

ingestglobalgrid2050 / solar-design-studio/202609270439-site-world/world/layers/canopy.mjs · lines 1–217 of 217 · version 2026-09-27 04:39 · Not reached from any entry point (candidate for review) · pattern decodeCanopyTile (shared by 2 files)

What it does
It lists and searches canopy tiles, hedges and LiDAR terrain. Defines 11 functions or classes, including setIndexSha, decodeCanopyTile, blockOffset, treeTicks, along.
Why it exists in the grid workflow
Domain stage sub33kv_design (path word 'design'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary. Domain terms in the code: LiDAR terrain, 400kV.
Inputs & outputs
In: JSON file canopy-tiles.json; JSON file hedges.json. Coordinate systems named: OSGB36 / EPSG:27700 (British National Grid). Voltage levels named: 400kV.
What it achieves
It sets index sha (`setIndexSha`), decodes canopy tile (`decodeCanopyTile`) and loads hedges (`loadHedges`). Hedges and tree canopies as sparse vertical ticks, each from the ground up to the real canopy height measured by the Environment Agency LiDAR (first-return surface minus bare earth). Its code works with 400 kV.
Why it matters
No other file in the estate was found to import it.
Application
globalgrid2050 · solar-design-studio/202609270439-site-world/world/layers · JavaScript module
Uses
none found
Used by
none found
Produces
setIndexSha, HEADER_BYTES, LOAD_RADIUS, TOWER_MASK_M, STRIDE, HEDGE_STEP, SOLID_STEP, COLORS, decodeCanopyTile, blockOffset, treeTicks, along
Defines here
setIndexSha, decodeCanopyTile, blockOffset, treeTicks, along, hedgeGeometry, finite, createCanopy, statusLine, loadHedges, hedges
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 solar-design-studio/202609270439-site-world/world/layers/canopy.mjs:1-217 · blob 1516a9180aa1c7bbc86d2b9795df28fdf0974ecc · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: transform · effects: none found · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: unreached_review
// Layer: canopy. Hedges and tree canopies as sparse vertical ticks, each from the ground up to the real canopy
// height measured by the Environment Agency LiDAR (first-return surface minus bare earth). Hedges are also solids:
// a walker cannot pass through one, the drone can fly over it. Off by default: until setEnabled(true) (or
// api.config.enabled) it fetches nothing and draws nothing. Tiles and hedge lines come from the lidar pipeline
// (lidar/src/canopy_tiles.py: two classification methods paired on the GPU, a CPU witness); every file is
// fetched through the substrate's hash check (api.fetchVerified). Heights of the ground come from ctx.heightAt.
//
// RULE: no buildings are shown. Only classes 1 (hedge) and 2 (tree) are ever drawn; class 3 (structure) and
// class 4 (uncertain: the two methods disagree, or vegetation on the rim of a structure) are skipped here even
// if a tile were to carry a height for them.
// TOWERS: a steel lattice tower returns first and last pulses like a tree, so the classifier can call it one. Every
// known tower (the grid layer's and the national layer's, from api.towers()) masks tree cells within TOWER_MASK_M.
//
// Tile format (.gcn, little-endian). 32-byte header:
//   0 magic "GGC1" | 4 u16 version=1 | 6 u16 samples=257 | 8 u16 spacing_mm | 10 u16 flags
//   12 i32 origin_e_m | 16 i32 origin_n_m (south-west corner) | 20 u16 height_step_mm (200) | 22 u16 reserved
//   24 u32 nodata_count | 28 u32 shown_count (classes 1 and 2)
//   Body: samples*samples u8 class, then samples*samples u8 canopy height in steps; rows south to north, west to east.
// Index (canopy-tiles.json): { tiles: [{ key, file, sha256, e0, n0, hedge, tree }], hedges: { file, sha256 } }.
// hedges.json: { lines: [{ id, pts: [[e, n, height_m], ...], width_m }] } in British National Grid metres.

const INDEX_PATH = 'canopy/canopy-tiles.json';
// Filled by the build tool with the SHA-256 of canopy-tiles.json. Until then the layer loads nothing.
let INDEX_SHA256 = '__CANOPY_TILES_SHA256__';
export function setIndexSha(sha) { INDEX_SHA256 = sha; }

export const HEADER_BYTES = 32, NODATA = 255, HEDGE = 1, TREE = 2, SHOWN = Object.freeze([HEDGE, TREE]);
export const LOAD_RADIUS = 300, MAX_IN_FLIGHT = 2, MAX_TILES = 16;
export const TOWER_MASK_M = 12;     // a 400 kV lattice base is about 8 to 10 m square; its arms reach further up
export const STRIDE = 4;            // one tree tick per 4 m square, at a fixed pseudo-random cell inside it
export const HEDGE_STEP = 1.5;      // metres between hedge ticks along a line
export const SOLID_STEP = 1, SOLID_RADIUS = 60, SOLID_MIN_HALF = 0.5, SOLID_MAX_HALF = 1.5, SOLID_SINK = 0.5;
export const COLORS = { tree: [0.50, 0.72, 0.52, 0.44], hedge: [0.78, 0.84, 0.52, 0.62], hedgeTop: [0.74, 0.80, 0.50, 0.30] };

// Pure: bytes -> { samples, spacing, e0, n0, step, nodata, shown, cls, h }.
export function decodeCanopyTile(buffer) {
  const bytes = buffer instanceof ArrayBuffer ? buffer : buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
  if (bytes.byteLength < HEADER_BYTES) throw Error('canopy tile shorter than its header');
  const dv = new DataView(bytes);
  const magic = String.fromCharCode(dv.getUint8(0), dv.getUint8(1), dv.getUint8(2), dv.getUint8(3));
  if (magic !== 'GGC1') throw Error(`canopy tile magic "${magic}" is not GGC1`);
  const version = dv.getUint16(4, true);
  if (version !== 1) throw Error(`canopy tile version ${version} is not 1`);
  const samples = dv.getUint16(6, true), spacingMm = dv.getUint16(8, true), stepMm = dv.getUint16(20, true);
  if (samples < 2 || spacingMm === 0 || stepMm === 0) throw Error('canopy tile has no usable grid');
  const n = samples * samples;
  if (bytes.byteLength !== HEADER_BYTES + 2 * n) throw Error(`canopy tile is ${bytes.byteLength} bytes, expected ${HEADER_BYTES + 2 * n}`);
  return {
    samples, spacing: spacingMm / 1000, e0: dv.getInt32(12, true), n0: dv.getInt32(16, true), step: stepMm / 1000,
    nodata: dv.getUint32(24, true), shown: dv.getUint32(28, true),
    cls: new Uint8Array(bytes, HEADER_BYTES, n), h: new Uint8Array(bytes, HEADER_BYTES + n, n)
  };
}

// A fixed pseudo-random offset per block, so ticks do not stand in rows but are the same every frame.
export function blockOffset(bi, bj, stride) {
  let x = (Math.imul(bi, 73856093) ^ Math.imul(bj, 19349663)) >>> 0;
  x = Math.imul(x ^ (x >>> 15), 2246822519) >>> 0; x = (x ^ (x >>> 13)) >>> 0;
  return [x % stride, (x >>> 8) % stride];
}

// Pure: vertical tree ticks for one tile. x0, y0: the tile's south-west corner in local metres. Only class 2.
// Returns Float32Array of segments (6 floats each) relative to origin.
// mask: tower positions [[x, y], ...] in the same local metres; tree cells within maskM of one are not drawn.
export function treeTicks(t, x0, y0, heightAt, { stride = STRIDE, origin = [0, 0, 0], mask = [], maskM = TOWER_MASK_M } = {}) {
  const out = [], s = t.samples, last = s - 1, size = last * t.spacing; // the last row and column belong to the next tile
  const m = mask.filter(([mx, my]) => mx > x0 - maskM && mx < x0 + size + maskM && my > y0 - maskM && my < y0 + size + maskM);
  for (let bj = 0; bj * stride < last; bj++) {
    for (let bi = 0; bi * stride < last; bi++) {
      const [oi, oj] = blockOffset(bi + Math.round(t.e0 / stride), bj + Math.round(t.n0 / stride), stride);
      const i = bi * stride + oi, j = bj * stride + oj;
      if (i >= last || j >= last) continue;
      const k = j * s + i;
      if (t.cls[k] !== TREE || t.h[k] === 0) continue;
      const x = x0 + i * t.spacing, y = y0 + j * t.spacing;
      if (m.length && m.some(([mx, my]) => (x - mx) ** 2 + (y - my) ** 2 <= maskM * maskM)) continue;
      const g = finite(heightAt(x, y));
      out.push(x - origin[0], y - origin[1], g - origin[2], x - origin[0], y - origin[1], g + t.h[k] * t.step - origin[2]);
    }
  }
  return new Float32Array(out);
}

// Points every `step` metres along a polyline [[x, y, h], ...] (local metres), height interpolated. Pure.
export function along(pts, step) {
  const out = [];
  for (let k = 0; k + 1 < pts.length; k++) {
    const [ax, ay, ah] = pts[k], [bx, by, bh] = pts[k + 1], L = Math.hypot(bx - ax, by - ay);
    const n = Math.max(1, Math.ceil(L / step));
    for (let m = k === 0 ? 0 : 1; m <= n; m++) { const f = m / n; out.push([ax + (bx - ax) * f, ay + (by - ay) * f, ah + (bh - ah) * f]); }
  }
  return out;
}

// Pure: hedge ticks, a faint top line and solid boxes. lines: hedges.json lines; en: site origin { e, n }.
// Returns { ticks: Float32Array, tops: Float32Array, solids: [{ min, max, line }] } (ticks/tops relative to origin).
export function hedgeGeometry(lines, en, heightAt, { step = HEDGE_STEP, solidStep = SOLID_STEP, origin = [0, 0, 0] } = {}) {
  const ticks = [], tops = [], solids = [];
  for (const l of lines || []) {
    if (!Array.isArray(l.pts) || l.pts.length < 2) continue;
    const pts = l.pts.map(([e, n, h]) => [e - en.e, n - en.n, Math.max(0, +h || 0)]);
    let prev = null;
    for (const [x, y, h] of along(pts, step)) {
      const g = finite(heightAt(x, y)), top = [x - origin[0], y - origin[1], g + h - origin[2]];
      ticks.push(x - origin[0], y - origin[1], g - origin[2], ...top);
      if (prev) tops.push(...prev, ...top);
      prev = top;
    }
    const half = Math.min(SOLID_MAX_HALF, Math.max(SOLID_MIN_HALF, (+l.width_m || 1) / 2));
    for (const [x, y, h] of along(pts, solidStep)) {
      if (!(h > 0)) continue;
      const g = finite(heightAt(x, y));
      solids.push({ min: [x - half, y - half, g - SOLID_SINK], max: [x + half, y + half, g + h], line: l.id });
    }
  }
  return { ticks: new Float32Array(ticks), tops: new Float32Array(tops), solids };
}

const finite = h => (Number.isFinite(h) ? h : 0);
// The layer: tiles come through the shared loader (api.lib.tiles), which the substrate hash-checks.
export const LIB_VERSION = 1;

export function createCanopy() {
  let api = null, tl = null, enabled = false;
  let hedgeLines = null, hedgeBuilt = null;  // hedgeBuilt: { version, heightAt, batches, solids }

  function statusLine() {
    const n = tl?.index ? tl.index.length : 0, h = hedgeLines ? hedgeLines.length : 0;
    return `${n} canopy tiles, ${h} hedge lines; structures and uncertain cells hidden`;
  }

  // The hedge lines come with the index: fetched once it has arrived, through the same hash check.
  function loadHedges(j, dir) {
    if (!j.hedges?.file || !j.hedges?.sha256) return;
    return api.fetchJSON(dir + j.hedges.file, j.hedges.sha256).then(hj => {
      if (!hj || !Array.isArray(hj.lines)) throw Error('hedges.json has no lines list');
      hedgeLines = hj.lines;
      layer.status = statusLine();
      api.invalidate({ ground: false });
    });
  }

  function hedges(ctx) {
    if (!hedgeLines) return null;
    const origin = tl.origin, version = `${ctx.groundVersion || 0}@${origin.e},${origin.n}`;
    if (!hedgeBuilt || hedgeBuilt.version !== version || hedgeBuilt.heightAt !== ctx.heightAt) {
      const o = [0, 0, finite(ctx.heightAt(0, 0))];
      const g = hedgeGeometry(hedgeLines, origin, ctx.heightAt, { origin: o });
      hedgeBuilt = { version, heightAt: ctx.heightAt, solids: g.solids, batches: [
        { key: 'canopy/hedge-ticks', version, origin: o, color: COLORS.hedge, positions: g.ticks },
        { key: 'canopy/hedge-tops', version, origin: o, color: COLORS.hedgeTop, positions: g.tops }] };
    }
    return hedgeBuilt;
  }

  const layer = {
    id: 'canopy',
    status: 'off',
    init(a) {
      if (!a.lib?.tiles) { layer.status = 'refused: api.lib.tiles not provided'; return; }
      api = a;
      tl = a.lib.tiles.createTileLoader(a, {
        file: 'canopy-tiles.json', path: INDEX_PATH, sha: () => INDEX_SHA256, tileNoun: 'canopy tile', decode: decodeCanopyTile,
        radius: LOAD_RADIUS, maxInFlight: MAX_IN_FLIGHT, maxTiles: MAX_TILES, active: () => enabled, keep: t => t.tree !== 0,
        say: t => { layer.status = t; }, onReady: () => { layer.status = statusLine(); }, afterReady: loadHedges,
        onIndexError: (e, whileLoading) => { layer.status = (whileLoading ? 'canopy-tiles.json' : 'hedges.json') + ' failed: ' + e.message; },
        onMove: () => { hedgeBuilt = null; }
      });
      if (a.config?.enabled) layer.setEnabled(true);
    },
    setEnabled(on) {
      enabled = !!on;
      layer.status = enabled ? (tl?.state === 'ready' ? statusLine() : 'on') : 'off';
      if (enabled) tl?.load();
      api?.invalidate({ ground: false });
      return enabled;
    },
    get enabled() { return enabled; },
    // ctx: { pos, heightAt, groundVersion }
    lines(ctx) {
      if (!api || !enabled) return [];
      tl.frame(ctx.pos);
      const gv = ctx.groundVersion || 0, out = [], o0 = tl.origin;
      const mask = (typeof api.towers === 'function' ? api.towers() : []).filter(p => Number.isFinite(p?.e) && Number.isFinite(p?.n))
        .map(p => [p.e - o0.e, p.n - o0.n]);
      for (const [key, t] of tl.near(ctx.pos)) {
        const size = (t.tile.samples - 1) * t.tile.spacing, pad = TOWER_MASK_M;
        const mine = mask.filter(([x, y]) => x > t.x0 - pad && x < t.x0 + size + pad && y > t.y0 - pad && y < t.y0 + size + pad);
        const version = mine.length ? `${gv}|${mine.map(([x, y]) => `${x.toFixed(1)},${y.toFixed(1)}`).join(';')}` : `${gv}`; // new towers, new ticks
        if (!t.cache || t.cache.version !== version || t.cache.heightAt !== ctx.heightAt) {
          const cx = t.x0 + t.size / 2, cy = t.y0 + t.size / 2, o = [cx, cy, finite(ctx.heightAt(cx, cy))];
          const positions = treeTicks(t.tile, t.x0, t.y0, ctx.heightAt, { origin: o, mask: mine });
          t.cache = { version, heightAt: ctx.heightAt, batch: { key: `canopy/tree/${key}`, version, origin: o, color: COLORS.tree, positions } };
        }
        tl.touch(t);
        if (t.cache.batch.positions.length) out.push(t.cache.batch);
      }
      const h = hedges(ctx);
      if (h) out.push(...h.batches.filter(b => b.positions.length));
      return out;
    },
    // Hedge boxes near the viewer (built with the ground the last lines() call saw). Walkers are blocked;
    // each box stops at the hedge top, so the drone passes over.
    solids(ctx) {
      if (!enabled || !hedgeBuilt || !ctx?.pos) return [];
      const [x, y] = ctx.pos;
      const d = api.lib.tiles.rectDist;
      return hedgeBuilt.solids.filter(b => d(x, y, b.min[0], b.min[1], b.max[0], b.max[1]) <= SOLID_RADIUS);
    },
    // For tests and the console.
    debug: () => ({ ...tl?.debug(), enabled,
      hedgeLines: hedgeLines ? hedgeLines.length : 0, solids: hedgeBuilt ? hedgeBuilt.solids.length : 0, ...tl?.stats })
  };
  return layer;
}

export default createCanopy();

Contours

A JavaScript module in the layers folder of GlobalGrid2050 that sets index sha, decodes contour tile and calculates planar distances, covering contour tiles and LiDAR terrain, using Douglas-Peucker and SHA-256.

ingestglobalgrid2050 / solar-design-studio/202609270439-site-world/world/layers/contours.mjs · lines 1–182 of 182 · version 2026-09-27 04:39 · Not reached from any entry point (candidate for review) · pattern decodeContourTile (shared by 2 files)

What it does
It lists and searches contour tiles and LiDAR terrain. Defines 14 functions or classes, including statusLine, setIndexSha, multiple, isIndex, onInterval.
Why it exists in the grid workflow
Domain stage sub33kv_design (path word 'design'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary. Domain terms in the code: LiDAR terrain.
Inputs & outputs
In: JSON file contour-tiles.json. Coordinate systems named: OSGB36 / EPSG:27700 (British National Grid).
What it achieves
It sets index sha (`setIndexSha`) and decodes contour tile (`decodeContourTile`). Contour lines draped 5 cm above the ground at a chosen interval (0.5, 1 or 5 m; default 1 m), with every 5 m line (an index contour) drawn slightly brighter.
Why it matters
No other file in the estate was found to import it.
Application
globalgrid2050 · solar-design-studio/202609270439-site-world/world/layers · JavaScript module
Uses
none found
Used by
none found
Produces
CAVEAT, EA_CREDIT, statusLine, setIndexSha, INTERVALS, LIFT_M, LOAD_RADIUS, LABEL_MIN_M, COLORS, isIndex, onInterval, decodeContourTile
Defines here
statusLine, setIndexSha, multiple, isIndex, onInterval, decodeContourTile, segments, labelsFor, fmt, drape, ivKey, createContours, ready, d
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 solar-design-studio/202609270439-site-world/world/layers/contours.mjs:1-182 · blob 132dfa1989b4a1ca65e6f089c3305284bbb8d170 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: transform · effects: none found · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: unreached_review
// Layer: contours. Contour lines draped 5 cm above the ground at a chosen interval (0.5, 1 or 5 m; default
// 1 m), with every 5 m line (an index contour) drawn slightly brighter. Optional height labels on index
// contours. Off by default: until setEnabled(true) (or api.config.enabled) it fetches nothing and draws
// nothing. Lines come from ggc1 tiles written by the lidar pipeline (lidar/src/contour_tiles.py: every
// 0.5 m level traced twice on the GPU, marching squares against row and column crossings, with a CPU
// witness; Douglas-Peucker simplified at 0.25 m). Every file is fetched through the substrate's hash check
// (api.fetchJSON with the sha256 from the index). Heights for draping come from ctx.heightAt, the ground.
//
// Tile (ggc1 JSON): { format: 'ggc1', e0, n0, tile_m: 256, unit_m: 0.01, dp_tol_m,
//   levels: [{ z: metres, lines: [[x0, y0, x1, y1, ...], ...] }] }  x, y whole centimetres from the tile's
//   south-west corner (British National Grid metres = e0 + x * unit_m). Heights: metres above ODN.

const INDEX_PATH = 'contours/contour-tiles.json';
// Filled by the build tool with the SHA-256 of contour-tiles.json. Until then the layer loads nothing.
let INDEX_SHA256 = '__CONTOUR_TILES_SHA256__';
// Said wherever the layer's status is shown: what the lines are drawn from, and how far they may sit
// from the true level in plan (the index's dp_tol_basis: traced chords plus simplification, 0.553 m measured), then
// the Environment Agency's credit (the index's attribution when it carries one).
export const CAVEAT = 'computed from the EA LIDAR terrain; up to about 0.55 m from the true contour between vertices; ' +
  'heights in metres above Ordnance Datum Newlyn';
export const EA_CREDIT = '© Environment Agency copyright and/or database right 2022. All rights reserved.';
export const statusLine = (tiles, credit = EA_CREDIT) => `index: ${tiles} contour tiles; ${CAVEAT}. ${credit || EA_CREDIT}`;

export function setIndexSha(sha) { INDEX_SHA256 = sha; }

export const INTERVALS = [0.5, 1, 5], DEFAULT_INTERVAL = 1, INDEX_EVERY = 5;
export const LIFT_M = 0.05, STEP_M = 2;                      // 5 cm above ground; re-drape at least every 2 m
export const LOAD_RADIUS = 300, MAX_IN_FLIGHT = 2, MAX_TILES = 16;
export const LABEL_MIN_M = 40, MAX_LABELS = 48;
// Quiet on a dark background: minor lines a pale sage, index lines a little brighter and firmer.
export const COLORS = { minor: [0.62, 0.68, 0.60, 0.30], index: [0.80, 0.84, 0.74, 0.52] };

const multiple = (z, step) => Math.abs(z / step - Math.round(z / step)) < 1e-9;
export const isIndex = z => multiple(z, INDEX_EVERY);
export const onInterval = (z, iv) => multiple(z, iv);

// Pure: checks a parsed ggc1 tile and returns it with numbers in metres ready to use.
export function decodeContourTile(doc) {
  if (!doc || doc.format !== 'ggc1') throw Error(`contour tile format "${doc && doc.format}" is not ggc1`);
  if (!Number.isFinite(doc.e0) || !Number.isFinite(doc.n0)) throw Error('contour tile has no south-west corner');
  const unit = doc.unit_m ?? 0.01;
  if (!(unit > 0)) throw Error('contour tile has no unit');
  if (!Array.isArray(doc.levels)) throw Error('contour tile has no levels list');
  for (const lv of doc.levels) {
    if (!Number.isFinite(lv.z) || !Array.isArray(lv.lines)) throw Error('contour level without z or lines');
    for (const l of lv.lines) if (!Array.isArray(l) || l.length % 2 || l.length < 4) throw Error(`contour line at ${lv.z} m is not a list of x, y pairs`);
  }
  return { e0: doc.e0, n0: doc.n0, unit, size: doc.tile_m || 256, tol: doc.dp_tol_m, levels: doc.levels };
}

// Pure: line segments for one tile at one interval, as { minor, index } Float32Arrays of segment pairs.
// x0, y0: the tile's south-west corner in local metres. Each stored segment is split so no drawn piece is
// longer than `step`, and every vertex is draped at heightAt + lift. Positions are relative to origin.
export function segments(t, x0, y0, interval, heightAt, { lift = LIFT_M, step = STEP_M, origin = [0, 0, 0] } = {}) {
  const out = { minor: [], index: [] }, u = t.unit;
  for (const lv of t.levels) {
    if (!onInterval(lv.z, interval)) continue;
    const dst = isIndex(lv.z) ? out.index : out.minor;
    for (const l of lv.lines) {
      let px = x0 + l[0] * u, py = y0 + l[1] * u, pz = drape(heightAt, px, py, lift);
      for (let k = 2; k < l.length; k += 2) {
        const qx = x0 + l[k] * u, qy = y0 + l[k + 1] * u, n = Math.max(1, Math.ceil(Math.hypot(qx - px, qy - py) / step));
        let ax = px, ay = py, az = pz;
        for (let s = 1; s <= n; s++) {
          const x = s === n ? qx : px + (qx - px) * s / n, y = s === n ? qy : py + (qy - py) * s / n, z = drape(heightAt, x, y, lift);
          dst.push(ax - origin[0], ay - origin[1], az - origin[2], x - origin[0], y - origin[1], z - origin[2]);
          ax = x; ay = y; az = z;
        }
        px = qx; py = qy; pz = az;
      }
    }
  }
  return { minor: new Float32Array(out.minor), index: new Float32Array(out.index) };
}

// Pure: one label per index contour line at least LABEL_MIN_M long, at the vertex nearest its middle,
// turned along the line. Local metres (not origin-relative); the host's text overlay draws them.
export function labelsFor(t, x0, y0, heightAt, { lift = LIFT_M, minLen = LABEL_MIN_M } = {}) {
  const out = [], u = t.unit;
  for (const lv of t.levels) {
    if (!isIndex(lv.z)) continue;
    for (const l of lv.lines) {
      const cum = [0];
      for (let k = 2; k < l.length; k += 2) cum.push(cum[cum.length - 1] + Math.hypot(l[k] - l[k - 2], l[k + 1] - l[k - 1]) * u);
      const len = cum[cum.length - 1];
      if (len < minLen) continue;
      let m = 0;
      for (let i = 1; i < cum.length; i++) if (Math.abs(cum[i] - len / 2) < Math.abs(cum[m] - len / 2)) m = i;
      const j = Math.min(m + 1, cum.length - 1), i = j - 1;
      const x = x0 + l[2 * m] * u, y = y0 + l[2 * m + 1] * u;
      out.push({ pos: [x, y, drape(heightAt, x, y, lift)], text: `${fmt(lv.z)} m`, z: lv.z,
        angle: Math.atan2(l[2 * j + 1] - l[2 * i + 1], l[2 * j] - l[2 * i]) });
    }
  }
  return out;
}

const fmt = z => (Number.isInteger(z) ? String(z) : z.toFixed(1));
const drape = (h, x, y, lift) => { const v = h(x, y); return (Number.isFinite(v) ? v : 0) + lift; };
const ivKey = iv => iv.toFixed(1);   // the index writes by_interval keys as "0.5", "1.0", "5.0"

// The layer: tiles come through the shared loader (api.lib.tiles), which the substrate hash-checks.
export const LIB_VERSION = 1;

export function createContours() {
  let api = null, tl = null, enabled = false, interval = DEFAULT_INTERVAL, labelsOn = false, credit = EA_CREDIT;
  const ready = () => statusLine(tl.index.length, credit);

  const layer = {
    id: 'contours',
    status: 'off',
    init(a) {
      if (!a.lib?.tiles) { layer.status = 'refused: api.lib.tiles not provided'; return; }
      api = a;
      tl = a.lib.tiles.createTileLoader(a, {
        file: 'contour-tiles.json', path: INDEX_PATH, sha: () => INDEX_SHA256, tileNoun: 'contour tile', json: true,
        decode: decodeContourTile, size: t => t.size, radius: LOAD_RADIUS, maxInFlight: MAX_IN_FLIGHT, maxTiles: MAX_TILES,
        active: () => enabled, keep: t => (t.by_interval?.[ivKey(interval)]?.lines ?? 1) > 0,
        say: t => { layer.status = t; }, onReady: () => { layer.status = ready(); },
        onIndex: j => { if (typeof j?.attribution === 'string' && /Environment Agency/.test(j.attribution)) credit = j.attribution; },
        onMove: () => { for (const t of tl.loaded.values()) t.labels = null; }
      });
      if (INTERVALS.includes(a.config?.interval)) interval = a.config.interval;
      if (a.config?.labels) labelsOn = true;
      if (a.config?.enabled) layer.setEnabled(true);
    },
    setEnabled(on) {
      enabled = !!on;
      layer.status = enabled ? (tl?.state === 'ready' ? ready() : 'on') : 'off';
      if (enabled) tl?.load();
      api?.invalidate({ ground: false });
      return enabled;
    },
    setInterval(m) {
      if (!INTERVALS.includes(m)) throw Error(`contour interval must be one of ${INTERVALS.join(', ')} m`);
      interval = m;
      tl?.pump();
      api?.invalidate({ ground: false });
      return interval;
    },
    setLabels(on) { labelsOn = !!on; api?.invalidate({ ground: false }); return labelsOn; },
    get enabled() { return enabled; },
    get interval() { return interval; },
    get labelsOn() { return labelsOn; },
    // ctx: { pos, heightAt, groundVersion }
    lines(ctx) {
      if (!api || !enabled) return [];
      tl.frame(ctx.pos);
      const gv = ctx.groundVersion || 0, out = [], version = `${interval}@${gv}`;
      for (const [key, t] of tl.near(ctx.pos)) {
        if (!t.cache || t.cache.version !== version || t.cache.heightAt !== ctx.heightAt) {
          const cx = t.x0 + t.size / 2, cy = t.y0 + t.size / 2, o = [cx, cy, drape(ctx.heightAt, cx, cy, 0)];
          const s = segments(t.tile, t.x0, t.y0, interval, ctx.heightAt, { origin: o });
          t.cache = { version, heightAt: ctx.heightAt, batches: ['minor', 'index'].filter(k => s[k].length).map(k => ({
            key: `contours/${key}/${k}`, version, origin: o, color: COLORS[k], positions: s[k] })) };
        }
        tl.touch(t);
        out.push(...t.cache.batches);
      }
      return out;
    },
    // Optional height labels on index contours, nearest tiles first: [{ pos, text, z, angle }]. Empty
    // unless setLabels(true); the renderer needs a text overlay to show them.
    labels(ctx) {
      if (!api || !enabled || !labelsOn) return [];
      const out = [], gv = ctx.groundVersion || 0;
      for (const [, t] of tl.near(ctx.pos)) {
        if (!t.labels || t.labels.gv !== gv || t.labels.heightAt !== ctx.heightAt) {
          t.labels = { gv, heightAt: ctx.heightAt, list: labelsFor(t.tile, t.x0, t.y0, ctx.heightAt) };
        }
        out.push(...t.labels.list);
      }
      const d = l => Math.hypot(l.pos[0] - ctx.pos[0], l.pos[1] - ctx.pos[1]);
      return out.sort((a, b) => d(a) - d(b)).slice(0, MAX_LABELS);
    },
    // For tests and the console.
    debug: () => ({ ...tl?.debug(), enabled, interval, labelsOn, ...tl?.stats })
  };
  return layer;
}

export default createContours();