feat(weather): a host can supply decoded frames to the weather layers

A weather feed may also be a frame source. With an optional
getFrameImage({ product, time, size, bbox, signal }) the layers take
whole-extent images, detail windows and the global mosaic from it; with an
optional getFrameTile({ product, time, z, x, y, size, signal }) regional
tiles and their prefetch come from it, null being a transparent tile. Pixels
may be an ImageBitmap, ImageData, canvas or VideoFrame of exactly the
requested size; the layer draws them once and closes them. Cancellation
reaches the source, and a feed without these methods reads the image and
tile routes as before.
This commit is contained in:
Sameh Khamis committed 2026-11-10 21:54:08 -08:00
1 parent aff5ce7f17
commit e18353f769
12 files changed
+1068 -31

No files matched your search

+98
View File
@@ -1,3 +1,9 @@
import {
weatherPixelSize,
drawWeatherPixels,
closeWeatherPixels,
} from './source.js';
/** Tile a bounded decoded canvas without network requests or scene state. */
export function createRasterTileProvider({
cesium,
@@ -48,3 +54,95 @@ export function createRasterTileProvider({
},
};
}
/**
* Tile one product time from a host frame source. Every resolved tile is a
* canvas: supplied pixels are drawn into a new one and released, and a null
* tile is one shared transparent canvas. Beyond `maxConcurrent` tiles in
* flight requestImage returns undefined, Cesium's signal to ask again later. A
* failed tile rejects, which Cesium reports through errorEvent. Aborting
* `signal` cancels every tile in flight.
*/
export function createFrameTileProvider({
cesium,
frameSource,
product,
time,
tilingScheme,
rectangle = cesium.Rectangle.MAX_VALUE,
maximumLevel,
credit,
createCanvas,
tileSize = 256,
maxConcurrent = 6,
signal,
}) {
let inFlight = 0;
let empty = null;
const canvas = () => {
const tile = createCanvas();
tile.width = tile.height = tileSize;
return tile;
};
async function load(x, y, level, request) {
const controller = new AbortController();
const abort = () => controller.abort(signal.reason);
signal?.addEventListener('abort', abort, { once: true });
try {
signal?.throwIfAborted();
const pixels = await frameSource.getTile({
product,
time,
z: level,
x,
y,
size: tileSize,
signal: controller.signal,
});
if (pixels === null) {
controller.signal.throwIfAborted();
return (empty ??= canvas());
}
try {
controller.signal.throwIfAborted();
const { width, height } = weatherPixelSize(pixels);
if (width !== tileSize || height !== tileSize)
throw new Error('Invalid weather tile dimensions');
const tile = canvas();
drawWeatherPixels(tile.getContext('2d'), pixels);
return tile;
} finally {
closeWeatherPixels(pixels);
}
} catch (error) {
// A cancelled tile is unloaded, not failed, so Cesium may ask again.
if (controller.signal.aborted && request && cesium.RequestState)
request.state = cesium.RequestState.CANCELLED;
throw error;
} finally {
signal?.removeEventListener('abort', abort);
}
}
return {
tilingScheme,
rectangle,
tileWidth: tileSize,
tileHeight: tileSize,
minimumLevel: 0,
maximumLevel,
ready: true,
tileDiscardPolicy: undefined,
credit,
errorEvent: new cesium.Event(),
hasAlphaChannel: true,
getTileCredits: () => undefined,
pickFeatures: () => undefined,
requestImage(x, y, level, request) {
if (inFlight >= maxConcurrent) return undefined;
inFlight++;
return load(x, y, level, request).finally(() => {
inFlight--;
});
},
};
}