@brandlab-digital/site-core 3.4.0 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1074 @@
1
+ /**
2
+ * Service-area maps (Brandlab 3.6.0): shared boundary data and the map engine.
3
+ * Behaviour only, never appearance: this module turns the registry's NSW suburb
4
+ * boundaries (and a site's own towns) into plain, serialisable map models of SVG
5
+ * path strings, a viewBox, label points, link targets and a state per shape. Each
6
+ * site draws the models in its own style.
7
+ *
8
+ * Three models, one per page type:
9
+ *
10
+ * - `overviewMap`: the home page's "where we work" and the locations hub. Served
11
+ * regions highlighted, others muted, the base marked, region label points.
12
+ * - `regionMap`: one region's suburbs. Indexed suburbs are linked, served suburbs
13
+ * without an indexable page are served but unlinked, the rest muted; job points.
14
+ * - `suburbLocator`: a suburb or town, its distance-ranked nearest neighbours and
15
+ * nearby jobs. Undefined when there are no neighbours or jobs to show.
16
+ *
17
+ * Every model splits into a static base layer (land, the focus area, served
18
+ * shapes and outlines), which a site serves once as a cached SVG file
19
+ * (`renderAreaMapBaseSvg`) and references with `<use>`, and a small inline
20
+ * overlay of linked shapes, labels and markers. Only indexable pages are ever
21
+ * linked: link state comes from the site's estate and indexing decisions.
22
+ *
23
+ * Server data: like `site-core/geography`, this entry refuses browser bundles.
24
+ * Build the models on the server and pass plain values to components.
25
+ */
26
+ import { decodeAreaMapArc, simplifyAreaMapLine, } from './area-map-encode.js';
27
+ import boundaries from './area-map-data.json' with { type: 'json' };
28
+ import { centralCoastGeography, getSuburbCentre, sydneyGeography } from './geography.js';
29
+ /**
30
+ * The registry boundary document: NSW Spatial Services suburb boundaries for the
31
+ * Sydney and Central Coast registries, simplified, with regions, sub-regions,
32
+ * councils and land (standards/registry/nsw-area-map-boundaries.json).
33
+ */
34
+ export const areaMapBoundaries = boundaries;
35
+ /** The credit every map drawn from the shared boundaries shows, in its caption (CC BY 4.0 requires it). */
36
+ export const AREA_MAP_ATTRIBUTION = areaMapBoundaries.attribution;
37
+ const lonLatArcs = new WeakMap();
38
+ function arcLonLat(document, index) {
39
+ let cache = lonLatArcs.get(document);
40
+ if (!cache) {
41
+ cache = new Array(document.arcs.length);
42
+ lonLatArcs.set(document, cache);
43
+ }
44
+ let points = cache[index];
45
+ if (!points) {
46
+ const arc = document.arcs[index];
47
+ if (arc === undefined)
48
+ throw new Error(`Area-map arc ${index} does not exist.`);
49
+ const [scaleX, scaleY] = document.transform.scale;
50
+ const [originX, originY] = document.transform.translate;
51
+ points = decodeAreaMapArc(arc).map(([x, y]) => [Math.round((originX + x * scaleX) * 1e7) / 1e7, Math.round((originY + y * scaleY) * 1e7) / 1e7]);
52
+ cache[index] = points;
53
+ }
54
+ return points;
55
+ }
56
+ const arcIndexOf = (reference) => (reference < 0 ? ~reference : reference);
57
+ function ringLonLat(document, ring) {
58
+ const points = [];
59
+ for (const reference of ring) {
60
+ const arc = arcLonLat(document, arcIndexOf(reference));
61
+ const ordered = reference < 0 ? [...arc].reverse() : arc;
62
+ points.push(...(points.length > 0 ? ordered.slice(1) : ordered));
63
+ }
64
+ return points;
65
+ }
66
+ function shapeLonLat(document, key) {
67
+ const rings = Object.hasOwn(document.shapes, key) ? document.shapes[key] : undefined;
68
+ return rings?.map((ring) => ringLonLat(document, ring));
69
+ }
70
+ const registries = [sydneyGeography, centralCoastGeography];
71
+ /** A registry slug as the boundaries key it: a direct locality, region or sub-region first, then a legacy alias. */
72
+ function resolveRegistrySlug(slug) {
73
+ const shapes = areaMapBoundaries.shapes;
74
+ if (Object.hasOwn(shapes, `suburb:${slug}`) || Object.hasOwn(shapes, `region:${slug}`))
75
+ return slug;
76
+ for (const registry of registries) {
77
+ if (Object.hasOwn(registry.aliases, slug))
78
+ return registry.aliases[slug];
79
+ }
80
+ return slug;
81
+ }
82
+ /** A registry suburb's or locality's boundary (legacy slugs resolve), as longitude, latitude rings. */
83
+ export function suburbOutline(slug) {
84
+ return shapeLonLat(areaMapBoundaries, `suburb:${resolveRegistrySlug(slug)}`);
85
+ }
86
+ /** A registry region's or sub-region's outline: its localities dissolved into one shape. */
87
+ export function regionOutline(slug) {
88
+ return shapeLonLat(areaMapBoundaries, `region:${slug}`);
89
+ }
90
+ /**
91
+ * A council's outline as the registry assigns localities to it (each locality's
92
+ * primary council), dissolved. Only the localities in the registries count, so a
93
+ * council that extends beyond Sydney and the Central Coast is drawn in part.
94
+ */
95
+ export function councilOutline(slug) {
96
+ return shapeLonLat(areaMapBoundaries, `council:${slug}`);
97
+ }
98
+ /** All land the shared boundaries cover: the registries and the land around them. Water is everything else. */
99
+ export function landOutline() {
100
+ return shapeLonLat(areaMapBoundaries, 'land') ?? [];
101
+ }
102
+ /** A site-owned place's boundary from the site's document (`brandlab-area-map places`). */
103
+ export function placeOutline(document, slug) {
104
+ return shapeLonLat(document, `place:${slug}`);
105
+ }
106
+ /** Why a boundary document cannot be read: its kind, transform, arcs, shapes and credit. Empty when it is sound. */
107
+ export function areaMapBoundaryProblems(document) {
108
+ const problems = [];
109
+ if (document?.kind !== 'brandlab-area-map-boundaries')
110
+ return ['not a Brandlab area-map boundary document'];
111
+ if (!document.attribution?.trim())
112
+ problems.push('no attribution');
113
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(document.generated ?? ''))
114
+ problems.push('generated is not a YYYY-MM-DD date');
115
+ const { scale, translate } = document.transform ?? {};
116
+ if (!scale || !translate || ![...scale, ...translate].every(Number.isFinite) || !(scale[0] > 0 && scale[1] > 0)) {
117
+ problems.push('the transform needs a positive scale and a translate');
118
+ return problems;
119
+ }
120
+ document.arcs.forEach((arc, index) => {
121
+ try {
122
+ if (decodeAreaMapArc(arc).length < 2)
123
+ problems.push(`arc ${index} has fewer than two points`);
124
+ }
125
+ catch (error) {
126
+ problems.push(`arc ${index}: ${error.message}`);
127
+ }
128
+ });
129
+ for (const [key, rings] of Object.entries(document.shapes)) {
130
+ if (!/^(?:land|context|(?:suburb|region|council|place):[a-z0-9]+(?:-[a-z0-9]+)*)$/.test(key))
131
+ problems.push(`shape ${key}: unknown key`);
132
+ if (rings.length === 0)
133
+ problems.push(`shape ${key}: no rings`);
134
+ for (const ring of rings) {
135
+ for (const reference of ring) {
136
+ if (!Number.isInteger(reference) || arcIndexOf(reference) >= document.arcs.length)
137
+ problems.push(`shape ${key}: arc ${reference} does not exist`);
138
+ }
139
+ }
140
+ }
141
+ return problems;
142
+ }
143
+ /** The box around rings and centres; undefined when there is nothing to measure. */
144
+ export function areaMapBounds(items) {
145
+ let west = Infinity;
146
+ let south = Infinity;
147
+ let east = -Infinity;
148
+ let north = -Infinity;
149
+ const add = (longitude, latitude) => {
150
+ west = Math.min(west, longitude);
151
+ east = Math.max(east, longitude);
152
+ south = Math.min(south, latitude);
153
+ north = Math.max(north, latitude);
154
+ };
155
+ for (const item of items) {
156
+ if ('latitude' in item)
157
+ add(item.longitude, item.latitude);
158
+ else
159
+ for (const ring of item)
160
+ for (const [longitude, latitude] of ring)
161
+ add(longitude, latitude);
162
+ }
163
+ return Number.isFinite(west) ? { west, south, east, north } : undefined;
164
+ }
165
+ const DEGREE_KM = 111.32;
166
+ const round = (value, places) => {
167
+ const factor = 10 ** places;
168
+ return Math.round(value * factor) / factor;
169
+ };
170
+ /** The frame that fits a box: centred, padded, optionally at a set aspect, with a whole-number width and height. */
171
+ export function fitAreaMapFrame(bounds, options = {}) {
172
+ const width = options.width ?? 1000;
173
+ const padding = options.padding ?? 0.08;
174
+ if (!Number.isInteger(width) || width < 50)
175
+ throw new Error('fitAreaMapFrame width must be a whole number of 50 or more.');
176
+ if (!(padding >= 0 && padding < 1))
177
+ throw new Error('fitAreaMapFrame padding must be from 0 up to 1.');
178
+ const middle = (bounds.north + bounds.south) / 2;
179
+ const cos = Math.cos((middle * Math.PI) / 180);
180
+ const minimum = (options.minSpanKm ?? 0) / DEGREE_KM;
181
+ let spanX = Math.max((bounds.east - bounds.west) * cos, minimum, 1e-4);
182
+ let spanY = Math.max(bounds.north - bounds.south, minimum, 1e-4);
183
+ const margin = padding * Math.max(spanX, spanY);
184
+ spanX += 2 * margin;
185
+ spanY += 2 * margin;
186
+ if (options.aspect !== undefined) {
187
+ if (!(options.aspect > 0))
188
+ throw new Error('fitAreaMapFrame aspect must be positive.');
189
+ if (spanY / spanX < options.aspect)
190
+ spanY = spanX * options.aspect;
191
+ else
192
+ spanX = spanY / options.aspect;
193
+ }
194
+ const centreX = (bounds.west + bounds.east) / 2;
195
+ const centreY = (bounds.north + bounds.south) / 2;
196
+ const scale = round(width / spanX, 4);
197
+ return {
198
+ width,
199
+ height: Math.max(1, Math.round(spanY * scale)),
200
+ west: round(centreX - spanX / cos / 2, 6),
201
+ north: round(centreY + spanY / 2, 6),
202
+ cos: round(cos, 8),
203
+ scale,
204
+ };
205
+ }
206
+ /** A point's position in a frame, in map units (one decimal place). */
207
+ export function projectAreaMapPoint(frame, centre) {
208
+ return {
209
+ x: round((centre.longitude - frame.west) * frame.cos * frame.scale, 1),
210
+ y: round((frame.north - centre.latitude) * frame.scale, 1),
211
+ };
212
+ }
213
+ function frameBounds(frame) {
214
+ return {
215
+ west: frame.west,
216
+ north: frame.north,
217
+ east: frame.west + frame.width / (frame.cos * frame.scale),
218
+ south: frame.north - frame.height / frame.scale,
219
+ };
220
+ }
221
+ const intersects = (a, b) => a.west <= b.east && b.west <= a.east && a.south <= b.north && b.south <= a.north;
222
+ const CLIP_MARGIN = 4;
223
+ const projectedCache = new WeakMap();
224
+ function projectedArc(document, index, frame, options) {
225
+ let frames = projectedCache.get(document);
226
+ if (!frames) {
227
+ frames = new Map();
228
+ projectedCache.set(document, frames);
229
+ }
230
+ const frameKey = `${frame.west},${frame.north},${frame.cos},${frame.scale},${options.tolerance},${options.precision}`;
231
+ let arcs = frames.get(frameKey);
232
+ if (!arcs) {
233
+ arcs = new Map();
234
+ frames.set(frameKey, arcs);
235
+ // Keep the last few frames: one page draws one or two maps.
236
+ if (frames.size > 6)
237
+ frames.delete(frames.keys().next().value);
238
+ }
239
+ let points = arcs.get(index);
240
+ if (!points) {
241
+ const projected = arcLonLat(document, index).map(([longitude, latitude]) => [(longitude - frame.west) * frame.cos * frame.scale, (frame.north - latitude) * frame.scale]);
242
+ points = [];
243
+ for (const [x, y] of simplifyAreaMapLine(projected, options.tolerance)) {
244
+ const point = [round(x, options.precision), round(y, options.precision)];
245
+ const last = points[points.length - 1];
246
+ if (!last || last[0] !== point[0] || last[1] !== point[1])
247
+ points.push(point);
248
+ }
249
+ arcs.set(index, points);
250
+ }
251
+ return points;
252
+ }
253
+ function projectedRings(document, key, frame, options) {
254
+ const rings = Object.hasOwn(document.shapes, key) ? document.shapes[key] : [];
255
+ return rings.map((ring) => {
256
+ const points = [];
257
+ for (const reference of ring) {
258
+ const arc = projectedArc(document, arcIndexOf(reference), frame, options);
259
+ const ordered = reference < 0 ? [...arc].reverse() : arc;
260
+ points.push(...(points.length > 0 ? ordered.slice(1) : ordered));
261
+ }
262
+ return points;
263
+ });
264
+ }
265
+ function clipBox(frame) {
266
+ return [-CLIP_MARGIN, -CLIP_MARGIN, frame.width + CLIP_MARGIN, frame.height + CLIP_MARGIN];
267
+ }
268
+ /** Sutherland-Hodgman: a ring clipped to a box (even-odd fill keeps holes right). */
269
+ function clipRing(ring, box, precision) {
270
+ let minX = Infinity;
271
+ let minY = Infinity;
272
+ let maxX = -Infinity;
273
+ let maxY = -Infinity;
274
+ for (const [x, y] of ring) {
275
+ minX = Math.min(minX, x);
276
+ maxX = Math.max(maxX, x);
277
+ minY = Math.min(minY, y);
278
+ maxY = Math.max(maxY, y);
279
+ }
280
+ if (maxX < box[0] || minX > box[2] || maxY < box[1] || minY > box[3])
281
+ return [];
282
+ if (minX >= box[0] && maxX <= box[2] && minY >= box[1] && maxY <= box[3])
283
+ return [...ring];
284
+ let output = [...ring];
285
+ const edges = [
286
+ [box[0], 0, 1],
287
+ [box[2], 0, -1],
288
+ [box[1], 1, 1],
289
+ [box[3], 1, -1],
290
+ ];
291
+ for (const [limit, axis, direction] of edges) {
292
+ const input = output;
293
+ output = [];
294
+ if (input.length === 0)
295
+ break;
296
+ const inside = (point) => (point[axis] - limit) * direction >= 0;
297
+ for (let index = 0; index < input.length; index += 1) {
298
+ const current = input[index];
299
+ const previous = input[(index - 1 + input.length) % input.length];
300
+ const crossing = () => {
301
+ const t = (limit - previous[axis]) / (current[axis] - previous[axis]);
302
+ const point = [previous[0] + t * (current[0] - previous[0]), previous[1] + t * (current[1] - previous[1])];
303
+ point[axis] = limit;
304
+ point[1 - axis] = round(point[1 - axis], precision);
305
+ return point;
306
+ };
307
+ if (inside(current)) {
308
+ if (!inside(previous))
309
+ output.push(crossing());
310
+ output.push(current);
311
+ }
312
+ else if (inside(previous))
313
+ output.push(crossing());
314
+ }
315
+ }
316
+ return output;
317
+ }
318
+ function formatNumber(value, precision) {
319
+ const text = String(round(value, precision));
320
+ return text === '-0' ? '0' : text;
321
+ }
322
+ /** Rings as one SVG path: absolute move, relative line-tos, closed; rings with under three corners are dropped. */
323
+ function ringsPath(rings, precision) {
324
+ const parts = [];
325
+ for (const ring of rings) {
326
+ const points = ring.filter((point, index) => index === 0 || point[0] !== ring[index - 1][0] || point[1] !== ring[index - 1][1]);
327
+ if (points.length > 1 && points[0][0] === points[points.length - 1][0] && points[0][1] === points[points.length - 1][1])
328
+ points.pop();
329
+ if (points.length < 3)
330
+ continue;
331
+ let path = `M${formatNumber(points[0][0], precision)} ${formatNumber(points[0][1], precision)}l`;
332
+ let first = true;
333
+ for (let index = 1; index < points.length; index += 1) {
334
+ for (const delta of [points[index][0] - points[index - 1][0], points[index][1] - points[index - 1][1]]) {
335
+ const text = formatNumber(delta, precision);
336
+ path += first || text.startsWith('-') ? text : ` ${text}`;
337
+ first = false;
338
+ }
339
+ }
340
+ parts.push(`${path}z`);
341
+ }
342
+ return parts.join('');
343
+ }
344
+ function linesPath(lines, precision) {
345
+ const parts = [];
346
+ for (const line of lines) {
347
+ if (line.length < 2)
348
+ continue;
349
+ let path = `M${formatNumber(line[0][0], precision)} ${formatNumber(line[0][1], precision)}l`;
350
+ let first = true;
351
+ for (let index = 1; index < line.length; index += 1) {
352
+ for (const delta of [line[index][0] - line[index - 1][0], line[index][1] - line[index - 1][1]]) {
353
+ const text = formatNumber(delta, precision);
354
+ path += first || text.startsWith('-') ? text : ` ${text}`;
355
+ first = false;
356
+ }
357
+ }
358
+ parts.push(path);
359
+ }
360
+ return parts.join('');
361
+ }
362
+ /** Liang-Barsky: a polyline cut to a box, as the runs that stay inside. */
363
+ function clipLine(line, box, precision) {
364
+ const runs = [];
365
+ let run = [];
366
+ for (let index = 1; index < line.length; index += 1) {
367
+ const [x0, y0] = line[index - 1];
368
+ const [x1, y1] = line[index];
369
+ const dx = x1 - x0;
370
+ const dy = y1 - y0;
371
+ let t0 = 0;
372
+ let t1 = 1;
373
+ let visible = true;
374
+ for (const [p, q] of [
375
+ [-dx, x0 - box[0]],
376
+ [dx, box[2] - x0],
377
+ [-dy, y0 - box[1]],
378
+ [dy, box[3] - y0],
379
+ ]) {
380
+ if (p === 0) {
381
+ if (q < 0)
382
+ visible = false;
383
+ }
384
+ else {
385
+ const t = q / p;
386
+ if (p < 0)
387
+ t0 = Math.max(t0, t);
388
+ else
389
+ t1 = Math.min(t1, t);
390
+ }
391
+ }
392
+ if (!visible || t0 > t1) {
393
+ if (run.length > 1)
394
+ runs.push(run);
395
+ run = [];
396
+ continue;
397
+ }
398
+ const start = t0 === 0 ? [x0, y0] : [round(x0 + t0 * dx, precision), round(y0 + t0 * dy, precision)];
399
+ const end = t1 === 1 ? [x1, y1] : [round(x0 + t1 * dx, precision), round(y0 + t1 * dy, precision)];
400
+ if (run.length === 0)
401
+ run.push(start);
402
+ run.push(end);
403
+ if (t1 < 1) {
404
+ if (run.length > 1)
405
+ runs.push(run);
406
+ run = [];
407
+ }
408
+ }
409
+ if (run.length > 1)
410
+ runs.push(run);
411
+ return runs;
412
+ }
413
+ /** A shape's rings in a frame: projected, simplified to the frame, rounded and clipped to it. */
414
+ function frameRings(document, key, frame, options) {
415
+ const box = clipBox(frame);
416
+ return projectedRings(document, key, frame, options)
417
+ .map((ring) => clipRing(ring, box, options.precision))
418
+ .filter((ring) => ring.length >= 3);
419
+ }
420
+ /** Draws longitude, latitude rings in a frame as one SVG path (even-odd fill), simplified and clipped to it. */
421
+ export function areaMapPath(frame, rings, options = {}) {
422
+ const resolved = { tolerance: options.tolerance ?? 0.6, precision: options.precision ?? 0 };
423
+ const box = clipBox(frame);
424
+ return ringsPath(rings.map((ring) => {
425
+ const projected = ring.map(([longitude, latitude]) => [(longitude - frame.west) * frame.cos * frame.scale, (frame.north - latitude) * frame.scale]);
426
+ const simplified = simplifyAreaMapLine(projected, resolved.tolerance).map(([x, y]) => [round(x, resolved.precision), round(y, resolved.precision)]);
427
+ return clipRing(simplified, box, resolved.precision);
428
+ }), resolved.precision);
429
+ }
430
+ function insideRings(x, y, rings) {
431
+ let inside = false;
432
+ for (const ring of rings) {
433
+ for (let index = 0, previous = ring.length - 1; index < ring.length; previous = index++) {
434
+ const [xi, yi] = ring[index];
435
+ const [xj, yj] = ring[previous];
436
+ if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi)
437
+ inside = !inside;
438
+ }
439
+ }
440
+ return inside;
441
+ }
442
+ function signedDistance(x, y, rings) {
443
+ let minimum = Infinity;
444
+ for (const ring of rings) {
445
+ for (let index = 0, previous = ring.length - 1; index < ring.length; previous = index++) {
446
+ const [ax, ay] = ring[previous];
447
+ const [bx, by] = ring[index];
448
+ const dx = bx - ax;
449
+ const dy = by - ay;
450
+ const t = dx === 0 && dy === 0 ? 0 : Math.max(0, Math.min(1, ((x - ax) * dx + (y - ay) * dy) / (dx * dx + dy * dy)));
451
+ minimum = Math.min(minimum, (x - ax - t * dx) ** 2 + (y - ay - t * dy) ** 2);
452
+ }
453
+ }
454
+ return (insideRings(x, y, rings) ? 1 : -1) * Math.sqrt(minimum);
455
+ }
456
+ /**
457
+ * The pole of inaccessibility of even-odd rings: the inside point farthest from
458
+ * every edge (a grid search refined to `precision` map units), so a label sits
459
+ * inside even a crescent or a coastal shape, where a centroid can fall outside.
460
+ */
461
+ export function areaMapLabelPoint(rings, precision = 1) {
462
+ const shape = rings.filter((ring) => ring.length >= 3);
463
+ let minX = Infinity;
464
+ let minY = Infinity;
465
+ let maxX = -Infinity;
466
+ let maxY = -Infinity;
467
+ for (const ring of shape) {
468
+ for (const [x, y] of ring) {
469
+ minX = Math.min(minX, x);
470
+ maxX = Math.max(maxX, x);
471
+ minY = Math.min(minY, y);
472
+ maxY = Math.max(maxY, y);
473
+ }
474
+ }
475
+ if (!Number.isFinite(minX))
476
+ return undefined;
477
+ const size = Math.min(maxX - minX, maxY - minY);
478
+ if (size <= 0)
479
+ return { x: round(minX, 1), y: round(minY, 1), room: 0 };
480
+ const cell = (x, y, half) => {
481
+ const distance = signedDistance(x, y, shape);
482
+ return { x, y, half, distance, potential: distance + half * Math.SQRT2 };
483
+ };
484
+ // A max-heap on potential.
485
+ const heap = [];
486
+ const push = (item) => {
487
+ heap.push(item);
488
+ let index = heap.length - 1;
489
+ while (index > 0) {
490
+ const parent = (index - 1) >> 1;
491
+ if (heap[parent].potential >= heap[index].potential)
492
+ break;
493
+ [heap[parent], heap[index]] = [heap[index], heap[parent]];
494
+ index = parent;
495
+ }
496
+ };
497
+ const pop = () => {
498
+ const top = heap[0];
499
+ const last = heap.pop();
500
+ if (heap.length > 0) {
501
+ heap[0] = last;
502
+ let index = 0;
503
+ for (;;) {
504
+ const left = index * 2 + 1;
505
+ const right = left + 1;
506
+ let largest = index;
507
+ if (left < heap.length && heap[left].potential > heap[largest].potential)
508
+ largest = left;
509
+ if (right < heap.length && heap[right].potential > heap[largest].potential)
510
+ largest = right;
511
+ if (largest === index)
512
+ break;
513
+ [heap[largest], heap[index]] = [heap[index], heap[largest]];
514
+ index = largest;
515
+ }
516
+ }
517
+ return top;
518
+ };
519
+ const half = size / 2;
520
+ for (let x = minX; x < maxX; x += size)
521
+ for (let y = minY; y < maxY; y += size)
522
+ push(cell(x + half, y + half, half));
523
+ let best = cell((minX + maxX) / 2, (minY + maxY) / 2, 0);
524
+ // Start from the largest ring's area centroid as well, as polylabel does.
525
+ let largestArea = 0;
526
+ for (const ring of shape) {
527
+ let area = 0;
528
+ let cx = 0;
529
+ let cy = 0;
530
+ for (let index = 0, previous = ring.length - 1; index < ring.length; previous = index++) {
531
+ const [ax, ay] = ring[previous];
532
+ const [bx, by] = ring[index];
533
+ const cross = ax * by - bx * ay;
534
+ area += cross;
535
+ cx += (ax + bx) * cross;
536
+ cy += (ay + by) * cross;
537
+ }
538
+ if (Math.abs(area) > largestArea && area !== 0) {
539
+ largestArea = Math.abs(area);
540
+ const candidate = cell(cx / (3 * area), cy / (3 * area), 0);
541
+ if (candidate.distance > best.distance)
542
+ best = candidate;
543
+ }
544
+ }
545
+ let guard = 0;
546
+ while (heap.length > 0 && guard < 20_000) {
547
+ guard += 1;
548
+ const next = pop();
549
+ if (next.distance > best.distance)
550
+ best = next;
551
+ if (next.potential - best.distance <= precision)
552
+ continue;
553
+ const quarter = next.half / 2;
554
+ push(cell(next.x - quarter, next.y - quarter, quarter));
555
+ push(cell(next.x + quarter, next.y - quarter, quarter));
556
+ push(cell(next.x - quarter, next.y + quarter, quarter));
557
+ push(cell(next.x + quarter, next.y + quarter, quarter));
558
+ }
559
+ return { x: round(best.x, 1), y: round(best.y, 1), room: round(Math.max(0, best.distance), 1) };
560
+ }
561
+ function localitiesOf(registry) {
562
+ return [...registry.suburbs, ...registry.nonSuburbLocalities];
563
+ }
564
+ /**
565
+ * Binds a site's estate, indexing decisions, paths and site-owned towns for the
566
+ * map models. Throws when a published place has no boundary (a site-owned town
567
+ * needs `brandlab-area-map places`), so a missing shape never ships silently.
568
+ */
569
+ export function defineAreaMapSite(input) {
570
+ if (input.siteOwnedBoundaries) {
571
+ const problems = areaMapBoundaryProblems(input.siteOwnedBoundaries);
572
+ if (problems.length > 0)
573
+ throw new Error(`The site-owned area-map boundaries cannot be read: ${problems.slice(0, 5).join('; ')}.`);
574
+ }
575
+ const owned = new Map((input.siteOwnedPlaces ?? []).map((place) => [place.slug, place]));
576
+ const ordered = [input.registry, ...registries.filter((registry) => registry !== input.registry)];
577
+ const cache = new Map();
578
+ const resolve = (slug) => {
579
+ const town = owned.get(slug);
580
+ if (town) {
581
+ const key = `place:${slug}`;
582
+ const document = input.siteOwnedBoundaries;
583
+ return {
584
+ slug,
585
+ name: town.name,
586
+ kind: 'town',
587
+ centre: town.centre,
588
+ ...(document && Object.hasOwn(document.shapes, key) ? { shape: { document, key } } : {}),
589
+ };
590
+ }
591
+ const entry = input.estate.get(slug);
592
+ const direct = entry && !entry.siteOwned ? entry.registrySlug : slug;
593
+ for (const registry of ordered) {
594
+ const known = (candidate) => registry.regions.some((item) => item.slug === candidate) ||
595
+ registry.subRegions.some((item) => item.slug === candidate) ||
596
+ localitiesOf(registry).some((item) => item.slug === candidate);
597
+ const target = known(direct) ? direct : Object.hasOwn(registry.aliases, direct) ? registry.aliases[direct] : direct;
598
+ const region = registry.regions.find((entry) => entry.slug === target);
599
+ if (region)
600
+ return { slug, name: region.name, kind: 'region', shape: { document: areaMapBoundaries, key: `region:${target}` } };
601
+ const subRegion = registry.subRegions.find((entry) => entry.slug === target);
602
+ if (subRegion)
603
+ return { slug, name: subRegion.name, kind: 'subRegion', shape: { document: areaMapBoundaries, key: `region:${target}` } };
604
+ const locality = localitiesOf(registry).find((entry) => entry.slug === target);
605
+ if (locality) {
606
+ const centre = getSuburbCentre(target);
607
+ return {
608
+ slug,
609
+ name: locality.name,
610
+ kind: 'suburb',
611
+ shape: { document: areaMapBoundaries, key: `suburb:${target}` },
612
+ ...(centre ? { centre } : {}),
613
+ };
614
+ }
615
+ }
616
+ return undefined;
617
+ };
618
+ const site = {
619
+ input,
620
+ place(slug) {
621
+ if (!cache.has(slug))
622
+ cache.set(slug, resolve(slug));
623
+ return cache.get(slug);
624
+ },
625
+ stateOf(slug) {
626
+ if (!input.estate.has(slug))
627
+ return 'muted';
628
+ return input.decisions[slug]?.indexing === 'index' ? 'linked' : 'served';
629
+ },
630
+ };
631
+ const problems = [];
632
+ for (const slug of input.estate.published) {
633
+ const place = site.place(slug);
634
+ if (!place)
635
+ problems.push(`${slug}: not a registry place or a site-owned place`);
636
+ else if (!place.shape || !Object.hasOwn(place.shape.document.shapes, place.shape.key)) {
637
+ problems.push(place.kind === 'town'
638
+ ? `${slug}: no boundary; write the site-owned boundaries with brandlab-area-map places`
639
+ : `${slug}: no boundary in the shared area-map data`);
640
+ }
641
+ }
642
+ if (problems.length > 0)
643
+ throw new Error(`Area maps cannot draw every published place:\n- ${problems.join('\n- ')}`);
644
+ return site;
645
+ }
646
+ function pathOptions(options) {
647
+ return { tolerance: options.path?.tolerance ?? 0.6, precision: options.path?.precision ?? 0 };
648
+ }
649
+ const boundsCache = new WeakMap();
650
+ function shapeBounds(shape) {
651
+ let cache = boundsCache.get(shape.document);
652
+ if (!cache) {
653
+ cache = new Map();
654
+ boundsCache.set(shape.document, cache);
655
+ }
656
+ if (!cache.has(shape.key)) {
657
+ const rings = shapeLonLat(shape.document, shape.key);
658
+ cache.set(shape.key, rings ? areaMapBounds([rings]) : undefined);
659
+ }
660
+ return cache.get(shape.key);
661
+ }
662
+ function unionBounds(items) {
663
+ const present = items.filter((item) => item !== undefined);
664
+ if (present.length === 0)
665
+ return undefined;
666
+ return {
667
+ west: Math.min(...present.map((item) => item.west)),
668
+ south: Math.min(...present.map((item) => item.south)),
669
+ east: Math.max(...present.map((item) => item.east)),
670
+ north: Math.max(...present.map((item) => item.north)),
671
+ };
672
+ }
673
+ function centreBounds(centre) {
674
+ return { west: centre.longitude, east: centre.longitude, south: centre.latitude, north: centre.latitude };
675
+ }
676
+ function documentOf(site, key) {
677
+ if (key.startsWith('place:'))
678
+ return site.input.siteOwnedBoundaries;
679
+ return areaMapBoundaries;
680
+ }
681
+ function labelFor(site, place, state, frame, options) {
682
+ if (!place.shape)
683
+ return undefined;
684
+ const point = areaMapLabelPoint(frameRings(place.shape.document, place.shape.key, frame, options), 0.5);
685
+ if (!point || point.room <= 0)
686
+ return undefined;
687
+ return { key: place.shape.key, slug: place.slug, name: place.name, kind: place.kind, state, ...point };
688
+ }
689
+ function inside(frame, point) {
690
+ return point.x >= 0 && point.y >= 0 && point.x <= frame.width && point.y <= frame.height;
691
+ }
692
+ function markersFor(site, frame, options) {
693
+ const markers = [];
694
+ const base = options.base ? site.place(options.base) : undefined;
695
+ if (base?.centre) {
696
+ const point = projectAreaMapPoint(frame, base.centre);
697
+ if (inside(frame, point))
698
+ markers.push({ kind: 'base', slug: base.slug, name: base.name, ...point, count: 0 });
699
+ }
700
+ const counts = new Map();
701
+ for (const slug of options.jobs ?? [])
702
+ counts.set(slug, (counts.get(slug) ?? 0) + 1);
703
+ for (const [slug, count] of counts) {
704
+ const place = site.place(slug);
705
+ if (!place?.centre)
706
+ continue;
707
+ const point = projectAreaMapPoint(frame, place.centre);
708
+ if (inside(frame, point))
709
+ markers.push({ kind: 'job', slug: place.slug, name: place.name, ...point, count });
710
+ }
711
+ return markers;
712
+ }
713
+ function cropFor(frame, focus, aspect) {
714
+ const whole = { x: 0, y: 0, width: frame.width, height: frame.height };
715
+ if (!focus)
716
+ return whole;
717
+ const project = (longitude, latitude) => projectAreaMapPoint(frame, { longitude, latitude });
718
+ const topLeft = project(focus.west, focus.north);
719
+ const bottomRight = project(focus.east, focus.south);
720
+ const margin = 0.06 * Math.max(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);
721
+ let x0 = Math.max(0, topLeft.x - margin);
722
+ let y0 = Math.max(0, topLeft.y - margin);
723
+ let x1 = Math.min(frame.width, bottomRight.x + margin);
724
+ let y1 = Math.min(frame.height, bottomRight.y + margin);
725
+ let width = x1 - x0;
726
+ let height = y1 - y0;
727
+ if (height / width < aspect)
728
+ height = width * aspect;
729
+ else
730
+ width = height / aspect;
731
+ if (width > frame.width || height > frame.height) {
732
+ const fit = Math.min(frame.width / width, frame.height / height);
733
+ width *= fit;
734
+ height *= fit;
735
+ }
736
+ const centreX = (x0 + x1) / 2;
737
+ const centreY = (y0 + y1) / 2;
738
+ x0 = Math.min(Math.max(0, centreX - width / 2), frame.width - width);
739
+ y0 = Math.min(Math.max(0, centreY - height / 2), frame.height - height);
740
+ return { x: round(x0, 1), y: round(y0, 1), width: round(width, 1), height: round(height, 1) };
741
+ }
742
+ function shapeFor(site, place, frame, options) {
743
+ if (!place.shape)
744
+ return undefined;
745
+ const d = ringsPath(frameRings(place.shape.document, place.shape.key, frame, options), options.precision);
746
+ if (!d)
747
+ return undefined;
748
+ return { key: place.shape.key, slug: place.slug, name: place.name, kind: place.kind, state: 'linked', href: site.input.href(place.slug), d };
749
+ }
750
+ function model(kind, id, frame, parts) {
751
+ return {
752
+ kind,
753
+ id,
754
+ width: frame.width,
755
+ height: frame.height,
756
+ viewBox: `0 0 ${frame.width} ${frame.height}`,
757
+ frame,
758
+ ...parts,
759
+ attribution: AREA_MAP_ATTRIBUTION,
760
+ };
761
+ }
762
+ /** The regions of a registry and the published suburbs and towns of the site inside each. */
763
+ function regionStates(site) {
764
+ const { registry, estate } = site.input;
765
+ const publishedRegion = new Map();
766
+ for (const slug of estate.published) {
767
+ const place = site.place(slug);
768
+ if (place?.kind !== 'suburb')
769
+ continue;
770
+ const target = resolveRegistrySlug(slug);
771
+ const locality = localitiesOf(registry).find((entry) => entry.slug === target);
772
+ if (locality)
773
+ publishedRegion.set(locality.regionSlug, true);
774
+ }
775
+ return registry.regions.map((region) => {
776
+ const state = site.stateOf(region.slug);
777
+ return {
778
+ slug: region.slug,
779
+ state: state !== 'muted' ? state : publishedRegion.has(region.slug) ? 'served' : 'muted',
780
+ };
781
+ });
782
+ }
783
+ /**
784
+ * The home page's and locations hub's map: every region in the frame outlined,
785
+ * served regions highlighted (linked when the region page is indexable), the
786
+ * others muted, published towns, the base and job places marked, and a label
787
+ * point for each region and town.
788
+ */
789
+ export function overviewMap(site, options = {}) {
790
+ const drawing = pathOptions(options);
791
+ const regions = regionStates(site);
792
+ const towns = site.input.estate.published.flatMap((slug) => {
793
+ const place = site.place(slug);
794
+ return place?.kind === 'town' ? [{ place, state: site.stateOf(slug) }] : [];
795
+ });
796
+ const servedRegions = regions.filter((region) => region.state !== 'muted');
797
+ const fit = options.fit ?? (servedRegions.length > 0 ? 'registry' : 'site-owned');
798
+ const fitted = unionBounds([
799
+ ...(fit !== 'site-owned' ? servedRegions.map((region) => shapeBounds({ document: areaMapBoundaries, key: `region:${region.slug}` })) : []),
800
+ ...(fit !== 'registry' ? towns.map((town) => (town.place.shape ? shapeBounds(town.place.shape) : undefined)) : []),
801
+ ]);
802
+ const focus = fitted ??
803
+ unionBounds(site.input.registry.regions.map((region) => shapeBounds({ document: areaMapBoundaries, key: `region:${region.slug}` })));
804
+ const frame = fitAreaMapFrame(focus, { width: options.width ?? 1000, padding: options.padding ?? 0.1 });
805
+ const visibleBounds = frameBounds(frame);
806
+ const stateOfRegion = new Map(regions.map((region) => [region.slug, region.state]));
807
+ const visible = registries
808
+ .flatMap((registry) => registry.regions)
809
+ .filter((region, index, all) => all.findIndex((entry) => entry.slug === region.slug) === index)
810
+ .filter((region) => {
811
+ const bounds = shapeBounds({ document: areaMapBoundaries, key: `region:${region.slug}` });
812
+ return bounds !== undefined && intersects(bounds, visibleBounds);
813
+ });
814
+ const shapes = [];
815
+ const labels = [];
816
+ const served = [];
817
+ for (const region of visible) {
818
+ const state = stateOfRegion.get(region.slug) ?? 'muted';
819
+ const place = site.place(region.slug) ?? {
820
+ slug: region.slug,
821
+ name: region.name,
822
+ kind: 'region',
823
+ shape: { document: areaMapBoundaries, key: `region:${region.slug}` },
824
+ };
825
+ if (state === 'linked') {
826
+ const shape = shapeFor(site, place, frame, drawing);
827
+ if (shape)
828
+ shapes.push(shape);
829
+ }
830
+ else if (state === 'served')
831
+ served.push(`region:${region.slug}`);
832
+ const label = labelFor(site, place, state, frame, drawing);
833
+ if (label)
834
+ labels.push(label);
835
+ }
836
+ for (const town of towns) {
837
+ if (!town.place.shape)
838
+ continue;
839
+ const bounds = shapeBounds(town.place.shape);
840
+ if (!bounds || !intersects(bounds, visibleBounds))
841
+ continue;
842
+ if (town.state === 'linked') {
843
+ const shape = shapeFor(site, town.place, frame, drawing);
844
+ if (shape)
845
+ shapes.push(shape);
846
+ }
847
+ else
848
+ served.push(town.place.shape.key);
849
+ const label = labelFor(site, town.place, town.state, frame, drawing);
850
+ if (label)
851
+ labels.push(label);
852
+ }
853
+ return model('overview', 'overview', frame, {
854
+ shapes,
855
+ labels,
856
+ markers: markersFor(site, frame, options),
857
+ crop: cropFor(frame, fitted, options.phoneAspect ?? 1.15),
858
+ layers: {
859
+ focus: [],
860
+ served,
861
+ outline: [
862
+ ...visible.map((region) => `region:${region.slug}`),
863
+ ...towns.flatMap((town) => (town.place.shape ? [town.place.shape.key] : [])),
864
+ ],
865
+ },
866
+ });
867
+ }
868
+ function regionMembers(registry, regionSlug) {
869
+ const region = registry.regions.find((entry) => entry.slug === regionSlug);
870
+ if (region)
871
+ return localitiesOf(registry).filter((locality) => locality.regionSlug === regionSlug);
872
+ const subRegion = registry.subRegions.find((entry) => entry.slug === regionSlug);
873
+ if (!subRegion)
874
+ return [];
875
+ return localitiesOf(registry).filter((locality) => locality.regionSlug === subRegion.parentRegionSlug &&
876
+ ((subRegion.councilSlugs?.includes(locality.councilSlug) ?? false) || (subRegion.suburbSlugs?.includes(locality.slug) ?? false)));
877
+ }
878
+ /**
879
+ * A region page's map: the region's localities, indexed suburbs linked (their
880
+ * pages), served suburbs without an indexable page served but unlinked, the rest
881
+ * muted; neighbouring regions outlined for context; job places and the base
882
+ * marked; a label point for every locality in the region.
883
+ */
884
+ export function regionMap(site, regionSlug, options = {}) {
885
+ const drawing = pathOptions(options);
886
+ const region = site.place(regionSlug);
887
+ if (!region || (region.kind !== 'region' && region.kind !== 'subRegion') || !region.shape) {
888
+ throw new Error(`regionMap: ${regionSlug} is not a registry region or sub-region.`);
889
+ }
890
+ const registry = [site.input.registry, ...registries].find((entry) => regionMembers(entry, resolveRegistrySlug(regionSlug)).length > 0);
891
+ const members = registry ? regionMembers(registry, resolveRegistrySlug(regionSlug)) : [];
892
+ const regionBounds = shapeBounds(region.shape);
893
+ const frame = fitAreaMapFrame(regionBounds, { width: options.width ?? 1000, padding: options.padding ?? 0.06 });
894
+ const visibleBounds = frameBounds(frame);
895
+ // The site may publish a member under a legacy slug: state and links follow the published slug.
896
+ const publishedFor = new Map();
897
+ for (const slug of site.input.estate.published)
898
+ publishedFor.set(resolveRegistrySlug(slug), slug);
899
+ const shapes = [];
900
+ const labels = [];
901
+ const served = [];
902
+ for (const member of [...members].sort((a, b) => a.name.localeCompare(b.name))) {
903
+ const slug = publishedFor.get(member.slug) ?? member.slug;
904
+ const place = site.place(slug);
905
+ if (!place?.shape)
906
+ continue;
907
+ const state = site.stateOf(slug);
908
+ if (state === 'linked') {
909
+ const shape = shapeFor(site, place, frame, drawing);
910
+ if (shape)
911
+ shapes.push(shape);
912
+ }
913
+ else if (state === 'served')
914
+ served.push(place.shape.key);
915
+ const label = labelFor(site, place, state, frame, drawing);
916
+ if (label)
917
+ labels.push(label);
918
+ }
919
+ const neighbours = registries
920
+ .flatMap((entry) => entry.regions)
921
+ .filter((entry, index, all) => all.findIndex((other) => other.slug === entry.slug) === index)
922
+ .filter((entry) => {
923
+ const bounds = shapeBounds({ document: areaMapBoundaries, key: `region:${entry.slug}` });
924
+ return bounds !== undefined && intersects(bounds, visibleBounds);
925
+ })
926
+ .map((entry) => `region:${entry.slug}`);
927
+ return model('region', `region-${region.slug}`, frame, {
928
+ shapes,
929
+ labels,
930
+ markers: markersFor(site, frame, options),
931
+ crop: cropFor(frame, regionBounds, options.phoneAspect ?? 1.15),
932
+ layers: {
933
+ focus: [region.shape.key],
934
+ served,
935
+ outline: [...new Set([...neighbours, ...members.map((member) => `suburb:${member.slug}`)])],
936
+ },
937
+ });
938
+ }
939
+ /**
940
+ * A suburb or town page's locator: the place itself (focus), its nearest
941
+ * neighbours (linked when indexable, in the order given), nearby job places and
942
+ * the base, with every locality border in the frame for context. Undefined when
943
+ * there are no neighbours and no jobs to show: omit the map then.
944
+ */
945
+ export function suburbLocator(site, slug, options) {
946
+ const drawing = pathOptions(options);
947
+ const focus = site.place(slug);
948
+ if (!focus?.shape || (focus.kind !== 'suburb' && focus.kind !== 'town')) {
949
+ throw new Error(`suburbLocator: ${slug} is not a published suburb or town with a boundary.`);
950
+ }
951
+ const neighbours = options.neighbours
952
+ .filter((neighbour) => neighbour !== slug)
953
+ .flatMap((neighbour) => {
954
+ const place = site.place(neighbour);
955
+ return place?.shape ? [place] : [];
956
+ });
957
+ const jobPlaces = (options.jobs ?? []).flatMap((job) => site.place(job)?.centre ?? []);
958
+ if (neighbours.length === 0 && jobPlaces.length === 0)
959
+ return undefined;
960
+ const focusBounds = shapeBounds(focus.shape);
961
+ const fitted = unionBounds([
962
+ focusBounds,
963
+ ...neighbours.map((neighbour) => (neighbour.centre ? centreBounds(neighbour.centre) : shapeBounds(neighbour.shape))),
964
+ ...jobPlaces.map(centreBounds),
965
+ ]);
966
+ const frame = fitAreaMapFrame(fitted, {
967
+ width: options.width ?? 800,
968
+ padding: options.padding ?? 0.16,
969
+ minSpanKm: options.minSpanKm ?? 2.5,
970
+ });
971
+ const visibleBounds = frameBounds(frame);
972
+ const shapes = [];
973
+ const labels = [];
974
+ const served = [];
975
+ const focusLabel = labelFor(site, focus, 'focus', frame, drawing);
976
+ if (focusLabel)
977
+ labels.push(focusLabel);
978
+ for (const neighbour of neighbours) {
979
+ const state = site.stateOf(neighbour.slug);
980
+ if (state === 'linked') {
981
+ const shape = shapeFor(site, neighbour, frame, drawing);
982
+ if (shape)
983
+ shapes.push(shape);
984
+ }
985
+ else if (state === 'served')
986
+ served.push(neighbour.shape.key);
987
+ const label = labelFor(site, neighbour, state, frame, drawing);
988
+ if (label)
989
+ labels.push(label);
990
+ }
991
+ const context = localitiesOf(sydneyGeography)
992
+ .concat(localitiesOf(centralCoastGeography))
993
+ .map((locality) => `suburb:${locality.slug}`)
994
+ .concat(Object.keys(site.input.siteOwnedBoundaries?.shapes ?? {}).filter((key) => key.startsWith('place:')))
995
+ .filter((key, index, all) => all.indexOf(key) === index)
996
+ .filter((key) => {
997
+ const document = documentOf(site, key);
998
+ const bounds = document ? shapeBounds({ document, key }) : undefined;
999
+ return bounds !== undefined && intersects(bounds, visibleBounds);
1000
+ });
1001
+ return model('suburb', `suburb-${focus.slug}`, frame, {
1002
+ shapes,
1003
+ labels,
1004
+ markers: markersFor(site, frame, options),
1005
+ crop: cropFor(frame, fitted, options.phoneAspect ?? 1.15),
1006
+ layers: { focus: [focus.shape.key], served, outline: context },
1007
+ });
1008
+ }
1009
+ // ---------------------------------------------------------------------------
1010
+ // The static base layer
1011
+ // ---------------------------------------------------------------------------
1012
+ /** The base layer's groups, by id: what `<use href="<file>#<id>">` draws. */
1013
+ export const AREA_MAP_LAYER_IDS = ['land', 'focus', 'served', 'outline'];
1014
+ /** The base layer groups a model draws (land and outline always; focus and served when it has them). */
1015
+ export function areaMapLayerIds(model) {
1016
+ return AREA_MAP_LAYER_IDS.filter((id) => id === 'land' || id === 'outline' || model.layers[id].length > 0);
1017
+ }
1018
+ function fillPath(site, keys, frame, options) {
1019
+ const rings = [];
1020
+ for (const key of keys) {
1021
+ const document = documentOf(site, key);
1022
+ if (document && Object.hasOwn(document.shapes, key))
1023
+ rings.push(...frameRings(document, key, frame, options));
1024
+ }
1025
+ return ringsPath(rings, options.precision);
1026
+ }
1027
+ function outlinePath(site, keys, frame, options) {
1028
+ const box = clipBox(frame);
1029
+ const lines = [];
1030
+ const seen = new Map();
1031
+ for (const key of keys) {
1032
+ const document = documentOf(site, key);
1033
+ if (!document || !Object.hasOwn(document.shapes, key))
1034
+ continue;
1035
+ if (!seen.has(document))
1036
+ seen.set(document, new Set());
1037
+ const arcs = seen.get(document);
1038
+ for (const ring of document.shapes[key]) {
1039
+ for (const reference of ring) {
1040
+ const index = arcIndexOf(reference);
1041
+ if (arcs.has(index))
1042
+ continue;
1043
+ arcs.add(index);
1044
+ lines.push(...clipLine(projectedArc(document, index, frame, options), box, options.precision));
1045
+ }
1046
+ }
1047
+ }
1048
+ return linesPath(lines, options.precision);
1049
+ }
1050
+ /**
1051
+ * The static base layer of a model as an SVG document: `land` (all land in the
1052
+ * frame; water is the space around it), `focus`, `served` and `outline` (unique
1053
+ * borders, `fill="none"`, non-scaling strokes). The groups set no colours: the
1054
+ * page draws each with `<use href="<file>#<id>">` and styles the `<use>` with its
1055
+ * own tokens. Serve it from a static route, never inline.
1056
+ */
1057
+ export function renderAreaMapBaseSvg(site, model, options = {}) {
1058
+ const drawing = { tolerance: options.tolerance ?? 0.6, precision: options.precision ?? 0 };
1059
+ const { frame } = model;
1060
+ const siteLand = site.input.siteOwnedBoundaries;
1061
+ const land = ringsPath([
1062
+ ...frameRings(areaMapBoundaries, 'land', frame, drawing),
1063
+ ...(siteLand && Object.hasOwn(siteLand.shapes, 'land') ? frameRings(siteLand, 'land', frame, drawing) : []),
1064
+ ], drawing.precision);
1065
+ const groups = [`<g id="land"><path d="${land}" fill-rule="evenodd"/></g>`];
1066
+ if (model.layers.focus.length > 0)
1067
+ groups.push(`<g id="focus"><path d="${fillPath(site, model.layers.focus, frame, drawing)}" fill-rule="evenodd"/></g>`);
1068
+ if (model.layers.served.length > 0)
1069
+ groups.push(`<g id="served"><path d="${fillPath(site, model.layers.served, frame, drawing)}" fill-rule="evenodd"/></g>`);
1070
+ groups.push(`<g id="outline" fill="none"><path d="${outlinePath(site, model.layers.outline, frame, drawing)}" stroke-linejoin="round" stroke-linecap="round" vector-effect="non-scaling-stroke"/></g>`);
1071
+ const credit = model.attribution.replace(/--/g, '-').replace(/[<>&]/g, '');
1072
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${model.viewBox}" width="${frame.width}" height="${frame.height}"><!-- ${credit} -->${groups.join('')}</svg>\n`;
1073
+ }
1074
+ //# sourceMappingURL=area-map.js.map