Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

WV-2974 optimize map rendering (#5261)

Patrick Moulden9bc1f310d32b
diff --git a/web/js/mapUI/components/update-collections/updateCollections.js b/web/js/mapUI/components/update-collections/updateCollections.js
index 73cbc106f..e63dd0a2a 100644
--- a/web/js/mapUI/components/update-collections/updateCollections.js
+++ b/web/js/mapUI/components/update-collections/updateCollections.js
@@ -55,9 +55,8 @@ function UpdateCollections () {
} catch (error) {
// errors will clutter console, turn this on for debugging
// console.error(error);
+ throw new Error('Failed to query headers');
}
- // Return undefined if query fails
- return undefined;
};
const findLayerCollections = (layers, dailyDate, subdailyDate, forceUpdate) => {
@@ -83,8 +82,8 @@ function UpdateCollections () {
const headerPromises = layersToUpdate.map((layer) => getHeaders(layer, selectedDate));
try {
- const results = await Promise.all(headerPromises);
- const validCollections = results.filter((result) => result !== undefined);
+ const results = await Promise.allSettled(headerPromises);
+ const validCollections = results.filter(({ status, value }) => status === 'fulfilled' && value).map(({ value }) => value);
updateCollection(validCollections);
} catch (error) {
// errors will clutter console, turn this on for debugging
diff --git a/web/js/mapUI/components/update-date/updateDate.js b/web/js/mapUI/components/update-date/updateDate.js
index 272480f47..91ca5b1ad 100644
--- a/web/js/mapUI/components/update-date/updateDate.js
+++ b/web/js/mapUI/components/update-date/updateDate.js
@@ -79,11 +79,7 @@ function UpdateDate(props) {
const layers = mapLayerCollection.getArray();
const activeLayers = getAllActiveLayers(state);
- const visibleLayers = activeLayers.filter(
- ({ id }) => layers
- .map(({ wv }) => lodashGet(wv, 'def.id'))
- .includes(id),
- ).filter(({ visible }) => visible);
+ const visibleLayers = activeLayers.filter(({ id, visible }) => layers.findIndex(({ wv }) => wv?.def?.id === id) !== -1 && visible);
const layerPromises = visibleLayers.map(async (def) => {
const { id, type } = def;
@@ -107,7 +103,7 @@ function UpdateDate(props) {
updateVectorStyles(def);
}
});
- await Promise.all(layerPromises);
+ await Promise.allSettled(layerPromises);
updateLayerVisibilities();
if (!outOfStepChange) {
preloadNextTiles();
@@ -125,11 +121,9 @@ function UpdateDate(props) {
});
}
return updateDate(action.outOfStep);
- } if (action.type === layerConstants.TOGGLE_LAYER_VISIBILITY || action.type === layerConstants.TOGGLE_OVERLAY_GROUP_VISIBILITY) {
- const outOfStep = false;
- // if date not changing we do not want to recreate titiler layer
- const skipTitiler = true;
- return updateDate(outOfStep, skipTitiler);
+ }
+ if (action.type === layerConstants.TOGGLE_LAYER_VISIBILITY || action.type === layerConstants.TOGGLE_OVERLAY_GROUP_VISIBILITY) {
+ return updateLayerVisibilities();
}
};
diff --git a/web/js/mapUI/components/update-projection/updateProjection.js b/web/js/mapUI/components/update-projection/updateProjection.js
index 0ab19e479..143479fec 100644
--- a/web/js/mapUI/components/update-projection/updateProjection.js
+++ b/web/js/mapUI/components/update-projection/updateProjection.js
@@ -129,7 +129,8 @@ function UpdateProjection(props) {
const options = getGranuleOptions(layerState, def, compare.activeString, granuleOptions);
return createLayer(def, options);
});
- const createdLayers = await Promise.all(layerPromises);
+ const layerResults = await Promise.allSettled(layerPromises);
+ const createdLayers = layerResults.filter(({ status }) => status === 'fulfilled').map(({ value }) => value);
mapUI.setLayers(createdLayers);
} else {
const stateArray = [['active', 'selected'], ['activeB', 'selectedB']];
diff --git a/web/js/ol/lookupimagetile.js b/web/js/ol/lookupimagetile.js
index 778003251..5a865ba01 100644
--- a/web/js/ol/lookupimagetile.js
+++ b/web/js/ol/lookupimagetile.js
@@ -42,7 +42,7 @@ class LookupImageTile extends OlImageTile {
LookupImageTile.prototype.getImage = function() {
return this.canvas_;
};
-LookupImageTile.prototype.load = function() {
+LookupImageTile.prototype.load = async function() {
if (this.state === OlTileState.IDLE) {
this.state = OlTileState.LOADING;
const that = this;
@@ -130,65 +130,67 @@ LookupImageTile.prototype.load = function() {
if (lookupCount > 1 && lookupCount < 25) {
imageProcessed = true;
const pixelsToDisplay = getPixelColorsToDisplay(this.lookup_);
- fetch(this.src_)
- .then((response) => response.arrayBuffer())
- .then((buffer) => {
- // decode the buffer PNG file
- const decodedPNG = UPNG.decode(buffer);
- const { width, height } = decodedPNG;
-
- // Create an array buffer matching the pixel dimensions of the provided image
- const bufferSize = height * width * 4;
- const arrBuffer = new Uint32Array(bufferSize);
-
- // Extract the colormap values. This is an array of integers representing rgba values.
- // Used in sets of 4 (i.e. colorMapArr[0] = r, colorMapArr[1] = b, etc.)
- // colorMapArr assumes a max of 256 colors
- const colorMapArr = getColormap(decodedPNG.tabs.PLTE);
-
- // Extract the pixel data. This is an array of integers corresponding to the colormap
- // i.e. if pixelData[0] == 5, this pixel is the color of the 5th entry in the colormap
- const pixelData = decodedPNG.data;
-
- // iterate through the pixelData, drawing each pixel using the appropriate color
- for (let i = 0; i < pixelData.length; i += 1) {
- const arrBuffIndex = i * 4;
- const lookupIndex = pixelData[i] * 4;
-
- // Determine desired RGBA for this pixel
- const r = colorMapArr[lookupIndex];
- const g = colorMapArr[lookupIndex + 1];
- const b = colorMapArr[lookupIndex + 2];
- const a = 255;
- // Concatentate to 'r,g,b,a' string & check if that color is in the pixelsToDisplay array
- const rgbaStr = `${r},${g},${b},${a}`;
- const drawThisColor = pixelsToDisplay[rgbaStr];
-
- // If the intended color exists in pixelsToDisplay obj, draw that color, otherwise draw transparent
- if (drawThisColor !== undefined) {
- arrBuffer[arrBuffIndex + 0] = r;
- arrBuffer[arrBuffIndex + 1] = g;
- arrBuffer[arrBuffIndex + 2] = b;
- arrBuffer[arrBuffIndex + 3] = a;
- } else {
- // console.log('drawThisColor undefined, rgbaStr:', rgbaStr);
- arrBuffer[arrBuffIndex] = 0;
- arrBuffer[arrBuffIndex + 1] = 0;
- arrBuffer[arrBuffIndex + 2] = 0;
- arrBuffer[arrBuffIndex + 3] = 0;
- }
+ try {
+ const res = await fetch(this.src_);
+ if (!res.ok) throw new Error(`Failed to fetch image: ${res.status} ${res.statusText}`);
+ const buffer = await res.arrayBuffer();
+ // decode the buffer PNG file
+ const decodedPNG = UPNG.decode(buffer);
+ const { width, height } = decodedPNG;
+
+ // Create an array buffer matching the pixel dimensions of the provided image
+ const bufferSize = height * width * 4;
+ const arrBuffer = new Uint32Array(bufferSize);
+
+ // Extract the colormap values. This is an array of integers representing rgba values.
+ // Used in sets of 4 (i.e. colorMapArr[0] = r, colorMapArr[1] = b, etc.)
+ // colorMapArr assumes a max of 256 colors
+ const colorMapArr = getColormap(decodedPNG.tabs.PLTE);
+
+ // Extract the pixel data. This is an array of integers corresponding to the colormap
+ // i.e. if pixelData[0] == 5, this pixel is the color of the 5th entry in the colormap
+ const pixelData = decodedPNG.data;
+
+ // iterate through the pixelData, drawing each pixel using the appropriate color
+ for (let i = 0; i < pixelData.length; i += 1) {
+ const arrBuffIndex = i * 4;
+ const lookupIndex = pixelData[i] * 4;
+
+ // Determine desired RGBA for this pixel
+ const r = colorMapArr[lookupIndex];
+ const g = colorMapArr[lookupIndex + 1];
+ const b = colorMapArr[lookupIndex + 2];
+ const a = 255;
+ // Concatentate to 'r,g,b,a' string & check if that color is in the pixelsToDisplay array
+ const rgbaStr = `${r},${g},${b},${a}`;
+ const drawThisColor = pixelsToDisplay[rgbaStr];
+
+ // If the intended color exists in pixelsToDisplay obj, draw that color, otherwise draw transparent
+ if (drawThisColor !== undefined) {
+ arrBuffer[arrBuffIndex + 0] = r;
+ arrBuffer[arrBuffIndex + 1] = g;
+ arrBuffer[arrBuffIndex + 2] = b;
+ arrBuffer[arrBuffIndex + 3] = a;
+ } else {
+ // console.log('drawThisColor undefined, rgbaStr:', rgbaStr);
+ arrBuffer[arrBuffIndex] = 0;
+ arrBuffer[arrBuffIndex + 1] = 0;
+ arrBuffer[arrBuffIndex + 2] = 0;
+ arrBuffer[arrBuffIndex + 3] = 0;
}
+ }
- // Encode the image, creating a new PNG file
- const encodedBufferImage = UPNG.encode([arrBuffer], decodedPNG.width, decodedPNG.height, decodedPNG.depth);
- const blob = new Blob([encodedBufferImage], { type: 'image/png' });
- const dataURL = `${URL.createObjectURL(blob)}`;
- this.image_.src = dataURL;
- this.image_.addEventListener('load', onImageLoad);
- })
- .catch((error) => {
- console.error('Error:', error);
- });
+ // Encode the image, creating a new PNG file
+ const encodedBufferImage = UPNG.encode([arrBuffer], decodedPNG.width, decodedPNG.height, decodedPNG.depth);
+ const blob = new Blob([encodedBufferImage], { type: 'image/png' });
+ const dataURL = `${URL.createObjectURL(blob)}`;
+ this.image_.src = dataURL;
+ this.image_.addEventListener('load', onImageLoad);
+ } catch (error) {
+ that.state = OlTileState.ERROR;
+ that.changed();
+ console.error('Error:', error);
+ }
} else {
this.image_.src = this.src_;
this.image_.addEventListener('load', onImageLoad);