@metoceanapi/wxtiles-common 0.0.8 → 0.0.11-0

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 (162) hide show
  1. package/.github/workflows/lint.yml +1 -1
  2. package/.github/workflows/validate-pull-request-title.yml +17 -0
  3. package/LICENSE.txt +1 -1
  4. package/biome.json +26 -1
  5. package/controls/WxAPIControl/WxAPIControl-busy.ts +16 -0
  6. package/controls/WxAPIControl/WxAPIControl-datasets.ts +102 -0
  7. package/controls/WxAPIControl/WxAPIControl-filter.ts +66 -0
  8. package/controls/WxAPIControl/WxAPIControl-variables.ts +73 -0
  9. package/controls/WxAPIControl.ts +87 -120
  10. package/controls/WxInfoControl.ts +87 -2
  11. package/controls/WxLegendControl.ts +28 -2
  12. package/controls/WxStyleEditorControl.ts +129 -7
  13. package/controls/WxTimeControl.ts +133 -5
  14. package/docs/docs/.nojekyll +1 -0
  15. package/docs/docs/assets/highlight.css +92 -0
  16. package/docs/docs/assets/icons.svg +1 -0
  17. package/docs/docs/assets/style.css +1610 -0
  18. package/docs/docs/classes/controls_WxAPIControl.WxAPIControl.html +28 -0
  19. package/docs/docs/classes/controls_WxInfoControl.WxInfoControl.html +20 -0
  20. package/docs/docs/classes/controls_WxLegendControl.WxLegendControl.html +19 -0
  21. package/docs/docs/classes/controls_WxStyleEditorControl.WxStyleEditorControl.html +111 -0
  22. package/docs/docs/classes/controls_WxTimeControl.WxTimeControl.html +28 -0
  23. package/docs/docs/classes/managers_BaseDatasetManager.BaseDatasetManager.html +104 -0
  24. package/docs/docs/classes/managers_WxAllDatasetsManager.WxAllDatasetsManager.html +24 -0
  25. package/docs/docs/classes/types.WxRasterDataCache.html +27 -0
  26. package/docs/docs/classes/utils_RawCLUT.RawCLUT.html +18 -0
  27. package/docs/docs/classes/utils_qtree.QTree.html +22 -0
  28. package/docs/docs/classes/utils_wxtools.DataIntegral.html +10 -0
  29. package/docs/docs/classes/wxapi.WxAPI.html +45 -0
  30. package/docs/docs/classes/wxlayer_loader.Loader.html +31 -0
  31. package/docs/docs/classes/wxlayer_painter.Painter.html +20 -0
  32. package/docs/docs/classes/wxlayer_wxlayer.WxLayer.html +57 -0
  33. package/docs/docs/enums/utils_qtree.TileType.html +5 -0
  34. package/docs/docs/functions/utils_RawCLUT.WxCreateLegend.html +5 -0
  35. package/docs/docs/functions/utils_mercator.LatLonToPixels.html +6 -0
  36. package/docs/docs/functions/utils_mercator.PixelsToLatLon.html +6 -0
  37. package/docs/docs/functions/utils_mercator.PixelsToLonLat.html +6 -0
  38. package/docs/docs/functions/utils_mercator.coordToPixel.html +5 -0
  39. package/docs/docs/functions/utils_tests_wxtools.tests.html +1 -0
  40. package/docs/docs/functions/utils_wxtools.HEXtoRGBA.html +3 -0
  41. package/docs/docs/functions/utils_wxtools.HashXYZ.html +4 -0
  42. package/docs/docs/functions/utils_wxtools.RGBAtoHEX.html +3 -0
  43. package/docs/docs/functions/utils_wxtools.RGBtoHEX.html +3 -0
  44. package/docs/docs/functions/utils_wxtools.WXLOG.html +2 -0
  45. package/docs/docs/functions/utils_wxtools.WxGetColorSchemes.html +2 -0
  46. package/docs/docs/functions/utils_wxtools.WxGetColorStyles.html +2 -0
  47. package/docs/docs/functions/utils_wxtools.WxTilesLibSetup.html +3 -0
  48. package/docs/docs/functions/utils_wxtools.WxTilesLogging.html +5 -0
  49. package/docs/docs/functions/utils_wxtools.blurData.html +7 -0
  50. package/docs/docs/functions/utils_wxtools.buildIntegralPare.html +6 -0
  51. package/docs/docs/functions/utils_wxtools.cacheUriPromise.html +2 -0
  52. package/docs/docs/functions/utils_wxtools.create2DContext.html +6 -0
  53. package/docs/docs/functions/utils_wxtools.createEl.html +5 -0
  54. package/docs/docs/functions/utils_wxtools.createLevels.html +6 -0
  55. package/docs/docs/functions/utils_wxtools.fetchJson.html +6 -0
  56. package/docs/docs/functions/utils_wxtools.getClosestTimeString.html +5 -0
  57. package/docs/docs/functions/utils_wxtools.loadDataIntegral.html +5 -0
  58. package/docs/docs/functions/utils_wxtools.loadImage.html +4 -0
  59. package/docs/docs/functions/utils_wxtools.loadImageData.html +5 -0
  60. package/docs/docs/functions/utils_wxtools.makeConverter.html +9 -0
  61. package/docs/docs/functions/utils_wxtools.mixColor.html +3 -0
  62. package/docs/docs/functions/utils_wxtools.refineColor.html +3 -0
  63. package/docs/docs/functions/utils_wxtools.uriXYZ.html +5 -0
  64. package/docs/docs/functions/wxlayer_WxImplementation.WxLayerBaseImplementationMixin.html +4 -0
  65. package/docs/docs/functions/wxlayer_loadertools.applyMask.html +10 -0
  66. package/docs/docs/functions/wxlayer_loadertools.makeBox.html +4 -0
  67. package/docs/docs/functions/wxlayer_loadertools.splitCoords.html +6 -0
  68. package/docs/docs/functions/wxlayer_loadertools.subData.html +5 -0
  69. package/docs/docs/functions/wxlayer_loadertools.subDataDegree.html +5 -0
  70. package/docs/docs/functions/wxlayer_loadertools.subMask.html +5 -0
  71. package/docs/docs/hierarchy.html +1 -0
  72. package/docs/docs/index.html +7 -0
  73. package/docs/docs/interfaces/types.IsoInfo.html +8 -0
  74. package/docs/docs/interfaces/types.SLinePoint.html +4 -0
  75. package/docs/docs/interfaces/types.WxAPIOptions.html +37 -0
  76. package/docs/docs/interfaces/types.WxAllBoundariesMeta.html +8 -0
  77. package/docs/docs/interfaces/types.WxAllDatasetsShortMetas.html +4 -0
  78. package/docs/docs/interfaces/types.WxBoundaryMeta.html +10 -0
  79. package/docs/docs/interfaces/types.WxData.html +10 -0
  80. package/docs/docs/interfaces/types.WxDataSetManagerOptions.html +14 -0
  81. package/docs/docs/interfaces/types.WxDatasetMeta.html +20 -0
  82. package/docs/docs/interfaces/types.WxDatasetShortMeta.html +9 -0
  83. package/docs/docs/interfaces/types.WxLayerAPI.html +24 -0
  84. package/docs/docs/interfaces/types.WxLayerBaseAPI.html +16 -0
  85. package/docs/docs/interfaces/types.WxLayerOptions.html +12 -0
  86. package/docs/docs/interfaces/types.WxLngLat.html +6 -0
  87. package/docs/docs/interfaces/types.WxRasterData.html +13 -0
  88. package/docs/docs/interfaces/types.WxRequestInit.html +3 -0
  89. package/docs/docs/interfaces/types.WxTileInfo.html +18 -0
  90. package/docs/docs/interfaces/types.WxVariableMeta.html +13 -0
  91. package/docs/docs/interfaces/types.WxVariablesMetas.html +4 -0
  92. package/docs/docs/interfaces/utils_RawCLUT.WxLegend.html +14 -0
  93. package/docs/docs/interfaces/utils_RawCLUT.WxTick.html +10 -0
  94. package/docs/docs/interfaces/utils_qtree.Tree.html +7 -0
  95. package/docs/docs/interfaces/utils_wxtools.CachedUriLoaderPromiseFunc.html +2 -0
  96. package/docs/docs/interfaces/utils_wxtools.ColorStylesIncomplete.html +2 -0
  97. package/docs/docs/interfaces/utils_wxtools.Converter.html +5 -0
  98. package/docs/docs/interfaces/utils_wxtools.DataPicture.html +6 -0
  99. package/docs/docs/interfaces/utils_wxtools.IntegralPare.html +6 -0
  100. package/docs/docs/interfaces/utils_wxtools.WxColorSchemes.html +3 -0
  101. package/docs/docs/interfaces/utils_wxtools.WxColorStyleStrict.html +95 -0
  102. package/docs/docs/interfaces/utils_wxtools.WxColorStyleWeak.html +95 -0
  103. package/docs/docs/interfaces/utils_wxtools.WxColorStylesStrict.html +2 -0
  104. package/docs/docs/interfaces/utils_wxtools.WxColorStylesWeakMixed.html +2 -0
  105. package/docs/docs/interfaces/utils_wxtools.WxTilesLibOptions.html +8 -0
  106. package/docs/docs/interfaces/utils_wxtools.WxUnits.html +2 -0
  107. package/docs/docs/interfaces/utils_wxtools.XYZ.html +5 -0
  108. package/docs/docs/modules/controls_WxAPIControl.html +1 -0
  109. package/docs/docs/modules/controls_WxInfoControl.html +1 -0
  110. package/docs/docs/modules/controls_WxLegendControl.html +1 -0
  111. package/docs/docs/modules/controls_WxStyleEditorControl.html +1 -0
  112. package/docs/docs/modules/controls_WxTimeControl.html +1 -0
  113. package/docs/docs/modules/defaults_colorschemes.html +1 -0
  114. package/docs/docs/modules/defaults_styles.html +1 -0
  115. package/docs/docs/modules/defaults_uconv.html +1 -0
  116. package/docs/docs/modules/managers_BaseDatasetManager.html +1 -0
  117. package/docs/docs/modules/managers_WxAllDatasetsManager.html +1 -0
  118. package/docs/docs/modules/types.html +1 -0
  119. package/docs/docs/modules/utils_RawCLUT.html +1 -0
  120. package/docs/docs/modules/utils_mercator.html +1 -0
  121. package/docs/docs/modules/utils_qtree.html +1 -0
  122. package/docs/docs/modules/utils_tests_wxtools.html +1 -0
  123. package/docs/docs/modules/utils_wxtools.html +1 -0
  124. package/docs/docs/modules/wxapi.html +1 -0
  125. package/docs/docs/modules/wxlayer_WxImplementation.html +1 -0
  126. package/docs/docs/modules/wxlayer_loader.html +1 -0
  127. package/docs/docs/modules/wxlayer_loadertools.html +1 -0
  128. package/docs/docs/modules/wxlayer_painter.html +1 -0
  129. package/docs/docs/modules/wxlayer_wxlayer.html +1 -0
  130. package/docs/docs/modules.html +1 -0
  131. package/docs/docs/types/types.ListenerMethod.html +1 -0
  132. package/docs/docs/types/types.SLine.html +2 -0
  133. package/docs/docs/types/types.WxDate.html +9 -0
  134. package/docs/docs/types/types.WxEventType.html +1 -0
  135. package/docs/docs/types/types.WxInstances.html +2 -0
  136. package/docs/docs/types/types.WxLayerVarsNames.html +2 -0
  137. package/docs/docs/types/types.WxURIs.html +2 -0
  138. package/docs/docs/types/utils_qtree.SubTrees.html +2 -0
  139. package/docs/docs/types/utils_qtree.SubTreesN.html +2 -0
  140. package/docs/docs/types/utils_qtree.TreeN.html +2 -0
  141. package/docs/docs/types/utils_wxtools.DataIntegrals.html +2 -0
  142. package/docs/docs/types/utils_wxtools.DataPictures.html +2 -0
  143. package/docs/docs/types/utils_wxtools.WxUnitTuple.html +2 -0
  144. package/docs/docs/types/utils_wxtools.colorMapTuple.html +2 -0
  145. package/docs/docs/variables/defaults_colorschemes.__colorSchemes_default_preset.html +1 -0
  146. package/docs/docs/variables/defaults_styles.__colorStyles_default_preset.html +1 -0
  147. package/docs/docs/variables/defaults_uconv.__units_default_preset.html +1 -0
  148. package/managers/BaseDatasetManager.ts +272 -0
  149. package/managers/WxAllDatasetsManager.ts +60 -0
  150. package/package.json +6 -3
  151. package/tsconfig.json +31 -0
  152. package/typedoc.json +4 -0
  153. package/types.ts +471 -0
  154. package/utils/qtree.ts +2 -1
  155. package/utils/tests_wxtools.ts +10 -10
  156. package/utils/wxtools.ts +1 -0
  157. package/wxapi.ts +222 -0
  158. package/wxlayer/WxImplementation.ts +374 -0
  159. package/wxlayer/loader.ts +305 -0
  160. package/wxlayer/loadertools.ts +380 -0
  161. package/wxlayer/painter.ts +451 -0
  162. package/wxlayer/wxlayer.ts +379 -0
package/wxapi.ts ADDED
@@ -0,0 +1,222 @@
1
+ // @ts-ignore
2
+ import "@metoceanapi/wxtiles-common/wxtiles.css";
3
+ import { WxAllDatasetsManager } from "./managers/WxAllDatasetsManager";
4
+ import { QTree } from "./utils/qtree";
5
+ import {
6
+ WXLOG,
7
+ WxTilesLibSetup,
8
+ type XYZ,
9
+ cacheUriPromise,
10
+ fetchJson,
11
+ loadImageData,
12
+ uriXYZ,
13
+ } from "./utils/wxtools";
14
+ import type { WxAPIOptions, WxDataSetManagerOptions, WxDatasetMeta } from "./types";
15
+
16
+ /**
17
+ * WxAPI is an initialisation object for the library. See {@link WxAPIOptions} for options.
18
+ * @example
19
+ * ```typescript
20
+ * import { WxAPI } from 'wx-tiles';
21
+ * const wxapi = new WxAPI({
22
+ * dataServerURL: 'https://server.com/',
23
+ * // other options
24
+ * });
25
+ * ```
26
+ */
27
+ export abstract class WxAPI {
28
+ /**
29
+ * A Promise that resolves when the {@link WxAPI} instance is fully initialized and ready to be used.
30
+ */
31
+ readonly initDone: Promise<void>;
32
+
33
+ /** The URL of the data server. See {@link WxAPIOptions} for configuration details. */
34
+ readonly dataServerURL: string;
35
+
36
+ /** see {@link WxAPIOptions}*/
37
+ readonly ext: string;
38
+
39
+ /** Optional configuration for HTTP requests. See {@link WxAPIOptions} for details. */
40
+ readonly requestInit?: RequestInit;
41
+
42
+ /************************/
43
+ /* NO EXPORT PART BEGIN */
44
+ /************************/
45
+
46
+ /** The URL for the mask tiles. See {@link WxAPIOptions} for more information. */
47
+ readonly maskURL: string;
48
+
49
+ /** @internal The channel used for mask tiles. See {@link WxAPIOptions} for details. */
50
+ readonly maskChannel: number;
51
+
52
+ /** @internal The depth level used for mask tiles. See {@link WxAPIOptions} for more information. */
53
+ readonly maskDepth: number;
54
+
55
+ /** @internal An instance of the QTree object, used internally for spatial indexing. */
56
+ readonly qtree: QTree;
57
+
58
+ /**
59
+ * @internal
60
+ * A function that loads mask tiles asynchronously.
61
+ * @param x - The X coordinate of the tile.
62
+ * @param y - The Y coordinate of the tile.
63
+ * @param z - The zoom level of the tile.
64
+ * @returns A Promise that resolves to the {@link ImageData} of the tile.
65
+ */
66
+ readonly loadMaskFunc: ({ x, y, z }: XYZ) => Promise<ImageData>;
67
+
68
+ /** @ignore @internal a manager for all datasets. Operates with short datasets*/
69
+ protected readonly _allDatasetsManager: WxAllDatasetsManager;
70
+
71
+ /************************/
72
+ /* NO EXPORT PART END */
73
+ /************************/
74
+
75
+ /**
76
+ * Initializes a new instance of the {@link WxAPI} class.
77
+ * @param options - Configuration options for the WxAPI instance. See {@link WxAPIOptions}.
78
+ */
79
+ constructor({
80
+ dataServerURL,
81
+ ext = "webp",
82
+ maskURL = "none",
83
+ maskChannel = "R",
84
+ maskDepth = 11,
85
+ qtreeURL = "none",
86
+ requestInit,
87
+ colorStyles,
88
+ units,
89
+ colorSchemes,
90
+ }: WxAPIOptions) {
91
+ WXLOG("WxAPI.constructor", dataServerURL);
92
+ WxTilesLibSetup({ colorStyles, units, colorSchemes });
93
+
94
+ // compose auto-URLs for qtree and mask
95
+ if (qtreeURL === 'auto')
96
+ qtreeURL = `${dataServerURL}masks/11+1.seamask.qtree`;
97
+ if (maskURL === 'auto') maskURL = `${dataServerURL}masks/{z}/{x}/{y}.${ext}`;
98
+
99
+ this.dataServerURL = dataServerURL;
100
+ this.ext = ext;
101
+ this.requestInit = requestInit;
102
+
103
+ this.maskURL = maskURL;
104
+ this.maskChannel =
105
+ maskChannel === "R"
106
+ ? 0
107
+ : maskChannel === "G"
108
+ ? 1
109
+ : maskChannel === "B"
110
+ ? 2
111
+ : maskChannel === "A"
112
+ ? 3
113
+ : 0; // default to R
114
+ this.maskDepth = maskDepth;
115
+ if (this.maskURL !== "none") {
116
+ const maskloader = cacheUriPromise(loadImageData);
117
+ this.loadMaskFunc = (coord: XYZ) =>
118
+ maskloader(uriXYZ(this.maskURL, coord), requestInit);
119
+ } else {
120
+ this.loadMaskFunc = () =>
121
+ Promise.reject(new Error("maskURL not defined"));
122
+ }
123
+
124
+ this._allDatasetsManager = new WxAllDatasetsManager(this);
125
+ this.qtree = new QTree(qtreeURL, requestInit);
126
+
127
+ this.initDone = Promise.all([
128
+ this._allDatasetsManager.ready,
129
+ this.qtree.ready,
130
+ ]).then(() => WXLOG("WxAPI.constructor initDone"));
131
+ }
132
+
133
+ async createDatasetManagerOptions(datasetName: string): Promise<WxDataSetManagerOptions> {
134
+ await this.initDone;
135
+ await this._allDatasetsManager.updateOne(datasetName);
136
+ WXLOG("WxAPI.createDatasetManager", datasetName);
137
+ const datasetShortMeta =
138
+ this._allDatasetsManager.allDatasetsShortMetas[datasetName];
139
+ const datasetCurrentInstance = datasetShortMeta?.instance;
140
+ if (!datasetCurrentInstance)
141
+ throw new Error(`Dataset/instance not found:${datasetName}`);
142
+ const instanced = datasetShortMeta.instanced;
143
+ let metasArray: WxDatasetMeta[];
144
+ const preURL = `${this.dataServerURL + datasetName}/`;
145
+ try {
146
+ // using the new infrastructure with WxServer & WxAggregator
147
+ metasArray = await fetchJson<WxDatasetMeta[]>(
148
+ `${preURL}metafull.json`,
149
+ this.requestInit,
150
+ );
151
+ } catch (e) {
152
+ WXLOG("Can't load metafull.json. Trying old infrastructure with NGINX");
153
+ const instances = await fetchJson<string[]>(
154
+ `${preURL}instances.json`,
155
+ this.requestInit,
156
+ );
157
+ if (!Array.isArray(instances) || !instances.length)
158
+ throw new Error("instances.json is not an array or empty");
159
+ metasArray = await Promise.all(
160
+ instances
161
+ .reverse()
162
+ .map(async (instance) =>
163
+ Object.assign(
164
+ await fetchJson<WxDatasetMeta>(
165
+ `${preURL + instance}/meta.json`,
166
+ this.requestInit,
167
+ ),
168
+ { instance },
169
+ ),
170
+ ),
171
+ );
172
+ }
173
+
174
+ const datasetCurrentMeta = metasArray[0];
175
+ const metas =
176
+ instanced && new Map(metasArray.map((meta) => [meta.instance, meta]));
177
+ return {
178
+ datasetName,
179
+ datasetCurrentInstance,
180
+ datasetCurrentMeta,
181
+ instanced,
182
+ metas,
183
+ wxAPI: this,
184
+ };
185
+ }
186
+
187
+ /**
188
+ * Get all variables for the given dataset name.
189
+ * @param datasetName - dataset name
190
+ * @returns {Promise<string[]>} - list of all available variables for the dataset */
191
+ async getDatasetAllVariables(
192
+ datasetName: string,
193
+ ): Promise<string[] | undefined> {
194
+ await this.initDone;
195
+ WXLOG("WxAPI.getDatasetVariables", datasetName);
196
+ return this._allDatasetsManager.allDatasetsShortMetas[datasetName]
197
+ ?.variables;
198
+ }
199
+
200
+ /**
201
+ * Returns datasets names which have given variable
202
+ * @argument {string} varName - variable name to search for in datasets
203
+ * @returns {Promise<string[]>} - list of datasets' names */
204
+ async filterDatasetsByVariableName(varName: string): Promise<string[]> {
205
+ await this.initDone;
206
+ WXLOG("WxAPI.filterDatasetsByVariableName:", varName);
207
+ const { allDatasetsShortMetas } = this._allDatasetsManager;
208
+ return Object.keys(allDatasetsShortMetas).filter((dsName) =>
209
+ allDatasetsShortMetas[dsName]?.variables.includes(varName),
210
+ );
211
+ }
212
+
213
+ /**
214
+ * Get the list of all available datasets' names
215
+ * @returns {Promise<string[]>} - list of all available datasets' names
216
+ * */
217
+ async getAllDatasetsNames(): Promise<string[]> {
218
+ await this.initDone;
219
+ WXLOG("WxAPI.getAllDatasetsNames");
220
+ return Object.keys(this._allDatasetsManager.allDatasetsShortMetas);
221
+ }
222
+ }
@@ -0,0 +1,374 @@
1
+ import type { BaseDatasetManager } from "../managers/BaseDatasetManager";
2
+ import type { WxDatasetMeta, WxDate, WxEventType, WxLayerVarsNames, WxRasterData, WxRasterDataCache, WxRequestInit, WxVariableMeta } from "../types";
3
+ import {
4
+ WXLOG,
5
+ type WxColorStyleStrict,
6
+ type WxColorStyleWeak,
7
+ WxGetColorStyles,
8
+ type XYZ,
9
+ } from "../utils/wxtools";
10
+ import type { WxLayer } from "./wxlayer";
11
+
12
+ // biome-ignore lint/suspicious/noExplicitAny: necessary for mixins
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
+ // @ts-ignore
340
+ if (e.name === "AbortError") {
341
+ throw e;
342
+ }
343
+
344
+ // ...or, if the loadTile->loadImage->throw 'reason' is 'instance-not-found', try to update wxdatasetManager, then update the layer
345
+ // if we aren't in the middle of updating wxdatasetManager (others may initiate update as well)
346
+ // @ts-ignore
347
+ if (e.reason === "instance-not-found" && !this._needUpdateDSManager) {
348
+ this._needUpdateDSManager = true;
349
+ WXLOG(
350
+ `WxTileSource.loadTile (${this.id}) instance-not-found. Trying to update wxdatasetManager and load again.`,
351
+ );
352
+ // try to update wxdatasetManager. No need to await for it to finish
353
+ this.wxdatasetManager
354
+ .update() // attempt to update wxdatasetManager
355
+ .then(() => {
356
+ this._needUpdateDSManager = false;
357
+ this.setTime(this.getTime()) // reload tiles with new time close to the current time
358
+ .then(() => this.fire("changed", this)); // and fire 'changed' event
359
+ }) // update wxdatasetManager
360
+ .catch((e) => {
361
+ // it leaves needUpdateDSManager = true, so the layer will appear empty after failed update.
362
+ WXLOG(
363
+ `WxTileSource.loadTile (${this.id}) instance-not-found. wxdatasetManager update failed.`,
364
+ e,
365
+ );
366
+ });
367
+ } // if (e.reason === 'instance-not-found')
368
+ } // catch loadTile error
369
+ } // if (!this.needUpdateDSManager) - outer
370
+
371
+ return null;
372
+ }
373
+ };
374
+ }