Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

WV-2708: Animation Date Range Check (#4340)

Ryan Weilerf5c2313fba33
diff --git a/web/js/components/kiosk/animation-tile-check/animation-tile-check.js b/web/js/components/kiosk/animation-tile-check/animation-tile-check.js
index 7a4938cbd..6448e91a4 100644
--- a/web/js/components/kiosk/animation-tile-check/animation-tile-check.js
+++ b/web/js/components/kiosk/animation-tile-check/animation-tile-check.js
@@ -14,28 +14,34 @@ function AnimationTileCheck(props) {
isPlaying,
} = props;
- const { activeString } = useSelector((state) => ({
- activeString: state.compare.activeString,
+ const {
+ config, proj, zoom, activeLayers,
+ } = useSelector((state) => ({
+ config: state.config,
+ proj: state.proj.selected,
+ zoom: Math.floor(state.map.ui.selected.getView().getZoom()),
+ activeLayers: getActiveLayers(state, state.compare.activeString).map((layer) => layer),
}));
- const activeLayers = useSelector((state) => getActiveLayers(state, activeString).map((layer) => layer));
const [frameDates, setFrameDates] = useState([]);
+ // This component is only renered when kioskMode is active & eic is subdaily or daily animation
+ // This means we can listen to the animation play trigger once the tileError check has completed
useEffect(() => {
if (isPlaying) getFrameDates();
}, [isPlaying]);
- // get an array of each frame date for duration of animation
+ // Get an array of each frame date for duration of animation
function determineFrameDates() {
const getNextDate = (date) => util.dateAdd(date, interval, delta);
const frameDatesArray = [];
let frameDate = startDate;
- // this puts an extra '.000Z' on the end of the date string
- frameDatesArray.push(frameDate.toISOString());
+
+ frameDatesArray.push(frameDate);
while (frameDate < endDate) {
frameDate = getNextDate(frameDate);
- frameDatesArray.push(frameDate.toISOString());
+ frameDatesArray.push(frameDate);
}
return frameDatesArray;
}
@@ -46,7 +52,7 @@ function AnimationTileCheck(props) {
}
return (
- <DateRangeTileCheck frameDates={frameDates} activeLayers={activeLayers} />
+ <DateRangeTileCheck frameDates={frameDates} activeLayers={activeLayers} config={config} proj={proj} zoom={zoom} />
);
}
diff --git a/web/js/components/kiosk/animation-tile-check/date-range-tile-check.js b/web/js/components/kiosk/animation-tile-check/date-range-tile-check.js
index f82f4287e..a5bbeeb98 100644
--- a/web/js/components/kiosk/animation-tile-check/date-range-tile-check.js
+++ b/web/js/components/kiosk/animation-tile-check/date-range-tile-check.js
@@ -1,92 +1,255 @@
-// import React, { useEffect, useState } from 'react';
-// import { useSelector, useDispatch } from 'react-redux';
-// import OlTileGridWMTS from 'ol/tilegrid/WMTS';
-// import OlSourceWMTS from 'ol/source/WMTS';
-// import OlLayerTile from 'ol/layer/Tile';
-// import TileState from 'ol/TileState';
-// import OlTileGridTileGrid from 'ol/tilegrid/TileGrid';
-// import PropTypes from 'prop-types';
+/* eslint-disable quote-props */
+import { useEffect } from 'react';
+import OlTileGridWMTS from 'ol/tilegrid/WMTS';
+import OlSourceWMTS from 'ol/source/WMTS';
+import PropTypes from 'prop-types';
+import { useDispatch } from 'react-redux';
+import { toggleCheckedAnimationAvailability as toggleCheckedAnimationAvailabilityAction } from '../../../modules/ui/actions';
+import util from '../../../util/util';
+import {
+ LEFT_WING_EXTENT, RIGHT_WING_EXTENT, LEFT_WING_ORIGIN, RIGHT_WING_ORIGIN,
+} from '../../../modules/map/constants';
+
+// Layers to be checked for tile availability need to have manually determined content-length thresholds.
+// Any active layers that are not listed in this object will NOT be checked for tile availability.
+// This represents a TOTAL content-length for all tiles for a single date for a single layer
+// To find a good value for the total content-length:
+// 1. Find a visibly verifiabled date range where the layer has full tiles for the entire range
+// 2. Add the layer id to the contentLengthThresholds object below with a value of 1
+// 3. Place a console statement in getAvailability() for console.log(resolvedLayersAvailability)
+// 4. Create a permalink with the kiosk parameter with kiosk=true && (eic='sa' || eic='da')
+// 5. Use the permalink & open the console
+// 6. Look through the conentLengthSum values for each layer/date to find a good value
+// Currently can only be used for WMTS tiles.
+const contentLengthThresholds = {
+ 'GOES-East_ABI_GeoColor': 160000,
+ 'GOES-West_ABI_GeoColor': 160000,
+ 'AMSRU2_Sea_Ice_Concentration_12km': 50000,
+};
function DateRangeTileCheck(props) {
- // const {
- // frameDates,
- // activeLayers,
- // } = props;
-
- // useEffect(() => {
- // if (frameDates.length){
- // // console.log('frameDates', frameDates, 'activeLayers', activeLayers)
- // parentFunction(activeLayers)
- // }
-
- // }, [frameDates])
-
- // function checkTileAvailability(url) {
- // return new Promise((resolve, reject) => {
- // const img = new Image();
- // img.onload = () => {
- // resolve(true);
- // };
- // img.onerror = () => {
- // resolve(false);
- // };
- // img.src = url;
- // });
- // }
-
- // const wmtsUrlFunction = (layer, date) => async function(tile, src) {
-
- // }
-
- // // #3 child function that accepts an array of frame dates and a layer and returns availability for the range
- // async function checkTilesForDates(dates, layer) {
- // const availability = {
- // availableTiles: [],
- // unavailableTiles: [],
- // };
-
- // for (const date of dates) {
- // const tileUrl = wmtsUrlFunction(layer, date); // Replace with your tile URL generation logic
- // const isAvailable = await checkTileAvailability(tileUrl);
-
- // if (isAvailable) {
- // availability.availableTiles.push(date);
- // } else {
- // availability.unavailableTiles.push(date);
- // }
- // }
- // return availability;
- // }
-
- // // #2 child function that accepts one layer and returns results for that layer
- // async function getAvailabilityForLayer(layer) {
- // console.log(layer)
-
- // const availability = await checkTilesForDates(frameDates, layer);
-
- // const id = layer.id
-
- // const layerAvailability = {
- // [id]: availability,
- // };
-
-
- // return layerAvailability
- // }
-
- // // parent function that accepts activeLayers and returns final results
- // function parentFunction(activeLayers) {
-
- // const allLayersAvailability = activeLayers.map((layer) => {
- // const layerAvailability = getAvailabilityForLayer(layer)
- // return layerAvailability;
- // })
-
- // console.log(allLayersAvailability)
- // }
-
+ const {
+ frameDates,
+ activeLayers,
+ config,
+ proj,
+ zoom,
+ } = props;
+
+ const dispatch = useDispatch();
+ const toggleCheckedAnimationAvailability = (available) => { dispatch(toggleCheckedAnimationAvailabilityAction(available)); };
+
+ useEffect(() => {
+ if (frameDates.length) {
+ getAvailability();
+ }
+ }, [frameDates]);
+
+ const calcExtentsFromLimits = (matrixSet, matrixSetLimits, day) => {
+ let extent;
+ let origin;
+
+ switch (day) {
+ case 1:
+ extent = LEFT_WING_EXTENT;
+ origin = LEFT_WING_ORIGIN;
+ break;
+ case -1:
+ extent = RIGHT_WING_EXTENT;
+ origin = RIGHT_WING_ORIGIN;
+ break;
+ default:
+ extent = proj.maxExtent;
+ origin = [extent[0], extent[3]];
+ break;
+ }
+
+ const resolutionLen = matrixSet.resolutions.length;
+ const setlimitsLen = matrixSetLimits && matrixSetLimits.length;
+
+ if (!matrixSetLimits || setlimitsLen !== resolutionLen || day) {
+ return { origin, extent };
+ }
+
+ const limitIndex = resolutionLen - 1;
+ const resolution = matrixSet.resolutions[limitIndex];
+ const tileWidth = matrixSet.tileSize[0] * resolution;
+ const tileHeight = matrixSet.tileSize[1] * resolution;
+ const {
+ minTileCol,
+ maxTileRow,
+ maxTileCol,
+ minTileRow,
+ } = matrixSetLimits[limitIndex];
+ const minX = extent[0] + (minTileCol * tileWidth);
+ const minY = extent[3] - ((maxTileRow + 1) * tileHeight);
+ const maxX = extent[0] + ((maxTileCol + 1) * tileWidth);
+ const maxY = extent[3] - (minTileRow * tileHeight);
+
+ return {
+ origin,
+ extent: [minX, minY, maxX, maxY],
+ };
+ };
+
+ const requestTilesWMTS = async (def, date) => {
+ const {
+ id, format, matrixIds, matrixSetLimits, projections, style, type,
+ } = def;
+
+ let contentLengthSum = 0;
+
+ const isGranule = type === 'granule';
+ const projectionsAttributes = projections[proj.id];
+ const projSource = projectionsAttributes.source;
+ const configSource = config.sources[projSource];
+ const urlParameters = `?TIME=${util.toISOStringSeconds(util.roundTimeOneMinute(date))}`;
+ const sourceURL = def.sourceOverride || configSource.url;
+ const configMatrixSet = configSource.matrixSets[projectionsAttributes.matrixSet];
+
+ const { tileMatrices, resolutions, tileSize } = configMatrixSet;
+ const { origin, extent } = calcExtentsFromLimits(configMatrixSet, matrixSetLimits, null);
+ const sizes = !tileMatrices ? [] : tileMatrices.map(({ matrixWidth, matrixHeight }) => [matrixWidth, matrixHeight]);
+
+ const tileGridOptions = {
+ origin,
+ extent,
+ sizes,
+ resolutions,
+ matrixIds: matrixIds || resolutions.map((set, index) => index),
+ tileSize: tileSize[0],
+ };
+
+ const tileLoadFunction = function(tile, src) {
+ return new Promise((resolve) => {
+ fetch(src)
+ .then((response) => response.blob().then((data) => [response, data]))
+ .then(([response, data]) => {
+ const contentLength = response.headers.get('content-length');
+ if (response.status === 200 && data !== undefined) {
+ contentLengthSum += parseInt(contentLength, 10);
+ }
+ resolve();
+ })
+ .catch((e) => {
+ console.error('Error loading tile', e);
+ resolve();
+ });
+ });
+ };
+
+
+ const wmtsSource = new OlSourceWMTS({
+ url: sourceURL + urlParameters,
+ layer: id,
+ cacheSize: 4096,
+ crossOrigin: 'anonymous',
+ format,
+ transition: isGranule ? 350 : 0,
+ matrixSet: configMatrixSet.id,
+ tileGrid: new OlTileGridWMTS(tileGridOptions),
+ wrapX: false,
+ style: typeof style === 'undefined' ? 'default' : style,
+ tileLoadFunction,
+ });
+
+ const [matrixWidth, matrixHeight] = sizes[zoom];
+
+ const promises = [];
+
+ for (let x = 0; x < matrixWidth; x += 1) {
+ for (let y = 0; y < matrixHeight; y += 1) {
+ const tileCoord = [zoom, x, y];
+ const tileUrl = wmtsSource.tileUrlFunction(tileCoord, 1, proj);
+ if (tileUrl !== undefined) {
+ const promise = wmtsSource.getTileLoadFunction()(tileCoord, tileUrl);
+ promises.push(promise);
+ }
+ }
+ }
+
+ await Promise.all(promises);
+ return {
+ date,
+ contentLengthSum,
+ };
+ };
+
+ // Invoked from getAvailability, accepts one layer and returns results for that layer
+ // Each layer will have a nested array of each frameDate with a content-length value
+ async function getAvailabilityForLayer(layer) {
+ const layerName = layer.id.toString();
+
+ // Start all the tile requests concurrently
+ const tilePromises = frameDates.map((date) => requestTilesWMTS(layer, date));
+
+ // Wait for all the requests to finish and gather the results
+ const tiles = await Promise.all(tilePromises);
+
+ const availability = {
+ layerName,
+ dates: tiles,
+ };
+
+ return availability;
+ }
+
+ // Parent function called when frameDates are calculated
+ async function getAvailability() {
+ // An array of layers that are eligible to be checked
+ const layersToBeChecked = activeLayers.filter((layer) => contentLengthThresholds[layer.id]);
+
+ // Map through each eligible layer and request availability
+ const layersAvailability = layersToBeChecked.map((layer) => {
+ const layerAvailability = getAvailabilityForLayer(layer);
+ return layerAvailability;
+ });
+
+ // Wait for all the requests to finish and gather the results with promise
+ const resolvedLayersAvailability = await Promise.all(layersAvailability);
+
+ // Create a set of unique dates that are below the threshold
+ // The size of this set will determine how many frames are missing tiles
+ const uniqueDatesBelowThreshold = new Set();
+
+ resolvedLayersAvailability.forEach((layer) => {
+ const threshold = contentLengthThresholds[layer.layerName];
+
+ layer.dates.forEach((date) => {
+ if (date.contentLengthSum < threshold) {
+ const dateStr = date.date.toISOString(); // Convert date to string for use as an object key
+ uniqueDatesBelowThreshold.add(dateStr); // Add the date to the set. If it's already there, it won't be added again.
+ }
+ });
+ });
+
+ const framesWithMissingTiles = uniqueDatesBelowThreshold.size;
+
+ // Calculate how many frames are missing tiles as a percentage
+ const percentMissing = (framesWithMissingTiles / frameDates.length) * 100;
+
+ // If there are less than 40% of frames with missing tiles we dispatch action to allow animation to play
+ // Else we do not do anything which means we display static imagery of the first frame
+ // The imagery at this date should already be satisfactory because the errorTile process has already been completed
+ // The percentMissing conditional value is completely arbitrary and can be changed
+ if (percentMissing < 40) {
+ console.log('Good Check:', percentMissing, '% ', 'of dates with missing frames');
+ toggleCheckedAnimationAvailability(true);
+ } else {
+ // THIS ELSE BLOCK IS ONLY FOR TESTING
+ console.log('Bad Check: ', percentMissing, '% ', ' of dates with missing frames. Aborting animation.');
+ }
+ }
return null;
}
+DateRangeTileCheck.propTypes = {
+ proj: PropTypes.object,
+ config: PropTypes.object,
+ activeLayers: PropTypes.array,
+ frameDates: PropTypes.array,
+ zoom: PropTypes.number,
+};
+
export default DateRangeTileCheck;
diff --git a/web/js/components/kiosk/util.js b/web/js/components/kiosk/util.js
index 76734dcc4..074422298 100644
--- a/web/js/components/kiosk/util.js
+++ b/web/js/components/kiosk/util.js
@@ -1,5 +1,5 @@
-/* eslint-disable import/prefer-default-export */
// formats date for kiosk mode and updates to EST
+// eslint-disable-next-line import/prefer-default-export
export function formatKioskDate(date, subdaily) {
const options = {
year: 'numeric',
@@ -24,3 +24,5 @@ export function formatKioskDate(date, subdaily) {
return formattedDate;
}
+
+
diff --git a/web/js/containers/animation-widget/animation-widget.js b/web/js/containers/animation-widget/animation-widget.js
index e53a5e908..510559366 100644
--- a/web/js/containers/animation-widget/animation-widget.js
+++ b/web/js/containers/animation-widget/animation-widget.js
@@ -49,6 +49,7 @@ function AnimationWidget (props) {
animationCustomModalOpen,
autoplay,
breakpoints,
+ checkAnimationAvailability,
currentDate,
delta,
endDate,
@@ -100,7 +101,6 @@ function AnimationWidget (props) {
const [collapsedWidgetPosition, setCollapsedWidgetPosition] = useState({ x: 0, y: 0 });
const [userHasMovedWidget, setUserHasMovedWidget] = useState(false);
const [speed, setSpeed] = useState(speedRedux);
- const [testMode, setTestMode] = useState(false);
const prevSubDailyMode = usePrevious(subDailyMode);
const prevHasFutureLayers = usePrevious(hasFutureLayers);
@@ -227,36 +227,34 @@ function AnimationWidget (props) {
return isActive ? (
<ErrorBoundary>
{
- testMode && (
+ checkAnimationAvailability ? (
<AnimationTileCheck
startDate={startDate}
endDate={endDate}
interval={interval}
delta={delta}
isPlaying={isPlaying}
- setTestMode={setTestMode}
/>
- )
+ ) : isPlaying ? (
+ <PlayQueue
+ isMobile={isMobile}
+ isLoopActive={looping}
+ isPlaying={isPlaying}
+ numberOfFrames={numberOfFrames}
+ snappedCurrentDate={snappedCurrentDate}
+ currentDate={currentDate}
+ startDate={startDate}
+ endDate={endDate}
+ interval={interval}
+ delta={delta}
+ speed={speed}
+ selectDate={selectDate}
+ togglePlaying={onPushPause}
+ promiseImageryForTime={promiseImageryForTime}
+ onClose={onPushPause}
+ />
+ ) : null
}
- {isPlaying && !testMode && (
- <PlayQueue
- isMobile={isMobile}
- isLoopActive={looping}
- isPlaying={isPlaying}
- numberOfFrames={numberOfFrames}
- snappedCurrentDate={snappedCurrentDate}
- currentDate={currentDate}
- startDate={startDate}
- endDate={endDate}
- interval={interval}
- delta={delta}
- speed={speed}
- selectDate={selectDate}
- togglePlaying={onPushPause}
- promiseImageryForTime={promiseImageryForTime}
- onClose={onPushPause}
- />
- )}
{isCollapsed ? (
<CollapsedAnimationWidget
hasSubdailyLayers={hasSubdailyLayers}
@@ -306,9 +304,8 @@ function AnimationWidget (props) {
subDailyMode={subDailyMode}
toggleCollapse={toggleCollapse}
/>
- ) : isKioskModeActive && !testMode
+ ) : isKioskModeActive
? null
-
: (
<DesktopAnimationWidget
animationCustomModalOpen={animationCustomModalOpen}
@@ -388,7 +385,10 @@ const mapStateToProps = (state) => {
maxDate = appNow;
}
- const { isDistractionFreeModeActive, isKioskModeActive } = ui;
+ const {
+ isDistractionFreeModeActive, isKioskModeActive, animationAvailabilityChecked, eic,
+ } = ui;
+ const checkAnimationAvailability = (eic === 'sa' || eic === 'da') && !animationAvailabilityChecked && isKioskModeActive && isPlaying;
const { isEmbedModeActive } = embed;
const animationIsActive = isActive
&& lodashGet(map, 'ui.selected.frameState_')
@@ -436,6 +436,7 @@ const mapStateToProps = (state) => {
appNow,
animationCustomModalOpen,
autoplay,
+ checkAnimationAvailability,
customSelected,
startDate,
endDate,
@@ -522,6 +523,7 @@ AnimationWidget.propTypes = {
animationCustomModalOpen: PropTypes.bool,
autoplay: PropTypes.bool,
breakpoints: PropTypes.object,
+ checkAnimationAvailability: PropTypes.bool,
snappedCurrentDate: PropTypes.object,
currentDate: PropTypes.object,
delta: PropTypes.number,
diff --git a/web/js/mapUI/components/kiosk/kiosk-animations/kiosk-animations.js b/web/js/mapUI/components/kiosk/kiosk-animations/kiosk-animations.js
index 8fcee0aa7..1ff0c10ab 100644
--- a/web/js/mapUI/components/kiosk/kiosk-animations/kiosk-animations.js
+++ b/web/js/mapUI/components/kiosk/kiosk-animations/kiosk-animations.js
@@ -81,6 +81,7 @@ function KioskAnimations({ ui }) {
return date.toString();
};
+ // Invoked after tileError check is complete, this will trigger the animation availability check
const handleAnimationSettings = () => {
const start = updateStartTime(selectedDate);
const end = selectedDate;
diff --git a/web/js/modules/ui/actions.js b/web/js/modules/ui/actions.js
index d44081444..2b207d702 100644
--- a/web/js/modules/ui/actions.js
+++ b/web/js/modules/ui/actions.js
@@ -7,6 +7,7 @@ import {
SET_ERROR_TILES,
DISPLAY_STATIC_MAP,
READY_FOR_KIOSK_ANIMATION,
+ CHECK_ANIMATION_AVAILABILITY,
} from './constants';
import { CLOSE as CLOSE_MODAL } from '../modal/constants';
@@ -30,6 +31,8 @@ export default function toggleDistractionFreeMode() {
};
}
+// Keeping this action for testing purposes.
+// Kiosk mode should always be activated through the permalink parameters.
export function toggleKioskMode(isActive) {
return {
type: TOGGLE_KIOSK_MODE,
@@ -37,6 +40,7 @@ export function toggleKioskMode(isActive) {
};
}
+// Layerbuilder tracks tile requests that return errors or blank tiles and dispatches this action for specific layers.
export function setErrorTiles(errorTiles) {
return {
type: SET_ERROR_TILES,
@@ -50,6 +54,8 @@ export function setErrorTiles(errorTiles) {
};
}
+// After each tile request we call this to clear the error tiles for the current date.
+// We want to preserve the lastCheckedDate value from the previous dispatched setErrorTiles action.
export function clearErrorTiles() {
return (dispatch, getState) => {
const { ui: { errorTiles: { lastCheckedDate } } } = getState();
@@ -74,6 +80,8 @@ export function clearErrorTiles() {
};
}
+// Dispatched when maximum date/time threshold is met when checking for error/blank tiles.
+// Displays a static image map stored locally in repo.
export function toggleStaticMap(isActive) {
return {
type: DISPLAY_STATIC_MAP,
@@ -81,9 +89,21 @@ export function toggleStaticMap(isActive) {
};
}
+// When kiosk mode is active && (eic === 'sa' || eic === 'da') this action is dispatched once we step the date back to full imagery.
+// This signals that the animation availability to ready to be checked.
export function toggleReadyForKioskAnimation(toggleAnimation) {
return {
type: READY_FOR_KIOSK_ANIMATION,
toggleAnimation,
};
}
+
+// When kiosk mode is active && (eic === 'sa' || eic === 'da') this action is dispatched once the animation has been triggered.
+// Once availability is measured and passes the threshold metric this action will dispatch.
+// This signals that the animation is ready to be played.
+export function toggleCheckedAnimationAvailability(toggleCheck) {
+ return {
+ type: CHECK_ANIMATION_AVAILABILITY,
+ toggleCheck,
+ };
+}
diff --git a/web/js/modules/ui/constants.js b/web/js/modules/ui/constants.js
index d6f2d71f1..8775e42db 100644
--- a/web/js/modules/ui/constants.js
+++ b/web/js/modules/ui/constants.js
@@ -4,3 +4,4 @@ export const SET_ERROR_TILES = 'UI/SET_ERROR_TILES';
export const CLEAR_ERROR_TILES = 'UI/CLEAR_ERROR_TILES';
export const DISPLAY_STATIC_MAP = 'UI/DISPLAY_STATIC_MAP';
export const READY_FOR_KIOSK_ANIMATION = 'UI/READY_FOR_KIOSK_ANIMATION';
+export const CHECK_ANIMATION_AVAILABILITY = 'UI/CHECK_ANIMATION_AVAILABILITY';
diff --git a/web/js/modules/ui/reducers.js b/web/js/modules/ui/reducers.js
index 1696a7dd2..5dcab1f08 100644
--- a/web/js/modules/ui/reducers.js
+++ b/web/js/modules/ui/reducers.js
@@ -5,13 +5,14 @@ import {
DISPLAY_STATIC_MAP,
CLEAR_ERROR_TILES,
READY_FOR_KIOSK_ANIMATION,
+ CHECK_ANIMATION_AVAILABILITY,
} from './constants';
export const uiState = {
isDistractionFreeModeActive: false,
isKioskModeActive: false,
displayStaticMap: false,
- eic: '', // sa == subdaily-animation, da == daily-animation, ss== subdaily-static, ds == daily-static
+ eic: '', // 'sa' == subdaily-animation, 'da' == daily-animation
errorTiles: {
dailyTiles: [],
subdailyTiles: [],
@@ -20,6 +21,7 @@ export const uiState = {
lastCheckedDate: null,
},
readyForKioskAnimation: false,
+ animationAvailabilityChecked: false,
};
export default function uiReducers(state = uiState, action) {
@@ -58,8 +60,12 @@ export default function uiReducers(state = uiState, action) {
...state,
readyForKioskAnimation: action.toggleAnimation,
};
+ case CHECK_ANIMATION_AVAILABILITY:
+ return {
+ ...state,
+ animationAvailabilityChecked: action.toggleCheck,
+ };
default:
return state;
}
}
-