@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,372 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WXLOG,
|
|
3
|
+
type WxColorStyleStrict,
|
|
4
|
+
type WxColorStyleWeak,
|
|
5
|
+
WxGetColorStyles,
|
|
6
|
+
type XYZ,
|
|
7
|
+
} from "../utils/wxtools";
|
|
8
|
+
import type { WxLayer } from "./wxlayer";
|
|
9
|
+
import type { WxRequestInit, WxDatasetMeta, WxVariableMeta, WxDate, WxLayerVarsNames, WxLngLat, WxRasterDataCache, WxTileInfo, WxLayerBaseAPI, WxRasterData, WxLayerOptions, WxEventType } from "../types";
|
|
10
|
+
import { BaseDatasetManager } from "../managers/BaseDatasetManager";
|
|
11
|
+
|
|
12
|
+
type Constructor = new (...args: any[]) => {};
|
|
13
|
+
type GenericConstraintConstructor<T = {}> = new (...args: any[]) => T;
|
|
14
|
+
|
|
15
|
+
interface MixableI<WxDatasetManagerT extends BaseDatasetManager> {
|
|
16
|
+
id: string
|
|
17
|
+
_layer: WxLayer<WxDatasetManagerT>
|
|
18
|
+
|
|
19
|
+
fire<T extends keyof WxEventType>(type: T, data: WxEventType[T]): void
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// TODO FIXME need to qualify this more?
|
|
23
|
+
type Mixable<WxDatasetManagerT extends BaseDatasetManager> = GenericConstraintConstructor<MixableI<WxDatasetManagerT>>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Implementation of universal methods for the layer (for Mapbox and Leaflet)
|
|
27
|
+
* To be extended by framework specific implementation
|
|
28
|
+
* Wraps some methods of {@link WxLayer}
|
|
29
|
+
*/
|
|
30
|
+
export function WxLayerBaseImplementationMixin<WxDatasetManagerT extends BaseDatasetManager, BaseT extends Mixable<WxDatasetManagerT>>(Base: BaseT) {
|
|
31
|
+
return class extends Base {
|
|
32
|
+
/**
|
|
33
|
+
* @ignore
|
|
34
|
+
* if true, the source is animating
|
|
35
|
+
* */
|
|
36
|
+
_animation = false;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @ignore
|
|
40
|
+
* a seed for the animation
|
|
41
|
+
* */
|
|
42
|
+
_animationSeed = 0;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @ignore
|
|
46
|
+
* used to avoid multiple animation requests
|
|
47
|
+
* */
|
|
48
|
+
_redrawRequested?: Promise<void>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* returns the dataset manager of the source
|
|
52
|
+
* @returns {WxDataSetManager} the dataset manager of the source
|
|
53
|
+
* */
|
|
54
|
+
get wxdatasetManager(): WxDatasetManagerT {
|
|
55
|
+
WXLOG(`WxLayerBaseImplementation.wxdatasetManager (${this.id})`);
|
|
56
|
+
return this._layer.wxdatasetManager;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get the metadata of the current variable(s).
|
|
61
|
+
* @returns {WxVariableMeta} - The metadata of the current variable.
|
|
62
|
+
*/
|
|
63
|
+
getCurrentVariableMeta(): WxVariableMeta {
|
|
64
|
+
WXLOG(`WxLayerBaseImplementation.getCurrentVariableMeta (${this.id})`);
|
|
65
|
+
return { ...this._layer.currentVariableMeta };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
getDatasetMeta(): WxDatasetMeta {
|
|
69
|
+
return this._layer.wxdatasetManager.getInstanceMeta(this.getTime());
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Get the metadata of the current variable(s).
|
|
74
|
+
* @deprecated
|
|
75
|
+
* @returns {WxVariableMeta} - The metadata of the current variable.
|
|
76
|
+
*/
|
|
77
|
+
getMetadata(): WxVariableMeta {
|
|
78
|
+
WXLOG(`WxLayerBaseImplementation.getMetadata (${this.id})`);
|
|
79
|
+
return { ...this._layer.currentVariableMeta };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Get current variables (1 or 2) of the source/layer.
|
|
84
|
+
* @returns {WxLayerVarsNames} variables of the source.
|
|
85
|
+
*/
|
|
86
|
+
getVariablesNames(): WxLayerVarsNames {
|
|
87
|
+
WXLOG(`WxLayerBaseImplementation.getVariables (${this.id})`);
|
|
88
|
+
return [...this._layer.variables];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Clears the cache of the source.
|
|
93
|
+
*/
|
|
94
|
+
clearCache(): void {
|
|
95
|
+
WXLOG(`WxLayerBaseImplementation.clearCache (${this.id})`);
|
|
96
|
+
this._layer.clearCache();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get the raster data cache of the layer. Used in CustomWxTilesLayer.render.
|
|
101
|
+
* @internal
|
|
102
|
+
* @returns {WxRasterDataCache} The raster data cache of the layer.
|
|
103
|
+
*/
|
|
104
|
+
getCache(): WxRasterDataCache {
|
|
105
|
+
return this._layer.tilesRasterCache;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get a copy of the current style of the source.
|
|
110
|
+
* @returns {WxColorStyleStrict} A copy of the current style of the source.
|
|
111
|
+
*/
|
|
112
|
+
getCurrentStyleObjectCopy(): WxColorStyleStrict {
|
|
113
|
+
WXLOG(`WxLayerBaseImplementation.getCurrentStyleObjectCopy (${this.id})`);
|
|
114
|
+
return this._layer.getCurrentStyleObjectCopy();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Get the current time of the source.
|
|
119
|
+
* @returns {string} The current time of the source from array of times.
|
|
120
|
+
*/
|
|
121
|
+
getTime(): string {
|
|
122
|
+
WXLOG(`WxLayerBaseImplementation.getTime (${this.id})`);
|
|
123
|
+
return this._layer.getTime();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Get all available times for the source.
|
|
128
|
+
* @returns {string[]} An array of all available times for the source.
|
|
129
|
+
*/
|
|
130
|
+
getAllTimes(): string[] {
|
|
131
|
+
WXLOG(`WxLayerBaseImplementation.getTimes (${this.id})`);
|
|
132
|
+
return this._layer.wxdatasetManager.getAllTimes();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Set time and render the source. If the time is not available, the closest time will be used.
|
|
137
|
+
* @param {WxDate} time - Time to set.
|
|
138
|
+
* @param {WxRequestInit | undefined} requestInit - Request options for fetch.
|
|
139
|
+
* @returns {Promise<string>} A promise that resolves with current time step when the time is set and the source is loaded and rendered.
|
|
140
|
+
*/
|
|
141
|
+
async setTime(
|
|
142
|
+
time?: WxDate,
|
|
143
|
+
requestInit?: WxRequestInit,
|
|
144
|
+
redraw?: boolean,
|
|
145
|
+
): Promise<string> {
|
|
146
|
+
WXLOG(`WxLayerBaseImplementation.setTime (${this.id}) time=${time}`);
|
|
147
|
+
const oldtime = this.getTime();
|
|
148
|
+
this._layer.setURLsAndTime(time);
|
|
149
|
+
await this._reloadVisible(requestInit, redraw);
|
|
150
|
+
if (requestInit?.signal?.aborted) this._layer.setURLsAndTime(oldtime); // restore old time and URLs
|
|
151
|
+
return this.getTime();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Preloads tiles for faster rendering for a specific time is set {@link setTime}. If the time is not available, the closest time will be used.
|
|
156
|
+
* @param {WxDate} time - The time to preload.
|
|
157
|
+
* @param {WxRequestInit | undefined} requestInit - Request options for fetch.
|
|
158
|
+
* @returns {Promise<void>} A promise that resolves when the tiles are preloaded.
|
|
159
|
+
*/
|
|
160
|
+
async preloadTime(time: WxDate, requestInit?: WxRequestInit): Promise<void> {
|
|
161
|
+
WXLOG(`WxLayerBaseImplementation.preloadTime (${this.id}) time=${time}`);
|
|
162
|
+
return this._layer.preloadTime(time, this.coveringTiles(), requestInit);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Starts the particle animation for wind and currents if sutiable.
|
|
167
|
+
*/
|
|
168
|
+
startAnimation(): void {
|
|
169
|
+
if (this._animation) {
|
|
170
|
+
WXLOG(
|
|
171
|
+
`WxLayerBaseImplementation.startAnimation (${this.id}) already started`,
|
|
172
|
+
);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (this._layer.nonanimatable) {
|
|
177
|
+
WXLOG(
|
|
178
|
+
`WxLayerBaseImplementation.startAnimation (${this.id}) nonanimatable`,
|
|
179
|
+
);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
WXLOG(`WxLayerBaseImplementation.startAnimation (${this.id})`);
|
|
184
|
+
this._animation = true;
|
|
185
|
+
const animationStep = async (seed: number) => {
|
|
186
|
+
WXLOG(
|
|
187
|
+
`WxLayerBaseImplementation.startAnimation (${this.id}) animationStep`,
|
|
188
|
+
);
|
|
189
|
+
if (!this._animation || this._layer.nonanimatable) {
|
|
190
|
+
this._animation = false;
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
this._animationSeed = seed;
|
|
195
|
+
await this._redrawTiles();
|
|
196
|
+
requestAnimationFrame(animationStep);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
requestAnimationFrame(animationStep);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Stops the particle animation.
|
|
204
|
+
*/
|
|
205
|
+
async stopAnimation(): Promise<void> {
|
|
206
|
+
WXLOG(`WxLayerBaseImplementation.stopAnimation (${this.id})`);
|
|
207
|
+
this._animation = false;
|
|
208
|
+
return this._redrawTiles();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Set the coarse maximum zoom level to make tiles load faster during animation.
|
|
213
|
+
* @param {number} level - The coarse maximum zoom level to set.
|
|
214
|
+
* @returns {Promise<void>} A promise that resolves when the coarse maximum zoom level is set.
|
|
215
|
+
*/
|
|
216
|
+
async setCoarseLevel(level = 2): Promise<void> {
|
|
217
|
+
this._layer.coarseLevel = Math.max(
|
|
218
|
+
0,
|
|
219
|
+
Math.min(level, this.wxdatasetManager.getMaxZoom()),
|
|
220
|
+
);
|
|
221
|
+
// return this._reloadVisible(); // NOT needed? Hmmm... ибо used before loading new tile anyway
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** restores to the dataset's maximum zoom level */
|
|
225
|
+
async unsetCoarseLevel(): Promise<void> {
|
|
226
|
+
this._layer.coarseLevel = 0;
|
|
227
|
+
return this._reloadVisible();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Set the style of the source by its name from default styles.
|
|
232
|
+
* @param {string} wxstyleName - Name of the new style to set.
|
|
233
|
+
* @param {boolean} reload - If true, the source will be reloaded and rerendered.
|
|
234
|
+
* @returns {Promise<void>} A promise that resolves when the style is set.
|
|
235
|
+
*/
|
|
236
|
+
async setStyleByName(
|
|
237
|
+
wxstyleName: string,
|
|
238
|
+
reload = true,
|
|
239
|
+
): Promise<void> {
|
|
240
|
+
WXLOG(
|
|
241
|
+
`WxLayerBaseImplementation.setStyleByName (${this.id}) wxstyleName=${wxstyleName} reload=${reload}`,
|
|
242
|
+
);
|
|
243
|
+
return this.updateCurrentStyleObject(
|
|
244
|
+
WxGetColorStyles()[wxstyleName],
|
|
245
|
+
reload,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Update the current style object of the source partially or completely.
|
|
251
|
+
* @param {WxColorStyleWeak | undefined} style - Style's fields to set.
|
|
252
|
+
* @param {boolean} reload - If true, the source will be reloaded and rerendered.
|
|
253
|
+
* @param {WxRequestInit | undefined} requestInit - Request options.
|
|
254
|
+
* @returns {Promise<void>} A promise that resolves when the style is set.
|
|
255
|
+
*/
|
|
256
|
+
async updateCurrentStyleObject(
|
|
257
|
+
style?: WxColorStyleWeak,
|
|
258
|
+
reload = true,
|
|
259
|
+
requestInit?: WxRequestInit,
|
|
260
|
+
): Promise<void> {
|
|
261
|
+
WXLOG(
|
|
262
|
+
`WxLayerBaseImplementation.updateCurrentStyleObject (${this.id}) style=${style} reload=${reload}`,
|
|
263
|
+
);
|
|
264
|
+
this._layer.updateCurrentStyleObject(style);
|
|
265
|
+
this.startAnimation();
|
|
266
|
+
if (reload) return this._reloadVisible(requestInit);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** @ignore */
|
|
270
|
+
_redrawTiles(): Promise<void> {
|
|
271
|
+
if (this._redrawRequested) return this._redrawRequested;
|
|
272
|
+
this._redrawRequested = new Promise((resolve) => {
|
|
273
|
+
requestAnimationFrame(() => {
|
|
274
|
+
WXLOG(`WxTileSource _redrawTiles (${this.id})`);
|
|
275
|
+
|
|
276
|
+
this.update();
|
|
277
|
+
|
|
278
|
+
resolve();
|
|
279
|
+
this._redrawRequested = undefined;
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
return this._redrawRequested;
|
|
284
|
+
} // _redrawTiles
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* @ignore
|
|
288
|
+
* A dummy function to be replaced by ancestor classes.
|
|
289
|
+
*/
|
|
290
|
+
async _reloadVisible(
|
|
291
|
+
requestInit?: WxRequestInit,
|
|
292
|
+
redraw?: boolean,
|
|
293
|
+
): Promise<void> {}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* @ignore
|
|
297
|
+
* A dummy function to be replaced by ancestor classes.
|
|
298
|
+
*/
|
|
299
|
+
coveringTiles(): XYZ[] {
|
|
300
|
+
return [];
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* @ignore
|
|
305
|
+
* A dummy function to be replaced by ancestor classes.
|
|
306
|
+
* Force reload and redraw all tiles.
|
|
307
|
+
*/
|
|
308
|
+
update() {}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* A boolean flag indicating whether the dataset manager needs to be updated or not.
|
|
312
|
+
*/
|
|
313
|
+
_needUpdateDSManager = false;
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* @ignore
|
|
317
|
+
* @internal
|
|
318
|
+
* Helper method that loads a tile with the given coordinates and request options.
|
|
319
|
+
* It returns null during datasetManager update or in case of any other error (e.g. network error, not found, etc.)
|
|
320
|
+
* It tries to update datasetManager if e.reason === 'instance-not-found' and update the layer
|
|
321
|
+
*
|
|
322
|
+
* @param {XYZ} coords - The tile coordinates to be loaded.
|
|
323
|
+
* @param {WxRequestInit} requestInit - The request options.
|
|
324
|
+
* @returns {Promise<WxRasterData | null>} - A promise that resolves with the loaded tile or null.
|
|
325
|
+
*/
|
|
326
|
+
async _loadTileHelper(
|
|
327
|
+
coords: XYZ,
|
|
328
|
+
requestInit?: WxRequestInit,
|
|
329
|
+
): Promise<WxRasterData | null> {
|
|
330
|
+
// in case of DSManager update, return empty tile
|
|
331
|
+
// After update is complete, the framework will try to reload all tiles again
|
|
332
|
+
if (!this._needUpdateDSManager) {
|
|
333
|
+
try {
|
|
334
|
+
return await this._layer.loadTile(coords, requestInit);
|
|
335
|
+
} catch (e) {
|
|
336
|
+
// it's ok if the tile is not found. Just return empty tile, or...
|
|
337
|
+
|
|
338
|
+
// ...or, rethrow 'AbortError' to the framework to handle
|
|
339
|
+
if (e.name === "AbortError") {
|
|
340
|
+
throw e;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// ...or, if the loadTile->loadImage->throw 'reason' is 'instance-not-found', try to update wxdatasetManager, then update the layer
|
|
344
|
+
// if we aren't in the middle of updating wxdatasetManager (others may initiate update as well)
|
|
345
|
+
if (e.reason === "instance-not-found" && !this._needUpdateDSManager) {
|
|
346
|
+
this._needUpdateDSManager = true;
|
|
347
|
+
WXLOG(
|
|
348
|
+
`WxTileSource.loadTile (${this.id}) instance-not-found. Trying to update wxdatasetManager and load again.`,
|
|
349
|
+
);
|
|
350
|
+
// try to update wxdatasetManager. No need to await for it to finish
|
|
351
|
+
this.wxdatasetManager
|
|
352
|
+
.update() // attempt to update wxdatasetManager
|
|
353
|
+
.then(() => {
|
|
354
|
+
this._needUpdateDSManager = false;
|
|
355
|
+
this.setTime(this.getTime()) // reload tiles with new time close to the current time
|
|
356
|
+
.then(() => this.fire("changed", this)); // and fire 'changed' event
|
|
357
|
+
}) // update wxdatasetManager
|
|
358
|
+
.catch((e) => {
|
|
359
|
+
// it leaves needUpdateDSManager = true, so the layer will appear empty after failed update.
|
|
360
|
+
WXLOG(
|
|
361
|
+
`WxTileSource.loadTile (${this.id}) instance-not-found. wxdatasetManager update failed.`,
|
|
362
|
+
e,
|
|
363
|
+
);
|
|
364
|
+
});
|
|
365
|
+
} // if (e.reason === 'instance-not-found')
|
|
366
|
+
} // catch loadTile error
|
|
367
|
+
} // if (!this.needUpdateDSManager) - outer
|
|
368
|
+
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { TileType } from "../utils/qtree";
|
|
2
|
+
import {
|
|
3
|
+
type CachedUriLoaderPromiseFunc,
|
|
4
|
+
type DataIntegral,
|
|
5
|
+
type DataIntegrals,
|
|
6
|
+
type DataPictures,
|
|
7
|
+
WXLOG,
|
|
8
|
+
type XYZ,
|
|
9
|
+
blurData,
|
|
10
|
+
cacheUriPromise,
|
|
11
|
+
loadDataIntegral,
|
|
12
|
+
uriXYZ,
|
|
13
|
+
} from "../utils/wxtools";
|
|
14
|
+
import type { WxBoundaryMeta, WxRequestInit, WxURIs, WxData, SLine} from "../types";
|
|
15
|
+
import {
|
|
16
|
+
applyMask,
|
|
17
|
+
makeBox,
|
|
18
|
+
splitCoords,
|
|
19
|
+
subData,
|
|
20
|
+
subDataDegree,
|
|
21
|
+
subMask,
|
|
22
|
+
} from "./loadertools";
|
|
23
|
+
import type { WxLayer } from "./wxlayer";
|
|
24
|
+
import type { BaseDatasetManager } from "../managers/BaseDatasetManager";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Class to load and preprocess data from PNG tiles.
|
|
28
|
+
* This class is used internally by {@link WxLayer}.
|
|
29
|
+
* Direct usage is not recommended.
|
|
30
|
+
*/
|
|
31
|
+
export class Loader<WxDatasetManagerT extends BaseDatasetManager> {
|
|
32
|
+
/** Reference to the associated WxLayer */
|
|
33
|
+
protected readonly _layer: WxLayer<WxDatasetManagerT>;
|
|
34
|
+
|
|
35
|
+
/** Function to load, decode, and cache tile data from a URL */
|
|
36
|
+
protected readonly _loadDataFunc: CachedUriLoaderPromiseFunc<DataIntegral> =
|
|
37
|
+
cacheUriPromise(loadDataIntegral);
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Constructor - Do not use directly.
|
|
41
|
+
* Use WxLayer methods to manage this loader.
|
|
42
|
+
*/
|
|
43
|
+
constructor(layer: WxLayer<WxDatasetManagerT>) {
|
|
44
|
+
this._layer = layer;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Loads and preprocesses data for a single tile.
|
|
49
|
+
* @param tile - Tile coordinates (XYZ).
|
|
50
|
+
* @param requestInit - Optional HTTP request configuration.
|
|
51
|
+
* @returns A promise resolving to the processed tile data or `null` if the tile is invalid.
|
|
52
|
+
*/
|
|
53
|
+
async load(tile: XYZ, requestInit?: WxRequestInit): Promise<WxData | null> {
|
|
54
|
+
const preloaded = await this.cacheLoad(
|
|
55
|
+
tile,
|
|
56
|
+
this._layer.tilesURIs,
|
|
57
|
+
requestInit,
|
|
58
|
+
);
|
|
59
|
+
if (!preloaded) return null; // tile is cut by boundaries or mask QTree
|
|
60
|
+
const { rawdata, subCoords, tileType } = preloaded;
|
|
61
|
+
const { units } = this._layer.currentVariableMeta;
|
|
62
|
+
const interpolator = units === "degree" ? subDataDegree : subData;
|
|
63
|
+
const processor = (d: DataIntegral) =>
|
|
64
|
+
interpolator(blurData(d, this._layer.style.blurRadius), subCoords);
|
|
65
|
+
const data = <DataPictures>rawdata.map(processor); // preprocess all loaded data
|
|
66
|
+
this._vectorMagnitudesPrepare(data); // if vector data, prepare magnitudes
|
|
67
|
+
await this._applyMask(
|
|
68
|
+
data,
|
|
69
|
+
tile,
|
|
70
|
+
tileType,
|
|
71
|
+
!subCoords && rawdata.length === 1,
|
|
72
|
+
); // apply mask if needed
|
|
73
|
+
return { data, slines: this._createStreamLines(data) };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Loads tile data from the cache or API, ensuring validity against boundaries and masks.
|
|
78
|
+
* @param tile - Tile coordinates (XYZ).
|
|
79
|
+
* @param uris - URIs used to construct tile URLs.
|
|
80
|
+
* @param requestInit - Optional HTTP request configuration.
|
|
81
|
+
* @returns A promise resolving to raw tile data or `null` if the tile is invalid.
|
|
82
|
+
*/
|
|
83
|
+
async cacheLoad(
|
|
84
|
+
tile: XYZ,
|
|
85
|
+
uris: WxURIs,
|
|
86
|
+
requestInit?: WxRequestInit,
|
|
87
|
+
): Promise<{
|
|
88
|
+
rawdata: DataIntegrals;
|
|
89
|
+
subCoords?: XYZ | undefined;
|
|
90
|
+
tileType: TileType;
|
|
91
|
+
} | null> {
|
|
92
|
+
if (!this._checkInsideBoundaries(tile)) return null; // tile is cut by boundaries
|
|
93
|
+
|
|
94
|
+
const tileType = this._checkTypeAndMask(tile);
|
|
95
|
+
if (!tileType) return null; // tile is cut by mask
|
|
96
|
+
|
|
97
|
+
const { upCoords, subCoords } = splitCoords(
|
|
98
|
+
tile,
|
|
99
|
+
this._layer.getCoarseMaxZoom(),
|
|
100
|
+
);
|
|
101
|
+
const URLs = <WxURIs>uris.map((uri) => uriXYZ(uri, upCoords));
|
|
102
|
+
const requestInitCopy = Object.assign(
|
|
103
|
+
{},
|
|
104
|
+
this._layer.wxdatasetManager.wxAPI.requestInit,
|
|
105
|
+
{ signal: requestInit?.signal },
|
|
106
|
+
); // make initCopy, copy only signal
|
|
107
|
+
const rawdata = <DataIntegrals>(
|
|
108
|
+
await Promise.all(
|
|
109
|
+
URLs.map((url: string) => this._loadDataFunc(url, requestInitCopy)),
|
|
110
|
+
)
|
|
111
|
+
);
|
|
112
|
+
return { rawdata, subCoords, tileType };
|
|
113
|
+
// we don't need to process data, as it's for cache preloading only
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Clear cache */
|
|
117
|
+
clearCache(): void {
|
|
118
|
+
this._loadDataFunc.clearCache();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Applies a mask to the tile data based on the tile type and masking style.
|
|
123
|
+
* @param data - The processed tile data.
|
|
124
|
+
* @param tile - The tile coordinates (XYZ).
|
|
125
|
+
* @param tileType - Type of the tile (e.g., mixed, land, sea).
|
|
126
|
+
* @param needCopy - Whether to create a data copy before masking.
|
|
127
|
+
* @returns A promise resolving once the mask has been applied.
|
|
128
|
+
*/
|
|
129
|
+
protected async _applyMask(
|
|
130
|
+
data: DataPictures,
|
|
131
|
+
tile: XYZ,
|
|
132
|
+
tileType: TileType,
|
|
133
|
+
needCopy: boolean,
|
|
134
|
+
): Promise<void> {
|
|
135
|
+
const { style } = this._layer;
|
|
136
|
+
if (
|
|
137
|
+
(style.mask === "land" || style.mask === "sea") &&
|
|
138
|
+
tileType === TileType.Mixed
|
|
139
|
+
) {
|
|
140
|
+
if (needCopy) {
|
|
141
|
+
// !!make a copy before masking!! or original data will be spoiled
|
|
142
|
+
// needCopy is false if this is a subTile (already copied from parent)
|
|
143
|
+
const { raw: inRaw, dmin, dmax, dmul } = data[0];
|
|
144
|
+
data[0] = { raw: new Uint16Array(inRaw), dmin, dmax, dmul };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let maskImage: ImageData;
|
|
148
|
+
const { wxAPI } = this._layer.wxdatasetManager;
|
|
149
|
+
try {
|
|
150
|
+
const { upCoords, subCoords } = splitCoords(tile, wxAPI.maskDepth);
|
|
151
|
+
maskImage = await wxAPI.loadMaskFunc(upCoords);
|
|
152
|
+
maskImage = subMask(maskImage, subCoords, wxAPI.maskChannel);
|
|
153
|
+
} catch (e) {
|
|
154
|
+
style.mask = undefined;
|
|
155
|
+
WXLOG("Can't load Mask. Masking is disabled");
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
applyMask(data[0], maskImage, wxAPI.maskChannel, style.mask);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** @no_this @ignore */
|
|
164
|
+
protected _vectorMagnitudesPrepare(data: DataPictures): void {
|
|
165
|
+
if (data.length === 1) return; // no need to process
|
|
166
|
+
data.unshift({
|
|
167
|
+
raw: new Uint16Array(258 * 258),
|
|
168
|
+
dmin: 0,
|
|
169
|
+
dmax: 0,
|
|
170
|
+
dmul: 0,
|
|
171
|
+
});
|
|
172
|
+
const [l, u, v] = data; // length, u, v components
|
|
173
|
+
l.dmax = 1.42 * Math.max(-u.dmin, u.dmax, -v.dmin, v.dmax);
|
|
174
|
+
l.dmul = (l.dmax - l.dmin) / 65535;
|
|
175
|
+
for (let i = 0; i < 258 * 258; ++i) {
|
|
176
|
+
if (!u.raw[i] || !v.raw[i]) {
|
|
177
|
+
// l.raw[i] = 0; // by default it's 0
|
|
178
|
+
continue;
|
|
179
|
+
} // NODATA
|
|
180
|
+
const _u = u.dmin + u.dmul * u.raw[i]; // unpack U data
|
|
181
|
+
const _v = v.dmin + v.dmul * v.raw[i]; // unpack V data
|
|
182
|
+
l.raw[i] = Math.sqrt(_v * _v + _u * _u) / l.dmul; // pack data back to use the original rendering approach
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** @ignore */
|
|
187
|
+
protected _checkTypeAndMask(coords: XYZ): TileType | undefined {
|
|
188
|
+
const { mask } = this._layer.style;
|
|
189
|
+
if (mask === "land" || mask === "sea") {
|
|
190
|
+
const tileType = this._layer.wxdatasetManager.wxAPI.qtree.check(coords); // check 'type' of the tile
|
|
191
|
+
return mask === tileType ? undefined /* cut by QTree */ : tileType;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return TileType.Mixed;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** @ignore */
|
|
198
|
+
protected _checkInsideBoundaries(coords: XYZ): boolean {
|
|
199
|
+
const boundaries = this._layer.getBoundaries();
|
|
200
|
+
if (boundaries?.boundaries180) {
|
|
201
|
+
const bbox = makeBox(coords);
|
|
202
|
+
const rectIntersect = (b: WxBoundaryMeta) =>
|
|
203
|
+
!(
|
|
204
|
+
bbox.west > b.east ||
|
|
205
|
+
b.west > bbox.east ||
|
|
206
|
+
bbox.south > b.north ||
|
|
207
|
+
b.south > bbox.north
|
|
208
|
+
);
|
|
209
|
+
if (!boundaries.boundaries180.some(rectIntersect)) {
|
|
210
|
+
return false; // cut by boundaries
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** @ignore */
|
|
218
|
+
protected _createStreamLines(data: DataPictures): SLine[] {
|
|
219
|
+
if (data.length !== 3) return [];
|
|
220
|
+
const { style } = this._layer;
|
|
221
|
+
if (style.streamLineColor === "none") return [];
|
|
222
|
+
const streamLines: SLine[] = []; // an array of stremllines. Each section of streamline represents a position and size of a particle.
|
|
223
|
+
|
|
224
|
+
// idea is taken from the LIC (linear integral convolution) algorithm and the 'multipartical vector field visualisation'
|
|
225
|
+
// Having the stream lines as precalculated trajectories makes an animation more predictable and (IMHO) representative.
|
|
226
|
+
// Algorithm: use U and V as an increment to build a trajectory. To make trajectory more or less correct the algo
|
|
227
|
+
// does 20 moc steps and stores the point into streamline (sLine).
|
|
228
|
+
// Algo does two passes: forward and backward, to cope boundaries and improve visual effect.
|
|
229
|
+
const [l, u, v] = data;
|
|
230
|
+
const factor = (style.streamLineSpeedFactor || 1) / l.dmax;
|
|
231
|
+
const addDegrees = style.addDegrees
|
|
232
|
+
? 0.017453292519943 * style.addDegrees
|
|
233
|
+
: 0;
|
|
234
|
+
const gridStep = style.streamLineGridStep || 64;
|
|
235
|
+
const steps = style.streamLineSteps || 300;
|
|
236
|
+
for (let y = 0; y <= 256; y += gridStep) {
|
|
237
|
+
for (let x = 0; x <= 256; x += gridStep) {
|
|
238
|
+
if (!l.raw[1 + x + (1 + y) * 258]) continue; // NODATA
|
|
239
|
+
const sLine: SLine = []; // streamline
|
|
240
|
+
let xforw = x;
|
|
241
|
+
let yforw = y;
|
|
242
|
+
let oldDi = -1; // previous di. The first di will never be -1
|
|
243
|
+
let dx = 0;
|
|
244
|
+
let dy = 0;
|
|
245
|
+
for (
|
|
246
|
+
let i = 0;
|
|
247
|
+
i <= steps &&
|
|
248
|
+
xforw >= 0 &&
|
|
249
|
+
xforw <= 256 &&
|
|
250
|
+
yforw >= 0 &&
|
|
251
|
+
yforw <= 256;
|
|
252
|
+
i++
|
|
253
|
+
) {
|
|
254
|
+
// forward
|
|
255
|
+
if (!(i % (steps / 10))) sLine.push({ x: ~~xforw, y: ~~yforw }); // push each (steps/10)-th point // 7 points max
|
|
256
|
+
const di = ~~xforw + 1 + (~~yforw + 1) * 258;
|
|
257
|
+
if (di !== oldDi) {
|
|
258
|
+
// calc dx, dy only if di changed
|
|
259
|
+
if (!l.raw[di]) break; // NODATA - stop streamline creation
|
|
260
|
+
oldDi = di; // save old di
|
|
261
|
+
const dl = l.dmin + l.raw[di] * l.dmul;
|
|
262
|
+
const du = u.dmin + u.raw[di] * u.dmul;
|
|
263
|
+
const dv = v.dmin + v.raw[di] * v.dmul;
|
|
264
|
+
const ang = Math.atan2(du, dv) + addDegrees;
|
|
265
|
+
dx = factor * dl * Math.sin(ang);
|
|
266
|
+
dy = factor * dl * Math.cos(ang);
|
|
267
|
+
}
|
|
268
|
+
xforw += dx;
|
|
269
|
+
yforw -= dy; // negative - due to Lat goes up but screen's coordinates go down
|
|
270
|
+
} // for i forward
|
|
271
|
+
let xback = x;
|
|
272
|
+
let yback = y;
|
|
273
|
+
oldDi = -1; // previous di. The first di will never be -1
|
|
274
|
+
for (
|
|
275
|
+
let i = 1;
|
|
276
|
+
i <= steps &&
|
|
277
|
+
xback >= 0 &&
|
|
278
|
+
xback <= 256 &&
|
|
279
|
+
yback >= 0 &&
|
|
280
|
+
yback <= 256;
|
|
281
|
+
i++
|
|
282
|
+
) {
|
|
283
|
+
// backward // i = 1 because otherwise it produces the same first point hence visual artefact! 2 hours debugging!
|
|
284
|
+
if (!(i % (steps / 10))) sLine.unshift({ x: ~~xback, y: ~~yback }); // push each (steps/10)-th point // 6 points max
|
|
285
|
+
const di = ~~xback + 1 + (~~yback + 1) * 258;
|
|
286
|
+
if (di !== oldDi) {
|
|
287
|
+
// calc dx, dy only if di changed
|
|
288
|
+
if (!l.raw[di]) break; // NODATA - stop streamline creation
|
|
289
|
+
oldDi = di; // save old di
|
|
290
|
+
const dl = l.dmin + l.raw[di] * l.dmul;
|
|
291
|
+
const du = u.dmin + u.raw[di] * u.dmul;
|
|
292
|
+
const dv = v.dmin + v.raw[di] * v.dmul;
|
|
293
|
+
const ang = Math.atan2(du, dv) + addDegrees;
|
|
294
|
+
dx = factor * dl * Math.sin(ang);
|
|
295
|
+
dy = factor * dl * Math.cos(ang);
|
|
296
|
+
}
|
|
297
|
+
xback -= dx;
|
|
298
|
+
yback += dy; // positive - due to Lat goes up but screen's coordinates go down
|
|
299
|
+
} // for i backward
|
|
300
|
+
sLine.length > 2 && streamLines.push(sLine);
|
|
301
|
+
} // for x
|
|
302
|
+
} // for y
|
|
303
|
+
return streamLines;
|
|
304
|
+
} // _createSLines
|
|
305
|
+
}
|