@metoceanapi/wxtiles-common 0.0.8 → 0.0.10

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.
@@ -0,0 +1,376 @@
1
+ import { PixelsToLonLat, coordToPixel } from "../utils/mercator";
2
+ import type { DataPicture, XYZ } from "../utils/wxtools";
3
+ import type { WxBoundaryMeta } from "../types";
4
+
5
+ /**
6
+ * Convert a degree to a value on a linear scale, wrapping around at 360 degrees.
7
+ * @param start - The starting angle in degrees.
8
+ * @param end - The ending angle in degrees.
9
+ * @param amount - The interpolation amount between the start and end.
10
+ * @returns The interpolated angle, wrapped to the [0, 360) degree range.
11
+ */
12
+ function interpolatorDegreeLinear(
13
+ start: number,
14
+ end: number,
15
+ amount: number,
16
+ ): number {
17
+ const shortestAngle = ((((end - start) % 360) + 540) % 360) - 180;
18
+ return (start + shortestAngle * amount + 360) % 360;
19
+ }
20
+
21
+ type InterpolatorSquare = (
22
+ a: number,
23
+ b: number,
24
+ c: number,
25
+ d: number,
26
+ dxt: number,
27
+ dyt: number,
28
+ dmin: number,
29
+ dmul: number,
30
+ ) => number;
31
+
32
+ /**
33
+ * Interpolator function for square-based bilinear interpolation.
34
+ * This function calculates the interpolated value based on the corners of the square.
35
+ * The interpolation is applied within a 2D grid of values.
36
+ *
37
+ * @param a - Value at the top-left corner of the square.
38
+ * @param b - Value at the top-right corner of the square.
39
+ * @param c - Value at the bottom-left corner of the square.
40
+ * @param d - Value at the bottom-right corner of the square.
41
+ * @param dxt - Interpolation coefficient in the x-direction.
42
+ * @param dyt - Interpolation coefficient in the y-direction.
43
+ * @param dmin - Minimum data value.
44
+ * @param dmul - Multiplication factor for scaling.
45
+ * @returns The interpolated value within the square.
46
+ */
47
+ function interpolatorSquareDegree(
48
+ a: number,
49
+ b: number,
50
+ c: number,
51
+ d: number,
52
+ dxt: number,
53
+ dyt: number,
54
+ dmin: number,
55
+ dmul: number,
56
+ ): number {
57
+ // a----b
58
+ // | |
59
+ // | |
60
+ // c----d
61
+
62
+ // 16 possible variants for a,b,c,d != 0 ( != NaN)
63
+ const sq = (a ? 1 : 0) | (b ? 2 : 0) | (c ? 4 : 0) | (d ? 8 : 0);
64
+ switch (sq) {
65
+ case 0b0000:
66
+ return 0; // NaN
67
+ case 0b0001: // only a != NaN
68
+ return a;
69
+ case 0b0010: // only b != NaN
70
+ return b;
71
+ case 0b0011: // a, b != NaN
72
+ c = a;
73
+ d = b;
74
+ break;
75
+ case 0b0100: // ... etc
76
+ return c;
77
+ case 0b0101:
78
+ b = a;
79
+ d = c;
80
+ break;
81
+ case 0b0110:
82
+ d = (b + c) >> 1;
83
+ a = d;
84
+ break;
85
+ case 0b0111:
86
+ d = (b + c) >> 1;
87
+ break;
88
+ case 0b1000:
89
+ return d;
90
+ case 0b1001:
91
+ b = (a + d) >> 1;
92
+ c = b;
93
+ break;
94
+ case 0b1010:
95
+ a = b;
96
+ c = d;
97
+ break;
98
+ case 0b1011:
99
+ c = (a + d) >> 1;
100
+ break;
101
+ case 0b1100:
102
+ a = c;
103
+ b = d;
104
+ break;
105
+ case 0b1101:
106
+ b = (a + d) >> 1;
107
+ break;
108
+ case 0b1110:
109
+ a = (b + c) >> 1;
110
+ break;
111
+ }
112
+
113
+ // decode Data
114
+ a = dmin + dmul * a;
115
+ b = dmin + dmul * b;
116
+ c = dmin + dmul * c;
117
+ d = dmin + dmul * d;
118
+
119
+ // 2) bilinear
120
+ const u = interpolatorDegreeLinear(a, b, dxt); // upper line
121
+ const l = interpolatorDegreeLinear(c, d, dxt); // lower line
122
+ // Encode Data back before returning
123
+ const ul = (interpolatorDegreeLinear(u, l, dyt) - dmin) / dmul;
124
+ if (ul < 1) return 1; // As 0 is NaN, we don't need NaN here! so return 1 instead of NaN
125
+ return ul;
126
+ }
127
+
128
+ /**
129
+ * Interpolator function for square-based interpolation.
130
+ * This function calculates the interpolated value within a square grid.
131
+ *
132
+ * @param a - Value at the top-left corner of the square.
133
+ * @param b - Value at the top-right corner of the square.
134
+ * @param c - Value at the bottom-left corner of the square.
135
+ * @param d - Value at the bottom-right corner of the square.
136
+ * @param dxt - Interpolation coefficient in the x-direction.
137
+ * @param dyt - Interpolation coefficient in the y-direction.
138
+ * @param dmin - Minimum data value.
139
+ * @param dmul - Multiplication factor for scaling.
140
+ * @returns The interpolated value within the square.
141
+ */
142
+ function interpolatorSquare(
143
+ a: number,
144
+ b: number,
145
+ c: number,
146
+ d: number,
147
+ dxt: number,
148
+ dyt: number,
149
+ dmin: number,
150
+ dmul: number,
151
+ ): number {
152
+ // 0 1
153
+ // a --- b default version a --- b flipped version
154
+ // | / | | \ |
155
+ // | / x | - pyt | \ |
156
+ // c --- d c --- d
157
+ // 2 | 3
158
+ // pxt
159
+ //
160
+ // x - point to interpolate
161
+ // a, b, c, d - corners of the square
162
+ // 16 possible variants for a,b,c,d != 0 ( != NaN)
163
+ const sq = (a ? 1 : 0) | (b ? 2 : 0) | (c ? 4 : 0) | (d ? 8 : 0);
164
+ switch (sq) {
165
+ case 0b0111: // -cba -default version
166
+ return dxt + dyt < 1 ? dxt * (b - a) + dyt * (c - a) + a : 0;
167
+ case 0b1110: // dcb- -default version
168
+ return dxt + dyt < 1 ? 0 : dxt * (d - c) + dyt * (d - b) + b + c - d;
169
+ case 0b1011: // d-ba - flipped version
170
+ return dyt < dxt ? (1 - dxt) * (a - b) + dyt * (d - b) + b : 0;
171
+ case 0b1101: // dc-a - flipped version
172
+ return dyt < dxt ? 0 : (1 - dxt) * (c - d) + dyt * (c - a) + a + d - c;
173
+ case 0b1111: // dcba - default version
174
+ return dxt + dyt < 1
175
+ ? dxt * (b - a) + dyt * (c - a) + a
176
+ : dxt * (d - c) + dyt * (d - b) + b + c - d;
177
+ default:
178
+ return 0;
179
+ }
180
+ }
181
+
182
+ /**
183
+ * Subdivide a data picture using a specified interpolation method.
184
+ * The interpolation is performed to extract a sub-region (subtile) from a larger data picture.
185
+ *
186
+ * @param interpolator - The interpolation function to use.
187
+ * @param inputData - The input data picture from which to extract the subtile.
188
+ * @param subCoords - The coordinates of the subtile within the grid.
189
+ * @returns The subdivided data picture containing the interpolated subtile.
190
+ */
191
+ function subDataPicture(
192
+ interpolator: InterpolatorSquare,
193
+ inputData: DataPicture,
194
+ subCoords: XYZ,
195
+ ): DataPicture {
196
+ const subTileSize = 1 / 2 ** subCoords.z; // a size of a subtile
197
+ const subTileStartX = subCoords.x * 256 * subTileSize - 0.5; // upper left point of a subtile Shifted by 0.5 to get the center of the pixel
198
+ const subTileStartY = subCoords.y * 256 * subTileSize - 0.5;
199
+ const { raw: inRaw, dmin, dmax, dmul } = inputData;
200
+ const subData: DataPicture = {
201
+ raw: new Uint16Array(258 * 258),
202
+ dmin,
203
+ dmax,
204
+ dmul,
205
+ };
206
+ const { raw: outRaw } = subData;
207
+ for (
208
+ let outY = -1,
209
+ outIndex = 0,
210
+ inYf = subTileStartY - subTileSize + 1 /*+1 to shift index */;
211
+ outY <= 256;
212
+ outY++, inYf += subTileSize
213
+ ) {
214
+ const inYi = Math.floor(inYf); // don't use `~~` because of negatives on left and upper borders
215
+ const yt = inYf - inYi; // [0, 1] - `y` interpolati`on coeff
216
+ const inYi258 = inYi * 258;
217
+ for (
218
+ let outX = -1,
219
+ inXf = subTileStartX - subTileSize + 1 /*+1 to shift index */;
220
+ outX <= 256;
221
+ outX++, outIndex++, inXf += subTileSize
222
+ ) {
223
+ const inXi = Math.floor(inXf); // don't use ~~ because of negatives
224
+ const xt = inXf - inXi;
225
+ const inIndex = inXi + inYi258; // data index
226
+ // interpolation inside a rectangular
227
+ const a = inRaw[inIndex]; // upper left corner
228
+ const b = inRaw[inIndex + 1]; // upper right
229
+ const c = inRaw[inIndex + 258]; // lower left
230
+ const d = inRaw[inIndex + 258 + 1]; // lower right
231
+ outRaw[outIndex] = interpolator(a, b, c, d, xt, yt, dmin, dmul);
232
+ } // for x
233
+ } // for y
234
+
235
+ return subData;
236
+ }
237
+
238
+ /**
239
+ * Get sub-tile of a mask via baricemrtric interpolation
240
+ * @param {ImageData} inputData - input data
241
+ * @param {XYZ | undefined} subCoords - subtile coordinates
242
+ * @returns {ImageData} subtile data
243
+ * */
244
+ export function subMask(
245
+ inputData: ImageData,
246
+ subCoords: XYZ | undefined,
247
+ channel: number,
248
+ ): ImageData {
249
+ if (!subCoords) return inputData;
250
+
251
+ const clamp = (v: number) => (v < 0 ? 0 : v > 254 ? 254 : v);
252
+ const subTileSize = 1 / 2 ** subCoords.z; // a size of a subtile
253
+ const subTileStartX = subCoords.x * 256 * subTileSize - 0.5; // upper left point of a subtile Shifted by 0.5 to get the center of the pixel
254
+ const subTileStartY = subCoords.y * 256 * subTileSize - 0.5;
255
+ const { data: inData } = inputData;
256
+ const subData: ImageData = new ImageData(256, 256);
257
+ const { data: outData } = subData;
258
+ for (
259
+ let outY = 0, outIndex = channel, inYf = subTileStartY;
260
+ outY < 256;
261
+ outY++, inYf += subTileSize
262
+ ) {
263
+ const inYi = ~~clamp(inYf);
264
+ const yt = inYf - inYi; // [0, 1] - `y` interpolation coeff
265
+ const inYi256 = inYi * 256;
266
+ for (
267
+ let outX = 0, inXf = subTileStartX;
268
+ outX < 256;
269
+ outX++, outIndex += 4, inXf += subTileSize
270
+ ) {
271
+ const inXi = ~~clamp(inXf);
272
+ const xt = inXf - inXi;
273
+ const inIndex = (inXi + inYi256) * 4 + channel; // data index to pixel/channel used as a mask
274
+ // interpolation inside a rectangular
275
+ const a = inData[inIndex]; // upper left corner
276
+ const b = inData[inIndex + 4]; // upper right
277
+ const c = inData[inIndex + 4 * 256]; // lower left
278
+ const d = inData[inIndex + 4 * 256 + 4]; // lower right
279
+ const r = interpolatorSquare(a, b, c, d, xt, yt, 0, 0);
280
+ // const r = xt + yt < 1 ? xt * (b - a) + yt * (c - a) + a : xt * (d - c) + yt * (d - b) + b + c - d; // barycentric interpolation
281
+ outData[outIndex] = r > 127 ? 255 : 0;
282
+ } // for x
283
+ } // for y
284
+
285
+ return subData;
286
+ }
287
+
288
+ /**
289
+ * Get sub-tile of a regular data via baricemrtric interpolation
290
+ * @param {DataPicture} inputData - input data
291
+ * @param {XYZ | undefined} subCoords - subtile coordinates
292
+ * @returns {DataPicture} subtile data
293
+ * */
294
+ export function subData(inputData: DataPicture, subCoords?: XYZ): DataPicture {
295
+ if (!subCoords) return inputData;
296
+ return subDataPicture(interpolatorSquare, inputData, subCoords);
297
+ }
298
+
299
+ /**
300
+ * Get sub-tile of a degree data tile via bilinear degree interpolation, so middle of 350..10 degree is 0 degree.
301
+ * @param {DataPicture} inputData - input data
302
+ * @param {XYZ | undefined} subCoords - subtile coordinates
303
+ * @returns {DataPicture} subtile data
304
+ * */
305
+ export function subDataDegree(
306
+ inputData: DataPicture,
307
+ subCoords?: XYZ,
308
+ ): DataPicture {
309
+ if (!subCoords) return inputData;
310
+ return subDataPicture(interpolatorSquareDegree, inputData, subCoords);
311
+ }
312
+
313
+ /**
314
+ * Upply sea/land mask to a data tile
315
+ * 0 - for the masks from Sarah (current), 3 - for the masks from Mapbox
316
+ * @param {DataPicture} dataIn - data tile
317
+ * @param {ImageData} mask - sea/land mask
318
+ * @param {number} mc - mask channel of the mask picture to use (0 - Red, 1 - Green, 2 - Blue, 3 - Alpha)
319
+ * @param {'sea' | 'land'} maskType - sea or land masking to apply
320
+ * @returns {DataPicture} - masked data tile
321
+ * */
322
+ export function applyMask(
323
+ dataIn: DataPicture,
324
+ { data }: ImageData,
325
+ mc: number,
326
+ maskType: "land" | "sea",
327
+ ): DataPicture {
328
+ const sea = maskType === "sea";
329
+ const { raw } = dataIn;
330
+ for (let i = mc, j = 259, y = 0; y < 256; j += 2, y++) {
331
+ for (let x = 0; x < 256; x++, j++, i += 4) {
332
+ if (sea === !data[i]) {
333
+ raw[j] = 0; // Perform assignment clearly in the if block
334
+ }
335
+ }
336
+ }
337
+
338
+ return dataIn;
339
+ }
340
+
341
+ /**
342
+ * Create a bounding box for a tile
343
+ * @param coords - tile coordinates
344
+ * @returns {WxBoundaryMeta} [minLon, minLat, maxLon, maxLat]
345
+ * */
346
+ export function makeBox(coords: XYZ): WxBoundaryMeta {
347
+ const [px, py] = coordToPixel(coords.x, coords.y);
348
+ const [west, north] = PixelsToLonLat(px, py, coords.z);
349
+ const [east, south] = PixelsToLonLat(px + 256, py + 256, coords.z);
350
+ return { west, north, east, south };
351
+ }
352
+
353
+ /**
354
+ * Splits tile coordinates into a tile coords at maximum zoom and a subtile coords.
355
+ * If the tile is below maximum zoom, the subtile coords are undefined.
356
+ * @param coords - tile coordinates
357
+ * @param maxZoom - maximum zoom
358
+ * @returns [tile coords, subtile coords (or undefined)]
359
+ * */
360
+ export function splitCoords(
361
+ coords: XYZ,
362
+ maxZoom: number,
363
+ ): { upCoords: XYZ; subCoords?: XYZ } {
364
+ const zDif = coords.z - maxZoom;
365
+ if (zDif <= 0) {
366
+ return { upCoords: coords };
367
+ }
368
+
369
+ const upCoords = { x: coords.x >>> zDif, y: coords.y >>> zDif, z: maxZoom };
370
+ const subCoords = {
371
+ x: coords.x & ((1 << zDif) - 1),
372
+ y: coords.y & ((1 << zDif) - 1),
373
+ z: zDif,
374
+ };
375
+ return { upCoords, subCoords };
376
+ }