Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Make palettes permalink use redux-location-state #1623

Benjamin King63eff374cb51
diff --git a/web/js/combine-models.js b/web/js/combine-models.js
index fc730f617..f30e137dd 100644
--- a/web/js/combine-models.js
+++ b/web/js/combine-models.js
@@ -9,7 +9,6 @@ import { dataModel } from './map/data/model';
import { projectionModel } from './projection/model';
import { compareModel } from './compare/model';
import { tourModel } from './tour/model';
-import naturalEventsModel from './map/natural-events/model';
/**
* Return initiated legacy models Object
@@ -49,9 +48,6 @@ export function combineModels(config) {
if (config.features.dataDownload) {
models.data = dataModel(models, config);
}
- if (config.features.naturalEvents) {
- models.naturalEvents = naturalEventsModel(models, config);
- }
return models;
}
diff --git a/web/js/components/layer/settings/settings.js b/web/js/components/layer/settings/settings.js
index 4819b0cd6..aee2c8f3e 100644
--- a/web/js/components/layer/settings/settings.js
+++ b/web/js/components/layer/settings/settings.js
@@ -47,9 +47,10 @@ class LayerSettings extends React.Component {
setCustom,
palettesTranslate,
groupName,
- setRange
+ setRange,
+ layer
} = this.props;
- const { layer, activeIndex } = this.state;
+ const { activeIndex } = this.state;
let navElements = [];
let paneElements = [];
lodashEach(legends, (legend, i) => {
diff --git a/web/js/components/sidebar/layers/layer.js b/web/js/components/sidebar/layers/layer.js
index 9bcbed639..c0826fc82 100644
--- a/web/js/components/sidebar/layers/layer.js
+++ b/web/js/components/sidebar/layers/layer.js
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import Legend from './legend';
import { Draggable } from 'react-beautiful-dnd';
import util from '../../../util/util';
-import { isEmpty as lodashIsEmpty } from 'lodash';
+import { isEmpty as lodashIsEmpty, get as lodashGet } from 'lodash';
import googleTagManager from 'googleTagManager';
import { getPalette, getLegends } from '../../../modules/palettes/selectors';
import { openCustomContent } from '../../../modules/modal/actions';
@@ -285,11 +285,12 @@ function mapStateToProps(state, ownProps) {
index,
layerGroupName
} = ownProps;
- const { palettes } = state;
+ const { palettes, config } = state;
const hasPalette = !lodashIsEmpty(layer.palette);
const renderedPalettes = palettes.rendered;
+ const paletteName = lodashGet(config, `layers.${layer.id}.palette.id`);
const legends =
- hasPalette && renderedPalettes[layer.id]
+ hasPalette && renderedPalettes[paletteName]
? getLegends(layer.id, layerGroupName, state)
: [];
const isCustomPalette = hasPalette && palettes.custom[layer.id];
@@ -302,8 +303,8 @@ function mapStateToProps(state, ownProps) {
names,
index,
isCustomPalette,
- isLoading: palettes.isLoading[layer.id],
- renderedPalette: renderedPalettes[layer.id],
+ isLoading: palettes.isLoading[paletteName],
+ renderedPalette: renderedPalettes[paletteName],
layerGroupName,
isMobile: state.browser.is.small,
hasPalette,
diff --git a/web/js/location.js b/web/js/location.js
index afb585ca2..e8a8961bf 100644
--- a/web/js/location.js
+++ b/web/js/location.js
@@ -15,6 +15,7 @@ import {
import { initialCompareState } from './modules/compare/reducers';
import { resetLayers } from './modules/layers/selectors';
import { eventsReducerState } from './modules/natural-events/reducers';
+import { loadPalettes } from './modules/palettes/util';
import util from './util/util';
import update from 'immutability-helper';
@@ -54,6 +55,15 @@ export function mapLocationToState(state, location) {
}
});
}
+ if (parameters.l1 || parameters.l) {
+ stateFromLocation = loadPalettes(
+ parameters,
+ lodashAssign({}, stateFromLocation, {
+ palettes: state.palettes,
+ config
+ })
+ );
+ }
// one level deep merge
for (var key in stateFromLocation) {
const obj = lodashAssign({}, state[key], stateFromLocation[key]);
@@ -61,6 +71,7 @@ export function mapLocationToState(state, location) {
[key]: { $set: obj }
});
}
+
return update(state, { $merge: stateFromLocation });
} else {
const startTour = checkTourBuildTimestamp(state.config);
diff --git a/web/js/modules/layers/reducers.js b/web/js/modules/layers/reducers.js
index f0f1f3936..6b8315ff6 100644
--- a/web/js/modules/layers/reducers.js
+++ b/web/js/modules/layers/reducers.js
@@ -8,9 +8,16 @@ import {
REMOVE_LAYER,
UPDATE_OPACITY
} from './constants';
-import { SET_CUSTOM as SET_CUSTOM_PALETTE } from '../palettes/constants';
+import {
+ SET_CUSTOM as SET_CUSTOM_PALETTE,
+ SET_RANGE_AND_SQUASH
+} from '../palettes/constants';
import { resetLayers } from './selectors';
-import { cloneDeep as lodashCloneDeep, assign as lodashAssign } from 'lodash';
+import {
+ cloneDeep as lodashCloneDeep,
+ assign as lodashAssign,
+ findIndex as lodashFindIndex
+} from 'lodash';
import update from 'immutability-helper';
export const initialState = {
@@ -56,11 +63,26 @@ export function layerReducer(state = initialState, action) {
[action.index]: { visible: { $set: action.visible } }
}
});
+ case SET_RANGE_AND_SQUASH:
+ let layerIndex = lodashFindIndex(state[layerGroupStr], {
+ id: action.layerId
+ });
+ console.log(layerGroupStr, layerIndex);
+ return update(state, {
+ [layerGroupStr]: {
+ [layerIndex]: {
+ $merge: action.props
+ }
+ }
+ });
case SET_CUSTOM_PALETTE:
+ layerIndex = lodashFindIndex(state[layerGroupStr], {
+ id: action.layerId
+ });
return update(state, {
[layerGroupStr]: {
- [action.layerId]: {
- customs: {
+ [layerIndex]: {
+ custom: {
$set: action.paletteId
}
}
diff --git a/web/js/modules/layers/selectors.js b/web/js/modules/layers/selectors.js
index 1cf869121..6e045146a 100644
--- a/web/js/modules/layers/selectors.js
+++ b/web/js/modules/layers/selectors.js
@@ -107,6 +107,11 @@ export function addLayer(id, spec, layers, layerConfig) {
throw new Error('No such layer: ' + id);
}
def.visible = true;
+ def.min = spec.min || undefined;
+ def.custom = spec.custom || undefined;
+ def.max = spec.max || undefined;
+ def.squash = spec.squash || undefined;
+
if (!lodashIsUndefined(spec.visible)) {
def.visible = spec.visible;
} else if (!lodashIsUndefined(spec.hidden)) {
diff --git a/web/js/modules/layers/util.js b/web/js/modules/layers/util.js
index 4eb3a90f8..e863440fe 100644
--- a/web/js/modules/layers/util.js
+++ b/web/js/modules/layers/util.js
@@ -4,10 +4,14 @@ import {
isUndefined as lodashIsUndefined,
remove as lodashRemove,
findIndex as lodashFindIndex,
- each as lodashEach
+ each as lodashEach,
+ isNaN as lodashIsNaN,
+ assign as lodashAssign,
+ isArray
} from 'lodash';
import { addLayer } from './selectors';
+import { getPaletteAttributeArray } from '../palettes/util';
import update from 'immutability-helper';
import util from '../../util/util';
@@ -36,34 +40,19 @@ export function serializeLayers(currentLayers, state, groupName) {
value: def.opacity
});
}
- if (def.custom) {
- let paletteDef = palettes[def.id];
- if (paletteDef.custom) {
- item.attributes.push({
- id: 'palette',
- value: paletteDef.custom
- });
- }
- if (paletteDef.min) {
- var minValue = paletteDef.entries.values[paletteDef.min];
- item.attributes.push({
- id: 'min',
- value: minValue
- });
- }
- if (paletteDef.max) {
- var maxValue = paletteDef.entries.values[paletteDef.max];
- item.attributes.push({
- id: 'max',
- value: maxValue
- });
- }
- if (paletteDef.squash) {
- item.attributes.push({
- id: 'squash'
- });
- }
+
+ if (def.custom || def.min || def.max || def.min || def.squash) {
+ // If has palette attr
+ const paletteAttributeArray = getPaletteAttributeArray(
+ def.id,
+ palettes,
+ state
+ );
+ item.attributes = paletteAttributeArray.length
+ ? item.attributes.concat(paletteAttributeArray)
+ : item.attributes;
}
+
return util.appendAttributesForURL(item);
});
}
@@ -269,6 +258,7 @@ const createLayerArrayFromState = function(state, config) {
lodashEachRight(state, function(layerDef) {
let hidden = false;
let opacity = 1.0;
+ let max, min, squash, custom;
if (!config.layers[layerDef.id]) {
console.warn('No such layer: ' + layerDef.id);
return;
@@ -281,12 +271,68 @@ const createLayerArrayFromState = function(state, config) {
opacity = util.clamp(parseFloat(attr.value), 0, 1);
if (isNaN(opacity)) opacity = 0; // "opacity=0.0" is opacity in URL, resulting in NaN
}
+ if (attr.id === 'max' && typeof attr.value === 'string') {
+ let maxArray = [];
+ let values = util.toArray(attr.value.split(';'));
+ lodashEach(values, function(value, index) {
+ if (value === '') {
+ maxArray.push(undefined);
+ return;
+ }
+ let maxValue = parseFloat(value);
+ if (lodashIsNaN(maxValue)) {
+ console.warn('Invalid max value: ' + value);
+ } else {
+ maxArray.push(maxValue);
+ }
+ });
+ max = maxArray.length ? maxArray : undefined;
+ }
+ if (attr.id === 'min' && typeof attr.value === 'string') {
+ let minArray = [];
+ let values = util.toArray(attr.value.split(';'));
+ lodashEach(values, function(value, index) {
+ if (value === '') {
+ minArray.push(undefined);
+ return;
+ }
+ let minValue = parseFloat(value);
+ if (lodashIsNaN(minValue)) {
+ console.warn('Invalid min value: ' + value);
+ } else {
+ minArray.push(minValue);
+ }
+ });
+ min = minArray.length ? minArray : undefined;
+ }
+ if (attr.id === 'squash') {
+ if (attr.value === true) {
+ squash = [true];
+ } else if (typeof attr.value === 'string') {
+ let squashArray = [];
+ let values = util.toArray(attr.value.split(';'));
+ lodashEach(values, function(value) {
+ squashArray.push(value === 'true');
+ });
+ squash = squashArray.length ? squashArray : undefined;
+ }
+ }
+ if (attr.id === 'palette') {
+ let values = util.toArray(attr.value.split(';'));
+ custom = values;
+ }
});
layerArray = addLayer(
layerDef.id,
{
- hidden: hidden,
- opacity: opacity
+ hidden,
+ opacity,
+ // only include palette attributes if Array.length condition
+ // is true: https://stackoverflow.com/a/40560953/4589331
+ ...(isArray(custom) && { custom }),
+ ...(isArray(min) && { min }),
+ ...(isArray(squash) && { squash }),
+ ...(isArray(max) && { max })
},
layerArray,
config.layers
diff --git a/web/js/modules/palettes/actions.js b/web/js/modules/palettes/actions.js
index 206211316..0e9a1d23c 100644
--- a/web/js/modules/palettes/actions.js
+++ b/web/js/modules/palettes/actions.js
@@ -15,13 +15,14 @@ export function requestPalette(id) {
return (dispatch, getState) => {
const config = getState().config;
var layer = config.layers[id];
- const location = 'config/palettes/' + layer.palette.id + '.json';
+ const paletteID = layer.palette.id;
+ const location = 'config/palettes/' + paletteID + '.json';
return requestAction(
dispatch,
REQUEST_PALETTE,
location,
'application/json',
- id
+ paletteID
);
};
}
@@ -29,7 +30,6 @@ export function requestPalette(id) {
export function setRange(layerId, min, max, squash, index, groupName) {
return (dispatch, getState) => {
const state = getState();
- console.log(state);
const props = {
min: min,
max: max,
@@ -45,14 +45,16 @@ export function setRange(layerId, min, max, squash, index, groupName) {
dispatch({
type: SET_RANGE_AND_SQUASH,
groupName: groupName,
- palettes: newActivePalettesObj
+ activeString: groupName,
+ layerId,
+ palettes: newActivePalettesObj,
+ props
});
};
}
export function setCustom(layerId, paletteId, index, groupName) {
return (dispatch, getState) => {
const state = getState();
- console.log(state.palettes.rendered);
const newActivePalettesObj = setCustomSelector(
layerId,
paletteId,
@@ -65,6 +67,7 @@ export function setCustom(layerId, paletteId, index, groupName) {
layerId: layerId,
paletteId: paletteId,
groupName: groupName,
+ activeString: groupName,
palettes: newActivePalettesObj
});
};
diff --git a/web/js/modules/palettes/reducers.js b/web/js/modules/palettes/reducers.js
index 8d1674995..9b98e26b3 100644
--- a/web/js/modules/palettes/reducers.js
+++ b/web/js/modules/palettes/reducers.js
@@ -19,14 +19,15 @@ export const defaultPaletteState = {
};
export function getInitialPaletteState(config) {
const rendered = lodashGet(config, 'palettes.rendered') || {};
+ const custom = lodashGet(config, 'palettes.custom') || {};
return lodashAssign({}, defaultPaletteState, {
- rendered: rendered
+ rendered,
+ custom
});
}
export function paletteReducer(state = defaultPaletteState, action) {
const groupName = action.groupName || 'active';
-
switch (action.type) {
case REQUEST_PALETTE_START:
return lodashAssign({}, state, {
diff --git a/web/js/modules/palettes/selectors.js b/web/js/modules/palettes/selectors.js
index ec886ae5b..a2fe9d6d0 100644
--- a/web/js/modules/palettes/selectors.js
+++ b/web/js/modules/palettes/selectors.js
@@ -5,6 +5,7 @@ import {
parseInt as lodashParseInt,
cloneDeep as lodashCloneDeep
} from 'lodash';
+import { getMinValue, getMaxValue } from './util';
import update from 'immutability-helper';
/**
@@ -216,6 +217,25 @@ var updateLookup = function(layerId, palettesObj, state) {
});
return update(newPalettes, { [layerId]: { lookup: { $set: lookup } } });
};
+
+export function findIndex(layerId, type, value, index, groupStr, state) {
+ index = index || 0;
+ var values = getPalette(layerId, index, groupStr, state).entries.values;
+ var result;
+ lodashEach(values, function(check, index) {
+ var min = getMinValue(check);
+ var max = getMaxValue(check);
+ if (type === 'min' && value === min) {
+ result = index;
+ return false;
+ }
+ if (type === 'max' && value === max) {
+ result = index;
+ return false;
+ }
+ });
+ return result;
+}
export function setCustom(layerId, paletteId, index, groupName, state) {
const { config, palettes } = state;
if (!config.layers[layerId]) {
diff --git a/web/js/modules/palettes/util.js b/web/js/modules/palettes/util.js
index c5b123f00..fe542b4ff 100644
--- a/web/js/modules/palettes/util.js
+++ b/web/js/modules/palettes/util.js
@@ -1,5 +1,17 @@
-import { each as lodashEach } from 'lodash';
+import update from 'immutability-helper';
+import {
+ each as lodashEach,
+ find as lodashFind,
+ assign as lodashAssign
+} from 'lodash';
+import {
+ setCustom as setCustomSelector,
+ getCount,
+ setRange as setRangeSelector,
+ findIndex as findPaletteExtremeIndex
+} from './selectors';
import util from '../../util/util';
+
export function getCheckerboard() {
var size = 2;
var canvas = document.createElement('canvas');
@@ -93,3 +105,208 @@ export function loadCustom(config) {
'config/palettes-custom.json'
);
}
+export function getMinValue(v) {
+ return v.length ? v[0] : v;
+}
+
+export function getMaxValue(v) {
+ return v.length ? v[v.length - 1] : v;
+}
+export function parsePalettes(state, errors, config) {
+ if (state.palettes) {
+ var parts = state.palettes.split('~');
+ lodashEach(parts, function(part) {
+ var items = part.split(',');
+ var layerId = items[0];
+ var paletteId = items[1];
+ if (!config.layers[layerId]) {
+ errors.push({
+ message: 'Invalid layer for palette ' + paletteId + ': ' + layerId
+ });
+ } else if (!config.layers[layerId].palette) {
+ errors.push({
+ message: 'Layer ' + layerId + ' does not ' + 'support palettes'
+ });
+ } else {
+ var layer = lodashFind(state.l, {
+ id: layerId
+ });
+ if (layer) {
+ layer.attributes.push({
+ id: 'palette',
+ value: paletteId
+ });
+ } else {
+ errors.push({
+ message: 'Layer ' + layerId + ' is not ' + 'active'
+ });
+ }
+ }
+ });
+ delete state.palettes;
+ }
+}
+export function isSupported() {
+ var browser = util.browser;
+ return !(browser.ie || !browser.webWorkers || !browser.cors);
+}
+export function getPaletteAttributeArray(layerId, palettes, state) {
+ const count = getCount(layerId, state);
+ const DEFAULT_OBJ = { isActive: false, value: undefined };
+ let palObj = lodashAssign({}, { key: 'custom', array: [] }, DEFAULT_OBJ);
+ let minObj = lodashAssign({}, { key: 'min', array: [] }, DEFAULT_OBJ);
+ let maxObj = lodashAssign({}, { key: 'max', array: [] }, DEFAULT_OBJ);
+ let squashObj = lodashAssign({}, { key: 'squash', array: [] }, DEFAULT_OBJ);
+ let attrArray = [];
+ for (var i = 0; i < count; i++) {
+ let paletteDef = palettes[layerId].maps[i];
+ let entryLength = paletteDef.entries.values.length;
+ let maxValue = paletteDef.max
+ ? paletteDef.entries.values[paletteDef.max || entryLength]
+ : undefined;
+ let minValue = paletteDef.min
+ ? paletteDef.entries.values[paletteDef.min || 0]
+ : undefined;
+ palObj = createPaletteAttributeObject(
+ paletteDef,
+ paletteDef.custom,
+ palObj,
+ count
+ );
+ maxObj = createPaletteAttributeObject(paletteDef, maxValue, maxObj, count);
+ minObj = createPaletteAttributeObject(paletteDef, minValue, minObj, count);
+
+ squashObj = createPaletteAttributeObject(
+ paletteDef,
+ true,
+ squashObj,
+ count
+ );
+ }
+
+ [palObj, minObj, maxObj, squashObj].forEach(obj => {
+ if (obj.isActive) {
+ attrArray.push({
+ id: obj.key === 'custom' ? 'palette' : obj.key,
+ value: obj.value
+ });
+ }
+ });
+ return attrArray;
+}
+const createPaletteAttributeObject = function(def, value, attrObj, count) {
+ const key = attrObj.key;
+ const attrArray = attrObj.array;
+ let hasAtLeastOnePair = attrObj.isActive;
+ value = isArray(value) ? value.join(',') : value;
+ if (def[key] && value) {
+ attrArray.push(value);
+ hasAtLeastOnePair = true;
+ } else if (count > 1) {
+ attrArray.push('');
+ }
+ return lodashAssign({}, attrObj, {
+ array: attrArray,
+ isActive: hasAtLeastOnePair,
+ value: attrArray.join(';')
+ });
+};
+export function loadPalettes(permlinkState, state) {
+ var stateArray = [{ stateStr: 'l', groupStr: 'active' }];
+ if (!isSupported()) {
+ return self;
+ }
+ if (permlinkState.l1) {
+ stateArray = [
+ { stateStr: 'l', groupStr: 'active' },
+ { stateStr: 'l1', groupStr: 'activeB' }
+ ];
+ }
+ lodashEach(stateArray, stateObj => {
+ lodashEach(state.layers[stateObj.groupStr], function(layerDef) {
+ var layerId = layerDef.id;
+ var min = [];
+ var max = [];
+ var squash = [];
+ var count = 0;
+ if (layerDef.custom) {
+ lodashEach(layerDef.custom, function(value, index) {
+ try {
+ let newPalettes = setCustomSelector(
+ layerId,
+ value,
+ index,
+ stateObj.groupStr,
+ state
+ );
+ state = update(state, {
+ palettes: { [stateObj.groupStr]: { $set: newPalettes } }
+ });
+ } catch (error) {
+ console.warn(' Invalid palette: ' + value);
+ }
+ });
+ }
+ if (layerDef.min) {
+ lodashEach(layerDef.min, function(value, index) {
+ try {
+ min.push(
+ findPaletteExtremeIndex(
+ layerId,
+ 'min',
+ value,
+ index,
+ stateObj.groupStr,
+ state
+ )
+ );
+ } catch (error) {
+ console.warn('Unable to set min: ' + value);
+ }
+ });
+ }
+ if (layerDef.max) {
+ lodashEach(layerDef.max, function(value, index) {
+ try {
+ max.push(
+ findPaletteExtremeIndex(
+ layerId,
+ 'max',
+ value,
+ index,
+ stateObj.groupStr,
+ state
+ )
+ );
+ } catch (error) {
+ console.warn('Unable to set max index: ' + value);
+ }
+ });
+ }
+ if (layerDef.squash) {
+ squash = layerDef.squash;
+ }
+
+ if (min.length > 0 || max.length > 0) {
+ count = getCount(layerId, state);
+ for (var i = 0; i < count; i++) {
+ var vmin = min.length > 0 ? min[i] : undefined;
+ var vmax = max.length > 0 ? max[i] : undefined;
+ var vsquash = squash.length > 0 ? squash[i] : undefined;
+ let props = { min: vmin, max: vmax, squash: vsquash };
+ let newPalettes = setRangeSelector(
+ layerId,
+ props,
+ i,
+ state.palettes[stateObj.groupStr],
+ state
+ );
+ state = update(state, {
+ palettes: { [stateObj.groupStr]: { $set: newPalettes } }
+ });
+ }
+ }
+ });
+ });
+ return state;
+}