Interactive interface for browsing global, full-resolution satellite imagery
WV-2297: Improve granule dateline crossing behavior (#3973)
Jason Kent8ecc3fd95b13
diff --git a/web/js/map/granule/granule-layer-builder.js b/web/js/map/granule/granule-layer-builder.jsindex 39fb1b6e2..3427c7962 100644--- a/web/js/map/granule/granule-layer-builder.js+++ b/web/js/map/granule/granule-layer-builder.js@@ -1,5 +1,5 @@import OlLayerGroup from 'ol/layer/Group';-import { throttle as lodashThrottle } from 'lodash';+import { throttle as lodashThrottle, find } from 'lodash';import { DEFAULT_NUM_GRANULES } from '../../modules/layers/constants';import { updateGranuleLayerState } from '../../modules/layers/actions';import { getGranuleLayer } from '../../modules/layers/selectors';@@ -67,36 +67,41 @@ export default function granuleLayerBuilder(cache, store, createLayerWMTS) {* @param {data} CMR data* @param {id} layer id*/- const addGranuleCMRDateData = (data, id) => {+ const addGranuleCMRDateData = (data, conceptId, dateRanges) => {const { proj: { selected: { crs } } } = store.getState();- if (!CMRDataStore[id]) {- CMRDataStore[id] = {};- }- Object.values(data).forEach((entry) => {+ CMRDataStore[conceptId] = CMRDataStore[conceptId] || [];+ data.forEach((entry) => {const date = toISOStringSeconds(entry.time_start);- CMRDataStore[id][date] = transformGranuleData(entry, date, crs);+ const hasImagery = find(dateRanges, ({ startDate, endDate }) => isWithinDateRange(new Date(date), startDate, endDate));+ const existsForTime = find(CMRDataStore[conceptId], (g) => g.date === date);+ if (!hasImagery || existsForTime) {+ return;+ }+ const transformedGranule = transformGranuleData(entry, date, crs);+ CMRDataStore[conceptId].push(transformedGranule);+ });+ return CMRDataStore[conceptId].sort((a, b) => {+ const dateA = new Date(a).valueOf();+ const dateB = new Date(b).valueOf();+ return dateB - dateA;});};/**- * Query CMR to get dates filtered by day_night_flag+ * Query CMR to get dates* @param {object} def - Layer specs* @param {object} date - current selected date (Note: may not return this date, but this date will be the max returned)*/const getQueriedGranuleDates = async (def, date, activeString) => {const {- endDate, startDate, id, title, visible,+ endDate, startDate, id, title, visible, dateRanges,} = def;const { startQueryDate, endQueryDate } = getCMRQueryDates(date);const getGranulesUrl = getGranulesUrlSelector(store.getState());- const shortName = 'VJ102MOD'; // USE GRANULE LAYER ID- const params = {- shortName,- startDate: startQueryDate.toISOString(),- endDate: endQueryDate.toISOString(),- pageSize: 2000,- };+ // TODO: USE GRANULE LAYER conceptId+ const shortName = 'VJ102MOD';+ const conceptId = shortName;// update range/extend range checks and new dates (if applicable)const CMRDateStoreForLayer = CMRDateRanges[activeString][id];@@ -124,6 +129,14 @@ export default function granuleLayerBuilder(cache, store, createLayerWMTS) {showLoading();let data;try {+ const params = {+ // conceptId,+ shortName,+ startDate: startQueryDate.toISOString(),+ endDate: endQueryDate.toISOString(),+ dayNight: dayNightFilter,+ pageSize: 1000,+ };const response = await fetch(getGranulesUrl(params), CMR_AJAX_OPTIONS);data = await response.json();data = data.feed.entry;@@ -143,37 +156,10 @@ export default function granuleLayerBuilder(cache, store, createLayerWMTS) {} finally {hideLoading();}-- addGranuleCMRDateData(data, shortName);- return getGranules(shortName, date, startQueryDate);+ return addGranuleCMRDateData(data, conceptId, dateRanges);}// user previously queried CMR granule dates- return getGranules(shortName, date, startQueryDate);- };-- /**- * Process CMR granule data into granule date objects- * @param {String} layerId- * @param {Object} selectedDate- * @param {Object} startQueryDate- * @returns {Array} reducedGranuleDates- */- const getGranules = (layerId, selectedDate, startQueryDate) => {- const selected = toISOStringSeconds(selectedDate);- const queryStart = toISOStringSeconds(startQueryDate);- const granuleDates = CMRDataStore[layerId];- const granuleDateKeys = granuleDates ? Object.keys(granuleDates) : [];- let hitSelectedDate;-- return granuleDateKeys.reduce((granuleDateObjects, date) => {- const reachedStart = date === queryStart || new Date(date) > startQueryDate;- if (!reachedStart || hitSelectedDate) {- return granuleDateObjects;- }- hitSelectedDate = date === selected;- granuleDateObjects.push({ ...granuleDates[date] });- return granuleDateObjects;- }, []);+ return CMRDataStore[conceptId];};/**@@ -183,11 +169,11 @@ export default function granuleLayerBuilder(cache, store, createLayerWMTS) {* @param {object} attributes - Layer specs* @returns {array} collection of OpenLayers TileLayers*/- const createGranuleTileLayers = async (granules, def, attributes) => {+ const createGranuleTileLayers = (granules, def, attributes) => {const { period, id } = def;const { group, proj } = attributes;- const layerPromises = granules.map(async (granule) => {+ return granules.map((granule) => {const { date, polygon, shifted } = granule;const granuleISOKey = `${id}:${proj}:${date}::${group}:${shifted ? 'shifted' : ''}`;let tileLayer = cache.getItem(granuleISOKey);@@ -200,7 +186,7 @@ export default function granuleLayerBuilder(cache, store, createLayerWMTS) {polygon,shifted,};- tileLayer = await createLayerWMTS(def, options, null, store.getState());+ tileLayer = createLayerWMTS(def, options, null, store.getState());attributes.key = granuleISOKey;attributes.date = granuleISODate;@@ -209,9 +195,6 @@ export default function granuleLayerBuilder(cache, store, createLayerWMTS) {tileLayer.setVisible(false);return tileLayer;});-- const layers = await Promise.all(layerPromises);- return layers;};/**@@ -229,10 +212,9 @@ export default function granuleLayerBuilder(cache, store, createLayerWMTS) {const { id } = def;const { date, group } = attributes;const granuleAttributes = await getGranuleAttributes(def, options);- const { filteredGranules } = granuleAttributes;-- const granules = datelineShiftGranules(filteredGranules, date, crs);- const tileLayers = await createGranuleTileLayers(granules, def, attributes);+ const { visibleGranules } = granuleAttributes;+ const granules = datelineShiftGranules(visibleGranules, date, crs);+ const tileLayers = createGranuleTileLayers(granules, def, attributes);const layer = new OlLayerGroup({layers: tileLayers,extent: crs === 'EPSG:4326' ? FULL_MAP_EXTENT : maxExtent,@@ -243,7 +225,7 @@ export default function granuleLayerBuilder(cache, store, createLayerWMTS) {layer.wv = {...attributes,...granuleAttributes,- filteredGranules: granules,+ visibleGranules: granules,};// Don't update during animation due to the performance hit@@ -255,73 +237,49 @@ export default function granuleLayerBuilder(cache, store, createLayerWMTS) {};/**- * Granule layer request process- ** @method getGranuleAttributes- * @static- * @param {object} options- * @param {object} state* @param {object} def- * @param {object} group- * @param {object} date+ * @param {object} options* @returns {object} granuleAttributes*/const getGranuleAttributes = async (def, options) => {const state = store.getState();const { proj: { selected: { crs } } } = state;- let reorderedGranules = false;- let count = DEFAULT_NUM_GRANULES;-- if (options) {- const { granuleCount, granuleDates } = options;- count = granuleCount || DEFAULT_NUM_GRANULES;- if (granuleDates && granuleDates.length) {- reorderedGranules = granuleDates.length !== count ? false : granuleDates;- }- }-- if (!reorderedGranules) {- const granuleState = getGranuleLayer(state, def.id);- if (granuleState) {- count = granuleState.count;- }- }+ const { granuleCount, date, group } = options;+ const { count: currentCount } = getGranuleLayer(state, def.id) || {};+ const count = currentCount || granuleCount || DEFAULT_NUM_GRANULES;// get granule dates waiting for CMR query and filtering (if necessary)- const availableGranules = await getQueriedGranuleDates(def, options.date, options.group);- const filteredGranules = transformGranulesForProj(- filterGranules(def.dateRanges, availableGranules, count),- crs,- );+ const availableGranules = await getQueriedGranuleDates(def, date, group);+ const visibleGranules = filterGranules(availableGranules, count, date);+ const transformedGranules = transformGranulesForProj(visibleGranules, crs);return {count,- granuleDates: filteredGranules.map(({ date }) => date),- filteredGranules,- reorderedGranules,+ granuleDates: transformedGranules.map((g) => g.date),+ visibleGranules: transformedGranules,};};/*** Get the last n granule dates- * @param {Array} granuleDates+ * @param {Array} granuleDates - granule date/polygon metadata* @param {number} granuleCount - number of granules to add to collection* @returns {array} collection of granule objects with filtered granuleDates*/- const filterGranules = (dateRanges, granuleDates, granuleCount) => {- const dates = [];- for (let i = granuleDates.length - 1; i >= 0 && dates.length < granuleCount; i -= 1) {- const item = granuleDates[i];- const { dayNight, date } = item;+ const filterGranules = (availableGranules, granuleCount, nextDate) => {+ const granules = [];+ if (!availableGranules.length) return granules;++ for (let i = availableGranules.length - 1; i >= 0 && granules.length < granuleCount; i -= 1) {+ const item = availableGranules[i];+ const { date } = item;const granuleDate = new Date(date);- const hasImagery = dateRanges.some(- ({ startDate, endDate }) => isWithinDateRange(granuleDate, startDate, endDate),- );- if (dayNight === dayNightFilter && hasImagery) {- dates.unshift(item);+ if (granuleDate <= nextDate) {+ granules.unshift(item);}}- return dates;+ return granules;};return {diff --git a/web/js/map/granule/granule-layer-builder.test.js b/web/js/map/granule/granule-layer-builder.test.jsindex 708c3744c..a5ef95f88 100644--- a/web/js/map/granule/granule-layer-builder.test.js+++ b/web/js/map/granule/granule-layer-builder.test.js@@ -11,7 +11,7 @@ import { OPEN_BASIC } from '../../modules/modal/constants';const mockBaseCmrApi = 'mock.cmr.api/';-const queryString = '?shortName=VJ102MOD&temporal=2019-09-23T00%3A00%3A00.000Z%2C2019-09-25T00%3A00%3A00.000Z&pageSize=2000';+const queryString = '?shortName=VJ102MOD&day_night_flag=DAY&temporal=2019-09-23T00%3A00%3A00.000Z%2C2019-09-25T00%3A00%3A00.000Z&pageSize=1000';const cmrGranules = require('../../../mock/cmr_granules.json');fetchMock.mock(`${mockBaseCmrApi}granules.json${queryString}`, cmrGranules)@@ -106,7 +106,7 @@ describe('granule layer builder', () => {def: granuleLayerDef,};const granuleLayer = await createGranuleLayer(granuleLayerDef, attributes, options);- const filteredDates = granuleLayer.wv.filteredGranules.map(({ date }) => date);+ const filteredDates = granuleLayer.wv.visibleGranules.map(({ date }) => date);expect(granuleLayer.get('granuleGroup')).toEqual(true);expect(granuleLayer.wv.granuleDates).toEqual(expectedDates);diff --git a/web/js/map/granule/util.js b/web/js/map/granule/util.jsindex c5a5e0b72..bfa74e074 100644--- a/web/js/map/granule/util.js+++ b/web/js/map/granule/util.js@@ -24,14 +24,12 @@ import util from '../../util/util';*/export const datelineShiftGranules = (granules, currentDate, crs) => {const currentDayDate = new Date(currentDate).getUTCDate();- const datelineShiftNeeded = () => {+ const datelineShiftNeeded = (() => {if (crs !== 'EPSG:4326') return false;const sameDays = granules.every(({ date }) => new Date(date).getUTCDate() === currentDayDate);- const someCross = granules.some(({ polygon }) => polygon.some(([lon]) => lon > 180 || lon < -180));- return someCross && !sameDays;- };-- return !datelineShiftNeeded() ? granules : granules.map((granule) => {+ return !sameDays;+ })();+ return !datelineShiftNeeded ? granules : granules.map((granule) => {const { date, polygon } = granule;const sameDay = currentDayDate === new Date(date).getUTCDate();const westSide = polygon.some(([lon]) => lon < 0);@@ -81,8 +79,8 @@ export const getIndexForSortedInsert = (array, date) => {* @method isWithinDateRange* @static* @param {object} date - date object- * @param {object} startDate - date object- * @param {string} endDate - date object+ * @param {string} startDate - date string+ * @param {string} endDate - date string* @returns {boolean}*/export const isWithinDateRange = (date, startDate, endDate) => (startDate && endDate@@ -92,13 +90,13 @@ export const isWithinDateRange = (date, startDate, endDate) => (startDate && endexport const getGranuleFootprints = (layer) => {const {- def, filteredGranules, granuleDates,+ def, visibleGranules, granuleDates,} = layer.wv;const { endDate, startDate } = def;const mostRecentGranuleDate = granuleDates[0];const isMostRecentDateOutOfRange = new Date(mostRecentGranuleDate) > new Date(endDate);- return filteredGranules.reduce((dates, { date, polygon }) => {+ return visibleGranules.reduce((dates, { date, polygon }) => {const granuleDate = new Date(date);if (!isMostRecentDateOutOfRange && isWithinDateRange(granuleDate, startDate, endDate)) {dates[date] = polygon;diff --git a/web/js/map/granule/util.test.js b/web/js/map/granule/util.test.jsindex 5a5796152..b806cda6f 100644--- a/web/js/map/granule/util.test.js+++ b/web/js/map/granule/util.test.js@@ -35,14 +35,6 @@ const singleTransformedGranule = {};describe('shifting dateline granules', () => {- it('no shift when "next" day but dateline not crossed', async () => {- const selectedDate = new Date('2019-09-24T00:06:00.000Z');- const crs = 'EPSG:4326';- const granules = datelineShiftGranules(mockGranules.one, selectedDate, crs);-- expect(granules).toEqual(mockGranules.one);- });-it('no shift when datline is crossed with same day', async () => {const selectedDate = new Date('2019-09-24T00:24:00.000Z');const crs = 'EPSG:4326';@@ -51,7 +43,7 @@ describe('shifting dateline granules', () => {expect(granules).toEqual(mockGranules.three);});- it('shifts when datline is crossed and granules cross days', async () => {+ it('shifts when granules cross days', async () => {const selectedDate = new Date('2019-09-24T00:12:00.000Z');const crs = 'EPSG:4326';const shiftedGranules = datelineShiftGranules(mockGranules.two, selectedDate, crs);diff --git a/web/js/map/layerbuilder.js b/web/js/map/layerbuilder.jsindex 331cb14c5..a4de19c61 100644--- a/web/js/map/layerbuilder.js+++ b/web/js/map/layerbuilder.js@@ -166,9 +166,6 @@ export default function mapLayerBuilder(config, cache, store) {* @static* @param {object} def - Layer Specs* @param {object} options - Layer options- * @param {object} granuleLayerParam (optional: only used for granule layers)- * * @param {array} granuleDates - Reordered granule times- * * @param {number} granuleCount - number of granules in layer group* @returns {object} OpenLayers layer*/const createLayer = async (def, options = {}) => {diff --git a/web/js/map/ui.js b/web/js/map/ui.jsindex 42de44390..a83bc53e9 100644--- a/web/js/map/ui.js+++ b/web/js/map/ui.js@@ -915,13 +915,11 @@ export default function mapui(models, config, store) {if (compare.active && layers.length) {await updateCompareLayer(def, index, mapLayerCollection);} else if (temporalLayer) {- const index = findLayerIndex(def);if (index !== undefined && index !== -1) {const layerValue = layers[index];const layerOptions = type === 'granule'? { granuleCount: getGranuleCount(state, id) }: { previousLayer: layerValue ? layerValue.wv : null };-const updatedLayer = await createLayer(def, layerOptions);mapLayerCollection.setAt(index, updatedLayer);}@@ -1152,6 +1150,7 @@ export default function mapui(models, config, store) {],loadTilesWhileAnimating: true,loadTilesWhileInteracting: true,+ maxTilesLoading: 32,});map.wv = {scaleMetric,diff --git a/web/js/modules/smart-handoff/selectors.js b/web/js/modules/smart-handoff/selectors.jsindex 92592f1d2..57f5758a1 100644--- a/web/js/modules/smart-handoff/selectors.js+++ b/web/js/modules/smart-handoff/selectors.js@@ -17,6 +17,7 @@ export const getGranulesUrl = (state) => {bounding_box: params.bbox,collection_concept_id: params.conceptId,shortName: params.shortName,+ day_night_flag: params.dayNight,temporal: getTemporal(),pageSize: params.pageSize,};