Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

update Date and layer models #986

Benjamin King8bedbc2441fd
diff --git a/web/js/animation/model.js b/web/js/animation/model.js
index 071d7fbf3..e9b5e0a81 100644
--- a/web/js/animation/model.js
+++ b/web/js/animation/model.js
@@ -25,7 +25,7 @@ export function animationModel(models, config) {
* @returns {void}
*
*/
- self.load = function (state, errors) {
+ self.load = function(state, errors) {
self.rangeState = self.rangeState || {};
// self.rangeState.playing = false;
// if(state.a) {
@@ -63,7 +63,7 @@ export function animationModel(models, config) {
* @returns {void}
*
*/
- self.toggleActive = function () {
+ self.toggleActive = function() {
if (self.rangeState.state === 'off') {
self.rangeState.state = 'on';
} else {
@@ -81,7 +81,7 @@ export function animationModel(models, config) {
*
*/
- self.add = function (arra) {
+ self.add = function(arra) {
// var updatedState = _.clone(self.state);
// arra.forEach(function(prop) {
// updatedState[prop.id] = prop.value;
@@ -98,7 +98,7 @@ export function animationModel(models, config) {
* @returns {void}
*
*/
- self.save = function (state) {
+ self.save = function(state) {
var rangeState = self.rangeState;
// state.a = state.a || [];
// newState = {id: rangeState.state};
@@ -131,4 +131,4 @@ export function animationModel(models, config) {
// state.a.push(newState);
};
return self;
-};
+}
diff --git a/web/js/date/date.js b/web/js/date/date.js
index 65f154a87..19b1df2f3 100644
--- a/web/js/date/date.js
+++ b/web/js/date/date.js
@@ -5,16 +5,24 @@ export function parse(state, errors) {
state.t = state.time;
delete state.time;
}
- if (state.t) {
+ var tryCatchDate = function(stateDate) {
try {
- state.t = util.parseDateUTC(state.t);
+ state.t = util.parseDateUTC(stateDate);
} catch (error) {
- delete state.t;
errors.push({
- message: 'Invalid date: ' + state.t,
+ message: 'Invalid date: ' + stateDate,
cause: error
});
}
+ };
+ if (state.t) {
+ state.t = tryCatchDate(state.t);
+ }
+ if (state.t1) {
+ state.t1 = tryCatchDate(state.t1);
+ }
+ if (state.t2) {
+ state.t2 = tryCatchDate(state.t2);
}
if (state.now) {
diff --git a/web/js/date/model.js b/web/js/date/model.js
index 28db3f3a8..77f704356 100644
--- a/web/js/date/model.js
+++ b/web/js/date/model.js
@@ -6,6 +6,9 @@ export function dateModel(config, spec) {
var self = {};
self.events = util.events();
self.selected = null;
+ self.selectedA = null;
+ self.selectedB = null;
+ self.activeDate = 'selected';
self.monthAbbr = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN',
'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
@@ -13,25 +16,33 @@ export function dateModel(config, spec) {
var init = function () {
var initial = spec.initial || util.now();
self.select(initial);
+ if (config.features.compare) {
+ self.select(initial, 'selectedA');
+ self.select(util.dateAdd(initial, 'day', -5), 'selectedB');
+ }
};
self.string = function () {
return util.toISOStringDate(self.selected);
};
- self.select = function (date) {
+ self.select = function (date, selection) {
+ selection = selection || 'selected';
date = self.clamp(date);
var updated = false;
- if (!self.selected || date.getTime() !== self.selected.getTime()) {
- self.selected = date;
- self.events.trigger('select', date);
+ if (!self[selection] || date.getTime() !== self[selection].getTime()) {
+ self[selection] = date;
+ if (selection === self.activeDate) {
+ self.events.trigger('select', date);
+ }
updated = true;
}
return updated;
};
- self.add = function (interval, amount) {
- self.select(util.dateAdd(self.selected, interval, amount));
+ self.add = function (interval, amount, selection) {
+ selection = selection || self.selected;
+ self.select(util.dateAdd(selection, interval, amount), selection);
};
self.clamp = function (date) {
@@ -85,13 +96,20 @@ export function dateModel(config, spec) {
self.maxZoom = null;
- self.save = function (state) {
- state.t = self.selected.toISOString()
+ var dateToStringForUrl = function(date) {
+ return date.toISOString()
.split('T')[0] + '-' + 'T' + self.selected.toISOString()
.split('T')[1].slice(0, -5) + 'Z';
+ };
+ self.save = function (state) {
+ state.t = dateToStringForUrl(self.selected);
if (self.selectedZoom) {
state.z = self.selectedZoom.toString();
}
+ if (config.features.compare) {
+ state.t1 = dateToStringForUrl(self.selectedA);
+ state.t2 = dateToStringForUrl(self.selectedB);
+ }
};
self.load = function (state) {
@@ -101,6 +119,12 @@ export function dateModel(config, spec) {
if (state.z) {
self.selectedZoom = Number(state.z);
}
+ if (state.t1) {
+ self.select(state.t1, 'selectedA');
+ }
+ if (state.t2) {
+ self.select(state.t2, 'selectedB');
+ }
};
init();
return self;
diff --git a/web/js/layers/layers.js b/web/js/layers/layers.js
index 4c51d0aaa..3d278a397 100644
--- a/web/js/layers/layers.js
+++ b/web/js/layers/layers.js
@@ -41,9 +41,9 @@ var parse11 = function (state, errors, config) {
};
// Permalink version 1.2
-var parse12 = function (state, errors, config) {
+var parse12 = function (stateObj, errors, config) {
var parts;
- var str = state.l;
+ var str = stateObj;
// Split by layer definitions (commas not in parens)
var layerDefs = str.match(/[^(,]+(\([^)]*\))?,?/g);
var lstates = [];
@@ -81,12 +81,18 @@ var parse12 = function (state, errors, config) {
}
lstates.push(lstate);
});
- state.l = lstates;
+ return lstates;
};
export function parse(state, errors, config) {
if (state.l) {
- parse12(state, errors, config);
+ state.l = parse12(state.l, errors, config);
+ }
+ if (state.l1) {
+ state.l1 = parse12(state.l1, errors, config);
+ }
+ if (state.l2) {
+ state.l2 = parse12(state.l2, errors, config);
}
if (state.products) {
parse11(state, errors, config);
diff --git a/web/js/layers/model.js b/web/js/layers/model.js
index a61a0f2ab..c10eca366 100644
--- a/web/js/layers/model.js
+++ b/web/js/layers/model.js
@@ -4,7 +4,11 @@ import lodashFilter from 'lodash/filter';
import lodashFind from 'lodash/find';
import lodashFindIndex from 'lodash/findIndex';
import lodashIsUndefined from 'lodash/isUndefined';
+import lodashCloneDeep from 'lodash/cloneDeep';
+import { getActiveDateString } from '../compare/util';
import util from '../util/util';
+const activeGroups = ['active', 'activeA', 'activeB'];
+var loaded = false;
export function layersModel(models, config) {
var self = {};
@@ -13,24 +17,33 @@ export function layersModel(models, config) {
self.events = util.events();
self.active = [];
+ self.activeA = [];
+ self.activeB = [];
- var init = function () {
+ var init = function() {
self.reset();
};
- self.reset = function () {
+ self.reset = function() {
self.clear();
if (config.defaults && config.defaults.startingLayers) {
- lodashEach(config.defaults.startingLayers, function (start) {
- self.add(start.id, start);
+ lodashEach(config.defaults.startingLayers, function(start) {
+ lodashEach(activeGroups, activeLayerString => {
+ self[activeLayerString] = self.add(
+ start.id,
+ start,
+ activeLayerString
+ );
+ });
});
}
};
- self.get = function (spec) {
+ self.get = function(spec, activeLayers) {
spec = spec || {};
- var baselayers = forGroup('baselayers', spec);
- var overlays = forGroup('overlays', spec);
+ activeLayers = activeLayers || self.active;
+ var baselayers = forGroup('baselayers', spec, activeLayers);
+ var overlays = forGroup('overlays', spec, activeLayers);
if (spec.group === 'baselayers') {
return baselayers;
}
@@ -49,6 +62,7 @@ export function layersModel(models, config) {
return baselayers.concat(overlays);
};
+<<<<<<< HEAD
self.getTitles = function (layerId, proj) {
try {
proj = proj || models.proj.selected.id;
@@ -70,14 +84,32 @@ export function layersModel(models, config) {
};
} catch (err) {
throw new Error(`error in layer ${layerId}: ${err}`);
+=======
+ self.getTitles = function(layerId, proj) {
+ proj = proj || models.proj.selected.id;
+ var title, subtitle, tags;
+ if (config.layers[layerId].projections[proj]) {
+ var forProj = config.layers[layerId].projections[proj];
+ title = forProj.title;
+ subtitle = forProj.subtitle;
+ tags = forProj.tags;
+>>>>>>> update Date and layer models #986
}
};
- self.available = function (id) {
- var range = self.dateRange({
- layer: id
- });
- var date = models.date.selected;
+ self.available = function(id, date, activeLayers) {
+ activeLayers = activeLayers || self.active;
+ var activeDateString = getActiveDateString(
+ models.compare.abIsActive,
+ models.compareisCompareA
+ );
+ date = date || models.date[activeDateString];
+ var range = self.dateRange(
+ {
+ layer: id
+ },
+ activeLayers
+ );
if (range) {
if (date < range.start || date > range.end) {
return false;
@@ -88,9 +120,9 @@ export function layersModel(models, config) {
// Takes a layer id and returns a true or false value
// if the layer exists in the active layer list
- self.exists = function (layer) {
+ self.exists = function(layer, activeLayers) {
var found = false;
- lodashEach(self.active, function (current) {
+ lodashEach(activeLayers, function(current) {
if (layer === current.id) {
found = true;
}
@@ -98,12 +130,16 @@ export function layersModel(models, config) {
return found;
};
- self.dateRange = function (spec) {
+ self.dateRange = function(spec, activeLayers) {
+ activeLayers = activeLayers || self.active;
spec = spec || {};
- var layers = (spec.layer) ? [lodashFind(self.active, {
- id: spec.layer
- })]
- : self.active;
+ var layers = spec.layer
+ ? [
+ lodashFind(activeLayers, {
+ id: spec.layer
+ })
+ ]
+ : activeLayers;
var ignoreRange =
config.parameters &&
(config.parameters.debugGIBS || config.parameters.ignoreDateRange);
@@ -116,37 +152,32 @@ export function layersModel(models, config) {
var min = Number.MAX_VALUE;
var max = 0;
var range = false;
- lodashEach(layers, function (def) {
+ lodashEach(layers, function(def) {
if (def.startDate) {
range = true;
- var start = util.parseDateUTC(def.startDate)
- .getTime();
+ var start = util.parseDateUTC(def.startDate).getTime();
min = Math.min(min, start);
}
// For now, we assume that any layer with an end date is
// an ongoing product unless it is marked as inactive.
if (def.inactive && def.endDate) {
range = true;
- var end = util.parseDateUTC(def.endDate)
- .getTime();
+ var end = util.parseDateUTC(def.endDate).getTime();
max = Math.max(max, end);
} else if (def.endDate) {
range = true;
- max = util.now()
- .getTime();
+ max = util.now().getTime();
}
// If there is a start date but no end date, this is a
// product that is currently being created each day, set
// the max day to today.
if (def.startDate && !def.endDate) {
- max = util.now()
- .getTime();
+ max = util.now().getTime();
}
});
if (range) {
if (max === 0) {
- max = util.now()
- .getTime();
+ max = util.now().getTime();
}
return {
start: new Date(min),
@@ -155,14 +186,17 @@ export function layersModel(models, config) {
}
};
- self.add = function (id, spec) {
- if (lodashFind(self.active, {
- id: id
- })) {
- return;
+ self.add = function(id, spec, activeLayerString) {
+ activeLayerString = activeLayerString || 'active';
+ if (
+ lodashFind(self[activeLayerString], {
+ id: id
+ })
+ ) {
+ return self[activeLayerString];
}
spec = spec || {};
- var def = config.layers[id];
+ var def = lodashCloneDeep(config.layers[id]);
if (!def) {
throw new Error('No such layer: ' + id);
}
@@ -172,24 +206,26 @@ export function layersModel(models, config) {
} else if (!lodashIsUndefined(spec.hidden)) {
def.visible = !spec.hidden;
}
- def.opacity = (lodashIsUndefined(spec.opacity)) ? 1.0 : spec.opacity;
+ def.opacity = lodashIsUndefined(spec.opacity) ? 1.0 : spec.opacity;
if (def.group === 'overlays') {
- self.active.unshift(def);
+ self[activeLayerString].unshift(def);
split += 1;
} else {
- self.active.splice(split, 0, def);
+ self[activeLayerString].splice(split, 0, def);
}
self.events.trigger('add', def);
self.events.trigger('change');
+ return self[activeLayerString];
};
- self.remove = function (id) {
- var index = lodashFindIndex(self.active, {
+ self.remove = function(id, activeLayerString) {
+ activeLayerString = activeLayerString || 'active';
+ var index = lodashFindIndex(self[activeLayerString], {
id: id
});
- var def = self.active[index];
+ var def = self[activeLayerString][index];
if (index >= 0) {
- self.active.splice(index, 1);
+ self[activeLayerString].splice(index, 1);
if (index < split) {
split -= 1;
}
@@ -198,8 +234,9 @@ export function layersModel(models, config) {
}
};
- self.replace = function (idOld, idNew) {
- var index = lodashFindIndex(self.active, {
+ self.replace = function(idOld, idNew, activeLayerString) {
+ activeLayerString = activeLayerString || 'active';
+ var index = lodashFindIndex(self[activeLayerString], {
id: idOld
});
if (index < 0) {
@@ -208,77 +245,95 @@ export function layersModel(models, config) {
var newDef = config.layers[idNew];
newDef.visible = true;
newDef.opacity = 1.0;
- self.active[index] = newDef;
+ self[activeLayerString][index] = newDef;
self.events.trigger('update');
self.events.trigger('change');
};
- self.clear = function (projId) {
+ self.clear = function(projId, activeLayerString) {
+ activeLayerString = activeLayerString || 'active';
projId = projId || models.proj.selected.id;
- var defs = self.active.slice(0);
- lodashEach(defs, function (def) {
+ var defs = self[activeLayerString].slice(0);
+ lodashEach(defs, function(def) {
if (projId && def.projections[projId]) {
- self.remove(def.id);
+ self.remove(def.id, activeLayerString);
}
});
};
- self.pushToBottom = function (id) {
- var oldIndex = lodashFindIndex(self.active, {
+ self.pushToBottom = function(id, activeLayersString) {
+ activeLayersString = activeLayersString || 'active';
+ var oldIndex = lodashFindIndex(self[activeLayersString], {
id: id
});
if (oldIndex < 0) {
throw new Error('Layer is not active: ' + id);
}
- var def = self.active[oldIndex];
- self.active.splice(oldIndex, 1);
+ var def = self[activeLayersString][oldIndex];
+ self[activeLayersString].splice(oldIndex, 1);
if (def.group === 'baselayers') {
- self.active.push(def);
+ self[activeLayersString].push(def);
} else {
- self.active.splice(split - 1, 0, def);
+ self[activeLayersString].splice(split - 1, 0, def);
}
self.events.trigger('update');
self.events.trigger('change');
};
- self.moveBefore = function (sourceId, targetId) {
- var sourceIndex = lodashFindIndex(self.active, {
+ self.moveBefore = function(sourceId, targetId, activeLayersString) {
+ activeLayersString = activeLayersString || 'active';
+ var sourceIndex = lodashFindIndex(self[activeLayersString], {
id: sourceId
});
if (sourceIndex < 0) {
throw new Error('Layer is not active: ' + sourceId);
}
- var sourceDef = self.active[sourceIndex];
+ var sourceDef = self[activeLayersString][sourceIndex];
- var targetIndex = lodashFindIndex(self.active, {
+ var targetIndex = lodashFindIndex(self[activeLayersString], {
id: targetId
});
if (targetIndex < 0) {
throw new Error('Layer is not active: ' + targetId);
}
- self.active.splice(targetIndex, 0, sourceDef);
+ self[activeLayersString].splice(targetIndex, 0, sourceDef);
if (sourceIndex > targetIndex) {
sourceIndex++;
}
- self.active.splice(sourceIndex, 1);
+ self[activeLayersString].splice(sourceIndex, 1);
self.events.trigger('update', sourceDef, targetIndex);
self.events.trigger('change');
};
- self.setVisibility = function (id, visible) {
- var def = lodashFind(self.active, {
+ self.setVisibility = function(id, visible, activeLayersString) {
+ activeLayersString = activeLayersString || 'active';
+ var def = lodashFind(self[activeLayersString], {
id: id
});
if (def.visible !== visible) {
+ let index = lodashFindIndex(self[activeLayersString], {
+ id: id
+ });
def.visible = visible;
+ self[activeLayersString][index] = def;
self.events.trigger('visibility', def, visible);
self.events.trigger('change');
}
};
+ self.toggleVisibility = function(id, activeLayersString) {
+ activeLayersString = activeLayersString || 'active';
+ var index = lodashFindIndex(self[activeLayersString], {
+ id: id
+ });
- self.setOpacity = function (id, opacity) {
- var def = lodashFind(self.active, {
+ var visibility = !self[activeLayersString][index].visible;
+ self[activeLayersString][index].visible = visibility;
+ self.events.trigger('visibility', id, visibility, activeLayersString);
+ };
+ self.setOpacity = function(id, opacity, activeLayersString) {
+ activeLayersString = activeLayersString || 'active';
+ var def = lodashFind(self[activeLayersString], {
id: id
});
if (def.opacity !== opacity) {
@@ -288,14 +343,15 @@ export function layersModel(models, config) {
}
};
- self.isRenderable = function (id) {
- var def = lodashFind(self.active, {
+ self.isRenderable = function(id, activeLayers, date) {
+ activeLayers = activeLayers || self.active;
+ var def = lodashFind(activeLayers, {
id: id
});
if (!def) {
return false;
}
- if (!self.available(id)) {
+ if (!self.available(id, date, activeLayers)) {
return false;
}
if (!def.visible || def.opacity === 0) {
@@ -305,95 +361,157 @@ export function layersModel(models, config) {
return true;
}
var obscured = false;
- lodashEach(self.get({
- group: 'baselayers'
- }), function (otherDef) {
- if (otherDef.id === def.id) {
- return false;
- }
- if (otherDef.visible && otherDef.opacity === 1.0 &&
- self.available(otherDef.id)) {
- obscured = true;
- return false;
+ lodashEach(
+ self.get(
+ {
+ group: 'baselayers'
+ },
+ activeLayers
+ ),
+ function(otherDef) {
+ if (otherDef.id === def.id) {
+ return false;
+ }
+ if (
+ otherDef.visible &&
+ otherDef.opacity === 1.0 &&
+ self.available(otherDef.id, activeLayers)
+ ) {
+ obscured = true;
+ return false;
+ }
}
- });
+ );
return !obscured;
};
- self.save = function (state) {
+ self.save = function(state) {
+ var layers;
state.l = state.l || [];
- lodashEach(self.get(), function (def) {
- var lstate = lodashFind(state.l, {
- id: def.id
- });
- if (!lstate) {
- lstate = {
+ if (config.features.compare) {
+ state.l1 = state.l1 || [];
+ state.l2 = state.l2 || [];
+ layers = [
+ {
+ state: 'l',
+ str: 'active'
+ },
+ {
+ state: 'l1',
+ str: 'activeA'
+ },
+ {
+ state: 'l2',
+ str: 'activeB'
+ }
+ ];
+ } else {
+ layers = [{ state: 'l', active: 'active' }];
+ }
+ lodashEach(layers, obj => {
+ lodashEach(self.get({}, self[obj.str]), function(def) {
+ var lstate = lodashFind(state[obj.state], {
id: def.id
- };
- state.l.push(lstate);
- }
- if (!lstate.attributes) {
- lstate.attributes = [];
- }
- if (!def.visible) {
- lstate.attributes.push({
- id: 'hidden'
- });
- }
- if (def.opacity < 1) {
- lstate.attributes.push({
- id: 'opacity',
- value: def.opacity
});
- }
+ if (!lstate) {
+ lstate = {
+ id: def.id
+ };
+ state[obj.state].push(lstate);
+ }
+ if (!lstate.attributes) {
+ lstate.attributes = [];
+ }
+ if (!def.visible) {
+ lstate.attributes.push({
+ id: 'hidden'
+ });
+ }
+ if (def.opacity < 1) {
+ lstate.attributes.push({
+ id: 'opacity',
+ value: def.opacity
+ });
+ }
+ });
});
};
+ self.load = function(state, errors) {
+ if (loaded) return;
- self.load = function (state, errors) {
- if (!lodashIsUndefined(state.l)) {
- self.clear(models.proj.selected.id);
- lodashEachRight(state.l, function (layerDef) {
- if (!config.layers[layerDef.id]) {
- errors.push({
- message: 'No such layer: ' + layerDef.id
- });
- return;
+ var layers;
+ if (config.features.compare) {
+ layers = [
+ {
+ state: 'l',
+ active: 'active'
+ },
+ {
+ state: 'l1',
+ active: 'activeA'
+ },
+ {
+ state: 'l2',
+ active: 'activeB'
}
- var hidden = false;
- var opacity = 1.0;
- lodashEach(layerDef.attributes, function (attr) {
- if (attr.id === 'hidden') {
- hidden = true;
- }
- if (attr.id === 'opacity') {
- opacity = util.clamp(parseFloat(attr.value), 0, 1);
- if (isNaN(opacity)) opacity = 0; // "opacity=0.0" is opacity in URL, resulting in NaN
+ ];
+ } else {
+ layers = [{ state: 'l', active: 'active' }];
+ }
+ lodashEach(layers, obj => {
+ if (!lodashIsUndefined(state[obj.state])) {
+ self.clear(models.proj.selected.id, obj.active);
+ lodashEachRight(state[obj.state], function(layerDef) {
+ if (!config.layers[layerDef.id]) {
+ errors.push({
+ message: 'No such layer: ' + layerDef.id
+ });
+ return;
}
+ var hidden = false;
+ var opacity = 1.0;
+ lodashEach(layerDef.attributes, function(attr) {
+ if (attr.id === 'hidden') {
+ hidden = true;
+ }
+ if (attr.id === 'opacity') {
+ opacity = util.clamp(parseFloat(attr.value), 0, 1);
+ if (isNaN(opacity)) opacity = 0; // "opacity=0.0" is opacity in URL, resulting in NaN
+ }
+ });
+
+ self[obj.active] = self.add(
+ layerDef.id,
+ {
+ hidden: hidden,
+ opacity: opacity
+ },
+ obj.active
+ );
});
- self.add(layerDef.id, {
- hidden: hidden,
- opacity: opacity
- });
- });
- }
+ }
+ });
+ loaded = true;
};
-
- var forGroup = function (group, spec) {
+ var forGroup = function(group, spec, activeLayers) {
spec = spec || {};
var projId = spec.proj || models.proj.selected.id;
var results = [];
- var defs = lodashFilter(self.active, {
+ var defs = lodashFilter(activeLayers, {
group: group
});
- lodashEach(defs, function (def) {
+ lodashEach(defs, function(def) {
// Skip if this layer isn't available for the selected projection
if (!def.projections[projId]) {
return;
}
- if (spec.dynamic && !['subdaily', 'daily', 'monthly', 'yearly'].includes(def.period)) {
+ if (
+ spec.dynamic &&
+ !['subdaily', 'daily', 'monthly', 'yearly'].includes(def.period)
+ ) {
return;
}
- if (spec.renderable && !self.isRenderable(def.id)) {
+ if (spec.renderable && !self.isRenderable(def.id, activeLayers)) {
return;
}
if (spec.visible && !def.visible) {
@@ -409,4 +527,4 @@ export function layersModel(models, config) {
init();
return self;
-};
+}