@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
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/wxapi.ts ADDED
@@ -0,0 +1,223 @@
1
+ import "@metoceanapi/wxtiles-common/wxtiles.css";
2
+
3
+ import { QTree } from "./utils/qtree";
4
+ import {
5
+ WXLOG,
6
+ WxTilesLibSetup,
7
+ type XYZ,
8
+ cacheUriPromise,
9
+ fetchJson,
10
+ loadImageData,
11
+ uriXYZ,
12
+ } from "./utils/wxtools";
13
+ import type { WxAPIOptions, WxDatasetMeta, WxDataSetManagerOptions } from "./types";
14
+ import { BaseDatasetManager } from "./managers/BaseDatasetManager";
15
+ import { WxAllDatasetsManager } from "./managers/WxAllDatasetsManager";
16
+
17
+ /**
18
+ * WxAPI is an initialisation object for the library. See {@link WxAPIOptions} for options.
19
+ * @example
20
+ * ```typescript
21
+ * import { WxAPI } from 'wx-tiles';
22
+ * const wxapi = new WxAPI({
23
+ * dataServerURL: 'https://server.com/',
24
+ * // other options
25
+ * });
26
+ * ```
27
+ */
28
+ export abstract class WxAPI {
29
+ /**
30
+ * A Promise that resolves when the {@link WxAPI} instance is fully initialized and ready to be used.
31
+ */
32
+ readonly initDone: Promise<void>;
33
+
34
+ /** The URL of the data server. See {@link WxAPIOptions} for configuration details. */
35
+ readonly dataServerURL: string;
36
+
37
+ /** see {@link WxAPIOptions}*/
38
+ readonly ext: string;
39
+
40
+ /** Optional configuration for HTTP requests. See {@link WxAPIOptions} for details. */
41
+ readonly requestInit?: RequestInit;
42
+
43
+ /************************/
44
+ /* NO EXPORT PART BEGIN */
45
+ /************************/
46
+
47
+ /** The URL for the mask tiles. See {@link WxAPIOptions} for more information. */
48
+ readonly maskURL: string;
49
+
50
+ /** @internal The channel used for mask tiles. See {@link WxAPIOptions} for details. */
51
+ readonly maskChannel: number;
52
+
53
+ /** @internal The depth level used for mask tiles. See {@link WxAPIOptions} for more information. */
54
+ readonly maskDepth: number;
55
+
56
+ /** @internal An instance of the QTree object, used internally for spatial indexing. */
57
+ readonly qtree: QTree;
58
+
59
+ /**
60
+ * @internal
61
+ * A function that loads mask tiles asynchronously.
62
+ * @param x - The X coordinate of the tile.
63
+ * @param y - The Y coordinate of the tile.
64
+ * @param z - The zoom level of the tile.
65
+ * @returns A Promise that resolves to the {@link ImageData} of the tile.
66
+ */
67
+ readonly loadMaskFunc: ({ x, y, z }: XYZ) => Promise<ImageData>;
68
+
69
+ /** @ignore @internal a manager for all datasets. Operates with short datasets*/
70
+ protected readonly _allDatasetsManager: WxAllDatasetsManager;
71
+
72
+ /************************/
73
+ /* NO EXPORT PART END */
74
+ /************************/
75
+
76
+ /**
77
+ * Initializes a new instance of the {@link WxAPI} class.
78
+ * @param options - Configuration options for the WxAPI instance. See {@link WxAPIOptions}.
79
+ */
80
+ constructor({
81
+ dataServerURL,
82
+ ext = "webp",
83
+ maskURL = "none",
84
+ maskChannel = "R",
85
+ maskDepth = 11,
86
+ qtreeURL = "none",
87
+ requestInit,
88
+ colorStyles,
89
+ units,
90
+ colorSchemes,
91
+ }: WxAPIOptions) {
92
+ WXLOG("WxAPI.constructor", dataServerURL);
93
+ WxTilesLibSetup({ colorStyles, units, colorSchemes });
94
+
95
+ // compose auto-URLs for qtree and mask
96
+ if (qtreeURL === 'auto')
97
+ qtreeURL = `${dataServerURL}masks/11+1.seamask.qtree`;
98
+ if (maskURL === 'auto') maskURL = `${dataServerURL}masks/{z}/{x}/{y}.${ext}`;
99
+
100
+ this.dataServerURL = dataServerURL;
101
+ this.ext = ext;
102
+ this.requestInit = requestInit;
103
+
104
+ this.maskURL = maskURL;
105
+ this.maskChannel =
106
+ maskChannel === "R"
107
+ ? 0
108
+ : maskChannel === "G"
109
+ ? 1
110
+ : maskChannel === "B"
111
+ ? 2
112
+ : maskChannel === "A"
113
+ ? 3
114
+ : 0; // default to R
115
+ this.maskDepth = maskDepth;
116
+ if (this.maskURL !== "none") {
117
+ const maskloader = cacheUriPromise(loadImageData);
118
+ this.loadMaskFunc = (coord: XYZ) =>
119
+ maskloader(uriXYZ(this.maskURL, coord), requestInit);
120
+ } else {
121
+ this.loadMaskFunc = () =>
122
+ Promise.reject(new Error("maskURL not defined"));
123
+ }
124
+
125
+ this._allDatasetsManager = new WxAllDatasetsManager(this);
126
+ this.qtree = new QTree(qtreeURL, requestInit);
127
+
128
+ this.initDone = Promise.all([
129
+ this._allDatasetsManager.ready,
130
+ this.qtree.ready,
131
+ ]).then(() => WXLOG("WxAPI.constructor initDone"));
132
+ }
133
+
134
+ async createDatasetManagerOptions(datasetName: string): Promise<WxDataSetManagerOptions> {
135
+ await this.initDone;
136
+ await this._allDatasetsManager.updateOne(datasetName);
137
+ WXLOG("WxAPI.createDatasetManager", datasetName);
138
+ const datasetShortMeta =
139
+ this._allDatasetsManager.allDatasetsShortMetas[datasetName];
140
+ const datasetCurrentInstance = datasetShortMeta?.instance;
141
+ if (!datasetCurrentInstance)
142
+ throw new Error(`Dataset/instance not found:${datasetName}`);
143
+ const instanced = datasetShortMeta.instanced;
144
+ let metasArray: WxDatasetMeta[];
145
+ const preURL = `${this.dataServerURL + datasetName}/`;
146
+ try {
147
+ // using the new infrastructure with WxServer & WxAggregator
148
+ metasArray = await fetchJson<WxDatasetMeta[]>(
149
+ `${preURL}metafull.json`,
150
+ this.requestInit,
151
+ );
152
+ } catch (e) {
153
+ WXLOG("Can't load metafull.json. Trying old infrastructure with NGINX");
154
+ const instances = await fetchJson<string[]>(
155
+ `${preURL}instances.json`,
156
+ this.requestInit,
157
+ );
158
+ if (!Array.isArray(instances) || !instances.length)
159
+ throw new Error("instances.json is not an array or empty");
160
+ metasArray = await Promise.all(
161
+ instances
162
+ .reverse()
163
+ .map(async (instance) =>
164
+ Object.assign(
165
+ await fetchJson<WxDatasetMeta>(
166
+ `${preURL + instance}/meta.json`,
167
+ this.requestInit,
168
+ ),
169
+ { instance },
170
+ ),
171
+ ),
172
+ );
173
+ }
174
+
175
+ const datasetCurrentMeta = metasArray[0];
176
+ const metas =
177
+ instanced && new Map(metasArray.map((meta) => [meta.instance, meta]));
178
+ return {
179
+ datasetName,
180
+ datasetCurrentInstance,
181
+ datasetCurrentMeta,
182
+ instanced,
183
+ metas,
184
+ wxAPI: this,
185
+ };
186
+ }
187
+
188
+ /**
189
+ * Get all variables for the given dataset name.
190
+ * @param datasetName - dataset name
191
+ * @returns {Promise<string[]>} - list of all available variables for the dataset */
192
+ async getDatasetAllVariables(
193
+ datasetName: string,
194
+ ): Promise<string[] | undefined> {
195
+ await this.initDone;
196
+ WXLOG("WxAPI.getDatasetVariables", datasetName);
197
+ return this._allDatasetsManager.allDatasetsShortMetas[datasetName]
198
+ ?.variables;
199
+ }
200
+
201
+ /**
202
+ * Returns datasets names which have given variable
203
+ * @argument {string} varName - variable name to search for in datasets
204
+ * @returns {Promise<string[]>} - list of datasets' names */
205
+ async filterDatasetsByVariableName(varName: string): Promise<string[]> {
206
+ await this.initDone;
207
+ WXLOG("WxAPI.filterDatasetsByVariableName:", varName);
208
+ const { allDatasetsShortMetas } = this._allDatasetsManager;
209
+ return Object.keys(allDatasetsShortMetas).filter((dsName) =>
210
+ allDatasetsShortMetas[dsName]?.variables.includes(varName),
211
+ );
212
+ }
213
+
214
+ /**
215
+ * Get the list of all available datasets' names
216
+ * @returns {Promise<string[]>} - list of all available datasets' names
217
+ * */
218
+ async getAllDatasetsNames(): Promise<string[]> {
219
+ await this.initDone;
220
+ WXLOG("WxAPI.getAllDatasetsNames");
221
+ return Object.keys(this._allDatasetsManager.allDatasetsShortMetas);
222
+ }
223
+ }