@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/.github/workflows/lint.yml +24 -0
- package/.husky/pre-commit +1 -0
- package/GPLv3.txt +674 -0
- package/biome.json +12 -0
- package/controls/WxAPIControl.ts +131 -0
- package/controls/WxInfoControl.ts +114 -0
- package/controls/WxLegendControl.ts +132 -0
- package/controls/WxStyleEditorControl.ts +560 -0
- package/controls/WxTimeControl.ts +133 -0
- package/defaults/colorschemes.ts +82 -0
- package/defaults/styles.ts +63 -0
- package/defaults/uconv.ts +56 -0
- package/hooks/pre-commit +29 -0
- package/package.json +4 -7
- package/utils/RawCLUT.ts +272 -0
- package/utils/mercator.ts +136 -0
- package/utils/qtree.ts +185 -0
- package/utils/tests_wxtools.ts +194 -0
- package/utils/wxtools.ts +1108 -0
package/utils/qtree.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview This file contains the implementation of a hierarchical quad-tree to encode the land/sea mask.
|
|
3
|
+
* Each node in the tree can have up to 4 sub-trees, which can be either full trees or empty nodes.
|
|
4
|
+
* If a node has no sub-trees, it represents a sea tile.
|
|
5
|
+
* If a node has sub-trees, it represents a mixed tile.
|
|
6
|
+
* If a node has no sub-trees and is not a leaf node, it represents a land tile.
|
|
7
|
+
*
|
|
8
|
+
* The file exports the following:
|
|
9
|
+
* - `Tree` interface: A hierarchical quad-tree to encode the land/sea mask.
|
|
10
|
+
* - `TreeN` type: Tree or undefined.
|
|
11
|
+
* - `SubTrees` type: 4 sub-trees of a quad-tree node.
|
|
12
|
+
* - `SubTreesN` type: SubTrees or undefined.
|
|
13
|
+
* - `TileType` enum: Possible type of a tile.
|
|
14
|
+
* - `QTree` class: Utilizes quad-tree structure to get the type of the tile at the given coord.
|
|
15
|
+
* - `qtreeFromString` function: Converts a string to a tree.
|
|
16
|
+
* - `getTreeDepth` function: Gets the depth of the tree.
|
|
17
|
+
* - `qTreeCheckCoord` function: Gets the type of the tile at the given coord.
|
|
18
|
+
*/
|
|
19
|
+
import { WXLOG, type XYZ, fetchJson } from "./wxtools";
|
|
20
|
+
|
|
21
|
+
/** The ASCII code for an empty node in the qtree string */
|
|
22
|
+
const codeEmpty = "A".charCodeAt(0);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A hierarchical quad-tree to encode the land/sea mask.
|
|
26
|
+
* Each node in the tree can have up to 4 sub-trees, which can be either full trees or empty nodes.
|
|
27
|
+
* If a node has no sub-trees, it represents a sea tile.
|
|
28
|
+
* If a node has sub-trees, it represents a mixed tile.
|
|
29
|
+
* If a node has no sub-trees and is not a leaf node, it represents a land tile.
|
|
30
|
+
*/
|
|
31
|
+
export interface Tree {
|
|
32
|
+
nodes?: SubTreesN;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Tree or undefined */
|
|
36
|
+
export type TreeN = Tree | undefined;
|
|
37
|
+
|
|
38
|
+
/** 4 sub-trees of a quad-tree node */
|
|
39
|
+
export type SubTrees = [TreeN, TreeN, TreeN, TreeN];
|
|
40
|
+
|
|
41
|
+
/** SubTrees or undefined */
|
|
42
|
+
export type SubTreesN = SubTrees | undefined;
|
|
43
|
+
|
|
44
|
+
/** possible type of a tile */
|
|
45
|
+
export enum TileType {
|
|
46
|
+
Land = "land",
|
|
47
|
+
Mixed = "mixed",
|
|
48
|
+
Sea = "sea",
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** class QTree utilizes quad-tree structure to get the type of the tile at the given coord */
|
|
52
|
+
export class QTree {
|
|
53
|
+
protected _qtree: Tree = {}; // by default a tree with no nodes and depth 0 always gives TileType.Mixed
|
|
54
|
+
protected _qtreedepth = 0;
|
|
55
|
+
protected _ready: Promise<void>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Class constructor for QTree.
|
|
59
|
+
* @constructor
|
|
60
|
+
* @param {RequestInfo} input - The request info (e.g. URI) to load the QTree.
|
|
61
|
+
* @param {RequestInit | undefined} requestInit - The request initialization for loading the QTree. Includes properties such as method, headers, body, etc.
|
|
62
|
+
*/
|
|
63
|
+
constructor(input: RequestInfo, requestInit?: RequestInit | undefined) {
|
|
64
|
+
WXLOG("QTree.constructor");
|
|
65
|
+
this._ready = this._load(input, requestInit);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A getter that returns a Promise that resolves when the QTree is ready to be used.
|
|
70
|
+
* @returns {Promise<void>} A Promise that resolves when the QTree is ready to be used.
|
|
71
|
+
*/
|
|
72
|
+
get ready(): Promise<void> {
|
|
73
|
+
return this._ready;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Loads the QTree from the specified input and request initialization.
|
|
78
|
+
* @async
|
|
79
|
+
* @param {RequestInfo} input - The request info (e.g. URI) to load the QTree.
|
|
80
|
+
* @param {RequestInit | undefined} requestInit - The request initialization for loading the QTree. Includes properties such as method, headers, body, etc.
|
|
81
|
+
* @returns {Promise<void>} A Promise that resolves when the QTree is loaded.
|
|
82
|
+
* @throws {Error} If the QTree fails to load.
|
|
83
|
+
*/
|
|
84
|
+
protected async _load(
|
|
85
|
+
input: RequestInfo,
|
|
86
|
+
requestInit?: RequestInit | undefined,
|
|
87
|
+
): Promise<void> {
|
|
88
|
+
if (input === "none") return;
|
|
89
|
+
try {
|
|
90
|
+
const _seamask = await fetchJson<string>(input, requestInit);
|
|
91
|
+
this._qtree = qtreeFromString(_seamask, { pos: 0 });
|
|
92
|
+
} catch (e) {
|
|
93
|
+
throw new Error(`Failed to load QTree: ${e.message}`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
this._qtreedepth = getTreeDepth(this._qtree);
|
|
97
|
+
WXLOG(`QTree.load: qtreedepth=${this._qtreedepth}`);
|
|
98
|
+
} // _load
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Returns the type of the tile at the given coordinates.
|
|
102
|
+
* @param {XYZ} coord - The coordinates of the tile to check.
|
|
103
|
+
* @returns {TileType} The type of the tile at the given coordinates.
|
|
104
|
+
*/
|
|
105
|
+
check(coord: XYZ): TileType {
|
|
106
|
+
const d = coord.z - this._qtreedepth;
|
|
107
|
+
const deepest = d >= 0; // if the coord is deeper than the qtree, don't go deeper than the qtree
|
|
108
|
+
const subCoords = deepest
|
|
109
|
+
? { x: coord.x >> d, y: coord.y >> d, z: this._qtreedepth }
|
|
110
|
+
: { ...coord /*copy!*/ };
|
|
111
|
+
return qTreeCheckCoord(this._qtree, subCoords, deepest);
|
|
112
|
+
} // check
|
|
113
|
+
} // class QTree
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Converts a string representation of a quad-tree to a Tree object.
|
|
117
|
+
* @param {string} qtreeString - The string representation of the quad-tree.
|
|
118
|
+
* @param {{ pos: number }} posHolder - An object that holds the current position in the string.
|
|
119
|
+
* @returns {Tree} A Tree object representing the quad-tree.
|
|
120
|
+
*/
|
|
121
|
+
function qtreeFromString(
|
|
122
|
+
qtreeString: string,
|
|
123
|
+
posHolder: { pos: number },
|
|
124
|
+
): Tree {
|
|
125
|
+
const code = qtreeString.charCodeAt(posHolder.pos) - codeEmpty; // code (node descriptor) of the current node
|
|
126
|
+
posHolder.pos++; // move to the next char-code (node descriptor) in the qtree string
|
|
127
|
+
// for each node we have 4 sub nodes. if (code & i) then the node is a subtree, otherwise empty node - TileType.Land
|
|
128
|
+
const subTrees = [1, 2, 4, 8].map((i) =>
|
|
129
|
+
code & i ? qtreeFromString(qtreeString, posHolder) : undefined,
|
|
130
|
+
);
|
|
131
|
+
return subTrees.some((s) => s) ? { nodes: subTrees as SubTreesN } : {}; // if some sub != undefined, then the node is a "full tree" - TileType.Sea
|
|
132
|
+
// return { nodes: subTrees.some((s) => s) ? subTrees : undefined }; // if some sub != undefined, then the node is a "full tree" - TileType.Sea
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Returns the depth of the given quad-tree.
|
|
137
|
+
* @param {TreeN} tree - The quad-tree to get the depth of.
|
|
138
|
+
* @returns {number} The depth of the quad-tree.
|
|
139
|
+
*/
|
|
140
|
+
function getTreeDepth(tree: TreeN): number {
|
|
141
|
+
return tree?.nodes ? 1 + Math.max(...tree.nodes.map(getTreeDepth)) : 0; // max of all sub trees depths + 1
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Checks the type of the tile at the given coordinates in the quad-tree.
|
|
146
|
+
* @param {TreeN} tree - The quad-tree to check.
|
|
147
|
+
* @param {XYZ} coord - The coordinates of the tile to check.
|
|
148
|
+
* @param {boolean} deepest - Whether or not the given coordinates are at the deepest level of the quad-tree.
|
|
149
|
+
* @returns {TileType} The type of the tile at the given coordinates.
|
|
150
|
+
*/
|
|
151
|
+
function qTreeCheckCoord(tree: TreeN, coord: XYZ, deepest: boolean): TileType {
|
|
152
|
+
// Create a local variable to use for tree traversal
|
|
153
|
+
let currentTree = tree;
|
|
154
|
+
|
|
155
|
+
// Check current sub-tree
|
|
156
|
+
while (currentTree) {
|
|
157
|
+
if (deepest && coord.z === 0) return TileType.Mixed; // At the top level, for the deepest level of the sub-tree, 'coord' can only be mixed
|
|
158
|
+
if (!currentTree.nodes) return TileType.Sea; // If the tree has no nodes, then it's a sea tile
|
|
159
|
+
if (coord.z === 0) return TileType.Mixed; // If we're at the top level, then it's a mixed tile
|
|
160
|
+
|
|
161
|
+
coord.z--;
|
|
162
|
+
currentTree =
|
|
163
|
+
currentTree.nodes[
|
|
164
|
+
(((coord.y >> coord.z) & 1) << 1) | ((coord.x >> coord.z) & 1)
|
|
165
|
+
];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return TileType.Land; // If there is no sub-tree, then the 'coord' is a land tile
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// /**
|
|
172
|
+
// * Recursively checks the type of the tile at the given coordinates in the quad-tree.
|
|
173
|
+
// * @param {TreeN} tree - The quad-tree to check.
|
|
174
|
+
// * @param {XYZ} coord - The coordinates of the tile to check.
|
|
175
|
+
// * @param {boolean} deepest - Whether or not the given coordinates are at the deepest level of the quad-tree.
|
|
176
|
+
// * @returns {TileType} The type of the tile at the given coordinates.
|
|
177
|
+
// */
|
|
178
|
+
// function qTreeCheckCoordRecursive(tree: TreeN, coord: XYZ, deepest: boolean): TileType {
|
|
179
|
+
// if (tree === undefined) return TileType.Land;
|
|
180
|
+
// if (deepest && !coord.z) return TileType.Mixed; // at the deepest level the tree can only be mixed
|
|
181
|
+
// if (tree.nodes === undefined) return TileType.Sea;
|
|
182
|
+
// if (!coord.z) return TileType.Mixed;
|
|
183
|
+
// coord.z--;
|
|
184
|
+
// return qTreeCheckCoordRecursive(tree.nodes[(((coord.y >> coord.z) & 1) << 1) | ((coord.x >> coord.z) & 1)], coord, deepest);
|
|
185
|
+
// } // qTreeCheckCoordRecursive
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataIntegral,
|
|
3
|
+
blurData
|
|
4
|
+
} from "./wxtools";
|
|
5
|
+
|
|
6
|
+
function ft_buildIntegralNaive(raw: Uint16Array): Uint32Array {
|
|
7
|
+
const integral = new Uint32Array(258 * 258);
|
|
8
|
+
for (let y = 0; y < 258; y++) {
|
|
9
|
+
for (let x = 0; x < 258; x++) {
|
|
10
|
+
for (let iy = 0; iy <= y; iy++) {
|
|
11
|
+
for (let ix = 0; ix <= x; ix++) {
|
|
12
|
+
integral[y * 258 + x] += raw[iy * 258 + ix];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return integral;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function ft_printIm(raw: ArrayLike<number>, n: number, comment: string) {
|
|
21
|
+
console.log(comment);
|
|
22
|
+
for (let y = 0; y < n; y++) {
|
|
23
|
+
let s = "";
|
|
24
|
+
for (let x = 0; x < n; x++) {
|
|
25
|
+
s += `${raw[y * 258 + x]}\t`;
|
|
26
|
+
}
|
|
27
|
+
s += "...\t";
|
|
28
|
+
for (let x = 258 - n; x < 258; x++) {
|
|
29
|
+
s += `${raw[y * 258 + x]}\t`;
|
|
30
|
+
}
|
|
31
|
+
console.log(s);
|
|
32
|
+
}
|
|
33
|
+
let s = "";
|
|
34
|
+
for (let x = 0; x < n * 2 + 1; x++) s += "...\t";
|
|
35
|
+
console.log(s);
|
|
36
|
+
for (let y = 258 - n; y < 258; y++) {
|
|
37
|
+
let s = "";
|
|
38
|
+
for (let x = 0; x < n; x++) {
|
|
39
|
+
s += `${raw[y * 258 + x]}\t`;
|
|
40
|
+
}
|
|
41
|
+
s += "...\t";
|
|
42
|
+
for (let x = 258 - n; x < 258; x++) {
|
|
43
|
+
s += `${raw[y * 258 + x]}\t`;
|
|
44
|
+
}
|
|
45
|
+
console.log(s);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
console.log("**********");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function ft_Uint16ArrayRandom() {
|
|
52
|
+
const seed = 1;
|
|
53
|
+
// const rnd = () => Math.floor(Math.abs(Math.sin(seed++)) * 32000 + 32000);
|
|
54
|
+
const rnd = () => Math.floor(Math.random() * 32000);
|
|
55
|
+
return new Uint16Array(258 * 258).map(rnd);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function ft_Uint16ArrayAscent() {
|
|
59
|
+
return new Uint16Array(258 * 258).map((v, i) => (i % 258) + ~~(i / 258));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function ft_Uint16ArrayDescent() {
|
|
63
|
+
return new Uint16Array(258 * 258).map(
|
|
64
|
+
(v, i) => 514 - ((i % 258) + ~~(i / 258)),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function ft_Uint32ArrayCompare(a: ArrayLike<number>, b: ArrayLike<number>) {
|
|
69
|
+
if (a.length !== b.length) return false;
|
|
70
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return i;
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function ft_blurNaive(im: Uint16Array, radius: number): Uint16Array {
|
|
75
|
+
const raw = new Uint16Array(im);
|
|
76
|
+
for (let y = 2; y < 256; y++) {
|
|
77
|
+
for (let x = 2; x < 256; x++) {
|
|
78
|
+
if (im[y * 258 + x] === 0) continue;
|
|
79
|
+
let sum = 0;
|
|
80
|
+
let count = 0;
|
|
81
|
+
|
|
82
|
+
const rx = Math.min(radius, x - 1, 256 - x);
|
|
83
|
+
const ry = Math.min(radius, y - 1, 256 - y);
|
|
84
|
+
|
|
85
|
+
for (let iy = y - ry; iy <= y + ry; iy++) {
|
|
86
|
+
for (let ix = x - rx; ix <= x + rx; ix++) {
|
|
87
|
+
if (im[iy * 258 + ix] !== 0) {
|
|
88
|
+
sum += im[iy * 258 + ix];
|
|
89
|
+
count++;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
raw[y * 258 + x] = sum / count;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return raw;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function ft_buildDataIntegralAscent(): DataIntegral {
|
|
102
|
+
return new DataIntegral(ft_Uint16ArrayAscent(), 0, 1, 1);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function ft_buildDataIntegralDescend(): DataIntegral {
|
|
106
|
+
return new DataIntegral(ft_Uint16ArrayDescent(), 0, 1, 1);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function ft_buildDataIntegralRandom(): DataIntegral {
|
|
110
|
+
return new DataIntegral(ft_Uint16ArrayRandom(), 0, 1, 1);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function test_buildIntegralPare() {
|
|
114
|
+
console.log("test_buildIntegralPare");
|
|
115
|
+
const imU = ft_buildDataIntegralAscent(); //await loadDataIntegral('https://tilestest.metoceanapi.com/data/gfs.global/2023-04-26T18:00:00Z/cloud.cover/2023-05-06T15:00:00Z/3/0/3.webp');
|
|
116
|
+
|
|
117
|
+
const inpNaive = ft_buildIntegralNaive(imU.raw); // naive algo
|
|
118
|
+
|
|
119
|
+
const rezult = ft_Uint32ArrayCompare(imU.integral.integral, inpNaive);
|
|
120
|
+
console.log(rezult ? "PASSED" : "FAILED");
|
|
121
|
+
|
|
122
|
+
!rezult && ft_printIm(imU.integral.integral, 4, "integral");
|
|
123
|
+
!rezult && ft_printIm(inpNaive, 4, "naive");
|
|
124
|
+
|
|
125
|
+
console.log("test_buildIntegralPare - end");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function test_blurData() {
|
|
129
|
+
console.log("test_blurData");
|
|
130
|
+
|
|
131
|
+
// const imU = await loadDataIntegral('https://tilestest.metoceanapi.com/data/gfs.global/2023-04-26T18:00:00Z/cloud.cover/2023-05-06T15:00:00Z/3/0/3.webp');
|
|
132
|
+
// const imD = await loadDataIntegral('https://tilestest.metoceanapi.com/data/gfs.global/2023-04-26T18:00:00Z/cloud.cover/2023-05-06T15:00:00Z/3/0/3.webp');
|
|
133
|
+
|
|
134
|
+
const blurRadius = 4;
|
|
135
|
+
const imU = ft_buildDataIntegralRandom();
|
|
136
|
+
const blurNaive = ft_blurNaive(imU.raw, blurRadius); // blur image using naive algorithm
|
|
137
|
+
blurData(imU, blurRadius); // blur image
|
|
138
|
+
|
|
139
|
+
const rezult = ft_Uint32ArrayCompare(imU.raw, blurNaive);
|
|
140
|
+
if (rezult) {
|
|
141
|
+
const printN = 3;
|
|
142
|
+
ft_printIm(imU.raw, printN, "clear image U");
|
|
143
|
+
ft_printIm(blurNaive, printN, "naive BLUR");
|
|
144
|
+
ft_printIm(imU.raw, printN, "fast integral BLUR");
|
|
145
|
+
const x = rezult % 258;
|
|
146
|
+
const y = ~~(rezult / 258);
|
|
147
|
+
console.log(
|
|
148
|
+
"test_blurData - FAILED: radius=",
|
|
149
|
+
blurRadius,
|
|
150
|
+
", (x,y)=",
|
|
151
|
+
x,
|
|
152
|
+
y,
|
|
153
|
+
" diff=",
|
|
154
|
+
imU.raw[y * 258 + x] - blurNaive[y * 258 + x],
|
|
155
|
+
);
|
|
156
|
+
} else {
|
|
157
|
+
console.log("test_blurData - PASSED");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// blurData(imU, 0); // deblur image
|
|
161
|
+
// ft_printIm(imU.raw, 4, 'deblur image');
|
|
162
|
+
|
|
163
|
+
// blurData(imD, 1);
|
|
164
|
+
// ft_printIm(imD.raw, 4, '');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function test_blurDataBoundaries() {
|
|
168
|
+
console.log("test_blurDataBoundaries");
|
|
169
|
+
// not finished
|
|
170
|
+
const blurRadius = 1;
|
|
171
|
+
const imT = ft_buildDataIntegralRandom();
|
|
172
|
+
const imB = ft_buildDataIntegralRandom();
|
|
173
|
+
// copy last two rows imT to first two rows imB
|
|
174
|
+
for (let i = 0; i < 258; i++) {
|
|
175
|
+
imB.raw[258 * 0 + i] = imT.raw[258 * 256 + i];
|
|
176
|
+
imB.raw[258 * 1 + i] = imT.raw[258 * 257 + i];
|
|
177
|
+
}
|
|
178
|
+
// print
|
|
179
|
+
ft_printIm(imT.raw, 3, "clear image T");
|
|
180
|
+
ft_printIm(imB.raw, 3, "clear image B");
|
|
181
|
+
blurData(imT, blurRadius);
|
|
182
|
+
blurData(imB, blurRadius);
|
|
183
|
+
ft_printIm(imT.raw, 3, "BLUR image T");
|
|
184
|
+
ft_printIm(imB.raw, 3, "BLUR image B");
|
|
185
|
+
|
|
186
|
+
return;
|
|
187
|
+
} // test_blurDataBoundaries
|
|
188
|
+
|
|
189
|
+
export async function tests() {
|
|
190
|
+
// TESTS are not usable in general. They are just for development and debugging
|
|
191
|
+
await test_buildIntegralPare();
|
|
192
|
+
await test_blurData();
|
|
193
|
+
await test_blurDataBoundaries();
|
|
194
|
+
}
|