@spellkave/world-map 0.0.0-bootstrap → 0.0.0-stage

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.
@@ -1,126 +0,0 @@
1
- /**
2
- * OSM JSON writer — Overpass/`osm-json` shape that sk-adr8.13 will serve:
3
- * `{ version, generator, elements: [...] }`.
4
- *
5
- * Ids stay decimal strings (never coerced to number).
6
- */
7
-
8
- import type { OsmDocument, OsmNode, OsmRelation, OsmWay } from "../model.ts";
9
-
10
- export interface OsmJsonDocument {
11
- version: number;
12
- generator: string;
13
- /** World tick (Spellkave extension; consumers may ignore). */
14
- osm3s?: { timestamp_osm_base?: string };
15
- elements: OsmJsonElement[];
16
- }
17
-
18
- export type OsmJsonElement =
19
- | {
20
- type: "node";
21
- id: string;
22
- lat: number;
23
- lon: number;
24
- timestamp: string;
25
- version: number;
26
- changeset: number;
27
- user: string;
28
- uid: number;
29
- tags?: Record<string, string>;
30
- }
31
- | {
32
- type: "way";
33
- id: string;
34
- timestamp: string;
35
- version: number;
36
- changeset: number;
37
- user: string;
38
- uid: number;
39
- nodes: string[];
40
- tags?: Record<string, string>;
41
- }
42
- | {
43
- type: "relation";
44
- id: string;
45
- timestamp: string;
46
- version: number;
47
- changeset: number;
48
- user: string;
49
- uid: number;
50
- members: Array<{ type: string; ref: string; role: string }>;
51
- tags?: Record<string, string>;
52
- };
53
-
54
- function maybeTags(tags: Record<string, string>): Record<string, string> | undefined {
55
- const entries = Object.entries(tags).filter(([, v]) => v !== undefined && v !== "");
56
- if (entries.length === 0) return undefined;
57
- return Object.fromEntries(entries);
58
- }
59
-
60
- function nodeEl(n: OsmNode): OsmJsonElement {
61
- const el: OsmJsonElement = {
62
- type: "node",
63
- id: n.id,
64
- lat: n.lat,
65
- lon: n.lon,
66
- timestamp: n.timestamp,
67
- version: n.version,
68
- changeset: n.changeset,
69
- user: n.user,
70
- uid: n.uid,
71
- };
72
- const tags = maybeTags(n.tags);
73
- if (tags) (el as { tags?: Record<string, string> }).tags = tags;
74
- return el;
75
- }
76
-
77
- function wayEl(w: OsmWay): OsmJsonElement {
78
- const el: OsmJsonElement = {
79
- type: "way",
80
- id: w.id,
81
- timestamp: w.timestamp,
82
- version: w.version,
83
- changeset: w.changeset,
84
- user: w.user,
85
- uid: w.uid,
86
- nodes: [...w.refs],
87
- };
88
- const tags = maybeTags(w.tags);
89
- if (tags) (el as { tags?: Record<string, string> }).tags = tags;
90
- return el;
91
- }
92
-
93
- function relEl(r: OsmRelation): OsmJsonElement {
94
- const el: OsmJsonElement = {
95
- type: "relation",
96
- id: r.id,
97
- timestamp: r.timestamp,
98
- version: r.version,
99
- changeset: r.changeset,
100
- user: r.user,
101
- uid: r.uid,
102
- members: r.members.map((m) => ({ type: m.type, ref: m.ref, role: m.role })),
103
- };
104
- const tags = maybeTags(r.tags);
105
- if (tags) (el as { tags?: Record<string, string> }).tags = tags;
106
- return el;
107
- }
108
-
109
- export function renderOsmJson(doc: OsmDocument): OsmJsonDocument {
110
- const elements: OsmJsonElement[] = [
111
- ...doc.nodes.map(nodeEl),
112
- ...doc.ways.map(wayEl),
113
- ...doc.relations.map(relEl),
114
- ];
115
- return {
116
- version: 0.6,
117
- generator: doc.generator,
118
- osm3s: { timestamp_osm_base: doc.timestamp },
119
- elements,
120
- };
121
- }
122
-
123
- /** JSON string with ids left as strings (no number coercion). */
124
- export function renderOsmJsonString(doc: OsmDocument, pretty = true): string {
125
- return JSON.stringify(renderOsmJson(doc), null, pretty ? 2 : undefined) + (pretty ? "\n" : "");
126
- }
@@ -1,87 +0,0 @@
1
- /**
2
- * OSM XML 0.6 writer (read-only export; no changeset upload).
3
- * ADR-0123 D-4: version/changeset/uid/user/timestamp + xmlns:spellkave.
4
- *
5
- * Element order is nodes → ways → relations (JOSM/osmium convention).
6
- */
7
-
8
- import type { OsmDocument, OsmMeta } from "../model.ts";
9
-
10
- /** ADR-0123 D-4 namespace for spellkave:* root attributes. */
11
- export const SPELLKAVE_OSM_NS = "https://spellkave.com/osm";
12
-
13
- export function escapeXml(s: string): string {
14
- return s
15
- .replace(/&/g, "&amp;")
16
- .replace(/</g, "&lt;")
17
- .replace(/>/g, "&gt;")
18
- .replace(/"/g, "&quot;")
19
- .replace(/'/g, "&apos;");
20
- }
21
-
22
- function metaAttrs(el: OsmMeta): string {
23
- return [
24
- `id="${el.id}"`,
25
- `version="${el.version}"`,
26
- `changeset="${el.changeset}"`,
27
- `uid="${el.uid}"`,
28
- `user="${escapeXml(el.user)}"`,
29
- `visible="${el.visible}"`,
30
- `timestamp="${escapeXml(el.timestamp)}"`,
31
- ].join(" ");
32
- }
33
-
34
- function tagsXml(tags: Record<string, string>, indent: string): string {
35
- return Object.entries(tags)
36
- .filter(([, v]) => v !== undefined && v !== null && v !== "")
37
- .map(([k, v]) => `${indent}<tag k="${escapeXml(k)}" v="${escapeXml(v)}"/>`)
38
- .join("\n");
39
- }
40
-
41
- export function renderOsmXml(doc: OsmDocument): string {
42
- const lines: string[] = [];
43
- lines.push(`<?xml version="1.0" encoding="UTF-8"?>`);
44
- // Declare the spellkave prefix so browser DOMParser accepts the root attr
45
- // (ADR-0123 D-4: spellkave:tick on <osm>).
46
- lines.push(
47
- `<osm version="0.6" generator="${escapeXml(doc.generator)}" ` +
48
- `xmlns:spellkave="${SPELLKAVE_OSM_NS}" ` +
49
- `spellkave:tick="${doc.tick}" timestamp="${escapeXml(doc.timestamp)}">`,
50
- );
51
-
52
- for (const n of doc.nodes) {
53
- const tagBlock = tagsXml(n.tags, " ");
54
- const head = ` <node ${metaAttrs(n)} lat="${n.lat.toFixed(9)}" lon="${n.lon.toFixed(9)}"`;
55
- if (tagBlock) {
56
- lines.push(`${head}>`);
57
- lines.push(tagBlock);
58
- lines.push(` </node>`);
59
- } else {
60
- lines.push(`${head}/>`);
61
- }
62
- }
63
-
64
- for (const w of doc.ways) {
65
- lines.push(` <way ${metaAttrs(w)}>`);
66
- for (const ref of w.refs) {
67
- lines.push(` <nd ref="${ref}"/>`);
68
- }
69
- const tagBlock = tagsXml(w.tags, " ");
70
- if (tagBlock) lines.push(tagBlock);
71
- lines.push(` </way>`);
72
- }
73
-
74
- for (const r of doc.relations) {
75
- lines.push(` <relation ${metaAttrs(r)}>`);
76
- for (const m of r.members) {
77
- lines.push(` <member type="${m.type}" ref="${m.ref}" role="${escapeXml(m.role)}"/>`);
78
- }
79
- const tagBlock = tagsXml(r.tags, " ");
80
- if (tagBlock) lines.push(tagBlock);
81
- lines.push(` </relation>`);
82
- }
83
-
84
- lines.push(`</osm>`);
85
- lines.push("");
86
- return lines.join("\n");
87
- }