@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.
- package/biome.json +12 -0
- package/managers/BaseDatasetManager.ts +274 -0
- package/managers/WxAllDatasetsManager.ts +60 -0
- package/package.json +3 -2
- package/types.ts +471 -0
- package/wxapi.ts +223 -0
- package/wxlayer/WxImplementation.ts +372 -0
- package/wxlayer/loader.ts +305 -0
- package/wxlayer/loadertools.ts +376 -0
- package/wxlayer/painter.ts +451 -0
- package/wxlayer/wxlayer.ts +379 -0
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { RawCLUT } from "../utils/RawCLUT";
|
|
2
|
+
import type {
|
|
3
|
+
DataPictures,
|
|
4
|
+
WxColorStyleStrict,
|
|
5
|
+
WxColorStyleWeak,
|
|
6
|
+
XYZ,
|
|
7
|
+
} from "../utils/wxtools";
|
|
8
|
+
import {
|
|
9
|
+
HashXYZ,
|
|
10
|
+
RGBtoHEX,
|
|
11
|
+
WXLOG,
|
|
12
|
+
WxGetColorStyles,
|
|
13
|
+
create2DContext,
|
|
14
|
+
} from "../utils/wxtools";
|
|
15
|
+
import type { WxRequestInit, WxAllBoundariesMeta, WxVariableMeta, WxDate, WxURIs, WxTileInfo, WxLayerVarsNames, WxRasterData, WxLayerOptions } from "../types";
|
|
16
|
+
import { WxRasterDataCache } from "../types";
|
|
17
|
+
import { Loader } from "./loader";
|
|
18
|
+
import { Painter } from "./painter";
|
|
19
|
+
import type { BaseDatasetManager } from "../managers/BaseDatasetManager";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @internal
|
|
23
|
+
* Used in {@link WxLayerBaseImplementation} to manipulate the the layer's style, data and time
|
|
24
|
+
* */
|
|
25
|
+
export class WxLayer<WxDatasetManagerT extends BaseDatasetManager> {
|
|
26
|
+
/** @internal Variables to be displayed by the layer */
|
|
27
|
+
readonly variables: WxLayerVarsNames; // variables of the dataset if vector then [eastward, northward]
|
|
28
|
+
|
|
29
|
+
/** @internal Data manager created this layer */
|
|
30
|
+
readonly wxdatasetManager: WxDatasetManagerT;
|
|
31
|
+
|
|
32
|
+
/** @internal Current time*/
|
|
33
|
+
protected _time: string;
|
|
34
|
+
|
|
35
|
+
/** @internal Current variable's Meta data of the layer*/
|
|
36
|
+
currentVariableMeta: WxVariableMeta;
|
|
37
|
+
|
|
38
|
+
/** @internal current URIs to fetch tiles */
|
|
39
|
+
tilesURIs: WxURIs;
|
|
40
|
+
|
|
41
|
+
/** @internal current Style Object */
|
|
42
|
+
style: WxColorStyleStrict;
|
|
43
|
+
|
|
44
|
+
/** @internal Current "Color lookup table" object */
|
|
45
|
+
CLUT: RawCLUT;
|
|
46
|
+
|
|
47
|
+
/** @internal cahce: URI->rendered data on canvases */
|
|
48
|
+
tilesRasterCache: WxRasterDataCache = new WxRasterDataCache();
|
|
49
|
+
|
|
50
|
+
/** @internal current coarsing zoom level */
|
|
51
|
+
coarseLevel = 0;
|
|
52
|
+
|
|
53
|
+
/** @internal Painter object to render tiles */
|
|
54
|
+
protected readonly _painter: Painter<WxDatasetManagerT> = new Painter(this);
|
|
55
|
+
|
|
56
|
+
/** @internal Loader object to load and preprocess tiles */
|
|
57
|
+
protected readonly _loader: Loader<WxDatasetManagerT> = new Loader<WxDatasetManagerT>(this);
|
|
58
|
+
|
|
59
|
+
constructor(wxLayerOptions: WxLayerOptions<WxDatasetManagerT>) {
|
|
60
|
+
WXLOG(`WxLayer.constructor:
|
|
61
|
+
datasetName: ${wxLayerOptions.wxdatasetManager.datasetName},
|
|
62
|
+
variables: ${wxLayerOptions.variables.join(", ")},
|
|
63
|
+
wxstyleName: ${wxLayerOptions.wxstyleName},
|
|
64
|
+
wxstyle: ${JSON.stringify(wxLayerOptions.wxstyle)},
|
|
65
|
+
time: ${wxLayerOptions.time}`);
|
|
66
|
+
|
|
67
|
+
// check variables
|
|
68
|
+
if (
|
|
69
|
+
!wxLayerOptions.variables?.length ||
|
|
70
|
+
wxLayerOptions.variables.length > 2
|
|
71
|
+
) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
`datasetName: ${wxLayerOptions.wxdatasetManager.datasetName}: only 1 or 2 variables are supported but ${wxLayerOptions.variables} was given`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
for (const v of wxLayerOptions.variables) {
|
|
78
|
+
if (!wxLayerOptions.wxdatasetManager.isVariableValid(v)) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`datasetName: ${wxLayerOptions.wxdatasetManager.datasetName}: variable ${v} is not valid`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this.variables = wxLayerOptions.variables;
|
|
86
|
+
this.wxdatasetManager = wxLayerOptions.wxdatasetManager;
|
|
87
|
+
[this.tilesURIs, this._time] = this._createURLsAndTime(wxLayerOptions.time);
|
|
88
|
+
|
|
89
|
+
const styles = WxGetColorStyles();
|
|
90
|
+
const baseStyle = styles.base;
|
|
91
|
+
const wxOptStyle =
|
|
92
|
+
wxLayerOptions.wxstyleName && styles[wxLayerOptions.wxstyleName];
|
|
93
|
+
this.currentVariableMeta = this._getCurrentVariableMeta();
|
|
94
|
+
[this.style, this.CLUT] = this._createStyleAndCLUT({
|
|
95
|
+
...baseStyle,
|
|
96
|
+
...wxOptStyle,
|
|
97
|
+
...wxLayerOptions.wxstyle,
|
|
98
|
+
});
|
|
99
|
+
} // constructor
|
|
100
|
+
|
|
101
|
+
/** Check if the layer is vector and may be animated according to style */
|
|
102
|
+
get nonanimatable(): boolean {
|
|
103
|
+
WXLOG("WxLayer.nonanimatabl()");
|
|
104
|
+
return (
|
|
105
|
+
this.variables.length < 2 ||
|
|
106
|
+
this.style.streamLineStatic ||
|
|
107
|
+
this.style.streamLineColor === "none"
|
|
108
|
+
);
|
|
109
|
+
} // animatable
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @internal
|
|
113
|
+
* Get maximum zoom according to the layer's Dataset manager
|
|
114
|
+
* @returns {number} maximum zoom
|
|
115
|
+
*/
|
|
116
|
+
getMaxZoom(): number {
|
|
117
|
+
WXLOG("WxLayer.getMaxZoom");
|
|
118
|
+
return this.wxdatasetManager.isInstanced()
|
|
119
|
+
? this.wxdatasetManager.getInstanceMeta(this.getTime()).maxZoom
|
|
120
|
+
: this.wxdatasetManager.getMaxZoom();
|
|
121
|
+
} // getMaxZoom
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @internal
|
|
125
|
+
* Get maximum zoom according to the layer's Dataset manager and the current coarse level
|
|
126
|
+
* @returns {number} maximum zoom
|
|
127
|
+
* */
|
|
128
|
+
getCoarseMaxZoom(): number {
|
|
129
|
+
WXLOG("WxLayer.getCoarseMaxZoom");
|
|
130
|
+
return this.getMaxZoom() - this.coarseLevel;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @internal
|
|
135
|
+
* Get boundaries zoom according to the layer's Dataset manager
|
|
136
|
+
* @returns {WxAllBoundariesMeta | undefined} boundaries
|
|
137
|
+
* */
|
|
138
|
+
getBoundaries(): WxAllBoundariesMeta | undefined {
|
|
139
|
+
return this.wxdatasetManager.isInstanced()
|
|
140
|
+
? this.wxdatasetManager.getInstanceMeta(this.getTime()).boundaries
|
|
141
|
+
: this.wxdatasetManager.getBoundaries();
|
|
142
|
+
} // getBoundaries
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* @internal
|
|
146
|
+
* Get the current time step of the layer
|
|
147
|
+
* @returns {string} time step in the dataset meta's format */
|
|
148
|
+
getTime(): string {
|
|
149
|
+
WXLOG("WxLayer.getTime");
|
|
150
|
+
return this._time;
|
|
151
|
+
} // getTime
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @internal
|
|
155
|
+
* Clear the cache of tiles
|
|
156
|
+
* Not to use when loading is in progress. */
|
|
157
|
+
clearCache(): void {
|
|
158
|
+
WXLOG("WxLayer.clearCache");
|
|
159
|
+
this.tilesRasterCache.clear();
|
|
160
|
+
this._loader.clearCache();
|
|
161
|
+
} // clearCache
|
|
162
|
+
|
|
163
|
+
/** @internal */
|
|
164
|
+
getCurrentStyleObjectCopy(): WxColorStyleStrict {
|
|
165
|
+
WXLOG("WxLayer.getCurrentStyleObjectCopy");
|
|
166
|
+
return Object.assign({}, this.style);
|
|
167
|
+
} // getCurrentStyleObjectCopy
|
|
168
|
+
|
|
169
|
+
/** @internal */
|
|
170
|
+
updateCurrentStyleObject(style?: WxColorStyleWeak): void {
|
|
171
|
+
WXLOG(`WxLayer.updateCurrentStyleObject ${JSON.stringify(style)}`);
|
|
172
|
+
[this.style, this.CLUT] = this._createStyleAndCLUT(style);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** @internal Used by {@link WxTileSource.getLayerInfoAtLatLon} */
|
|
176
|
+
getTileData(
|
|
177
|
+
tileCoord: XYZ,
|
|
178
|
+
tilePixel: { x: number; y: number },
|
|
179
|
+
): WxTileInfo | undefined {
|
|
180
|
+
// WXLOG(`WxLayer.getTileData tileCoord: ${tileCoord.x}, ${tileCoord.y}, ${tileCoord.z}, tilePixel: ${tilePixel.x}, ${tilePixel.y}`);
|
|
181
|
+
const tile = this.tilesRasterCache.get(HashXYZ(tileCoord));
|
|
182
|
+
if (!tile) return; // no tile
|
|
183
|
+
const tileData = this._getPixelInfo(tilePixel, tile.data.data);
|
|
184
|
+
if (!tileData) return; // oops! no data at the pixel
|
|
185
|
+
const { raw, data } = tileData;
|
|
186
|
+
const rgba = raw.map((r) => this.CLUT.colorsI[r]);
|
|
187
|
+
const hexColor = rgba.map(RGBtoHEX);
|
|
188
|
+
const inStyleUnits = data.map((d) => this.CLUT.DataToStyle(d));
|
|
189
|
+
return {
|
|
190
|
+
data,
|
|
191
|
+
raw,
|
|
192
|
+
rgba,
|
|
193
|
+
hexColor,
|
|
194
|
+
inStyleUnits,
|
|
195
|
+
tilePoint: tilePixel,
|
|
196
|
+
styleUnits: this.style.units,
|
|
197
|
+
dataUnits: this.currentVariableMeta.units,
|
|
198
|
+
};
|
|
199
|
+
} // _getTileData
|
|
200
|
+
|
|
201
|
+
/** @internal reassign URIs and a time step with a new given time step */
|
|
202
|
+
setURLsAndTime(time_?: WxDate): void {
|
|
203
|
+
WXLOG(`WxLayer.setURLsAndTime time=${time_}`);
|
|
204
|
+
[this.tilesURIs, this._time] = this._createURLsAndTime(time_);
|
|
205
|
+
if (this.wxdatasetManager.isInstanced()) {
|
|
206
|
+
this.currentVariableMeta = this._getCurrentVariableMeta();
|
|
207
|
+
[this.style, this.CLUT] = this._createStyleAndCLUT();
|
|
208
|
+
}
|
|
209
|
+
} // _setURLsAndTime
|
|
210
|
+
|
|
211
|
+
getPaintedCanvas(
|
|
212
|
+
data: WxRasterData,
|
|
213
|
+
animation: boolean,
|
|
214
|
+
seed: number,
|
|
215
|
+
): HTMLCanvasElement {
|
|
216
|
+
return this._painter.getPaintedCanvas(data, animation, seed);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @internal
|
|
221
|
+
* load, cache, draw the tile. Abortable
|
|
222
|
+
* */
|
|
223
|
+
async loadTile(
|
|
224
|
+
tile: XYZ,
|
|
225
|
+
requestInit?: WxRequestInit,
|
|
226
|
+
): Promise<WxRasterData | null> {
|
|
227
|
+
return this._loadCacheDrawTile(tile, this.tilesRasterCache, requestInit);
|
|
228
|
+
} // _loadTile
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* @internal
|
|
232
|
+
* cache given time step for faster access when needed. Resolved when done.
|
|
233
|
+
* @param time_ time step to cache
|
|
234
|
+
* @param requestInit request options
|
|
235
|
+
* @returns {Promise<void>}
|
|
236
|
+
* */
|
|
237
|
+
async preloadTime(
|
|
238
|
+
time_: WxDate,
|
|
239
|
+
tiles: XYZ[],
|
|
240
|
+
requestInit?: WxRequestInit,
|
|
241
|
+
): Promise<void> {
|
|
242
|
+
WXLOG(`WxLayer.preloadTime time=${time_}`);
|
|
243
|
+
const [tilesURIs] = this._createURLsAndTime(time_);
|
|
244
|
+
await Promise.allSettled(
|
|
245
|
+
tiles.map((tile) => this._loader.cacheLoad(tile, tilesURIs, requestInit)),
|
|
246
|
+
); // fill up cache
|
|
247
|
+
} // _preloadTime
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* @internal
|
|
251
|
+
* Load all tiles, draw it on canvas, save to cache and return. Resolved when done.
|
|
252
|
+
* @param tiles
|
|
253
|
+
* @param requestInit
|
|
254
|
+
* @returns {Promise<viod>} */
|
|
255
|
+
async reloadTiles(tiles: XYZ[], requestInit?: WxRequestInit): Promise<void> {
|
|
256
|
+
WXLOG("WxLayer.reloadTiles");
|
|
257
|
+
const tilesRasterCache = new WxRasterDataCache(); // new tiles visual data cache
|
|
258
|
+
await Promise.allSettled(
|
|
259
|
+
tiles.map((tile) =>
|
|
260
|
+
this._loadCacheDrawTile(tile, tilesRasterCache, requestInit),
|
|
261
|
+
),
|
|
262
|
+
); // fill up cache
|
|
263
|
+
|
|
264
|
+
if (!requestInit?.signal?.aborted) {
|
|
265
|
+
this.tilesRasterCache.clear(); // clear old visual data cache
|
|
266
|
+
this.tilesRasterCache = tilesRasterCache; // replace cache
|
|
267
|
+
} else tilesRasterCache.clear(); // clear unneeded cache
|
|
268
|
+
} // _reloadTiles
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* @internal
|
|
272
|
+
* @ignore
|
|
273
|
+
* */
|
|
274
|
+
protected async _loadCacheDrawTile(
|
|
275
|
+
tile: XYZ,
|
|
276
|
+
tilesRasterCache: WxRasterDataCache,
|
|
277
|
+
requestInit?: WxRequestInit,
|
|
278
|
+
): Promise<WxRasterData | null> {
|
|
279
|
+
const tileRasterData = tilesRasterCache.get(HashXYZ(tile));
|
|
280
|
+
if (tileRasterData) return tileRasterData;
|
|
281
|
+
|
|
282
|
+
const data = await this._loader.load(tile, requestInit);
|
|
283
|
+
if (!data) return null; // also happens when tile is cut by qTree or by Mask
|
|
284
|
+
|
|
285
|
+
const ctxFill = create2DContext(256, 256);
|
|
286
|
+
const ctxText = ctxFill; // check if some browsers need separate canvas for text
|
|
287
|
+
const ctxStreamLines =
|
|
288
|
+
this.variables.length === 2 ? create2DContext(256, 256) : ctxFill;
|
|
289
|
+
const raster_data: WxRasterData = {
|
|
290
|
+
ctxFill,
|
|
291
|
+
ctxText,
|
|
292
|
+
ctxStreamLines,
|
|
293
|
+
data,
|
|
294
|
+
};
|
|
295
|
+
this._painter.paint(raster_data);
|
|
296
|
+
tilesRasterCache.set(HashXYZ(tile), raster_data);
|
|
297
|
+
return raster_data;
|
|
298
|
+
} // _loadCacheDrawTile
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* @internal
|
|
302
|
+
* @ignore
|
|
303
|
+
* creates/calculates meta data for vector layers
|
|
304
|
+
* */
|
|
305
|
+
protected _getCurrentVariableMeta(): WxVariableMeta {
|
|
306
|
+
const variablesMetas = this.variables.map((v) => {
|
|
307
|
+
const variableMeta = this.wxdatasetManager.getInstanceVariableMeta(
|
|
308
|
+
v,
|
|
309
|
+
this._time,
|
|
310
|
+
); // meta <-> instance!!!
|
|
311
|
+
if (!variableMeta)
|
|
312
|
+
throw new Error(
|
|
313
|
+
`datasetName ${this.wxdatasetManager.datasetName}: variable ${v} is not valid`,
|
|
314
|
+
);
|
|
315
|
+
return variableMeta;
|
|
316
|
+
});
|
|
317
|
+
let { min, max, units, vector } = variablesMetas[0];
|
|
318
|
+
if (this.variables.length > 1) {
|
|
319
|
+
// for the verctor field we need to get the min and max of the vectors' length
|
|
320
|
+
// but convert and calculate ALL vector length just for that is too much
|
|
321
|
+
// so we just use estimation based on the max of the vector components
|
|
322
|
+
// hence min of a vector length can't be less than 0
|
|
323
|
+
min = 0;
|
|
324
|
+
// max of a field can't be less than max of the components multiplied by sqrt(2)
|
|
325
|
+
max =
|
|
326
|
+
1.42 *
|
|
327
|
+
Math.max(
|
|
328
|
+
-variablesMetas[0].min,
|
|
329
|
+
variablesMetas[0].max,
|
|
330
|
+
-variablesMetas[1].min,
|
|
331
|
+
variablesMetas[1].max,
|
|
332
|
+
);
|
|
333
|
+
// tese values arn't real! but they are good enough for the estimation
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return { min, max, units, vector };
|
|
337
|
+
} // _getCurrentMeta
|
|
338
|
+
|
|
339
|
+
/** @ignore x, y - pixel on tile, data - data tile */
|
|
340
|
+
protected _getPixelInfo(
|
|
341
|
+
{ x, y }: { x: number; y: number },
|
|
342
|
+
data: DataPictures,
|
|
343
|
+
): { raw: number[]; data: number[] } | undefined {
|
|
344
|
+
WXLOG(`WxLayer._getPixelInfo x=${x} y=${y}`);
|
|
345
|
+
const index = (y + 1) * 258 + (x + 1);
|
|
346
|
+
if (!data?.[0]?.raw?.[index]) return; // check if data is loaded and the pixel is not empty
|
|
347
|
+
return {
|
|
348
|
+
raw: data.map((data) => data.raw[index]),
|
|
349
|
+
data: data.map((data) => data.raw[index] * data.dmul + data.dmin),
|
|
350
|
+
};
|
|
351
|
+
} // _getPixelInfo
|
|
352
|
+
|
|
353
|
+
/** @ignore */
|
|
354
|
+
protected _createStyleAndCLUT(
|
|
355
|
+
style_?: WxColorStyleWeak,
|
|
356
|
+
): [WxColorStyleStrict, RawCLUT] {
|
|
357
|
+
// don't use {...obj} spread operator! as we need to assign 'undefined' values
|
|
358
|
+
const style = Object.assign({}, this.style, style_); // deep copy, so could be (and is) changed
|
|
359
|
+
const CLUT = this._prepareCLUT(style);
|
|
360
|
+
return [style, CLUT];
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** @ignore Create a new CLUT for the given style */
|
|
364
|
+
protected _prepareCLUT(style: WxColorStyleStrict): RawCLUT {
|
|
365
|
+
const { min, max, units } = this.currentVariableMeta;
|
|
366
|
+
return new RawCLUT(style, units, [min, max], this.variables.length === 2);
|
|
367
|
+
} // _prepareCLUTf
|
|
368
|
+
|
|
369
|
+
/** @ignore calculate valid timestep and URIs */
|
|
370
|
+
protected _createURLsAndTime(time_?: WxDate): [WxURIs, string] {
|
|
371
|
+
const validTime = this.wxdatasetManager.getNearestValidTime(time_);
|
|
372
|
+
const tilesURIs = <WxURIs>(
|
|
373
|
+
this.variables.map((variable) =>
|
|
374
|
+
this.wxdatasetManager.createURI(variable, validTime),
|
|
375
|
+
)
|
|
376
|
+
);
|
|
377
|
+
return [tilesURIs, validTime];
|
|
378
|
+
} // _createURLsAndTime
|
|
379
|
+
}
|