@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
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MVT layer split + geojson-vt index build (browser-safe pure builders).
|
|
3
|
+
* HTTP / operator cache stay in @spellkave/player-mcp.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import geojsonvt from "geojson-vt";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
type BackdropEntry,
|
|
10
|
+
type ExportManifest,
|
|
11
|
+
exportWorld,
|
|
12
|
+
type GeoJsonFeature,
|
|
13
|
+
type GeoJsonFeatureCollection,
|
|
14
|
+
type OsmDocument,
|
|
15
|
+
renderGeoJson,
|
|
16
|
+
type WorldOsmInput,
|
|
17
|
+
} from "../index.ts";
|
|
18
|
+
|
|
19
|
+
type GeoJSONVT = ReturnType<typeof geojsonvt>;
|
|
20
|
+
|
|
21
|
+
export const TILE_LAYER_NAMES = [
|
|
22
|
+
"spellkave_node",
|
|
23
|
+
"spellkave_room",
|
|
24
|
+
"spellkave_corridor",
|
|
25
|
+
"spellkave_building",
|
|
26
|
+
"spellkave_backdrop_frame",
|
|
27
|
+
] as const;
|
|
28
|
+
|
|
29
|
+
export type TileLayerName = (typeof TILE_LAYER_NAMES)[number];
|
|
30
|
+
|
|
31
|
+
export const GEOJSON_VT_OPTIONS = {
|
|
32
|
+
maxZoom: 22,
|
|
33
|
+
tolerance: 3,
|
|
34
|
+
extent: 4096,
|
|
35
|
+
buffer: 64,
|
|
36
|
+
indexMaxZoom: 5,
|
|
37
|
+
indexMaxPoints: 100_000,
|
|
38
|
+
promoteId: "spellkave:node_id",
|
|
39
|
+
generateId: false,
|
|
40
|
+
} as const;
|
|
41
|
+
|
|
42
|
+
export interface TileLayerFields {
|
|
43
|
+
id: string;
|
|
44
|
+
fields: Record<string, "String" | "Number" | "Boolean">;
|
|
45
|
+
description: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface WorldTilesCache {
|
|
49
|
+
slug: string;
|
|
50
|
+
tick: number;
|
|
51
|
+
timestamp: string;
|
|
52
|
+
export: {
|
|
53
|
+
doc: OsmDocument;
|
|
54
|
+
backdrops: BackdropEntry[];
|
|
55
|
+
manifest: ExportManifest;
|
|
56
|
+
};
|
|
57
|
+
geojson: GeoJsonFeatureCollection;
|
|
58
|
+
layers: Record<TileLayerName, GeoJsonFeatureCollection>;
|
|
59
|
+
indexes: Record<TileLayerName, GeoJSONVT>;
|
|
60
|
+
bounds: [number, number, number, number];
|
|
61
|
+
center: [number, number];
|
|
62
|
+
levels: string[];
|
|
63
|
+
etag: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function emptyFc(): GeoJsonFeatureCollection {
|
|
67
|
+
return { type: "FeatureCollection", features: [] };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function propString(props: GeoJsonFeature["properties"], key: string): string | undefined {
|
|
71
|
+
const v = props[key];
|
|
72
|
+
if (v === undefined || v === null) return undefined;
|
|
73
|
+
return String(v);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Split renderGeoJson output + relations/backdrops into MVT source layers. */
|
|
77
|
+
export function splitTileLayers(
|
|
78
|
+
doc: OsmDocument,
|
|
79
|
+
geojson: GeoJsonFeatureCollection,
|
|
80
|
+
backdrops: readonly BackdropEntry[],
|
|
81
|
+
): Record<TileLayerName, GeoJsonFeatureCollection> {
|
|
82
|
+
const nodes = emptyFc();
|
|
83
|
+
const rooms = emptyFc();
|
|
84
|
+
const corridors = emptyFc();
|
|
85
|
+
const buildings = emptyFc();
|
|
86
|
+
const frames = emptyFc();
|
|
87
|
+
|
|
88
|
+
for (const f of geojson.features) {
|
|
89
|
+
const g = f.geometry;
|
|
90
|
+
const props = f.properties;
|
|
91
|
+
if (g.type === "Point") {
|
|
92
|
+
nodes.features.push(f);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (g.type === "Polygon") {
|
|
96
|
+
if (props.indoor === "room" || props["spellkave:extent"] === "yes") {
|
|
97
|
+
rooms.features.push(f);
|
|
98
|
+
}
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (g.type === "LineString") {
|
|
102
|
+
corridors.features.push(f);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Building footprints: bbox-union of member room polygons (or skip).
|
|
107
|
+
const roomsByGraph = new Map<string, GeoJsonFeature[]>();
|
|
108
|
+
for (const f of rooms.features) {
|
|
109
|
+
const nodeId = propString(f.properties, "spellkave:node_id") ?? "";
|
|
110
|
+
// Infer graph from node id prefix when available; else skip grouping.
|
|
111
|
+
const graph =
|
|
112
|
+
propString(f.properties, "spellkave:graph") ??
|
|
113
|
+
(nodeId.includes("/") ? nodeId.split("/").slice(0, -1).join("/") : "");
|
|
114
|
+
if (!graph) continue;
|
|
115
|
+
const list = roomsByGraph.get(graph) ?? [];
|
|
116
|
+
list.push(f);
|
|
117
|
+
roomsByGraph.set(graph, list);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
for (const rel of doc.relations) {
|
|
121
|
+
if (rel.tags.type !== "building") continue;
|
|
122
|
+
const graphId = rel.tags["spellkave:graph_id"] ?? "";
|
|
123
|
+
const memberRooms = roomsByGraph.get(graphId) ?? [];
|
|
124
|
+
// Also match rooms whose node_id starts with graphId/
|
|
125
|
+
const extra = rooms.features.filter((f) => {
|
|
126
|
+
const nid = propString(f.properties, "spellkave:node_id") ?? "";
|
|
127
|
+
return nid.startsWith(`${graphId}/`);
|
|
128
|
+
});
|
|
129
|
+
const pool = memberRooms.length > 0 ? memberRooms : extra;
|
|
130
|
+
if (pool.length === 0) continue;
|
|
131
|
+
|
|
132
|
+
let minLon = Number.POSITIVE_INFINITY;
|
|
133
|
+
let minLat = Number.POSITIVE_INFINITY;
|
|
134
|
+
let maxLon = Number.NEGATIVE_INFINITY;
|
|
135
|
+
let maxLat = Number.NEGATIVE_INFINITY;
|
|
136
|
+
for (const f of pool) {
|
|
137
|
+
if (f.geometry.type !== "Polygon") continue;
|
|
138
|
+
for (const ring of f.geometry.coordinates) {
|
|
139
|
+
for (const c of ring) {
|
|
140
|
+
const lon = c[0];
|
|
141
|
+
const lat = c[1];
|
|
142
|
+
if (lon === undefined || lat === undefined) continue;
|
|
143
|
+
if (lon < minLon) minLon = lon;
|
|
144
|
+
if (lat < minLat) minLat = lat;
|
|
145
|
+
if (lon > maxLon) maxLon = lon;
|
|
146
|
+
if (lat > maxLat) maxLat = lat;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!Number.isFinite(minLon)) continue;
|
|
151
|
+
|
|
152
|
+
const props: Record<string, string> = {
|
|
153
|
+
type: rel.tags.type ?? "building",
|
|
154
|
+
building: rel.tags.building ?? "yes",
|
|
155
|
+
};
|
|
156
|
+
for (const key of ["name", "min_level", "max_level", "building:levels"] as const) {
|
|
157
|
+
const value = rel.tags[key];
|
|
158
|
+
if (value) props[key] = value;
|
|
159
|
+
}
|
|
160
|
+
for (const [k, v] of Object.entries(rel.tags)) {
|
|
161
|
+
if (k.startsWith("spellkave:")) props[k] = v;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
buildings.features.push({
|
|
165
|
+
type: "Feature",
|
|
166
|
+
id: rel.id,
|
|
167
|
+
geometry: {
|
|
168
|
+
type: "Polygon",
|
|
169
|
+
coordinates: [
|
|
170
|
+
[
|
|
171
|
+
[minLon, maxLat],
|
|
172
|
+
[maxLon, maxLat],
|
|
173
|
+
[maxLon, minLat],
|
|
174
|
+
[minLon, minLat],
|
|
175
|
+
[minLon, maxLat],
|
|
176
|
+
],
|
|
177
|
+
],
|
|
178
|
+
},
|
|
179
|
+
properties: props,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
for (const bd of backdrops) {
|
|
184
|
+
// A polygon ring closes on its own first corner; a backdrop with no
|
|
185
|
+
// corners has no ring to close and is skipped rather than asserted.
|
|
186
|
+
const firstCorner = bd.coordinates[0];
|
|
187
|
+
if (firstCorner === undefined) continue;
|
|
188
|
+
const ring = [...bd.coordinates, firstCorner];
|
|
189
|
+
frames.features.push({
|
|
190
|
+
type: "Feature",
|
|
191
|
+
id: `backdrop:${bd.id}`,
|
|
192
|
+
geometry: { type: "Polygon", coordinates: [ring] },
|
|
193
|
+
properties: {
|
|
194
|
+
url: bd.url,
|
|
195
|
+
graph: bd.graph,
|
|
196
|
+
name: bd.name,
|
|
197
|
+
license: bd.license,
|
|
198
|
+
"spellkave:backdrop_id": bd.id,
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
spellkave_node: nodes,
|
|
205
|
+
spellkave_room: rooms,
|
|
206
|
+
spellkave_corridor: corridors,
|
|
207
|
+
spellkave_building: buildings,
|
|
208
|
+
spellkave_backdrop_frame: frames,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function computeBounds(geojson: GeoJsonFeatureCollection): [number, number, number, number] {
|
|
213
|
+
let minLon = Number.POSITIVE_INFINITY;
|
|
214
|
+
let minLat = Number.POSITIVE_INFINITY;
|
|
215
|
+
let maxLon = Number.NEGATIVE_INFINITY;
|
|
216
|
+
let maxLat = Number.NEGATIVE_INFINITY;
|
|
217
|
+
const push = (lon: number, lat: number) => {
|
|
218
|
+
if (lon < minLon) minLon = lon;
|
|
219
|
+
if (lat < minLat) minLat = lat;
|
|
220
|
+
if (lon > maxLon) maxLon = lon;
|
|
221
|
+
if (lat > maxLat) maxLat = lat;
|
|
222
|
+
};
|
|
223
|
+
const walk = (coords: unknown): void => {
|
|
224
|
+
if (!Array.isArray(coords)) return;
|
|
225
|
+
if (typeof coords[0] === "number" && typeof coords[1] === "number") {
|
|
226
|
+
push(coords[0] as number, coords[1] as number);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
for (const c of coords) walk(c);
|
|
230
|
+
};
|
|
231
|
+
for (const f of geojson.features) walk(f.geometry.coordinates);
|
|
232
|
+
if (!Number.isFinite(minLon)) return [-180, -85, 180, 85];
|
|
233
|
+
return [minLon, minLat, maxLon, maxLat];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function collectLevels(layers: Record<TileLayerName, GeoJsonFeatureCollection>): string[] {
|
|
237
|
+
const set = new Set<string>();
|
|
238
|
+
for (const name of TILE_LAYER_NAMES) {
|
|
239
|
+
for (const f of layers[name].features) {
|
|
240
|
+
const lvl = f.properties.level;
|
|
241
|
+
if (lvl === undefined || lvl === null || lvl === "") continue;
|
|
242
|
+
for (const part of String(lvl).split(";")) {
|
|
243
|
+
const t = part.trim();
|
|
244
|
+
if (t !== "") set.add(t);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return [...set].sort((a, b) => Number(a) - Number(b));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function fieldCatalog(layers: Record<TileLayerName, GeoJsonFeatureCollection>): TileLayerFields[] {
|
|
252
|
+
const out: TileLayerFields[] = [];
|
|
253
|
+
const descriptions: Record<TileLayerName, string> = {
|
|
254
|
+
spellkave_node: "Place-graph nodes (points)",
|
|
255
|
+
spellkave_room: "Indoor room polygons",
|
|
256
|
+
spellkave_corridor: "Corridor / path / steps edges",
|
|
257
|
+
spellkave_building: "Indoor building footprints",
|
|
258
|
+
spellkave_backdrop_frame: "Backdrop image corner frames",
|
|
259
|
+
};
|
|
260
|
+
for (const id of TILE_LAYER_NAMES) {
|
|
261
|
+
const fields: Record<string, "String" | "Number" | "Boolean"> = {};
|
|
262
|
+
for (const f of layers[id].features) {
|
|
263
|
+
for (const [k, v] of Object.entries(f.properties)) {
|
|
264
|
+
if (fields[k] !== undefined) continue;
|
|
265
|
+
if (typeof v === "number") fields[k] = "Number";
|
|
266
|
+
else if (typeof v === "boolean") fields[k] = "Boolean";
|
|
267
|
+
else fields[k] = "String";
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// Ensure common filter keys exist even on empty layers.
|
|
271
|
+
if (id !== "spellkave_backdrop_frame" && fields.level === undefined) {
|
|
272
|
+
fields.level = "String";
|
|
273
|
+
}
|
|
274
|
+
if (id === "spellkave_node" && fields.name === undefined) fields.name = "String";
|
|
275
|
+
out.push({ id, fields, description: descriptions[id] });
|
|
276
|
+
}
|
|
277
|
+
return out;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function buildWorldTilesCacheFromExport(
|
|
281
|
+
slug: string,
|
|
282
|
+
result: { doc: OsmDocument; backdrops: readonly BackdropEntry[]; manifest: ExportManifest },
|
|
283
|
+
): WorldTilesCache {
|
|
284
|
+
const geojson = renderGeoJson(result.doc);
|
|
285
|
+
const layers = splitTileLayers(result.doc, geojson, result.backdrops);
|
|
286
|
+
const indexes = {} as Record<TileLayerName, GeoJSONVT>;
|
|
287
|
+
for (const name of TILE_LAYER_NAMES) {
|
|
288
|
+
indexes[name] = geojsonvt(layers[name] as GeoJSON.FeatureCollection, {
|
|
289
|
+
...GEOJSON_VT_OPTIONS,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
const bounds = computeBounds(geojson);
|
|
293
|
+
const center: [number, number] = [(bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2];
|
|
294
|
+
const levels = collectLevels(layers);
|
|
295
|
+
const tick = result.manifest.tick;
|
|
296
|
+
return {
|
|
297
|
+
slug,
|
|
298
|
+
tick,
|
|
299
|
+
timestamp: result.manifest.timestamp,
|
|
300
|
+
export: {
|
|
301
|
+
doc: result.doc,
|
|
302
|
+
backdrops: [...result.backdrops],
|
|
303
|
+
manifest: result.manifest,
|
|
304
|
+
},
|
|
305
|
+
geojson,
|
|
306
|
+
layers,
|
|
307
|
+
indexes,
|
|
308
|
+
bounds,
|
|
309
|
+
center,
|
|
310
|
+
levels,
|
|
311
|
+
etag: `"tick-${tick}"`,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Build a tiles cache from plain WorldOsmInput rows (operator snapshot / test helper). */
|
|
316
|
+
export function buildWorldTilesCache(
|
|
317
|
+
slug: string,
|
|
318
|
+
input: WorldOsmInput,
|
|
319
|
+
opts: { tick?: number; timestamp?: string } = {},
|
|
320
|
+
): WorldTilesCache {
|
|
321
|
+
const result = exportWorld(input, {
|
|
322
|
+
tick: opts.tick ?? 0,
|
|
323
|
+
timestamp: opts.timestamp,
|
|
324
|
+
});
|
|
325
|
+
return buildWorldTilesCacheFromExport(slug, result);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export function vectorLayerFields(cache: WorldTilesCache): TileLayerFields[] {
|
|
329
|
+
return fieldCatalog(cache.layers);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export function manifestOf(cache: WorldTilesCache): ExportManifest {
|
|
333
|
+
return cache.export.manifest;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function backdropsOf(cache: WorldTilesCache): BackdropEntry[] {
|
|
337
|
+
return cache.export.backdrops;
|
|
338
|
+
}
|