Interactive interface for browsing global, full-resolution satellite imagery
import {
cloneDeep as lodashCloneDeep,
findIndex as lodashFindIndex,
omitBy as lodashOmitBy,
} from 'lodash';
import update from 'immutability-helper';
import {
ADD_LAYER,
INIT_SECOND_LAYER_GROUP,
SYNC_SECOND_LAYER_GROUP,
REORDER_LAYERS,
TOGGLE_LAYER_VISIBILITY,
TOGGLE_COLLAPSE_OVERLAY_GROUP,
TOGGLE_OVERLAY_GROUP_VISIBILITY,
TOGGLE_OVERLAY_GROUPS,
REMOVE_LAYER,
UPDATE_OPACITY,
ADD_LAYERS_FOR_EVENT,
ADD_GRANULE_LAYER_DATES,
UPDATE_GRANULE_LAYER_OPTIONS,
UPDATE_GRANULE_LAYER_GEOMETRY,
CHANGE_GRANULE_SATELLITE_INSTRUMENT_GROUP,
REORDER_OVERLAY_GROUPS,
REMOVE_GROUP,
UPDATE_DDV_LAYER,
UPDATE_COLLECTION,
ADD_GRANULE_DATE_RANGES,
ADD_TEMPO_DATE_RANGES,
UPDATE_DAY_COUNT,
} from './constants';
import {
SET_CUSTOM as SET_CUSTOM_PALETTE,
CLEAR_CUSTOM as CLEAR_CUSTOM_PALETTE,
SET_THRESHOLD_RANGE_SQUASH_AND_NOCLIP,
SET_DISABLED_CLASSIFICATION,
} from '../palettes/constants';
import {
CLEAR_VECTORSTYLE,
SET_VECTORSTYLE,
SET_FILTER_RANGE,
} from '../vector-styles/constants';
import { resetLayers } from './selectors';
import { getOverlayGroups } from './util';
import safeLocalStorage from '../../util/local-storage';
const { GROUP_OVERLAYS } = safeLocalStorage.keys;
const groupState = {
groupOverlays: true,
layers: [],
overlayGroups: [],
prevLayers: [],
granuleFootprints: {},
granuleLayers: {},
granulePlatform: '',
};
export const initialState = {
active: { ...groupState },
activeB: { ...groupState },
collections: {},
layerConfig: {},
startingLayers: [],
granuleFootprints: {},
eventLayers: [],
};
export function getInitialState(config) {
const { layers: layerConfig, defaults } = config;
const startingLayers = resetLayers(config);
const groupsALocalStorage = safeLocalStorage.getItem(GROUP_OVERLAYS) !== 'disabled';
const updatedState = {
...initialState,
active: {
...initialState.active,
groupOverlays: groupsALocalStorage,
layers: startingLayers,
overlayGroups: getOverlayGroups(startingLayers),
},
layerConfig,
startingLayers: defaults.startingLayers,
};
return updatedState;
}
export function layerReducer(state = initialState, action) {
const compareState = action.activeString;
const getPrevOverlayGroups = () => state[compareState].overlayGroups;
const getLayerIndex = () => {
const activeLayers = state[compareState].layers;
return lodashFindIndex(activeLayers, { id: action.id || action.layerId });
};
const getGroupIndex = () => lodashFindIndex(
getPrevOverlayGroups(),
{ groupName: action.groupName },
);
switch (action.type) {
case ADD_LAYER:
case REORDER_LAYERS:
case TOGGLE_OVERLAY_GROUP_VISIBILITY:
return update(state, {
[compareState]: {
$merge: {
layers: action.layers,
overlayGroups: getOverlayGroups(action.layers, getPrevOverlayGroups()),
prevLayers: [],
},
},
});
case REMOVE_LAYER:
case REMOVE_GROUP:
return update(state, {
[compareState]: {
$merge: {
layers: action.layers,
overlayGroups: getOverlayGroups(action.layers, getPrevOverlayGroups()),
prevLayers: [],
granuleLayers: action.granuleLayers,
},
},
});
case ADD_LAYERS_FOR_EVENT:
case REORDER_OVERLAY_GROUPS: {
const handleDefinedEventLayers = action?.eventLayers?.length
? { $set: action.eventLayers }
: { $push: [] };
return update(state, {
[compareState]: {
layers: { $set: action.layers },
overlayGroups: { $set: action.overlayGroups },
prevLayers: { $set: [] },
},
eventLayers: action.eventLayers === undefined
? { $push: [] }
: handleDefinedEventLayers,
});
}
case TOGGLE_OVERLAY_GROUPS:
return update(state, {
[compareState]: {
$merge: {
groupOverlays: action.groupOverlays,
layers: action.layers,
overlayGroups: action.overlayGroups,
prevLayers: action.prevLayers,
},
},
});
case TOGGLE_COLLAPSE_OVERLAY_GROUP: {
const groupIndex = getGroupIndex();
if (groupIndex < 0) return state;
return update(state, {
[compareState]: {
overlayGroups: {
[groupIndex]: {
collapsed: { $set: action.collapsed },
},
},
},
});
}
case INIT_SECOND_LAYER_GROUP:
return {
...state,
activeB: lodashCloneDeep(state.active),
};
// Apply changes made to A while compare was off: add layers added to A,
// and remove layers removed from A. Layers the user removed from or
// added to B during compare are left alone.
case SYNC_SECOND_LAYER_GROUP: {
const aLayerIds = new Set(state.active.layers.map((l) => l.id));
const bLayerIds = new Set(state.activeB.layers.map((l) => l.id));
const lastExitIds = new Set(action.lastExitALayerIds || []);
const newLayers = state.active.layers.filter(
(l) => !bLayerIds.has(l.id) && !lastExitIds.has(l.id),
);
const isRemovedFromA = (id) => lastExitIds.has(id) && !aLayerIds.has(id);
const keptLayers = state.activeB.layers.filter((l) => !isRemovedFromA(l.id));
if (!newLayers.length && keptLayers.length === state.activeB.layers.length) return state;
const layers = [...keptLayers, ...lodashCloneDeep(newLayers)];
return {
...state,
activeB: {
...state.activeB,
layers,
overlayGroups: getOverlayGroups(layers, state.activeB.overlayGroups),
granuleLayers: lodashOmitBy(
state.activeB.granuleLayers,
(_granuleLayer, id) => isRemovedFromA(id),
),
},
};
}
case TOGGLE_LAYER_VISIBILITY:
if (getLayerIndex() === -1) return state;
return update(state, {
[compareState]: {
layers: {
[getLayerIndex()]: {
visible: {
$set: action?.visible,
},
},
},
prevLayers: { $set: [] },
},
});
case UPDATE_OPACITY:
return update(state, {
[compareState]: {
layers: {
[getLayerIndex()]: {
opacity: { $set: action.opacity },
},
},
},
});
case UPDATE_DAY_COUNT: {
const layerIndex = getLayerIndex();
if (layerIndex < 0) return state;
return update(state, {
[compareState]: {
layers: {
[layerIndex]: {
dayCount: { $set: action.dayCount },
},
},
},
});
}
case SET_THRESHOLD_RANGE_SQUASH_AND_NOCLIP:
case SET_DISABLED_CLASSIFICATION: {
const layerIndex = getLayerIndex();
if (layerIndex < 0) return state;
return update(state, {
[compareState]: {
layers: {
[layerIndex]: {
$merge: action.props,
},
},
},
});
}
case CLEAR_CUSTOM_PALETTE: {
const layerIndex = getLayerIndex();
if (layerIndex < 0) { return state; }
return update(state, {
[compareState]: {
layers: {
[layerIndex]: {
custom: {
$set: undefined,
},
},
},
},
});
}
case SET_CUSTOM_PALETTE: {
return update(state, {
[compareState]: {
layers: {
[getLayerIndex()]: {
custom: {
$set: [action.paletteId],
},
},
},
},
});
}
case SET_FILTER_RANGE: {
return update(state, {
[compareState]: {
layers: {
[getLayerIndex()]: {
$merge: action.props,
},
},
},
});
}
case CLEAR_VECTORSTYLE: {
return update(state, {
[compareState]: {
layers: {
[getLayerIndex()]: {
custom: {
$set: undefined,
},
},
},
},
});
}
case SET_VECTORSTYLE: {
return update(state, {
[compareState]: {
layers: {
[getLayerIndex()]: {
custom: {
$set: action.vectorStyleId,
},
},
},
},
});
}
case ADD_GRANULE_LAYER_DATES: {
const {
id, activeKey, dates, granuleFootprints, granulePlatform, count,
} = action;
return update(state, {
[activeKey]: {
granuleLayers: {
$merge: {
[id]: {
dates,
count,
granuleFootprints,
granulePlatform,
},
},
},
granulePlatform: {
$set: granulePlatform,
},
granuleFootprints: {
$set: granuleFootprints,
},
},
});
}
case ADD_GRANULE_DATE_RANGES: {
const {
granuleDateRanges,
} = action;
const layerIndex = getLayerIndex();
if (layerIndex < 0) return state;
return update(state, {
[compareState]: {
layers: {
[layerIndex]: {
granuleDateRanges: { $set: granuleDateRanges },
},
},
},
});
}
case UPDATE_GRANULE_LAYER_OPTIONS: {
const {
id, activeKey, count, dates,
} = action;
// The entry may not exist yet if the map hasn't built this layer
return update(state, {
[activeKey]: {
granuleLayers: {
[id]: {
$apply: (granuleLayer = {}) => ({
...granuleLayer,
count,
dates: dates || granuleLayer.dates,
}),
},
},
},
});
}
case UPDATE_GRANULE_LAYER_GEOMETRY: {
const {
id, activeKey, dates, granuleFootprints, count,
} = action;
return update(state, {
[activeKey]: {
granuleLayers: {
[id]: {
$merge: {
dates,
granuleFootprints,
count,
},
},
},
granuleFootprints: {
$set: granuleFootprints,
},
},
});
}
case CHANGE_GRANULE_SATELLITE_INSTRUMENT_GROUP:
return update(state, {
[action.activeKey]: {
granulePlatform: {
$set: action.granulePlatform,
},
granuleFootprints: {
$set: action.geometry,
},
},
});
case UPDATE_COLLECTION: {
const updates = {};
action.payload.forEach((collection) => {
const {
id, date, type, version, projection,
} = collection;
// If the layer doesn't exist, initialize it
if (!state.collections[id]) {
updates[id] = {
$set: {
dates: [{
version,
type,
date,
projection,
}],
},
};
} else {
// If the layer exists, prepare to push to the dates array
const newEntry = {
date,
type,
version,
projection,
};
updates[id] = {
dates: { $push: [newEntry] },
};
}
});
return update(state, {
collections: {
$apply: (collections) => update(collections, updates),
},
});
}
// This is required because to update band combinations we need to actually remove
// and re-add these layers. This case sets the ddv layer back to its original index before being
// removed and added again
case UPDATE_DDV_LAYER: {
const { layerIndex, id, layers } = action;
const indexToMove = layers.findIndex((activeLayer) => activeLayer.id === id);
const [layerToMove] = layers.splice(indexToMove, 1);
layers.splice(layerIndex, 0, layerToMove);
return update(state, {
[compareState]: {
$merge: {
layers,
overlayGroups: getOverlayGroups(layers, getPrevOverlayGroups()),
prevLayers: [],
layerIndex: action.layerIndex,
},
},
});
}
case ADD_TEMPO_DATE_RANGES: {
const {
tempoDateRanges,
} = action;
const layerIndex = getLayerIndex();
if (layerIndex < 0) return state;
return update(state, {
[compareState]: {
layers: {
[layerIndex]: {
tempoDateRanges: { $set: tempoDateRanges },
},
},
},
});
}
default:
return state;
}
}