|
5 | 5 | * Functions and constants for handling webgl/luma.gl/deck.gl entities |
6 | 6 | */ |
7 | 7 |
|
8 | | -import {parse, fetchFile, load} from '@loaders.gl/core'; |
| 8 | +import {parse, fetchFile, load, FetchError} from '@loaders.gl/core'; |
9 | 9 | import {ImageLoader} from '@loaders.gl/images'; |
10 | 10 | import {NPYLoader} from '@loaders.gl/textures'; |
11 | 11 | import GL from '@luma.gl/constants'; |
12 | 12 | import {Texture2DProps} from '@luma.gl/webgl'; |
13 | 13 |
|
| 14 | +import {sleep} from '@kepler.gl/common-utils'; |
14 | 15 | import {getLoaderOptions} from '@kepler.gl/constants'; |
15 | 16 | import {RasterWebGL} from '@kepler.gl/deckgl-layers'; |
| 17 | +import {getApplicationConfig} from '@kepler.gl/utils'; |
16 | 18 |
|
17 | 19 | type ShaderModule = RasterWebGL.ShaderModule; |
18 | 20 | const { |
@@ -51,6 +53,7 @@ import { |
51 | 53 | NPYLoaderResponse, |
52 | 54 | RenderSubLayersProps |
53 | 55 | } from './types'; |
| 56 | +import {getRequestThrottle} from './request-throttle'; |
54 | 57 |
|
55 | 58 | /** |
56 | 59 | * Describe WebGL2 Texture parameters to use for given input data type |
@@ -260,81 +263,100 @@ type LoadingOptions = { |
260 | 263 | * @return image object to pass to Texture2D constructor |
261 | 264 | */ |
262 | 265 | export async function loadNpyArray( |
263 | | - request: {url: string; options: RequestInit}, |
| 266 | + request: {url: string; rasterServerUrl: string; options: RequestInit}, |
264 | 267 | split: true, |
265 | 268 | options?: LoadingOptions |
266 | 269 | ): Promise<Texture2DProps[] | null>; |
267 | 270 | export async function loadNpyArray( |
268 | | - request: {url: string; options: RequestInit}, |
| 271 | + request: {url: string; rasterServerUrl: string; options: RequestInit}, |
269 | 272 | split: false, |
270 | 273 | options?: LoadingOptions |
271 | 274 | ): Promise<Texture2DProps | null>; |
272 | 275 | export async function loadNpyArray( |
273 | | - request: {url: string; options: RequestInit}, |
| 276 | + request: {url: string; rasterServerUrl: string; options: RequestInit}, |
274 | 277 | split: boolean, |
275 | 278 | options?: LoadingOptions |
276 | 279 | ): Promise<Texture2DProps | Texture2DProps[] | null> { |
277 | | - try { |
278 | | - const {npy: npyOptions} = getLoaderOptions(); |
279 | | - const response: NPYLoaderResponse = await load(request.url, NPYLoader, { |
280 | | - npy: npyOptions, |
281 | | - fetch: options?.fetch |
282 | | - }); |
283 | | - |
284 | | - if (!response || !response.data || request.options.signal?.aborted) { |
285 | | - return null; |
| 280 | + const numAttempts = 1 + getApplicationConfig().rasterServerMaxRetries; |
| 281 | + |
| 282 | + const asset = await getRequestThrottle().throttleRequest(request.rasterServerUrl, async () => { |
| 283 | + for (let attempt = 0; attempt < numAttempts; attempt++) { |
| 284 | + try { |
| 285 | + const {npy: npyOptions} = getLoaderOptions(); |
| 286 | + const response: NPYLoaderResponse = await load(request.url, NPYLoader, { |
| 287 | + npy: npyOptions, |
| 288 | + fetch: options?.fetch |
| 289 | + }); |
| 290 | + |
| 291 | + if (!response || !response.data || request.options.signal?.aborted) { |
| 292 | + return null; |
| 293 | + } |
| 294 | + |
| 295 | + // Float64 data needs to be coerced to Float32 for the GPU |
| 296 | + if (response.data instanceof Float64Array) { |
| 297 | + response.data = Float32Array.from(response.data); |
| 298 | + } |
| 299 | + |
| 300 | + const {data, header} = response; |
| 301 | + const {shape} = header; |
| 302 | + const {format, dataFormat, type} = getWebGL2TextureParameters(data); |
| 303 | + |
| 304 | + // TODO: check height-width or width-height |
| 305 | + // Regardless, images usually square |
| 306 | + // TODO: handle cases of 256x256x1 instead of 1x256x256 |
| 307 | + const [z, height, width] = shape; |
| 308 | + |
| 309 | + // Since we now use WebGL2 data types for 8-bit textures, we set the following for all textures |
| 310 | + const mipmaps = false; |
| 311 | + const parameters = DEFAULT_HIGH_BIT_TEXTURE_PARAMETERS; |
| 312 | + |
| 313 | + if (!split) { |
| 314 | + return { |
| 315 | + data, |
| 316 | + width, |
| 317 | + height, |
| 318 | + format, |
| 319 | + dataFormat, |
| 320 | + type, |
| 321 | + parameters, |
| 322 | + mipmaps |
| 323 | + }; |
| 324 | + } |
| 325 | + |
| 326 | + // Split into individual arrays |
| 327 | + const channels: Texture2DProps[] = []; |
| 328 | + const channelSize = height * width; |
| 329 | + for (let i = 0; i < z; i++) { |
| 330 | + channels.push({ |
| 331 | + data: data.subarray(i * channelSize, (i + 1) * channelSize), |
| 332 | + width, |
| 333 | + height, |
| 334 | + format, |
| 335 | + dataFormat, |
| 336 | + type, |
| 337 | + parameters, |
| 338 | + mipmaps |
| 339 | + }); |
| 340 | + } |
| 341 | + return channels; |
| 342 | + } catch (error) { |
| 343 | + // Retry if Service Temporarily Unavailable 503 error etc. |
| 344 | + if ( |
| 345 | + attempt < numAttempts && |
| 346 | + error instanceof FetchError && |
| 347 | + getApplicationConfig().rasterServerServerErrorsToRetry?.includes( |
| 348 | + error.response?.status as number |
| 349 | + ) |
| 350 | + ) { |
| 351 | + await sleep(getApplicationConfig().rasterServerRetryDelay); |
| 352 | + continue; |
| 353 | + } |
| 354 | + } |
286 | 355 | } |
287 | | - |
288 | | - // Float64 data needs to be coerced to Float32 for the GPU |
289 | | - if (response.data instanceof Float64Array) { |
290 | | - response.data = Float32Array.from(response.data); |
291 | | - } |
292 | | - |
293 | | - const {data, header} = response; |
294 | | - const {shape} = header; |
295 | | - const {format, dataFormat, type} = getWebGL2TextureParameters(data); |
296 | | - |
297 | | - // TODO: check height-width or width-height |
298 | | - // Regardless, images usually square |
299 | | - // TODO: handle cases of 256x256x1 instead of 1x256x256 |
300 | | - const [z, height, width] = shape; |
301 | | - |
302 | | - // Since we now use WebGL2 data types for 8-bit textures, we set the following for all textures |
303 | | - const mipmaps = false; |
304 | | - const parameters = DEFAULT_HIGH_BIT_TEXTURE_PARAMETERS; |
305 | | - |
306 | | - if (!split) { |
307 | | - return { |
308 | | - data, |
309 | | - width, |
310 | | - height, |
311 | | - format, |
312 | | - dataFormat, |
313 | | - type, |
314 | | - parameters, |
315 | | - mipmaps |
316 | | - }; |
317 | | - } |
318 | | - |
319 | | - // Split into individual arrays |
320 | | - const channels: Texture2DProps[] = []; |
321 | | - const channelSize = height * width; |
322 | | - for (let i = 0; i < z; i++) { |
323 | | - channels.push({ |
324 | | - data: data.subarray(i * channelSize, (i + 1) * channelSize), |
325 | | - width, |
326 | | - height, |
327 | | - format, |
328 | | - dataFormat, |
329 | | - type, |
330 | | - parameters, |
331 | | - mipmaps |
332 | | - }); |
333 | | - } |
334 | | - return channels; |
335 | | - } catch { |
336 | 356 | return null; |
337 | | - } |
| 357 | + }); |
| 358 | + |
| 359 | + return asset; |
338 | 360 | } |
339 | 361 |
|
340 | 362 | /** |
|
0 commit comments