Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

WV-2947: EIC Updates - Measuring Individual Tile Layer Images (#4820)

Ryan Weilerf2d56843f1cc
diff --git a/web/js/app.js b/web/js/app.js
index e09a17cea..e2573fd26 100644
--- a/web/js/app.js
+++ b/web/js/app.js
@@ -72,7 +72,7 @@ class App extends React.Component {
kioskModeEnabled, notifications, numberOutagesUnseen,
} = this.props;
if (numberOutagesUnseen !== prevProps.numberOutagesUnseen) {
- if (numberOutagesUnseen > 0 && !kioskModeEnabled) {
+ if (numberOutagesUnseen > 0 && !kioskModeEnabled && process.env.NODE_ENV !== 'development') {
this.openNotification(notifications, numberOutagesUnseen);
}
}
diff --git a/web/js/mapUI/components/dev-test-mode/dev-console-test.js b/web/js/mapUI/components/dev-test-mode/dev-console-test.js
index 59d885438..9b0fdcc28 100644
--- a/web/js/mapUI/components/dev-test-mode/dev-console-test.js
+++ b/web/js/mapUI/components/dev-test-mode/dev-console-test.js
@@ -16,7 +16,6 @@ function ConsoleTest () {
console.log(map);
};
-
return (
<div className="d-flex flex-column justify-content-center align-items-center w-100 mt-3">
<div className="d-flex flex-row justify-content-center align-items-center">
diff --git a/web/js/mapUI/components/kiosk/tile-measurement/tile-measurement.js b/web/js/mapUI/components/kiosk/tile-measurement/tile-measurement.js
index 0732dd422..d7d05cb35 100644
--- a/web/js/mapUI/components/kiosk/tile-measurement/tile-measurement.js
+++ b/web/js/mapUI/components/kiosk/tile-measurement/tile-measurement.js
@@ -76,7 +76,7 @@ function TileMeasurement({ ui }) {
break;
}
} catch (error) {
- console.error(`Error while processing layer ${layers[j].id} for date ${dates[i]}: `, error);
+ console.error(`No WMS image tile available for layer ${layers[j].id} on date ${dates[i]}: `, error);
break;
}
}
@@ -87,7 +87,6 @@ function TileMeasurement({ ui }) {
layersMeetingThresholdForDate = 0;
}
-
const firstLayerWithBestDate = findBestDate(layers, bestDates);
if (!firstLayerWithBestDate) {
@@ -129,12 +128,50 @@ function TileMeasurement({ ui }) {
}
};
- const verifyTilesAndHandleErrors = (abortProceedure) => {
+ const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
+
+ const verifyTilesAndHandleErrors = async (abortProceedure) => {
console.log('Verifying tiles on map...');
- const tileCount = countTilesForSpecifiedLayers(ui, layersToMeasure);
- const loadedTiles = tileCount.totalLoadedTileCount > 0;
- console.log(`Total tiles loaded: ${tileCount.totalLoadedTileCount} and abortProceedure === ${abortProceedure}}`);
+ // most of these variables are purely for debugging purposes
+ let tileCount = 0;
+ let loadedTilesCount = 0;
+ let errorTilesCount = 0;
+ let emptyTilesCount = 0;
+ let totalTilesLoadedWithBadImageCount = 0;
+ let otherTileStates = [];
+
+ // In rare cases the TileLayer may not have finished loading tiles at the time of measurement
+ // We can verify this by checking the otherTileStates array for values of 1 that indicate that tiles were still loading
+ let retries = 0;
+ while (retries < 10) {
+ console.log('Attempt #', retries + 1, 'to verify tiles on map...');
+ const {
+ totalExpectedTileCount,
+ totalLoadedTileCount,
+ totalTilesLoadedWithBadImage,
+ totalErrorTiles,
+ totalEmptyTiles,
+ totalOtherTileStates,
+ } = countTilesForSpecifiedLayers(ui, layersToMeasure);
+ tileCount = totalExpectedTileCount;
+ loadedTilesCount = totalLoadedTileCount;
+ errorTilesCount = totalErrorTiles;
+ emptyTilesCount = totalEmptyTiles;
+ totalTilesLoadedWithBadImageCount = totalTilesLoadedWithBadImage;
+ otherTileStates = totalOtherTileStates;
+ if (loadedTilesCount === 0) {
+ retries += 1;
+ await delay(1000);
+ } else {
+ break;
+ }
+ }
+
+ const loadedTiles = loadedTilesCount > 0;
+ const tileStatus = `Out of an expected ${tileCount} tiles, ${loadedTilesCount} were loaded. There were ${totalTilesLoadedWithBadImageCount} tiles loaded with bad images, ${errorTilesCount} error tiles, and ${emptyTilesCount} empty tiles. There were ${otherTileStates.length} other tile states: ${otherTileStates.join(', ')}`;
+ console.log(tileStatus);
+ console.log('LoadedTiles === ', loadedTiles);
if ((eic === 'da' || eic === 'sa') && !abortProceedure) {
setEICMeasurementComplete();
@@ -156,11 +193,8 @@ function TileMeasurement({ ui }) {
// #1 Parent function that is called from useEffect.
const calculateMeasurements = async () => {
+ console.log('Entering EIC mode...');
try {
- console.log('Entering EIC mode...');
-
- setMeasurementsStarted(true);
-
const measurementLayers = findLayersToMeasure();
if (!measurementLayers.length) {
console.error('No layers found to be measured... Aborting...');
@@ -194,9 +228,10 @@ function TileMeasurement({ ui }) {
useEffect(() => {
if (!measurementsStarted && activeLayers && eic && ui.selected) {
+ setMeasurementsStarted(true);
calculateMeasurements();
}
- });
+ }, [ui.selected]);
return null;
}
diff --git a/web/js/mapUI/components/kiosk/tile-measurement/utils/verify-map-tiles.js b/web/js/mapUI/components/kiosk/tile-measurement/utils/verify-map-tiles.js
index 1dc17dc67..ba8f08017 100644
--- a/web/js/mapUI/components/kiosk/tile-measurement/utils/verify-map-tiles.js
+++ b/web/js/mapUI/components/kiosk/tile-measurement/utils/verify-map-tiles.js
@@ -1,7 +1,40 @@
import TileLayer from 'ol/layer/Tile';
import { transformExtent } from 'ol/proj';
-// Helper function to process the tile layer
+// We cannot rely on the invidual tile's state to determine if the image truely loaded with imagery
+// We draw the image to a canvas and check the pixels to see if it is a single color or transparent
+const checkTileImage = (image) => {
+ if (image.complete && image.naturalWidth !== 0) {
+ const canvas = document.createElement('canvas');
+ const context = canvas.getContext('2d');
+ canvas.width = image.width;
+ canvas.height = image.height;
+ context.drawImage(image, 0, 0);
+
+ const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
+ const { data } = imageData;
+
+ let isSingleColor = true;
+ let isTransparent = true;
+ const firstPixel = [data[0], data[1], data[2], data[3]];
+
+ for (let i = 0; i < data.length; i += 4) {
+ if (data[i + 3] !== 0) { // If alpha is not 0
+ isTransparent = false;
+ if (data[i] !== firstPixel[0] || data[i + 1] !== firstPixel[1] || data[i + 2] !== firstPixel[2] || data[i + 3] !== firstPixel[3]) {
+ isSingleColor = false;
+ break;
+ }
+ }
+ }
+
+ return isSingleColor || isTransparent;
+ }
+ console.log('Image invalid.');
+ return false;
+};
+
+// Process each individual tile for a given TileLayer
const processTileLayer = (layer, map, view) => {
const source = layer.getSource();
const size = map.getSize();
@@ -13,17 +46,40 @@ const processTileLayer = (layer, map, view) => {
const resolution = view.getResolutionForZoom(zoom);
const currentZ = tileGrid.getZForResolution(resolution);
- let expectedTileCount = 0;
- let loadedTileCount = 0;
+ /*
+ 0 - ol.TileState.IDLE: The tile has not started loading yet. It is in the initial state.
+ 1 - ol.TileState.LOADING: The tile is currently in the process of loading.
+ 2 - ol.TileState.LOADED: The tile has finished loading successfully.
+ 3 - ol.TileState.ERROR: There was an error in loading the tile.
+ 4 - ol.TileState.EMPTY: The tile is empty, indicating there's no data for this tile.
+ 5 - ol.TileState.ABORT: The loading of the tile was aborted.
+ */
+
+ let expectedTileCount = 0; // all tiles
+ let loadedTileCount = 0; // 2
+ let tilesLoadedWithBadImage = 0; // 2 but image is invalid
+ let errorTiles = 0; // 3
+ let emptyTiles = 0; // 4
+ const otherTileStates = [];
- const tileCoordFunction = (tileCoord) => {
+ const tileCoordFunction = async (tileCoord) => {
const tile = source.getTile(tileCoord[0], tileCoord[1], tileCoord[2], 1, sourceProjection);
const tileState = tile.getState();
+ expectedTileCount += 1;
if (tileState === 2) {
- loadedTileCount += 1;
- expectedTileCount += 1;
+ const image = tile.image_;
+ const isTileImageValid = checkTileImage(image);
+ if (!isTileImageValid) {
+ loadedTileCount += 1;
+ } else {
+ tilesLoadedWithBadImage += 1;
+ }
} else if (tileState === 3) {
- expectedTileCount += 1;
+ errorTiles += 1;
+ } else if (tileState === 4) {
+ emptyTiles += 1;
+ } else {
+ otherTileStates.push(tileState);
}
};
@@ -32,33 +88,48 @@ const processTileLayer = (layer, map, view) => {
return {
expectedTileCount,
loadedTileCount,
+ errorTiles,
+ emptyTiles,
+ otherTileStates,
+ tilesLoadedWithBadImage,
};
};
export default function countTilesForSpecifiedLayers (ui, layersToCheck) {
const map = ui.selected;
const view = map.getView();
- const layers = map.getLayers().getArray();
- const matchingLayers = layers.filter((layer) => {
- if (layer.wv && layer.wv.id) {
- const layerId = layer.wv.id;
- return layersToCheck.includes(layerId);
- }
- return false;
- });
-
+ // Match only the layers that are in the layersToMeasure array
+ const matchingLayers = map.getLayers().getArray().filter((layer) => layer.wv && layer.wv.id && layersToCheck.includes(layer.wv.id));
let totalExpectedTileCount = 0;
let totalLoadedTileCount = 0;
+ let totalTilesLoadedWithBadImage = 0;
+ let totalErrorTiles = 0;
+ let totalEmptyTiles = 0;
+ let totalOtherTileStates = [];
+ // Layers can be a single TileLayer or a LayerGroup consisting of 2 or more TileLayers
const processLayer = (layer) => {
if (layer instanceof TileLayer) {
- const { expectedTileCount, loadedTileCount } = processTileLayer(layer, map, view);
+ const {
+ expectedTileCount,
+ loadedTileCount,
+ errorTiles,
+ emptyTiles,
+ otherTileStates,
+ tilesLoadedWithBadImage,
+ } = processTileLayer(layer, map, view);
totalExpectedTileCount += expectedTileCount;
totalLoadedTileCount += loadedTileCount;
+ totalTilesLoadedWithBadImage += tilesLoadedWithBadImage;
+ totalErrorTiles += errorTiles;
+ totalEmptyTiles += emptyTiles;
+ totalOtherTileStates = totalOtherTileStates.concat(otherTileStates);
} else if (layer.getLayers) {
const subLayers = layer.getLayers().getArray();
subLayers.forEach(processLayer);
+ } else {
+ console.error('Layer is not an instance of a TileLayer or LayerGroup');
}
};
@@ -67,5 +138,9 @@ export default function countTilesForSpecifiedLayers (ui, layersToCheck) {
return {
totalExpectedTileCount,
totalLoadedTileCount,
+ totalTilesLoadedWithBadImage,
+ totalErrorTiles,
+ totalEmptyTiles,
+ totalOtherTileStates,
};
}