@metoceanapi/wxtiles-common 0.0.6 → 0.0.8

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/biome.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
3
+ "organizeImports": {
4
+ "enabled": true
5
+ },
6
+ "linter": {
7
+ "enabled": true,
8
+ "rules": {
9
+ "recommended": true
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,131 @@
1
+ declare interface WxAPI {
2
+ getAllDatasetsNames(): Promise<string[]>;
3
+ getDatasetAllVariables(string): Promise<string[] | undefined>;
4
+ };
5
+
6
+ // // Leaflet
7
+ // const apiControl = new WxAPIControl();
8
+ // map.addControl(new (L.Control.extend(apiControl.extender()))({ position: 'topright' }));
9
+
10
+ // // MBox
11
+ // const apiControl = new WxAPIControl();
12
+ // map.addControl(apiControl, 'top-right');
13
+
14
+ export class WxAPIControl {
15
+ private readonly _div: HTMLDivElement;
16
+ readonly datasets: HTMLSelectElement;
17
+ readonly variables: HTMLSelectElement;
18
+ protected _busy = false;
19
+
20
+ onchange?: (
21
+ dataset: string,
22
+ variable: string,
23
+ resetStyleAndFlyTo?: boolean,
24
+ ) => Promise<void>;
25
+
26
+ constructor(
27
+ private readonly wxapi: WxAPI,
28
+ dataset?: string,
29
+ variable?: string,
30
+ ) {
31
+ const div = document.createElement("div");
32
+ div.className = "mapboxgl-ctrl leaflet-control";
33
+ div.style.borderStyle = "solid";
34
+ div.style.borderColor = "#000";
35
+ div.style.backgroundColor = "#aaaaaaaa";
36
+ div.style.padding = "5px";
37
+ div.style.display = "flex";
38
+ div.style.flexDirection = "column";
39
+ this._div = div;
40
+
41
+ const datasetslabel = document.createElement("label");
42
+ datasetslabel.innerText = "Dataset";
43
+ div.appendChild(datasetslabel);
44
+
45
+ this.datasets = document.createElement("select");
46
+ div.appendChild(this.datasets);
47
+ this.fillDatasets(dataset, variable);
48
+ this.datasets.addEventListener("change", async () => {
49
+ if (this._busy) return;
50
+ this.setBusy(true);
51
+ const dataset = this.datasets.value;
52
+ await this.fillVariables();
53
+ const variables = this.variables.value;
54
+ await this.onchange?.(this.datasets.value, this.variables.value);
55
+ this.datasets.value = dataset;
56
+ this.variables.value = variables;
57
+ this.setBusy(false);
58
+ });
59
+
60
+ const variableslabel = document.createElement("label");
61
+ variableslabel.innerText = "Variable";
62
+ div.appendChild(variableslabel);
63
+
64
+ this.variables = document.createElement("select");
65
+ div.appendChild(this.variables);
66
+ this.variables.addEventListener("change", async () => {
67
+ if (this._busy) return;
68
+ this.setBusy(true);
69
+ const variables = this.variables.value;
70
+ await this.onchange?.(this.datasets.value, this.variables.value);
71
+ this.variables.value = variables;
72
+ this.setBusy(false);
73
+ });
74
+
75
+ this.datasets.value = dataset || "";
76
+ this.variables.value = variable || "";
77
+ }
78
+
79
+ setBusy(b: boolean) {
80
+ this._busy = b;
81
+ this._div.style.backgroundColor = b ? "yellow" : "lightgray";
82
+ }
83
+
84
+ async fillDatasets(dataset?: string, variable?: string): Promise<void> {
85
+ this.datasets.options.length = 0;
86
+ const datasetsNames = await this.wxapi.getAllDatasetsNames();
87
+ for (const dataset of datasetsNames) {
88
+ const option = document.createElement("option");
89
+ option.value = dataset;
90
+ option.text = dataset;
91
+ this.datasets.appendChild(option);
92
+ }
93
+
94
+ if (dataset) {
95
+ this.datasets.value = dataset;
96
+ }
97
+
98
+ await this.fillVariables(variable);
99
+ }
100
+
101
+ async fillVariables(variable?: string): Promise<void> {
102
+ this.variables.options.length = 0;
103
+ const dataset = this.datasets.value;
104
+ const variables = await this.wxapi.getDatasetAllVariables(dataset);
105
+ if (variables) {
106
+ for (const variable of variables) {
107
+ const option = document.createElement("option");
108
+ option.value = variable;
109
+ option.text = variable;
110
+ this.variables.appendChild(option);
111
+ }
112
+ }
113
+
114
+ if (variable && this.variables.options.length) {
115
+ this.variables.value = variable;
116
+ }
117
+ }
118
+
119
+ onAdd(/* map */) {
120
+ return this._div;
121
+ }
122
+
123
+ onRemove() {
124
+ this._div.parentNode?.removeChild(this._div);
125
+ }
126
+
127
+ // for Leaflet
128
+ extender() {
129
+ return { onAdd: () => this.onAdd(), onRemove: () => this.onRemove() };
130
+ }
131
+ }
@@ -0,0 +1,114 @@
1
+ declare interface WxTileInfo {
2
+ data: number[];
3
+ inStyleUnits: number[];
4
+ styleUnits: string;
5
+ dataUnits: string;
6
+ }
7
+
8
+ declare interface WxDatasetMeta {
9
+ sourceID?: string;
10
+ baseAtmosphericModel?: string;
11
+ model?: string;
12
+ }
13
+
14
+ declare interface WxDataSetManager {
15
+ readonly datasetName: string;
16
+ getInstanceMeta(instance?: string): WxDatasetMeta;
17
+ }
18
+
19
+ declare interface WxVariableMeta {
20
+ units: string;
21
+ min: number;
22
+ max: number;
23
+ }
24
+
25
+ declare type WxLayerVarsNames = [string] | [string, string];
26
+
27
+ declare interface WxLngLat {
28
+ lng: number;
29
+ lat: number;
30
+ }
31
+
32
+ declare interface WxTileSource {
33
+ get wxdatasetManager(): WxDataSetManager;
34
+ getCurrentVariableMeta(): WxVariableMeta;
35
+ getTime(): string;
36
+ getVariablesNames(): WxLayerVarsNames;
37
+ getLayerInfoAtLatLon(lnglat: WxLngLat, anymap: any): WxTileInfo | undefined;
38
+ }
39
+
40
+
41
+ // // Leaflet
42
+ // const infoControl = new WxInfoControl();
43
+ // map.addControl(new (L.Control.extend(infoControl.extender()))({ position: 'topright' }));
44
+
45
+ // // MBox
46
+ // const infoControl = new WxInfoControl();
47
+ // map.addControl(infoControl, 'top-right');
48
+
49
+ export class WxInfoControl {
50
+ private readonly div: HTMLDivElement;
51
+ private pos: { lng: number; lat: number } = { lng: 0, lat: 0 };
52
+ constructor() {
53
+ const div = document.createElement("div");
54
+ div.className = "mapboxgl-ctrl leaflet-control";
55
+ div.style.borderStyle = "solid";
56
+ div.style.borderColor = "#000";
57
+ div.style.backgroundColor = "#aaaaaaaa";
58
+ div.style.padding = "5px";
59
+ this.div = div;
60
+ }
61
+
62
+ onAdd(/* map */) {
63
+ return this.div;
64
+ }
65
+
66
+ onRemove() {
67
+ this.div.parentNode?.removeChild(this.div);
68
+ }
69
+
70
+ update(
71
+ wxsource: WxTileSource | undefined,
72
+ // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use case
73
+ map: any,
74
+ pos_?: { lng: number; lat: number },
75
+ ) {
76
+ if (pos_) this.pos = pos_;
77
+ if (!wxsource) {
78
+ this.div.innerHTML = "";
79
+ return;
80
+ }
81
+
82
+ const { min, max, units } = wxsource.getCurrentVariableMeta();
83
+ const { datasetName } = wxsource.wxdatasetManager;
84
+ const datasetCurrentMeta = wxsource.wxdatasetManager.getInstanceMeta(
85
+ wxsource.getTime(),
86
+ );
87
+ const { sourceID, baseAtmosphericModel, model } = datasetCurrentMeta;
88
+ this.div.innerHTML = `lnglat=(${this.pos.lng.toFixed(2)}, ${this.pos.lat.toFixed(2)})<br>`;
89
+ this.div.innerHTML += (sourceID && `sourceID=${sourceID}<br>`) || "";
90
+ this.div.innerHTML +=
91
+ (baseAtmosphericModel &&
92
+ `baseAtmosphericModel=${baseAtmosphericModel}<br>`) ||
93
+ "";
94
+ this.div.innerHTML += (model && `model=${model}<br>`) || "";
95
+ this.div.innerHTML += `dataset=${datasetName}<br>
96
+ variables=${wxsource.getVariablesNames()}<br>
97
+ time=${wxsource.getTime()}<br>
98
+ min=${min.toFixed(2)} ${units}, max=${max.toFixed(2)} ${units}<br>`;
99
+ const tileInfo: WxTileInfo | undefined = wxsource.getLayerInfoAtLatLon(
100
+ this.pos,
101
+ map,
102
+ );
103
+ if (tileInfo) {
104
+ this.div.innerHTML += `style=${tileInfo.inStyleUnits.map((d) => d.toFixed(2))} ${tileInfo.styleUnits}<br>
105
+ source=${tileInfo.data.map((d) => d.toFixed(2))} ${tileInfo.dataUnits}<br>`;
106
+ } else {
107
+ this.div.innerHTML += "style=outside<br>source=outside<br>";
108
+ }
109
+ }
110
+ // for Leaflet
111
+ extender() {
112
+ return { onAdd: () => this.onAdd(), onRemove: () => this.onRemove() };
113
+ }
114
+ }
@@ -0,0 +1,132 @@
1
+ import { WxCreateLegend } from '../utils/RawCLUT';
2
+ import { type WxColorStyleStrict } from '../utils/wxtools';
3
+
4
+ // // Leaflet
5
+ // const legendControl = new WxLegendControl();
6
+ // map.addControl(new (L.Control.extend(legendControl.extender()))({ position: 'topright' }));
7
+
8
+ // // MBox
9
+ // const legendControl = new WxLegendControl();
10
+ // map.addControl(legendControl, 'top-right');
11
+
12
+ export class WxLegendControl {
13
+ private readonly _div: HTMLDivElement;
14
+ private readonly _canvas: HTMLCanvasElement;
15
+ constructor() {
16
+ const div = document.createElement("div");
17
+ div.className = "mapboxgl-ctrl leaflet-control";
18
+ div.style.borderStyle = "solid";
19
+ div.style.borderColor = "#000";
20
+ div.style.backgroundColor = "#aaaaaaaa";
21
+ div.style.padding = "5px";
22
+ this._div = div;
23
+
24
+ this._canvas = document.createElement("canvas");
25
+ this._canvas.width = 600;
26
+ this._canvas.height = 40;
27
+ div.appendChild(this._canvas);
28
+ }
29
+
30
+ onAdd(/* map */) {
31
+ return this._div;
32
+ }
33
+
34
+ onRemove() {
35
+ this._div.parentNode?.removeChild(this._div);
36
+ }
37
+
38
+ drawLegend(style: WxColorStyleStrict) {
39
+ const { _canvas } = this;
40
+ const { width, height } = _canvas;
41
+ const halfHeight = (16 + height) >> 2;
42
+ const legend = WxCreateLegend(width - 50, style);
43
+
44
+ // draw legend
45
+ const ctx = _canvas.getContext("2d");
46
+ if (!ctx) {
47
+ throw new Error("Failed to get 2D context from canvas");
48
+ }
49
+ const imData = ctx.createImageData(width, height);
50
+ const im = new Uint32Array(imData.data.buffer);
51
+ im.fill(-1);
52
+
53
+ const startX = 2;
54
+ const startY = 2;
55
+ const startXY = startX + width * startY;
56
+
57
+ const trSize = halfHeight >> 1;
58
+ // left triangle
59
+ if (legend.showBelowMin) {
60
+ const c = legend.colors[0];
61
+ if (c) {
62
+ for (let x = 0; x < trSize; ++x) {
63
+ for (let y = trSize; y < trSize + x; ++y) {
64
+ im[startXY + x + y * width] = c;
65
+ im[startXY + x + (trSize * 2 - y) * width] = c;
66
+ }
67
+ }
68
+ }
69
+ }
70
+
71
+ for (let x = 0; x < legend.size; ++x) {
72
+ for (let y = 0; y < halfHeight; ++y) {
73
+ if (legend.colors[0]) {
74
+ im[startX + x + trSize + (y + startY + 1) * width] = legend.colors[x];
75
+ }
76
+ }
77
+ }
78
+
79
+ // right triangle
80
+ if (legend.showAboveMax) {
81
+ const c = legend.colors[legend.colors.length - 1];
82
+ if (c) {
83
+ for (let x = 0; x <= trSize; ++x) {
84
+ for (let y = trSize; y < trSize + x; ++y) {
85
+ im[startXY + trSize * 2 + legend.size - x + y * width] = c;
86
+ im[
87
+ startXY + trSize * 2 + legend.size - x + (trSize * 2 - y) * width
88
+ ] = c;
89
+ }
90
+ }
91
+ }
92
+ }
93
+
94
+ ctx.putImageData(imData, 0, 0);
95
+
96
+ // draw ticks
97
+ ctx.font = "8px sans-serif";
98
+ ctx.beginPath();
99
+ for (const tick of legend.ticks) {
100
+ ctx.strokeStyle = "#000";
101
+ ctx.moveTo(tick.pos + trSize + startX + 1, startY + 3);
102
+ ctx.lineTo(tick.pos + trSize + startX + 1, halfHeight);
103
+ ctx.fillText(
104
+ tick.dataString,
105
+ tick.pos + trSize + startX + 1,
106
+ halfHeight + 11,
107
+ );
108
+ }
109
+
110
+ ctx.font = "12px sans-serif";
111
+ ctx.fillText(`(${legend.units})`, 13, height - 5);
112
+ ctx.stroke();
113
+
114
+ ctx.strokeStyle = "#888";
115
+ ctx.strokeRect(1, 1, width - 3, height - 2); //for white background
116
+ }
117
+
118
+ clear() {
119
+ const { _canvas } = this;
120
+ const { width, height } = _canvas;
121
+ const ctx = _canvas.getContext("2d");
122
+ if (!ctx) {
123
+ throw new Error("Failed to get 2D context from canvas");
124
+ }
125
+ ctx.clearRect(0, 0, width, height);
126
+ }
127
+
128
+ // for Leaflet
129
+ extender() {
130
+ return { onAdd: () => this.onAdd(), onRemove: () => this.onRemove() };
131
+ }
132
+ }