@spellkave/world-map 0.0.0-bootstrap
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.
- package/LICENSE +89 -0
- package/README.md +67 -0
- package/bin/world-map.js +16 -0
- package/bin/world-osm.js +15 -0
- package/package.json +71 -0
- package/src/cli.ts +204 -0
- package/src/coords.ts +479 -0
- package/src/export.ts +861 -0
- package/src/ids.ts +81 -0
- package/src/index.ts +105 -0
- package/src/load-bundle.ts +155 -0
- package/src/model.ts +106 -0
- package/src/node/backdrop-resolve.ts +172 -0
- package/src/node/index.ts +39 -0
- package/src/node/jpeg-write.ts +266 -0
- package/src/node/rasterize.ts +27 -0
- package/src/node/terminal-capabilities.ts +89 -0
- package/src/node/terminal.ts +12 -0
- package/src/node/timg-backend.ts +136 -0
- package/src/node/tool-result-image.ts +53 -0
- package/src/poi.ts +61 -0
- package/src/rows.ts +480 -0
- package/src/svg/compose.ts +185 -0
- package/src/svg/h3-layout.ts +268 -0
- package/src/svg/index.ts +327 -0
- package/src/svg/inventory.ts +168 -0
- package/src/svg/layout.ts +431 -0
- package/src/svg/model.ts +459 -0
- package/src/svg/render-ascii.ts +486 -0
- package/src/svg/render-braille.ts +130 -0
- package/src/svg/render-svg.ts +578 -0
- package/src/svg/scale.ts +99 -0
- package/src/svg/types.ts +244 -0
- package/src/tags.ts +68 -0
- package/src/tiles/encode.ts +35 -0
- package/src/tiles/index.ts +25 -0
- package/src/tiles/layers.ts +338 -0
- package/src/tiles/style.ts +518 -0
- package/src/writers/geojson.ts +104 -0
- package/src/writers/json.ts +126 -0
- package/src/writers/xml.ts +87 -0
package/src/coords.ts
ADDED
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side place-graph projection (ADR-0029 D-1/D-3, ADR-0123 D-1).
|
|
3
|
+
*
|
|
4
|
+
* Consolidated from the retired spatial compatibility layer; this package now
|
|
5
|
+
* owns the projection and layout math for every host.
|
|
6
|
+
*
|
|
7
|
+
* Concepts:
|
|
8
|
+
* - **Sheet frame**: authored pos_x/pos_y; +x east, +y south (image rows).
|
|
9
|
+
* - **Local frame** (ADR-0029): +x east, +y north. Flip: ly = anchor_y − pos_y.
|
|
10
|
+
* - **Bearing**: clockwise degrees from north (projectLocalPosition).
|
|
11
|
+
* - **H3 origin**: root graph anchor sits on cellToLatLng(origin_cell_res10).
|
|
12
|
+
* - **Nest link**: portal edge parent↔child; child nest coincides with parent nest.
|
|
13
|
+
* - **Indoor children included** — the earlier H3 layout deliberately omitted
|
|
14
|
+
* indoor graphs; this generalisation is the product math (ADR-0123 D-1 case 3).
|
|
15
|
+
* - **No fake plane**: orphan roots without an origin cell are unplaced (D-1.5).
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { cellToLatLng, isValidCell, latLngToCell } from "h3-js";
|
|
19
|
+
|
|
20
|
+
import type { UnplacedGraph } from "./model.ts";
|
|
21
|
+
import type { EdgeRow, GraphRow, NodeRow, WorldOsmInput } from "./rows.ts";
|
|
22
|
+
import { normalizeWorldOsmInput } from "./rows.ts";
|
|
23
|
+
import { buildCoordinateKindMap, spatialClassOf } from "./tags.ts";
|
|
24
|
+
|
|
25
|
+
// ─── Projection math ─────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
export interface WorldOffset {
|
|
28
|
+
east_m: number;
|
|
29
|
+
north_m: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ProjectionParams {
|
|
33
|
+
bearing_deg: number;
|
|
34
|
+
scale: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Clockwise bearing; scale: graph unit → metres.
|
|
39
|
+
* Canonical `projectLocalPosition`; keep its established math byte-identical.
|
|
40
|
+
*/
|
|
41
|
+
export function projectLocalPosition(
|
|
42
|
+
params: ProjectionParams,
|
|
43
|
+
localX: number,
|
|
44
|
+
localY: number,
|
|
45
|
+
): WorldOffset {
|
|
46
|
+
const bearingRad = (params.bearing_deg * Math.PI) / 180;
|
|
47
|
+
const cosB = Math.cos(bearingRad);
|
|
48
|
+
const sinB = Math.sin(bearingRad);
|
|
49
|
+
const scale = params.scale > 0 ? params.scale : 1;
|
|
50
|
+
return {
|
|
51
|
+
east_m: scale * (localX * cosB + localY * sinB),
|
|
52
|
+
north_m: scale * (-localX * sinB + localY * cosB),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Port of shared tool runtime `composeProjection`. */
|
|
57
|
+
export function composeProjection(parent: WorldOffset, child: WorldOffset): WorldOffset {
|
|
58
|
+
return {
|
|
59
|
+
east_m: parent.east_m + child.east_m,
|
|
60
|
+
north_m: parent.north_m + child.north_m,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Mean metres per degree of latitude (WGS84 equirectangular; ADR-0123 G-3). */
|
|
65
|
+
export const METRES_PER_DEG_LAT = 111_320;
|
|
66
|
+
|
|
67
|
+
export function worldOffsetToLatLng(
|
|
68
|
+
off: WorldOffset,
|
|
69
|
+
lat0: number,
|
|
70
|
+
lng0: number,
|
|
71
|
+
): { lat: number; lon: number } {
|
|
72
|
+
const cosLat = Math.cos((lat0 * Math.PI) / 180);
|
|
73
|
+
const lat = lat0 + off.north_m / METRES_PER_DEG_LAT;
|
|
74
|
+
const lon = lng0 + off.east_m / (METRES_PER_DEG_LAT * (cosLat === 0 ? 1e-12 : cosLat));
|
|
75
|
+
return { lat, lon };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Lat/lng → plane (+x east, +y south for SVG y-down). */
|
|
79
|
+
export function latLngToPlane(
|
|
80
|
+
lat: number,
|
|
81
|
+
lng: number,
|
|
82
|
+
lat0: number,
|
|
83
|
+
lng0: number,
|
|
84
|
+
): { x: number; y: number } {
|
|
85
|
+
const cosLat = Math.cos((lat0 * Math.PI) / 180);
|
|
86
|
+
const east = (lng - lng0) * METRES_PER_DEG_LAT * cosLat;
|
|
87
|
+
const north = (lat - lat0) * METRES_PER_DEG_LAT;
|
|
88
|
+
return { x: east, y: -north };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function worldOffsetToPlane(
|
|
92
|
+
off: WorldOffset,
|
|
93
|
+
originEast = 0,
|
|
94
|
+
originNorth = 0,
|
|
95
|
+
): {
|
|
96
|
+
x: number;
|
|
97
|
+
y: number;
|
|
98
|
+
} {
|
|
99
|
+
return {
|
|
100
|
+
x: off.east_m - originEast,
|
|
101
|
+
y: -(off.north_m - originNorth),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ─── H3 cell helpers ─────────────────────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
/** Accept decimal string / hex / number / bigint → H3 hex index, or null. */
|
|
108
|
+
export function cellRes10ToHex(value: unknown): string | null {
|
|
109
|
+
if (value === null || value === undefined) return null;
|
|
110
|
+
if (typeof value === "bigint") {
|
|
111
|
+
if (value <= 0n) return null;
|
|
112
|
+
const hex = value.toString(16);
|
|
113
|
+
return isValidCell(hex) ? hex : null;
|
|
114
|
+
}
|
|
115
|
+
if (typeof value === "number") {
|
|
116
|
+
// H3 res10 ids exceed Number.MAX_SAFE_INTEGER — refuse rather than corrupt.
|
|
117
|
+
if (!(value > 0) || !Number.isFinite(value)) return null;
|
|
118
|
+
if (!Number.isSafeInteger(value)) return null;
|
|
119
|
+
const hex = BigInt(value).toString(16);
|
|
120
|
+
return isValidCell(hex) ? hex : null;
|
|
121
|
+
}
|
|
122
|
+
if (typeof value === "string") {
|
|
123
|
+
const s = value.trim();
|
|
124
|
+
if (s === "" || s === "0") return null;
|
|
125
|
+
if (/^\d+$/.test(s)) {
|
|
126
|
+
try {
|
|
127
|
+
const hex = BigInt(s).toString(16);
|
|
128
|
+
return isValidCell(hex) ? hex : null;
|
|
129
|
+
} catch {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (/^0x[0-9a-f]+$/i.test(s) || /^[0-9a-f]*[a-f][0-9a-f]*$/i.test(s)) {
|
|
134
|
+
const hex = s.replace(/^0x/i, "").toLowerCase();
|
|
135
|
+
return isValidCell(hex) ? hex : null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function cellCentroidLatLng(cell: unknown): { lat: number; lon: number } | null {
|
|
142
|
+
const hex = cellRes10ToHex(cell);
|
|
143
|
+
if (hex === null || !isValidCell(hex)) return null;
|
|
144
|
+
const [lat, lon] = cellToLatLng(hex);
|
|
145
|
+
return { lat, lon };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Acceptance helper: res-10 cell containing a WGS84 point (decimal string). */
|
|
149
|
+
export function latLngToRes10Decimal(lat: number, lon: number): string {
|
|
150
|
+
const hex = latLngToCell(lat, lon, 10);
|
|
151
|
+
return BigInt(`0x${hex}`).toString(10);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ─── Sheet → local flip ──────────────────────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
/** Sheet (y-down) → local (y-north) relative to an anchor sheet point. */
|
|
157
|
+
export function sheetToLocal(
|
|
158
|
+
posX: number,
|
|
159
|
+
posY: number,
|
|
160
|
+
anchorX: number,
|
|
161
|
+
anchorY: number,
|
|
162
|
+
): { lx: number; ly: number } {
|
|
163
|
+
return { lx: posX - anchorX, ly: anchorY - posY };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ─── Full-world composition ──────────────────────────────────────────────────
|
|
167
|
+
|
|
168
|
+
export interface PlacedNode {
|
|
169
|
+
node: NodeRow;
|
|
170
|
+
east_m: number;
|
|
171
|
+
north_m: number;
|
|
172
|
+
lat: number;
|
|
173
|
+
lon: number;
|
|
174
|
+
/** Composed bearing along the ancestor chain (degrees clockwise). */
|
|
175
|
+
composed_bearing_deg: number;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface ProjectWorldResult {
|
|
179
|
+
/** node_id → WGS84 */
|
|
180
|
+
positions: Map<string, { lat: number; lon: number }>;
|
|
181
|
+
/** Rich placement (used by export). */
|
|
182
|
+
placed: Map<string, PlacedNode>;
|
|
183
|
+
unplaced: UnplacedGraph[];
|
|
184
|
+
diagnostics: string[];
|
|
185
|
+
/** Root origin used as the equirectangular frame. */
|
|
186
|
+
origin: { lat: number; lon: number; hex: string } | null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface ProjectWorldOpts {
|
|
190
|
+
/** Extra diagnostics to prepend (e.g. loader notes). */
|
|
191
|
+
diagnostics?: string[];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
type NestLink = { parentNodeId: string; childNodeId: string };
|
|
195
|
+
|
|
196
|
+
function findNestLink(
|
|
197
|
+
parentGraphId: string,
|
|
198
|
+
childGraphId: string,
|
|
199
|
+
nodes: readonly NodeRow[],
|
|
200
|
+
edges: readonly EdgeRow[],
|
|
201
|
+
childAnchorId: string,
|
|
202
|
+
): NestLink | null {
|
|
203
|
+
const parentNodes = new Set(
|
|
204
|
+
nodes.filter((n) => n.place_graph_id === parentGraphId).map((n) => n.node_id),
|
|
205
|
+
);
|
|
206
|
+
const childNodes = new Set(
|
|
207
|
+
nodes.filter((n) => n.place_graph_id === childGraphId).map((n) => n.node_id),
|
|
208
|
+
);
|
|
209
|
+
for (const e of edges) {
|
|
210
|
+
if (parentNodes.has(e.from_node_id) && childNodes.has(e.to_node_id)) {
|
|
211
|
+
return { parentNodeId: e.from_node_id, childNodeId: e.to_node_id };
|
|
212
|
+
}
|
|
213
|
+
if (parentNodes.has(e.to_node_id) && childNodes.has(e.from_node_id)) {
|
|
214
|
+
return { parentNodeId: e.to_node_id, childNodeId: e.from_node_id };
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (childAnchorId && childNodes.has(childAnchorId)) {
|
|
218
|
+
const parentList = [...parentNodes];
|
|
219
|
+
if (parentList.length > 0) {
|
|
220
|
+
return { parentNodeId: parentList[0]!, childNodeId: childAnchorId };
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (childAnchorId && parentNodes.has(childAnchorId)) {
|
|
224
|
+
const childList = [...childNodes];
|
|
225
|
+
if (childList.length > 0) {
|
|
226
|
+
return { parentNodeId: childAnchorId, childNodeId: childList[0]! };
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Place every placeable node in WGS84 via ADR-0123 D-1.
|
|
234
|
+
* Accepts plain row bags (`readPlaceTopology` / TOML) without knowing origin.
|
|
235
|
+
*/
|
|
236
|
+
export function projectWorld(
|
|
237
|
+
input: WorldOsmInput,
|
|
238
|
+
opts: ProjectWorldOpts = {},
|
|
239
|
+
): ProjectWorldResult {
|
|
240
|
+
const rows = normalizeWorldOsmInput(input);
|
|
241
|
+
const { kindBySubstrate, usedBootstrapFallback } = buildCoordinateKindMap(
|
|
242
|
+
rows.substrate_definitions,
|
|
243
|
+
);
|
|
244
|
+
const diagnostics: string[] = [...(opts.diagnostics ?? [])];
|
|
245
|
+
if (usedBootstrapFallback) {
|
|
246
|
+
diagnostics.push(
|
|
247
|
+
"substrate_kind_fallback: no substrate_definitions rows; using kernel bootstrap seed (outdoor→h3_cell, indoor→graph_local, tactical→tactical_grid)",
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const graphs = rows.place_graphs.filter(
|
|
252
|
+
(g) => spatialClassOf(g.substrate_id, kindBySubstrate) !== "drop",
|
|
253
|
+
);
|
|
254
|
+
const nodes = rows.place_nodes.filter(
|
|
255
|
+
(n) => spatialClassOf(n.substrate_id, kindBySubstrate) !== "drop",
|
|
256
|
+
);
|
|
257
|
+
const edges = rows.place_edges;
|
|
258
|
+
|
|
259
|
+
const placement = placeWorld(graphs, nodes, edges);
|
|
260
|
+
const positions = new Map<string, { lat: number; lon: number }>();
|
|
261
|
+
for (const [id, p] of placement.placed) {
|
|
262
|
+
positions.set(id, { lat: p.lat, lon: p.lon });
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return {
|
|
266
|
+
positions,
|
|
267
|
+
placed: placement.placed,
|
|
268
|
+
unplaced: placement.unplacedGraphs,
|
|
269
|
+
diagnostics,
|
|
270
|
+
origin: placement.origin,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Internal placer over already-normalized typed rows.
|
|
276
|
+
* Unlike shared tool runtime h3-layout (outdoor-only draw), this includes indoor children.
|
|
277
|
+
*/
|
|
278
|
+
export function placeWorld(
|
|
279
|
+
graphs: readonly GraphRow[],
|
|
280
|
+
nodes: readonly NodeRow[],
|
|
281
|
+
edges: readonly EdgeRow[],
|
|
282
|
+
): {
|
|
283
|
+
placed: Map<string, PlacedNode>;
|
|
284
|
+
unplacedGraphs: UnplacedGraph[];
|
|
285
|
+
origin: { lat: number; lon: number; hex: string } | null;
|
|
286
|
+
} {
|
|
287
|
+
const gmeta = new Map(graphs.map((g) => [g.place_graph_id, g]));
|
|
288
|
+
const nodeById = new Map(nodes.map((n) => [n.node_id, n]));
|
|
289
|
+
const placed = new Map<string, PlacedNode>();
|
|
290
|
+
const unplacedGraphs: UnplacedGraph[] = [];
|
|
291
|
+
|
|
292
|
+
const roots = graphs.filter((g) => (g.parent_graph_id ?? "").trim() === "");
|
|
293
|
+
const placeableRoots = roots.filter((g) => cellRes10ToHex(g.origin_cell_res10) !== null);
|
|
294
|
+
for (const g of roots) {
|
|
295
|
+
if (cellRes10ToHex(g.origin_cell_res10) === null && !g.is_ephemeral_instance) {
|
|
296
|
+
unplacedGraphs.push({
|
|
297
|
+
place_graph_id: g.place_graph_id,
|
|
298
|
+
reason: "root graph without origin_cell_res10 (ADR-0123 D-1.5)",
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (placeableRoots.length === 0) {
|
|
303
|
+
return { placed, unplacedGraphs, origin: null };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const root = [...placeableRoots].sort((a, b) =>
|
|
307
|
+
a.place_graph_id.localeCompare(b.place_graph_id),
|
|
308
|
+
)[0]!;
|
|
309
|
+
const originHex = cellRes10ToHex(root.origin_cell_res10)!;
|
|
310
|
+
const [lat0, lng0] = cellToLatLng(originHex);
|
|
311
|
+
const origin = { lat: lat0, lon: lng0, hex: originHex };
|
|
312
|
+
|
|
313
|
+
const worldById = new Map<string, WorldOffset>();
|
|
314
|
+
const bearingByGraph = new Map<string, number>();
|
|
315
|
+
|
|
316
|
+
const commitNode = (n: NodeRow, off: WorldOffset, composedBearing: number): void => {
|
|
317
|
+
const { lat, lon } = worldOffsetToLatLng(off, lat0, lng0);
|
|
318
|
+
worldById.set(n.node_id, off);
|
|
319
|
+
placed.set(n.node_id, {
|
|
320
|
+
node: n,
|
|
321
|
+
east_m: off.east_m,
|
|
322
|
+
north_m: off.north_m,
|
|
323
|
+
lat,
|
|
324
|
+
lon,
|
|
325
|
+
composed_bearing_deg: composedBearing,
|
|
326
|
+
});
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
const placeGraphMetric = (
|
|
330
|
+
graphId: string,
|
|
331
|
+
anchorSheet: { x: number; y: number },
|
|
332
|
+
baseWorld: WorldOffset,
|
|
333
|
+
composedBearing: number,
|
|
334
|
+
): void => {
|
|
335
|
+
const meta = gmeta.get(graphId);
|
|
336
|
+
const params: ProjectionParams = {
|
|
337
|
+
bearing_deg: meta?.bearing ?? 0,
|
|
338
|
+
scale: meta?.metres_per_unit && meta.metres_per_unit > 0 ? meta.metres_per_unit : 1,
|
|
339
|
+
};
|
|
340
|
+
bearingByGraph.set(graphId, composedBearing);
|
|
341
|
+
for (const n of nodes.filter((nn) => nn.place_graph_id === graphId)) {
|
|
342
|
+
if (!n.pos_present) {
|
|
343
|
+
// D-1.4: non-metric outdoor with cell → centroid.
|
|
344
|
+
const c = cellCentroidLatLng(n.cell_res10);
|
|
345
|
+
if (c) {
|
|
346
|
+
const east = (c.lon - lng0) * METRES_PER_DEG_LAT * Math.cos((lat0 * Math.PI) / 180);
|
|
347
|
+
const north = (c.lat - lat0) * METRES_PER_DEG_LAT;
|
|
348
|
+
commitNode(n, { east_m: east, north_m: north }, composedBearing);
|
|
349
|
+
}
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
const { lx, ly } = sheetToLocal(n.pos_x, n.pos_y, anchorSheet.x, anchorSheet.y);
|
|
353
|
+
const rel = projectLocalPosition(params, lx, ly);
|
|
354
|
+
commitNode(n, composeProjection(baseWorld, rel), composedBearing + (meta?.bearing ?? 0));
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const rootAnchor = nodeById.get(root.anchor_node_id);
|
|
359
|
+
const rax = rootAnchor?.pos_x ?? 0;
|
|
360
|
+
const ray = rootAnchor?.pos_y ?? 0;
|
|
361
|
+
placeGraphMetric(root.place_graph_id, { x: rax, y: ray }, { east_m: 0, north_m: 0 }, 0);
|
|
362
|
+
|
|
363
|
+
for (const other of placeableRoots) {
|
|
364
|
+
if (other.place_graph_id === root.place_graph_id) continue;
|
|
365
|
+
const hex = cellRes10ToHex(other.origin_cell_res10)!;
|
|
366
|
+
const [olat, olng] = cellToLatLng(hex);
|
|
367
|
+
const east = (olng - lng0) * METRES_PER_DEG_LAT * Math.cos((lat0 * Math.PI) / 180);
|
|
368
|
+
const north = (olat - lat0) * METRES_PER_DEG_LAT;
|
|
369
|
+
const a = nodeById.get(other.anchor_node_id);
|
|
370
|
+
placeGraphMetric(
|
|
371
|
+
other.place_graph_id,
|
|
372
|
+
{ x: a?.pos_x ?? 0, y: a?.pos_y ?? 0 },
|
|
373
|
+
{ east_m: east, north_m: north },
|
|
374
|
+
0,
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// BFS nested children (indoor + outdoor + tactical) via nest links.
|
|
379
|
+
const placedGraphs = new Set<string>(placeableRoots.map((g) => g.place_graph_id));
|
|
380
|
+
let grew = true;
|
|
381
|
+
while (grew) {
|
|
382
|
+
grew = false;
|
|
383
|
+
for (const child of graphs) {
|
|
384
|
+
if (placedGraphs.has(child.place_graph_id)) continue;
|
|
385
|
+
const parentId = (child.parent_graph_id ?? "").trim();
|
|
386
|
+
if (!parentId || !placedGraphs.has(parentId)) continue;
|
|
387
|
+
const nest = findNestLink(parentId, child.place_graph_id, nodes, edges, child.anchor_node_id);
|
|
388
|
+
if (nest === null) {
|
|
389
|
+
unplacedGraphs.push({
|
|
390
|
+
place_graph_id: child.place_graph_id,
|
|
391
|
+
reason: `nested graph has no nest-link portal to parent '${parentId}'`,
|
|
392
|
+
});
|
|
393
|
+
placedGraphs.add(child.place_graph_id);
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
const parentWorld = worldById.get(nest.parentNodeId);
|
|
397
|
+
const childNest = nodeById.get(nest.childNodeId);
|
|
398
|
+
if (!parentWorld || !childNest) continue;
|
|
399
|
+
const parentBearing = bearingByGraph.get(parentId) ?? 0;
|
|
400
|
+
|
|
401
|
+
// Tactical grids or child graphs whose anchor is a node in the parent graph
|
|
402
|
+
const parentNodes = new Set(
|
|
403
|
+
nodes.filter((n) => n.place_graph_id === parentId).map((n) => n.node_id),
|
|
404
|
+
);
|
|
405
|
+
if (child.anchor_node_id && parentNodes.has(child.anchor_node_id)) {
|
|
406
|
+
const parentAnchor = nodeById.get(child.anchor_node_id);
|
|
407
|
+
const childGraphNodes = nodes.filter(
|
|
408
|
+
(n) => n.place_graph_id === child.place_graph_id && n.pos_present,
|
|
409
|
+
);
|
|
410
|
+
let centerX = 0;
|
|
411
|
+
let centerY = 0;
|
|
412
|
+
if (childGraphNodes.length > 0) {
|
|
413
|
+
const minX = Math.min(...childGraphNodes.map((n) => n.pos_x));
|
|
414
|
+
const maxX = Math.max(...childGraphNodes.map((n) => n.pos_x));
|
|
415
|
+
const minY = Math.min(...childGraphNodes.map((n) => n.pos_y));
|
|
416
|
+
const maxY = Math.max(...childGraphNodes.map((n) => n.pos_y));
|
|
417
|
+
centerX = (minX + maxX) / 2;
|
|
418
|
+
centerY = (minY + maxY) / 2;
|
|
419
|
+
}
|
|
420
|
+
placeGraphMetric(
|
|
421
|
+
child.place_graph_id,
|
|
422
|
+
{ x: centerX, y: centerY },
|
|
423
|
+
parentWorld,
|
|
424
|
+
parentBearing,
|
|
425
|
+
);
|
|
426
|
+
if (parentAnchor?.pose_elevation_present) {
|
|
427
|
+
for (const n of childGraphNodes) {
|
|
428
|
+
if (!n.pose_elevation_present || n.pose_elevation === 0) {
|
|
429
|
+
n.pose_elevation = parentAnchor.pose_elevation;
|
|
430
|
+
n.pose_elevation_present = true;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
} else {
|
|
435
|
+
placeGraphMetric(
|
|
436
|
+
child.place_graph_id,
|
|
437
|
+
{ x: childNest.pos_x, y: childNest.pos_y },
|
|
438
|
+
parentWorld,
|
|
439
|
+
parentBearing,
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
placedGraphs.add(child.place_graph_id);
|
|
443
|
+
grew = true;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
for (const g of graphs) {
|
|
448
|
+
if (placedGraphs.has(g.place_graph_id)) continue;
|
|
449
|
+
if (unplacedGraphs.some((u) => u.place_graph_id === g.place_graph_id)) continue;
|
|
450
|
+
const parentId = (g.parent_graph_id ?? "").trim();
|
|
451
|
+
if (parentId && unplacedGraphs.some((u) => u.place_graph_id === parentId)) {
|
|
452
|
+
unplacedGraphs.push({
|
|
453
|
+
place_graph_id: g.place_graph_id,
|
|
454
|
+
reason: `ancestor unplaced (${parentId})`,
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
return { placed, unplacedGraphs, origin };
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Project a sheet-cell point of a placed graph into WGS84.
|
|
464
|
+
* Used for map_layer image corners (ADR-0123 D-2 map_layer table).
|
|
465
|
+
*/
|
|
466
|
+
export function projectSheetPoint(
|
|
467
|
+
sheetX: number,
|
|
468
|
+
sheetY: number,
|
|
469
|
+
anchorSheet: { x: number; y: number },
|
|
470
|
+
baseWorld: WorldOffset,
|
|
471
|
+
bearingDeg: number,
|
|
472
|
+
metresPerUnit: number,
|
|
473
|
+
lat0: number,
|
|
474
|
+
lng0: number,
|
|
475
|
+
): { lat: number; lon: number } {
|
|
476
|
+
const { lx, ly } = sheetToLocal(sheetX, sheetY, anchorSheet.x, anchorSheet.y);
|
|
477
|
+
const rel = projectLocalPosition({ bearing_deg: bearingDeg, scale: metresPerUnit }, lx, ly);
|
|
478
|
+
return worldOffsetToLatLng(composeProjection(baseWorld, rel), lat0, lng0);
|
|
479
|
+
}
|