@metoceanapi/wxtiles-common 0.0.8 → 0.0.11

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.
Files changed (157) hide show
  1. package/LICENSE.txt +1 -1
  2. package/biome.json +12 -0
  3. package/controls/WxAPIControl/WxAPIControl-busy.ts +16 -0
  4. package/controls/WxAPIControl/WxAPIControl-datasets.ts +102 -0
  5. package/controls/WxAPIControl/WxAPIControl-filter.ts +66 -0
  6. package/controls/WxAPIControl/WxAPIControl-variables.ts +73 -0
  7. package/controls/WxAPIControl.ts +91 -120
  8. package/controls/WxInfoControl.ts +87 -2
  9. package/controls/WxLegendControl.ts +28 -2
  10. package/controls/WxStyleEditorControl.ts +123 -6
  11. package/controls/WxTimeControl.ts +132 -4
  12. package/docs/docs/.nojekyll +1 -0
  13. package/docs/docs/assets/highlight.css +92 -0
  14. package/docs/docs/assets/icons.svg +1 -0
  15. package/docs/docs/assets/style.css +1610 -0
  16. package/docs/docs/classes/controls_WxAPIControl.WxAPIControl.html +28 -0
  17. package/docs/docs/classes/controls_WxInfoControl.WxInfoControl.html +20 -0
  18. package/docs/docs/classes/controls_WxLegendControl.WxLegendControl.html +19 -0
  19. package/docs/docs/classes/controls_WxStyleEditorControl.WxStyleEditorControl.html +111 -0
  20. package/docs/docs/classes/controls_WxTimeControl.WxTimeControl.html +28 -0
  21. package/docs/docs/classes/managers_BaseDatasetManager.BaseDatasetManager.html +104 -0
  22. package/docs/docs/classes/managers_WxAllDatasetsManager.WxAllDatasetsManager.html +24 -0
  23. package/docs/docs/classes/types.WxRasterDataCache.html +27 -0
  24. package/docs/docs/classes/utils_RawCLUT.RawCLUT.html +18 -0
  25. package/docs/docs/classes/utils_qtree.QTree.html +22 -0
  26. package/docs/docs/classes/utils_wxtools.DataIntegral.html +10 -0
  27. package/docs/docs/classes/wxapi.WxAPI.html +45 -0
  28. package/docs/docs/classes/wxlayer_loader.Loader.html +31 -0
  29. package/docs/docs/classes/wxlayer_painter.Painter.html +20 -0
  30. package/docs/docs/classes/wxlayer_wxlayer.WxLayer.html +57 -0
  31. package/docs/docs/enums/utils_qtree.TileType.html +5 -0
  32. package/docs/docs/functions/utils_RawCLUT.WxCreateLegend.html +5 -0
  33. package/docs/docs/functions/utils_mercator.LatLonToPixels.html +6 -0
  34. package/docs/docs/functions/utils_mercator.PixelsToLatLon.html +6 -0
  35. package/docs/docs/functions/utils_mercator.PixelsToLonLat.html +6 -0
  36. package/docs/docs/functions/utils_mercator.coordToPixel.html +5 -0
  37. package/docs/docs/functions/utils_tests_wxtools.tests.html +1 -0
  38. package/docs/docs/functions/utils_wxtools.HEXtoRGBA.html +3 -0
  39. package/docs/docs/functions/utils_wxtools.HashXYZ.html +4 -0
  40. package/docs/docs/functions/utils_wxtools.RGBAtoHEX.html +3 -0
  41. package/docs/docs/functions/utils_wxtools.RGBtoHEX.html +3 -0
  42. package/docs/docs/functions/utils_wxtools.WXLOG.html +2 -0
  43. package/docs/docs/functions/utils_wxtools.WxGetColorSchemes.html +2 -0
  44. package/docs/docs/functions/utils_wxtools.WxGetColorStyles.html +2 -0
  45. package/docs/docs/functions/utils_wxtools.WxTilesLibSetup.html +3 -0
  46. package/docs/docs/functions/utils_wxtools.WxTilesLogging.html +5 -0
  47. package/docs/docs/functions/utils_wxtools.blurData.html +7 -0
  48. package/docs/docs/functions/utils_wxtools.buildIntegralPare.html +6 -0
  49. package/docs/docs/functions/utils_wxtools.cacheUriPromise.html +2 -0
  50. package/docs/docs/functions/utils_wxtools.create2DContext.html +6 -0
  51. package/docs/docs/functions/utils_wxtools.createEl.html +5 -0
  52. package/docs/docs/functions/utils_wxtools.createLevels.html +6 -0
  53. package/docs/docs/functions/utils_wxtools.fetchJson.html +6 -0
  54. package/docs/docs/functions/utils_wxtools.getClosestTimeString.html +5 -0
  55. package/docs/docs/functions/utils_wxtools.loadDataIntegral.html +5 -0
  56. package/docs/docs/functions/utils_wxtools.loadImage.html +4 -0
  57. package/docs/docs/functions/utils_wxtools.loadImageData.html +5 -0
  58. package/docs/docs/functions/utils_wxtools.makeConverter.html +9 -0
  59. package/docs/docs/functions/utils_wxtools.mixColor.html +3 -0
  60. package/docs/docs/functions/utils_wxtools.refineColor.html +3 -0
  61. package/docs/docs/functions/utils_wxtools.uriXYZ.html +5 -0
  62. package/docs/docs/functions/wxlayer_WxImplementation.WxLayerBaseImplementationMixin.html +4 -0
  63. package/docs/docs/functions/wxlayer_loadertools.applyMask.html +10 -0
  64. package/docs/docs/functions/wxlayer_loadertools.makeBox.html +4 -0
  65. package/docs/docs/functions/wxlayer_loadertools.splitCoords.html +6 -0
  66. package/docs/docs/functions/wxlayer_loadertools.subData.html +5 -0
  67. package/docs/docs/functions/wxlayer_loadertools.subDataDegree.html +5 -0
  68. package/docs/docs/functions/wxlayer_loadertools.subMask.html +5 -0
  69. package/docs/docs/hierarchy.html +1 -0
  70. package/docs/docs/index.html +7 -0
  71. package/docs/docs/interfaces/types.IsoInfo.html +8 -0
  72. package/docs/docs/interfaces/types.SLinePoint.html +4 -0
  73. package/docs/docs/interfaces/types.WxAPIOptions.html +37 -0
  74. package/docs/docs/interfaces/types.WxAllBoundariesMeta.html +8 -0
  75. package/docs/docs/interfaces/types.WxAllDatasetsShortMetas.html +4 -0
  76. package/docs/docs/interfaces/types.WxBoundaryMeta.html +10 -0
  77. package/docs/docs/interfaces/types.WxData.html +10 -0
  78. package/docs/docs/interfaces/types.WxDataSetManagerOptions.html +14 -0
  79. package/docs/docs/interfaces/types.WxDatasetMeta.html +20 -0
  80. package/docs/docs/interfaces/types.WxDatasetShortMeta.html +9 -0
  81. package/docs/docs/interfaces/types.WxLayerAPI.html +24 -0
  82. package/docs/docs/interfaces/types.WxLayerBaseAPI.html +16 -0
  83. package/docs/docs/interfaces/types.WxLayerOptions.html +12 -0
  84. package/docs/docs/interfaces/types.WxLngLat.html +6 -0
  85. package/docs/docs/interfaces/types.WxRasterData.html +13 -0
  86. package/docs/docs/interfaces/types.WxRequestInit.html +3 -0
  87. package/docs/docs/interfaces/types.WxTileInfo.html +18 -0
  88. package/docs/docs/interfaces/types.WxVariableMeta.html +13 -0
  89. package/docs/docs/interfaces/types.WxVariablesMetas.html +4 -0
  90. package/docs/docs/interfaces/utils_RawCLUT.WxLegend.html +14 -0
  91. package/docs/docs/interfaces/utils_RawCLUT.WxTick.html +10 -0
  92. package/docs/docs/interfaces/utils_qtree.Tree.html +7 -0
  93. package/docs/docs/interfaces/utils_wxtools.CachedUriLoaderPromiseFunc.html +2 -0
  94. package/docs/docs/interfaces/utils_wxtools.ColorStylesIncomplete.html +2 -0
  95. package/docs/docs/interfaces/utils_wxtools.Converter.html +5 -0
  96. package/docs/docs/interfaces/utils_wxtools.DataPicture.html +6 -0
  97. package/docs/docs/interfaces/utils_wxtools.IntegralPare.html +6 -0
  98. package/docs/docs/interfaces/utils_wxtools.WxColorSchemes.html +3 -0
  99. package/docs/docs/interfaces/utils_wxtools.WxColorStyleStrict.html +95 -0
  100. package/docs/docs/interfaces/utils_wxtools.WxColorStyleWeak.html +95 -0
  101. package/docs/docs/interfaces/utils_wxtools.WxColorStylesStrict.html +2 -0
  102. package/docs/docs/interfaces/utils_wxtools.WxColorStylesWeakMixed.html +2 -0
  103. package/docs/docs/interfaces/utils_wxtools.WxTilesLibOptions.html +8 -0
  104. package/docs/docs/interfaces/utils_wxtools.WxUnits.html +2 -0
  105. package/docs/docs/interfaces/utils_wxtools.XYZ.html +5 -0
  106. package/docs/docs/modules/controls_WxAPIControl.html +1 -0
  107. package/docs/docs/modules/controls_WxInfoControl.html +1 -0
  108. package/docs/docs/modules/controls_WxLegendControl.html +1 -0
  109. package/docs/docs/modules/controls_WxStyleEditorControl.html +1 -0
  110. package/docs/docs/modules/controls_WxTimeControl.html +1 -0
  111. package/docs/docs/modules/defaults_colorschemes.html +1 -0
  112. package/docs/docs/modules/defaults_styles.html +1 -0
  113. package/docs/docs/modules/defaults_uconv.html +1 -0
  114. package/docs/docs/modules/managers_BaseDatasetManager.html +1 -0
  115. package/docs/docs/modules/managers_WxAllDatasetsManager.html +1 -0
  116. package/docs/docs/modules/types.html +1 -0
  117. package/docs/docs/modules/utils_RawCLUT.html +1 -0
  118. package/docs/docs/modules/utils_mercator.html +1 -0
  119. package/docs/docs/modules/utils_qtree.html +1 -0
  120. package/docs/docs/modules/utils_tests_wxtools.html +1 -0
  121. package/docs/docs/modules/utils_wxtools.html +1 -0
  122. package/docs/docs/modules/wxapi.html +1 -0
  123. package/docs/docs/modules/wxlayer_WxImplementation.html +1 -0
  124. package/docs/docs/modules/wxlayer_loader.html +1 -0
  125. package/docs/docs/modules/wxlayer_loadertools.html +1 -0
  126. package/docs/docs/modules/wxlayer_painter.html +1 -0
  127. package/docs/docs/modules/wxlayer_wxlayer.html +1 -0
  128. package/docs/docs/modules.html +1 -0
  129. package/docs/docs/types/types.ListenerMethod.html +1 -0
  130. package/docs/docs/types/types.SLine.html +2 -0
  131. package/docs/docs/types/types.WxDate.html +9 -0
  132. package/docs/docs/types/types.WxEventType.html +1 -0
  133. package/docs/docs/types/types.WxInstances.html +2 -0
  134. package/docs/docs/types/types.WxLayerVarsNames.html +2 -0
  135. package/docs/docs/types/types.WxURIs.html +2 -0
  136. package/docs/docs/types/utils_qtree.SubTrees.html +2 -0
  137. package/docs/docs/types/utils_qtree.SubTreesN.html +2 -0
  138. package/docs/docs/types/utils_qtree.TreeN.html +2 -0
  139. package/docs/docs/types/utils_wxtools.DataIntegrals.html +2 -0
  140. package/docs/docs/types/utils_wxtools.DataPictures.html +2 -0
  141. package/docs/docs/types/utils_wxtools.WxUnitTuple.html +2 -0
  142. package/docs/docs/types/utils_wxtools.colorMapTuple.html +2 -0
  143. package/docs/docs/variables/defaults_colorschemes.__colorSchemes_default_preset.html +1 -0
  144. package/docs/docs/variables/defaults_styles.__colorStyles_default_preset.html +1 -0
  145. package/docs/docs/variables/defaults_uconv.__units_default_preset.html +1 -0
  146. package/managers/BaseDatasetManager.ts +274 -0
  147. package/managers/WxAllDatasetsManager.ts +60 -0
  148. package/package.json +6 -3
  149. package/tsconfig.json +19 -0
  150. package/typedoc.json +4 -0
  151. package/types.ts +471 -0
  152. package/wxapi.ts +223 -0
  153. package/wxlayer/WxImplementation.ts +372 -0
  154. package/wxlayer/loader.ts +305 -0
  155. package/wxlayer/loadertools.ts +380 -0
  156. package/wxlayer/painter.ts +451 -0
  157. 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
+ }