@dirkwa/s-100 0.0.0-stage → 0.0.1
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 +15 -0
- package/README.md +89 -2
- package/dist/geometry.d.ts +27 -0
- package/dist/geometry.js +85 -0
- package/dist/instructions.d.ts +45 -0
- package/dist/instructions.js +165 -0
- package/dist/iso8211.d.ts +43 -0
- package/dist/iso8211.js +208 -0
- package/dist/lua-data.d.ts +17 -0
- package/dist/lua-data.js +126 -0
- package/dist/portray-client.d.ts +25 -0
- package/dist/portray-client.js +59 -0
- package/dist/portray.d.ts +29 -0
- package/dist/portray.js +59 -0
- package/dist/render.d.ts +60 -0
- package/dist/render.js +350 -0
- package/dist/s101-records.d.ts +96 -0
- package/dist/s101-records.js +157 -0
- package/dist/s101-update.d.ts +43 -0
- package/dist/s101-update.js +321 -0
- package/dist/s101.d.ts +103 -0
- package/dist/s101.js +131 -0
- package/dist/worker.d.ts +34 -0
- package/dist/worker.js +54 -0
- package/lua/host.lua +182 -0
- package/package.json +73 -4
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type S101Dataset } from './s101.ts';
|
|
2
|
+
export interface FeatureCatalogue {
|
|
3
|
+
simple: Record<string, string>;
|
|
4
|
+
complex: Record<string, Record<string, number | null>>;
|
|
5
|
+
features: Record<string, {
|
|
6
|
+
bindings: Record<string, number | null>;
|
|
7
|
+
}>;
|
|
8
|
+
information: Record<string, {
|
|
9
|
+
bindings: Record<string, number | null>;
|
|
10
|
+
}>;
|
|
11
|
+
roles: string[];
|
|
12
|
+
informationAssociations: string[];
|
|
13
|
+
featureAssociations: string[];
|
|
14
|
+
}
|
|
15
|
+
export declare function spatialId(rrnm: number, rrid: number): string;
|
|
16
|
+
export declare function datasetToLua(ds: S101Dataset): string;
|
|
17
|
+
export declare function catalogueToLua(fc: FeatureCatalogue): string;
|
package/dist/lua-data.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// The dataset and feature catalogue as Lua table constructors, so the rules never cross back into JS.
|
|
2
|
+
import { RCNM } from "./s101.js";
|
|
3
|
+
const KIND = {
|
|
4
|
+
[RCNM.point]: 'Point',
|
|
5
|
+
[RCNM.multipoint]: 'MultiPoint',
|
|
6
|
+
[RCNM.curve]: 'Curve',
|
|
7
|
+
[RCNM.composite]: 'CompositeCurve',
|
|
8
|
+
[RCNM.surface]: 'Surface'
|
|
9
|
+
};
|
|
10
|
+
export function spatialId(rrnm, rrid) {
|
|
11
|
+
return `${KIND[rrnm] ?? rrnm}|${rrid}`;
|
|
12
|
+
}
|
|
13
|
+
function str(s) {
|
|
14
|
+
return ('"' +
|
|
15
|
+
s.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r') +
|
|
16
|
+
'"');
|
|
17
|
+
}
|
|
18
|
+
function num(n) {
|
|
19
|
+
return Number.isFinite(n) ? String(n) : 'nil';
|
|
20
|
+
}
|
|
21
|
+
function attrs(list) {
|
|
22
|
+
return ('{' +
|
|
23
|
+
list
|
|
24
|
+
.map((a) => `{c=${str(a.code)},v=${str(a.value)}${a.children.length ? ',k=' + attrs(a.children) : ''}}`)
|
|
25
|
+
.join(',') +
|
|
26
|
+
'}');
|
|
27
|
+
}
|
|
28
|
+
function inas(list) {
|
|
29
|
+
return ('{' +
|
|
30
|
+
list
|
|
31
|
+
.map((i) => `{i=${str(String(i.rrid))},a=${str(i.association)},r=${str(i.role)}}`)
|
|
32
|
+
.join(',') +
|
|
33
|
+
'}');
|
|
34
|
+
}
|
|
35
|
+
function refs(list) {
|
|
36
|
+
return ('{' +
|
|
37
|
+
list
|
|
38
|
+
.map((r) => `{s=${str(spatialId(r.rrnm, r.rrid))},o=${r.ornt}${r.smin ? `,mn=${r.smin}` : ''}${r.smax ? `,mx=${r.smax}` : ''}${r.usag ? `,u=${r.usag}` : ''}}`)
|
|
39
|
+
.join(',') +
|
|
40
|
+
'}');
|
|
41
|
+
}
|
|
42
|
+
/** Every spatial a feature reaches, its own and those its geometry is built from. */
|
|
43
|
+
function reachable(ds, rrnm, rrid, out) {
|
|
44
|
+
const id = spatialId(rrnm, rrid);
|
|
45
|
+
if (out.has(id))
|
|
46
|
+
return;
|
|
47
|
+
out.add(id);
|
|
48
|
+
if (rrnm === RCNM.curve) {
|
|
49
|
+
const c = ds.curves.get(rrid);
|
|
50
|
+
if (c?.begin !== undefined)
|
|
51
|
+
out.add(spatialId(RCNM.point, c.begin));
|
|
52
|
+
if (c?.end !== undefined)
|
|
53
|
+
out.add(spatialId(RCNM.point, c.end));
|
|
54
|
+
}
|
|
55
|
+
else if (rrnm === RCNM.composite) {
|
|
56
|
+
for (const r of ds.composites.get(rrid)?.refs ?? [])
|
|
57
|
+
reachable(ds, r.rrnm, r.rrid, out);
|
|
58
|
+
}
|
|
59
|
+
else if (rrnm === RCNM.surface) {
|
|
60
|
+
for (const r of ds.surfaces.get(rrid)?.refs ?? [])
|
|
61
|
+
reachable(ds, r.rrnm, r.rrid, out);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export function datasetToLua(ds) {
|
|
65
|
+
const parts = ['DS={'];
|
|
66
|
+
const usedBy = new Map();
|
|
67
|
+
parts.push('FI={' + [...ds.features.keys()].map((k) => str(String(k))).join(',') + '},');
|
|
68
|
+
parts.push('F={');
|
|
69
|
+
for (const f of ds.features.values()) {
|
|
70
|
+
const fid = String(f.id);
|
|
71
|
+
parts.push(`[${str(fid)}]={c=${str(f.code)},a=${attrs(f.attrs)},sp=${refs(f.spas)},ia=${inas(f.inas)},fa={${f.fasc
|
|
72
|
+
.map((a) => `{i=${str(String(a.rrid))},a=${str(a.association)},r=${str(a.role)}}`)
|
|
73
|
+
.join(',')}}},`);
|
|
74
|
+
const seen = new Set();
|
|
75
|
+
for (const r of f.spas)
|
|
76
|
+
reachable(ds, r.rrnm, r.rrid, seen);
|
|
77
|
+
for (const s of seen) {
|
|
78
|
+
const list = usedBy.get(s);
|
|
79
|
+
if (list)
|
|
80
|
+
list.push(fid);
|
|
81
|
+
else
|
|
82
|
+
usedBy.set(s, [fid]);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
parts.push('},I={');
|
|
86
|
+
for (const i of ds.information.values())
|
|
87
|
+
parts.push(`[${str(String(i.id))}]={c=${str(i.code)},a=${attrs(i.attrs)},ia=${inas(i.inas)}},`);
|
|
88
|
+
parts.push('},SP={');
|
|
89
|
+
for (const [id, p] of ds.points)
|
|
90
|
+
parts.push(`[${str(spatialId(RCNM.point, id))}]={k=1,x=${num(p.x)},y=${num(p.y)},z=${p.z === undefined ? 'nil' : num(p.z)},ia=${inas(p.inas)}},`);
|
|
91
|
+
for (const [id, m] of ds.multipoints)
|
|
92
|
+
parts.push(`[${str(spatialId(RCNM.multipoint, id))}]={k=2,p={${m.pts.map(num).join(',')}},ia=${inas(m.inas)}},`);
|
|
93
|
+
for (const [id, c] of ds.curves)
|
|
94
|
+
parts.push(`[${str(spatialId(RCNM.curve, id))}]={k=3,b=${c.begin ?? 'nil'},e=${c.end ?? 'nil'},ia=${inas(c.inas)}},`);
|
|
95
|
+
for (const [id, c] of ds.composites)
|
|
96
|
+
parts.push(`[${str(spatialId(RCNM.composite, id))}]={k=4,r=${refs(c.refs)},ia=${inas(c.inas)}},`);
|
|
97
|
+
for (const [id, s] of ds.surfaces)
|
|
98
|
+
parts.push(`[${str(spatialId(RCNM.surface, id))}]={k=5,r=${refs(s.refs)},ia=${inas(s.inas)}},`);
|
|
99
|
+
parts.push('},SF={');
|
|
100
|
+
for (const [s, fids] of usedBy)
|
|
101
|
+
parts.push(`[${str(s)}]={${fids.map(str).join(',')}},`);
|
|
102
|
+
parts.push('}}');
|
|
103
|
+
return parts.join('');
|
|
104
|
+
}
|
|
105
|
+
export function catalogueToLua(fc) {
|
|
106
|
+
const list = (a) => '{' + a.map(str).join(',') + '}';
|
|
107
|
+
const bind = (b) => '{' +
|
|
108
|
+
Object.entries(b)
|
|
109
|
+
.map(([k, v]) => `[${str(k)}]=${v === null ? 'false' : v}`)
|
|
110
|
+
.join(',') +
|
|
111
|
+
'}';
|
|
112
|
+
const p = ['FC={S={'];
|
|
113
|
+
for (const [k, v] of Object.entries(fc.simple))
|
|
114
|
+
p.push(`[${str(k)}]=${str(v)},`);
|
|
115
|
+
p.push('},C={');
|
|
116
|
+
for (const [k, v] of Object.entries(fc.complex))
|
|
117
|
+
p.push(`[${str(k)}]=${bind(v)},`);
|
|
118
|
+
p.push('},F={');
|
|
119
|
+
for (const [k, v] of Object.entries(fc.features))
|
|
120
|
+
p.push(`[${str(k)}]=${bind(v.bindings)},`);
|
|
121
|
+
p.push('},I={');
|
|
122
|
+
for (const [k, v] of Object.entries(fc.information))
|
|
123
|
+
p.push(`[${str(k)}]=${bind(v.bindings)},`);
|
|
124
|
+
p.push(`},R=${list(fc.roles)},IA=${list(fc.informationAssociations)},FA=${list(fc.featureAssociations)}}`);
|
|
125
|
+
return p.join('');
|
|
126
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ContextParameter } from './portray.ts';
|
|
2
|
+
export declare class PortrayalClient {
|
|
3
|
+
private readonly worker;
|
|
4
|
+
private pending;
|
|
5
|
+
private failed;
|
|
6
|
+
constructor(workerUrl: string | URL);
|
|
7
|
+
private send;
|
|
8
|
+
init(rules: Record<string, string>, hostLua: string, wasmUri: string): Promise<{
|
|
9
|
+
type: "ready";
|
|
10
|
+
}>;
|
|
11
|
+
/** Loads a base cell and its update files, given in order (.001, .002, ...). */
|
|
12
|
+
load(bytes: Uint8Array, fcLua: string, params: ContextParameter[], updates?: Uint8Array[]): Promise<{
|
|
13
|
+
type: "loaded";
|
|
14
|
+
ds: import("./s101.ts").S101Dataset;
|
|
15
|
+
instructions: Map<string, string>;
|
|
16
|
+
errors: string[];
|
|
17
|
+
ms: Record<string, number>;
|
|
18
|
+
}>;
|
|
19
|
+
rerun(params: ContextParameter[]): Promise<{
|
|
20
|
+
type: "portrayed";
|
|
21
|
+
instructions: Map<string, string>;
|
|
22
|
+
errors: string[];
|
|
23
|
+
ms: Record<string, number>;
|
|
24
|
+
}>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export class PortrayalClient {
|
|
2
|
+
worker;
|
|
3
|
+
pending = [];
|
|
4
|
+
failed;
|
|
5
|
+
constructor(workerUrl) {
|
|
6
|
+
this.worker = new Worker(workerUrl, { type: 'module' });
|
|
7
|
+
this.worker.onmessage = (e) => {
|
|
8
|
+
this.pending.shift()?.answer(e.data);
|
|
9
|
+
};
|
|
10
|
+
// A worker that cannot load or crashes answers nothing, so everything waiting on it fails here.
|
|
11
|
+
this.worker.onerror = (e) => {
|
|
12
|
+
this.failed = new Error(`portrayal worker failed: ${e.message || 'could not load'}`);
|
|
13
|
+
for (const p of this.pending.splice(0))
|
|
14
|
+
p.fail(this.failed);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
send(expect, req, transfer = []) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
if (this.failed) {
|
|
20
|
+
reject(this.failed);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const entry = {
|
|
24
|
+
answer: (r) => {
|
|
25
|
+
if (r.type === 'error')
|
|
26
|
+
reject(new Error(r.message));
|
|
27
|
+
else if (isType(r, expect))
|
|
28
|
+
resolve(r);
|
|
29
|
+
else
|
|
30
|
+
reject(new Error(`portrayal worker answered ${r.type}, expected ${expect}`));
|
|
31
|
+
},
|
|
32
|
+
fail: reject
|
|
33
|
+
};
|
|
34
|
+
this.pending.push(entry);
|
|
35
|
+
try {
|
|
36
|
+
this.worker.postMessage(req, transfer);
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
this.pending.splice(this.pending.indexOf(entry), 1);
|
|
40
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
init(rules, hostLua, wasmUri) {
|
|
45
|
+
return this.send('ready', { type: 'init', rules, hostLua, wasmUri });
|
|
46
|
+
}
|
|
47
|
+
/** Loads a base cell and its update files, given in order (.001, .002, ...). */
|
|
48
|
+
load(bytes, fcLua, params, updates = []) {
|
|
49
|
+
return this.send('loaded', { type: 'load', bytes, updates, fcLua, params }, [
|
|
50
|
+
...new Set([bytes.buffer, ...updates.map((u) => u.buffer)])
|
|
51
|
+
]);
|
|
52
|
+
}
|
|
53
|
+
rerun(params) {
|
|
54
|
+
return this.send('portrayed', { type: 'rerun', params });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function isType(r, type) {
|
|
58
|
+
return r.type === type;
|
|
59
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface ContextParameter {
|
|
2
|
+
name: string;
|
|
3
|
+
type: string;
|
|
4
|
+
value: string;
|
|
5
|
+
}
|
|
6
|
+
export interface PortrayalFiles {
|
|
7
|
+
/** Rule file name (without directory) to Lua source. */
|
|
8
|
+
rules: Record<string, string>;
|
|
9
|
+
host: string;
|
|
10
|
+
}
|
|
11
|
+
export interface PortrayalResult {
|
|
12
|
+
/** feature id to drawing instruction string */
|
|
13
|
+
instructions: Map<string, string>;
|
|
14
|
+
errors: string[];
|
|
15
|
+
ms: {
|
|
16
|
+
load: number;
|
|
17
|
+
portray: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export declare class Portrayer {
|
|
21
|
+
private engine?;
|
|
22
|
+
private readonly factory;
|
|
23
|
+
private readonly files;
|
|
24
|
+
constructor(files: PortrayalFiles, wasmUri?: string);
|
|
25
|
+
init(): Promise<void>;
|
|
26
|
+
portray(datasetLua: string, catalogueLua: string, params: ContextParameter[]): Promise<PortrayalResult>;
|
|
27
|
+
/** Portrays the loaded dataset again, e.g. after a context parameter changed. */
|
|
28
|
+
rerun(params: ContextParameter[]): Promise<PortrayalResult>;
|
|
29
|
+
}
|
package/dist/portray.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Runs an IHO S-100 Lua portrayal catalogue over a dataset in wasmoon.
|
|
2
|
+
import { LuaFactory } from 'wasmoon';
|
|
3
|
+
export class Portrayer {
|
|
4
|
+
engine;
|
|
5
|
+
factory;
|
|
6
|
+
files;
|
|
7
|
+
constructor(files, wasmUri) {
|
|
8
|
+
this.files = files;
|
|
9
|
+
this.factory = new LuaFactory(wasmUri);
|
|
10
|
+
}
|
|
11
|
+
async init() {
|
|
12
|
+
for (const [name, src] of Object.entries(this.files.rules))
|
|
13
|
+
await this.factory.mountFile(`/rules/${name}`, src);
|
|
14
|
+
this.engine = await this.factory.createEngine({ injectObjects: false, enableProxy: false });
|
|
15
|
+
await this.engine.doString(`package.path = '/rules/?.lua'`);
|
|
16
|
+
await this.engine.doString(this.files.host);
|
|
17
|
+
}
|
|
18
|
+
async portray(datasetLua, catalogueLua, params) {
|
|
19
|
+
const lua = this.engine;
|
|
20
|
+
if (!lua)
|
|
21
|
+
throw new Error('init() first');
|
|
22
|
+
const t0 = performance.now();
|
|
23
|
+
await lua.doString(catalogueLua);
|
|
24
|
+
await lua.doString(datasetLua);
|
|
25
|
+
await lua.doString(`typeInfo = nil require 'main'`);
|
|
26
|
+
const load = performance.now() - t0;
|
|
27
|
+
const r = await this.rerun(params);
|
|
28
|
+
r.ms.load = load;
|
|
29
|
+
return r;
|
|
30
|
+
}
|
|
31
|
+
/** Portrays the loaded dataset again, e.g. after a context parameter changed. */
|
|
32
|
+
async rerun(params) {
|
|
33
|
+
const lua = this.engine;
|
|
34
|
+
if (!lua)
|
|
35
|
+
throw new Error('init() first');
|
|
36
|
+
const t0 = performance.now();
|
|
37
|
+
await lua.doString(`OUT = {} ERRORS = {}`);
|
|
38
|
+
const cps = params
|
|
39
|
+
.map((p) => `PortrayalCreateContextParameter('${p.name}','${p.type}','${p.value.replace(/'/g, "\\'")}')`)
|
|
40
|
+
.join(',');
|
|
41
|
+
const t1 = performance.now();
|
|
42
|
+
await lua.doString(`PortrayalInitializeContextParameters({${cps}}) PortrayalMain()`);
|
|
43
|
+
const out = (await lua.doString(`return table.concat(OUT, '\\n')`));
|
|
44
|
+
const errors = (await lua.doString(`return table.concat(ERRORS, '\\n')`));
|
|
45
|
+
const t2 = performance.now();
|
|
46
|
+
const instructions = new Map();
|
|
47
|
+
for (const line of out.split('\n')) {
|
|
48
|
+
if (!line)
|
|
49
|
+
continue;
|
|
50
|
+
const tab = line.indexOf('\t');
|
|
51
|
+
instructions.set(line.slice(0, tab), line.slice(tab + 1));
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
instructions,
|
|
55
|
+
errors: errors ? errors.split('\n') : [],
|
|
56
|
+
ms: { load: t1 - t0, portray: t2 - t1 }
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import Feature from 'ol/Feature.js';
|
|
2
|
+
import { Style } from 'ol/style.js';
|
|
3
|
+
import { type DrawItem } from './instructions.ts';
|
|
4
|
+
import type { S101Dataset } from './s101.ts';
|
|
5
|
+
export declare const PX_PER_MM: number;
|
|
6
|
+
/** Screen-sized augmented paths of the S-101 catalogue reach at most 26 mm from their light; a layer drawing them needs this render buffer. */
|
|
7
|
+
export declare const AUGMENTED_RENDER_BUFFER: number;
|
|
8
|
+
export interface LineStyleDef {
|
|
9
|
+
interval: number;
|
|
10
|
+
width: number;
|
|
11
|
+
color: string;
|
|
12
|
+
dashes: [number, number][];
|
|
13
|
+
symbols: [string, number][];
|
|
14
|
+
}
|
|
15
|
+
export interface AreaFillDef {
|
|
16
|
+
symbol: string;
|
|
17
|
+
v1: [number, number];
|
|
18
|
+
v2: [number, number];
|
|
19
|
+
}
|
|
20
|
+
export interface Catalogue {
|
|
21
|
+
rules: Record<string, string>;
|
|
22
|
+
palettes: Partial<Record<string, Record<string, string>>>;
|
|
23
|
+
symbols: Partial<Record<string, string>>;
|
|
24
|
+
lineStyles: Partial<Record<string, LineStyleDef>>;
|
|
25
|
+
areaFills: Partial<Record<string, AreaFillDef>>;
|
|
26
|
+
context: {
|
|
27
|
+
name: string;
|
|
28
|
+
type: string;
|
|
29
|
+
value: string;
|
|
30
|
+
}[];
|
|
31
|
+
fcLua: string;
|
|
32
|
+
hostLua: string;
|
|
33
|
+
}
|
|
34
|
+
export interface ChartFeatures {
|
|
35
|
+
features: Feature[];
|
|
36
|
+
/** viewing groups seen */
|
|
37
|
+
groups: Set<number>;
|
|
38
|
+
}
|
|
39
|
+
/** One OL feature per drawing item; the item rides along as property `item`. */
|
|
40
|
+
export declare function buildFeatures(ds: S101Dataset, instructions: Map<string, string>): ChartFeatures;
|
|
41
|
+
/** Styles for one colour palette, cached per drawing item shape. */
|
|
42
|
+
export declare class Stylist {
|
|
43
|
+
private readonly cat;
|
|
44
|
+
private readonly colors;
|
|
45
|
+
private readonly css;
|
|
46
|
+
private readonly styles;
|
|
47
|
+
private readonly icons;
|
|
48
|
+
constructor(cat: Catalogue, palette: string);
|
|
49
|
+
color(token: string, transparency?: number): string;
|
|
50
|
+
private icon;
|
|
51
|
+
private pattern;
|
|
52
|
+
onPatternReady?: () => void;
|
|
53
|
+
style(item: DrawItem): Style[];
|
|
54
|
+
/** Colour, width and dash in pixels of a simple or a named catalogue line style. */
|
|
55
|
+
private lineLook;
|
|
56
|
+
private augmentedStyle;
|
|
57
|
+
private make;
|
|
58
|
+
}
|
|
59
|
+
/** Scale denominator shown at a Web Mercator resolution and latitude, for ScaleMinimum. */
|
|
60
|
+
export declare function scaleAt(resolution: number, latDeg: number): number;
|