@metoceanapi/wxtiles-common 0.0.7 → 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/types.ts ADDED
@@ -0,0 +1,471 @@
1
+ /**
2
+ * type array of strings - available instances of a dataset */
3
+
4
+ import type { WxTilesLibOptions, WxColorStyleStrict, WxColorStyleWeak, XYZ, DataPictures } from "./utils/wxtools";
5
+ import type { BaseDatasetManager } from "./managers/BaseDatasetManager";
6
+ import type { WxAPI } from "./wxapi";
7
+ import { WXLOG } from "./utils/wxtools";
8
+
9
+ /**
10
+ * Type alias for an array of strings representing available instances of a dataset
11
+ */
12
+ export type WxInstances = string[];
13
+
14
+ /**
15
+ * Short metadata describing a dataset */
16
+ export interface WxDatasetShortMeta {
17
+ /**
18
+ * Represents instances as timesteps for datasets that support instancing, such as NZ Radar.
19
+ * Each entry corresponds to a specific time step. */
20
+ instanced?: WxInstances;
21
+
22
+ /** The latest available instance of the dataset. */
23
+ instance: string;
24
+
25
+ /** List of all variables available in the dataset. */
26
+ variables: string[];
27
+ }
28
+
29
+ /**
30
+ * Metadata structure for all available datasets with their short descriptions.
31
+ */
32
+ export interface WxAllDatasetsShortMetas {
33
+ /**
34
+ * An object where the keys are dataset names and the values are their corresponding short metadata.
35
+ * If no metadata is available for a dataset, the value will be `undefined`.
36
+ */
37
+ [name: string]: WxDatasetShortMeta | undefined;
38
+ }
39
+
40
+ /**
41
+ * Metadata for a specific variable in a dataset.
42
+ */
43
+ export interface WxVariableMeta {
44
+ /**
45
+ * Units of measurement for the variable.
46
+ */
47
+ units: string;
48
+
49
+ /**
50
+ * Minimum value for the variable.
51
+ */
52
+ min: number;
53
+
54
+ /**
55
+ * Maximum value for the variable.
56
+ */
57
+ max: number;
58
+
59
+ /**
60
+ * Standardized name for the variable, if available.
61
+ */
62
+ standard_name?: string;
63
+
64
+ /**
65
+ * If the variable is part of a vector data pair (e.g., u, v components of wind),
66
+ * this contains the associated pair.
67
+ */
68
+ vector?: [string, string];
69
+ }
70
+
71
+ /**
72
+ * Interface representing a collection of variables and their metadata.
73
+ */
74
+ export interface WxVariablesMetas {
75
+ /**
76
+ * An object where keys are variable names and values are their corresponding metadata.
77
+ * If metadata for a variable is unavailable, the value will be `undefined`.
78
+ */
79
+ [name: string]: WxVariableMeta | undefined;
80
+ }
81
+
82
+ /**
83
+ * Geographic boundaries of a dataset.
84
+ */
85
+ export interface WxBoundaryMeta {
86
+ /** Westernmost longitude. */
87
+ west: number;
88
+
89
+ /** Northernmost latitude. */
90
+ north: number;
91
+
92
+ /** Easternmost longitude. */
93
+ east: number;
94
+
95
+ /** Southernmost latitude. */
96
+ south: number;
97
+ }
98
+
99
+ /** Used to make an abortable call to the layer update */
100
+ export interface WxRequestInit {
101
+ signal?: AbortSignal;
102
+ }
103
+
104
+ /**
105
+ * All possible boundary representations for a dataset, including normalized and wrapped formats.
106
+ */
107
+ export interface WxAllBoundariesMeta {
108
+ /**
109
+ * Original boundaries of the dataset in normalized format.
110
+ */
111
+ boundariesnorm: WxBoundaryMeta;
112
+
113
+ /**
114
+ * Array of boundaries wrapped to the [-180, 180] longitude range.
115
+ */
116
+ boundaries180: WxBoundaryMeta[];
117
+
118
+ /**
119
+ * Array of boundaries wrapped to the [0, 360] longitude range.
120
+ */
121
+ boundaries360: WxBoundaryMeta[];
122
+ }
123
+
124
+ /**
125
+ * Comprehensive metadata for a dataset.
126
+ */
127
+ export interface WxDatasetMeta {
128
+ /**
129
+ * List of all variables in the dataset.
130
+ */
131
+ variables: string[];
132
+
133
+ /**
134
+ * Metadata for each variable.
135
+ */
136
+ variablesMeta: WxVariablesMetas;
137
+
138
+ /**
139
+ * Maximum zoom level supported by the dataset.
140
+ */
141
+ maxZoom: number;
142
+
143
+ /**
144
+ * The latest available instance of the dataset.
145
+ */
146
+ instance: string;
147
+
148
+ /** Array of all available timesteps (instances) for the dataset. */
149
+ times: string[];
150
+
151
+ /** Geographic boundaries of the dataset. */
152
+ boundaries: WxAllBoundariesMeta;
153
+
154
+ /** Identifier for the dataset's source. */
155
+ sourceID?: string;
156
+
157
+ /** Base atmospheric model used for the dataset, if applicable. */
158
+ baseAtmosphericModel?: string;
159
+
160
+ /** Name of the model used to generate the dataset, if applicable. */
161
+ model?: string;
162
+ }
163
+
164
+ /**
165
+ * Configuration options for constructing a {@link WxAPI} object.
166
+ * @example
167
+ * ```typescript
168
+ * const requestInit: RequestInit = {
169
+ * headers: {
170
+ * Authorization: "Bearer <token>",
171
+ * },
172
+ * }; // Add more options if needed, such as mode or credentials.
173
+ * const options: WxAPIOptions = {
174
+ * dataServerURL: "https://tiles.metoceanapi.com/data/",
175
+ * requestInit,
176
+ * };
177
+ * const wxapi = new WxAPI(options);
178
+ * ```
179
+ */
180
+ export interface WxAPIOptions extends WxTilesLibOptions {
181
+ /**
182
+ * Base URL of the data server.
183
+ */
184
+ dataServerURL: string;
185
+
186
+ /** extension of the tiles*/
187
+ ext?: string;
188
+
189
+ /**
190
+ * Full URL for mask tiles. Example: `https://server.com/masks/{z}/{x}/{y}.webp`.
191
+ * @default 'auto' - Automatically set to `dataServerURL + 'masks/{z}/{x}/{y}.webp'`.
192
+ * 'none' - Masks are disabled.
193
+ */
194
+ maskURL?: "none" | "auto" | string;
195
+
196
+ /**
197
+ * RGBA channel to use for masking.
198
+ * 0 - Masks from Sarah (current),
199
+ * 3 - Masks from Mapbox.
200
+ * @default 'R'
201
+ */
202
+ maskChannel?: "R" | "G" | "B" | "A";
203
+
204
+ /**
205
+ * Maximum zoom level for mask tiles on the server.
206
+ * @default 9
207
+ */
208
+ maskDepth?: number;
209
+
210
+ /**
211
+ * URL for the QTree file. QTree is a hierarchical structure that enables efficient spatial queries for sea/land detection.
212
+ * @default 'auto' - Automatically set to `dataServerURL + 'masks/9+1.seamask.qtree'`.
213
+ * 'none' - Disables QTree usage.
214
+ */
215
+ qtreeURL?: "none" | "auto" | string;
216
+
217
+ /**
218
+ * Options for HTTP requests made to the backend data server, such as headers, credentials, and CORS settings.
219
+ */
220
+ requestInit?: RequestInit;
221
+ }
222
+
223
+ /**
224
+ * Mandatory Interface to be implemented by a {@link WxLayerBaseImplementation}
225
+ */
226
+ export interface WxLayerBaseAPI {
227
+ wxdatasetManager: BaseDatasetManager;
228
+ getCurrentVariableMeta(): WxVariableMeta;
229
+ getVariablesNames(): WxLayerVarsNames;
230
+ clearCache(): void;
231
+ getCurrentStyleObjectCopy(): WxColorStyleStrict;
232
+ getTime(): string;
233
+ setTime(time?: WxDate, requestInit?: WxRequestInit): Promise<string>;
234
+ preloadTime(time: WxDate, requestInit?: WxRequestInit): Promise<void>;
235
+ startAnimation(): void;
236
+ stopAnimation(): Promise<void>;
237
+ setCoarseLevel(level: number): Promise<void>;
238
+ unsetCoarseLevel(): Promise<void>;
239
+ setStyleByName(wxstyleName: string, reload: boolean): Promise<void>;
240
+ updateCurrentStyleObject(
241
+ style?: WxColorStyleWeak,
242
+ reload?: boolean,
243
+ requestInit?: WxRequestInit,
244
+ ): Promise<void>;
245
+ }
246
+
247
+ export type ListenerMethod = <T extends keyof WxEventType>(
248
+ arg?: WxEventType[T],
249
+ ) => void;
250
+
251
+ export type WxEventType = {
252
+ // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use case
253
+ changed: any;
254
+ };
255
+
256
+ /**
257
+ * Mandatory Interface to be implemented by a {@link WxTileSource} implementation
258
+ * These methods *Requires* framework specific implementation
259
+ * */
260
+ export interface WxLayerAPI extends WxLayerBaseAPI {
261
+ // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use case
262
+ getLayerInfoAtLatLon(lnglat: WxLngLat, anymap: any): WxTileInfo | undefined;
263
+ _reloadVisible(requestInit?: WxRequestInit): Promise<void>;
264
+ update(): void;
265
+ coveringTiles(): XYZ[];
266
+ on<T extends keyof WxEventType>(type: T, listener: ListenerMethod): void;
267
+ off<T extends keyof WxEventType>(type: T, listener: ListenerMethod): void;
268
+ once<T extends keyof WxEventType>(type: T, listener: ListenerMethod): void;
269
+ }
270
+
271
+ /**
272
+ * Type used to set a time step for the layer.
273
+ * Can be
274
+ * 1. a string in the dataset's format,
275
+ * 2. a number of a time step in the dataset's meta data,
276
+ * 3. a number of milliseconds since 1970-01-01T00:00:00Z
277
+ * 4. a Date object.
278
+ * */
279
+ export type WxDate = string | number | Date;
280
+
281
+
282
+ /** Information about the current point on map returned by the {@link WxLayer} method. */
283
+ export interface WxTileInfo {
284
+ /** values of all variables in datasets's units */
285
+ data: number[];
286
+
287
+ /** RAW values of all variables in internal representation */
288
+ raw: number[];
289
+
290
+ /** **uint32** colors defined by curent values {@link WxTileInfo.data} and the style */
291
+ rgba: number[];
292
+
293
+ /** web colors defined by curent values {@link WxTileInfo.data} and the style */
294
+ hexColor: string[];
295
+
296
+ /** values of all variables in Style's units */
297
+ inStyleUnits: number[];
298
+
299
+ /** The current point on map. */
300
+ tilePoint: { x: number; y: number };
301
+
302
+ /** WxUnits defined style */
303
+ styleUnits: string;
304
+
305
+ /** WxUnits defined dataset */
306
+ dataUnits: string;
307
+ }
308
+
309
+ /** Array of variable names to be used in {@link WxLayer} */
310
+ export type WxLayerVarsNames = [string] | [string, string];
311
+
312
+ /** @internal Array of URIs to request tiles */
313
+ export type WxURIs = [string] | [string, string];
314
+
315
+ /** Lon/Lat coordinates */
316
+ export interface WxLngLat {
317
+ /** Longitude */
318
+ lng: number;
319
+
320
+ /** Latitude */
321
+ lat: number;
322
+ }
323
+
324
+ /**
325
+ * @internal
326
+ * Used in {@link WxLayerBaseImplementation} cache of the renderes tiles
327
+ * */
328
+ export class WxRasterDataCache extends Map<string, WxRasterData> {
329
+ clear(): void {
330
+ WXLOG("TilesCache.clear()");
331
+
332
+ for (const v of this.values()) {
333
+ if (!v.rd) return;
334
+ v.rd.gl.deleteTexture(v.rd.vectorTextureU);
335
+ v.rd.gl.deleteTexture(v.rd.vectorTextureV);
336
+ }
337
+
338
+ super.clear();
339
+ }
340
+ }
341
+
342
+ /**
343
+ * Interface to represent information related to isolines
344
+ */
345
+ export interface IsoInfo {
346
+ x: number;
347
+ y: number;
348
+ d: number;
349
+ dr: number;
350
+ db: number;
351
+ mli: number;
352
+ }
353
+
354
+ /**
355
+ * Interface representing raster data required for rendering a single tile.
356
+ * Contains necessary contexts and raw data for rendering.
357
+ */
358
+ export interface WxRasterData {
359
+ /**
360
+ * Context for rendering background fill and isolines.
361
+ */
362
+ ctxFill: CanvasRenderingContext2D;
363
+
364
+ /**
365
+ * Context for rendering vector arrows, and other text
366
+ * */
367
+ ctxText: CanvasRenderingContext2D;
368
+
369
+ /**
370
+ * Context for rendering animated streamlines
371
+ * */
372
+ ctxStreamLines: CanvasRenderingContext2D;
373
+
374
+ /**
375
+ * {@link WxData} data object for this tile
376
+ * */
377
+ data: WxData;
378
+
379
+ /**
380
+ * Optional WebGL textures for vector field representations.
381
+ */
382
+ rd?: {
383
+ vectorTextureU: WebGLTexture;
384
+ vectorTextureV: WebGLTexture;
385
+ gl: WebGLRenderingContext;
386
+ };
387
+ }
388
+
389
+ /** Represents a point on a pixel grid */
390
+ export interface SLinePoint {
391
+ x: number;
392
+ y: number;
393
+ }
394
+
395
+ /** Represents a line on a pixel grid */
396
+ export type SLine = SLinePoint[];
397
+
398
+ /** Data structure for a single tile */
399
+ export interface WxData {
400
+ /**
401
+ * Tile data array.
402
+ * - Contains one element for scalar data.
403
+ * - Contains three elements for vector data (U, V components, and vector lengths).
404
+ */
405
+ data: DataPictures;
406
+
407
+ /** Streamlines for visualizing vector fields */
408
+ slines: SLine[];
409
+ }
410
+
411
+ /**
412
+ * @internal
413
+ * Do not use this type directly, use {@link WxSourceLayerOptions} instead
414
+ * */
415
+ export interface WxLayerOptions<WxDatasetManagerT> {
416
+ /** Variables of the layer */
417
+ variables: WxLayerVarsNames;
418
+
419
+ /** Dataset Manager */
420
+ wxdatasetManager: WxDatasetManagerT;
421
+
422
+ /** initial time step */
423
+ time?: WxDate;
424
+
425
+ /** initial style name */
426
+ wxstyleName?: string;
427
+
428
+ /** initial style, applyed on top of style passed by name {@link wxstyleName} */
429
+ wxstyle?: WxColorStyleWeak;
430
+ }
431
+
432
+ /**
433
+ * Options for constructing a {@link WxDataSetManager} instance.
434
+ */
435
+ export interface WxDataSetManagerOptions {
436
+ /**
437
+ * @internal
438
+ * The name of the dataset to manage.
439
+ */
440
+ datasetName: string;
441
+
442
+ /**
443
+ * @internal
444
+ * Current instance of the dataset
445
+ * */
446
+ datasetCurrentInstance: string;
447
+
448
+ /**
449
+ * @internal
450
+ * Array of time steps for instanced datasets.
451
+ * */
452
+ instanced?: WxInstances;
453
+
454
+ /**
455
+ * @internal
456
+ * Metadata for the current instance of the dataset.
457
+ * */
458
+ datasetCurrentMeta: WxDatasetMeta;
459
+
460
+ /**
461
+ * @internal
462
+ * Metadata for all instances of the dataset, if instanced.
463
+ * */
464
+ metas?: Map<string, WxDatasetMeta>;
465
+
466
+ /**
467
+ * @internal
468
+ * Reference to the {@link WxAPI} instance used to interact with the WxTiles API.
469
+ * */
470
+ wxAPI: WxAPI;
471
+ }
package/utils/wxtools.ts CHANGED
@@ -183,9 +183,19 @@ export interface WxColorStylesStrict {
183
183
  [name: string]: WxColorStyleStrict | undefined;
184
184
  }
185
185
 
186
- let _units: WxUnits;
187
- let _colorSchemes: WxColorSchemes;
188
- let _colorStylesUnrolled: WxColorStylesStrict;
186
+
187
+ /*const context = {
188
+ [_units: WxUnits]: __units_default_preset,
189
+ [_colorSchemes: WxColorSchemes]: null,
190
+ [_colorStylesUnrolled: WxColorStylesStrict]: null,
191
+ }
192
+ */
193
+ const context = {
194
+ _units: __units_default_preset,
195
+ _colorSchemes: __colorSchemes_default_preset,
196
+ _colorStylesUnrolled: __colorStyles_default_preset,
197
+ wxlogging: false,
198
+ }
189
199
 
190
200
  /** Options for the wxtiles library */
191
201
  export interface WxTilesLibOptions {
@@ -209,14 +219,14 @@ export function WxTilesLibSetup({
209
219
  colorSchemes = {},
210
220
  }: WxTilesLibOptions = {}): void {
211
221
  WXLOG("WxTile lib setup: start");
212
- _units = Object.assign({}, __units_default_preset, units);
213
- _colorSchemes = Object.assign(
222
+ context._units = Object.assign({}, __units_default_preset, units);
223
+ context._colorSchemes = Object.assign(
214
224
  {},
215
225
  colorSchemes,
216
226
  __colorSchemes_default_preset,
217
227
  );
218
228
  const toUnroll = Object.assign({}, colorStyles, __colorStyles_default_preset);
219
- _colorStylesUnrolled = unrollStylesParent(toUnroll);
229
+ context._colorStylesUnrolled = unrollStylesParent(toUnroll);
220
230
  WXLOG("WxTile lib setup: styles unrolled");
221
231
 
222
232
  // Make sure fonts are loaded & ready!
@@ -236,14 +246,14 @@ export function WxTilesLibSetup({
236
246
  * Get all {@link WxColorStylesStrict} used by the library
237
247
  * */
238
248
  export function WxGetColorStyles(): WxColorStylesStrict {
239
- return _colorStylesUnrolled;
249
+ return context._colorStylesUnrolled;
240
250
  }
241
251
 
242
252
  /**
243
253
  * Get all {@link WxColorSchemes} used by the library
244
254
  * */
245
255
  export function WxGetColorSchemes(): WxColorSchemes {
246
- return _colorSchemes;
256
+ return context._colorSchemes;
247
257
  }
248
258
 
249
259
  /** A function type with extra properties. */
@@ -279,7 +289,7 @@ export function makeConverter(
279
289
  return c; // trivial
280
290
  }
281
291
 
282
- const localUnitsCopy = Object.assign({}, _units, customUnits);
292
+ const localUnitsCopy = Object.assign({}, context._units, customUnits);
283
293
  const fromUnit = localUnitsCopy[from];
284
294
  const toUnit = localUnitsCopy[to];
285
295
  if (!fromUnit || !toUnit) {
@@ -1039,7 +1049,6 @@ export function getClosestTimeString(
1039
1049
  );
1040
1050
  }
1041
1051
 
1042
- let wxlogging = false;
1043
1052
  /**
1044
1053
  * Logging helper
1045
1054
  * */
@@ -1058,9 +1067,9 @@ export let WXLOG = (...args: any[]) => {};
1058
1067
  * */
1059
1068
  // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use case
1060
1069
  export function WxTilesLogging(on?: boolean | ((...args: any[]) => void)) {
1061
- wxlogging = !!on;
1062
- console.log("WXLOGing", wxlogging);
1063
- WXLOG = typeof on === "function" ? on : wxlogging ? console.log : () => {};
1070
+ context.wxlogging = !!on;
1071
+ console.log("WXLOGing", context.wxlogging);
1072
+ WXLOG = typeof on === "function" ? on : context.wxlogging ? console.log : () => {};
1064
1073
  }
1065
1074
 
1066
1075
  /**