@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/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/utils/wxtools.ts +22 -13
- 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
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
|
+
}
|
|
@@ -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
|
+
}
|