Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

snap times based on layer with the shortest interval

Jason Kentf2ec3b7ce81a
diff --git a/web/js/map/layerbuilder.js b/web/js/map/layerbuilder.js
index 6704317ad..dd2f784aa 100644
--- a/web/js/map/layerbuilder.js
+++ b/web/js/map/layerbuilder.js
@@ -11,7 +11,6 @@ import SourceVectorTile from 'ol/source/VectorTile';
import lodashCloneDeep from 'lodash/cloneDeep';
import lodashMerge from 'lodash/merge';
import lodashEach from 'lodash/each';
-import lodashGet from 'lodash/get';
import { lookupFactory } from '../ol/lookupimagetile';
import {
isActive as isPaletteActive,
@@ -23,7 +22,11 @@ import {
getKey as getVectorStyleKeys,
setStyleFunction
} from '../modules/vector-styles/selectors';
-import { datesinDateRanges, prevDateInDateRange } from '../modules/layers/util';
+import {
+ nearestInterval,
+ datesinDateRanges,
+ prevDateInDateRange
+} from '../modules/layers/util';
export function mapLayerBuilder(models, config, cache, ui, store) {
var self = {};
@@ -127,28 +130,6 @@ export function mapLayerBuilder(models, config, cache, ui, store) {
return layer;
};
- /**
- * For subdaily layers, round the time down to nearest interval.
- * NOTE: Assumes intervals are the same for all ranges!
- * @param {object} def
- * @param {date} date
- * @return {date}
- */
- const nearestInterval = function(def, date) {
- const dateInterval = lodashGet(def, 'dateRanges[0].dateInterval');
- const interval = Number(dateInterval);
- const remainder = date.getMinutes() % interval;
- const newMinutes = date.getMinutes() - remainder;
- const newDate = new Date(
- date.getFullYear(),
- date.getMonth(),
- date.getDate(),
- date.getHours(),
- newMinutes
- );
- return newDate;
- };
-
/**
* Returns the closest date, from the layer's array of availableDates
*
diff --git a/web/js/modules/image-download/util.js b/web/js/modules/image-download/util.js
index 121e43e82..32d3634d7 100644
--- a/web/js/modules/image-download/util.js
+++ b/web/js/modules/image-download/util.js
@@ -1,26 +1,52 @@
import lodashEach from 'lodash/each';
+import lodashGet from 'lodash/get';
import util from '../../util/util';
+import { nearestInterval } from '../layers/util';
const GEO_ESTIMATION_CONSTANT = 256.0;
const POLAR_ESTIMATION_CONSTANT = 0.002197265625;
-export function getDownloadUrl(url, proj, layers, lonlats, dimensions, dateTime, isWorldfile) {
+/**
+ * Get a date time snapped to the interval of the layer with the shortest interval.
+ * This should give us the snapped time closest to the current app time.
+ * @param {Object} layerDefs - layer definitions for all visible layers
+ * @param {Date} dateTime - current application dateTime
+ * @returns {Date}
+ */
+const getLatestIntervalTime = function(layerDefs, dateTime) {
+ const subDailyDefs = layerDefs.filter((def) => def.period === 'subdaily') || [];
+ const defsSortedByInterval = subDailyDefs.sort((defA, defB) => {
+ const intervalA = Number(lodashGet(defA, 'dateRanges[0].dateInterval'));
+ const intervalB = Number(lodashGet(defB, 'dateRanges[0].dateInterval'));
+ return intervalA - intervalB;
+ });
+
+ return subDailyDefs.length
+ ? nearestInterval(defsSortedByInterval[0], dateTime)
+ : new Date(dateTime.setUTCHours(0, 0, 0, 0));
+};
+
+/**
+ * Get the snapshots URL to download an image
+ * @param {String} url
+ * @param {Object} proj
+ * @param {Array} layers
+ * @param {Object} lonlats
+ * @param {Object} dimensions
+ * @param {Date} dateTime
+ * @param {Boolean} isWorldfile
+ */
+export function getDownloadUrl(url, proj, layerDefs, lonlats, dimensions, dateTime, isWorldfile) {
const { crs } = proj.selected;
- const layersArray = imageUtilGetLayers(layers, proj.id);
- const layerWraps = imageUtilGetLayerWrap(layers);
- const opacities = imageUtilGetLayerOpacities(layers);
+ const layersArray = imageUtilGetLayers(layerDefs, proj.id);
+ const layerWraps = imageUtilGetLayerWrap(layerDefs);
+ const opacities = imageUtilGetLayerOpacities(layerDefs);
const imgFormat = 'image/jpeg';
const { height, width } = dimensions;
-
- // TODO get real value
- const hasSubdailyLayers = false;
- if (!hasSubdailyLayers) {
- dateTime.setUTCHours(0, 0, 0, 0);
- }
-
+ const snappedDateTime = getLatestIntervalTime(layerDefs, dateTime);
const params = [
'REQUEST=GetSnapshot',
- `TIME=${util.toISOStringSeconds(dateTime)}`,
+ `TIME=${util.toISOStringSeconds(snappedDateTime)}`,
`BBOX=${bboxWMS13(lonlats, crs)}`,
`CRS=${crs}`,
`LAYERS=${layersArray.join(',')}`,
diff --git a/web/js/modules/layers/util.js b/web/js/modules/layers/util.js
index 7204b4a02..b4edd9794 100644
--- a/web/js/modules/layers/util.js
+++ b/web/js/modules/layers/util.js
@@ -1,4 +1,5 @@
import {
+ get as lodashGet,
cloneDeep as lodashCloneDeep,
eachRight as lodashEachRight,
isUndefined as lodashIsUndefined,
@@ -21,6 +22,28 @@ import isFirstDayOfMonth from 'date-fns/is_first_day_of_month';
import isLastDayOfMonth from 'date-fns/is_last_day_of_month';
import lastDayOfYear from 'date-fns/last_day_of_year';
+/**
+ * For subdaily layers, round the time down to nearest interval.
+ * NOTE: Assumes intervals are the same for all ranges!
+ * @param {object} def
+ * @param {date} date
+ * @return {date}
+ */
+export function nearestInterval(def, date) {
+ const dateInterval = lodashGet(def, 'dateRanges[0].dateInterval');
+ const interval = Number(dateInterval);
+ const remainder = date.getMinutes() % interval;
+ const newMinutes = date.getMinutes() - remainder;
+ const newDate = new Date(
+ date.getFullYear(),
+ date.getMonth(),
+ date.getDate(),
+ date.getHours(),
+ newMinutes
+ );
+ return newDate;
+};
+
/**
* Find the closest previous date from an array of dates
*