Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Reorganize vector interation code #2144

Benjamin King97dafa386f6c
diff --git a/web/js/map/ui.js b/web/js/map/ui.js
index 978216108..db26c9799 100644
--- a/web/js/map/ui.js
+++ b/web/js/map/ui.js
@@ -51,6 +51,7 @@ import { CLEAR_ROTATE, RENDERED, UPDATE_MAP_UI, FITTED_TO_LEADING_EXTENT } from
import { getLeadingExtent } from '../modules/map/util';
import vectorDialog from '../containers/vector-dialog';
import { setSelected } from '../modules/vector-styles/actions';
+import { onMapClickGetVectorFeatures, updateVectorSelection } from '../modules/vector-styles/util';
export function mapui(models, config, store, ui) {
var layerBuilder, createLayer;
@@ -88,7 +89,7 @@ export function mapui(models, config, store, ui) {
self.layerKey = layerBuilder.layerKey;
createLayer = self.createLayer = layerBuilder.createLayer;
self.promiseDay = precache.promiseDay;
-
+ self.selectedVectors = {};
/**
* Suscribe to redux store and listen for
* specific action types
@@ -138,6 +139,15 @@ export function mapui(models, config, store, ui) {
case vectorStyleConstants.CLEAR_VECTORSTYLE:
case CALCULATE_RESPONSIVE_STATE:
return onResize();
+ case vectorStyleConstants.SET_SELECTED_VECTORS:
+ let newSelection = action.payload;
+ let state = store.getState();
+ let { compare, layers } = state;
+ let activeLayerStr = compare.activeString;
+ let lastSelection = self.selectedVectors;
+ updateVectorSelection(action.payload, lastSelection, layers[activeLayerStr], 'selection', state);
+ self.selectedVectors = newSelection;
+ return;
case CHANGE_UNITS:
return toggleMeasurementUnits(action.value);
case USE_GREAT_CIRCLE:
@@ -990,63 +1000,7 @@ export function mapui(models, config, store, ui) {
if (store.getState().data.active) ui.data.onActivate();
};
map.on('rendercomplete', onRenderComplete);
- map.on('click', function (e) {
- let metaArray = [];
- let selectedIdArray = [];
- let selectedFeatures = [];
- let defs = [];
- const state = store.getState();
- const vectorStyles = config.vectorStyles;
- map.forEachFeatureAtPixel(e.pixel, function (feature, layer) {
- const def = lodashGet(layer, 'wv.def');
- if (!def) return;
- if (def.vectorData && def.vectorData.id && def.title) {
- let features = feature.getProperties();
- const vectorDataId = def.vectorData.id;
- const data = config.vectorData[vectorDataId];
- const properties = data.mvt_properties;
- const titleKey = lodashFind(properties, { Function: 'Identify' })['Identifier'];
-
- const obj = {
- legend: properties,
- // features: orderObjectToArray(features, data.mvt_properties),
- features: features,
- id: vectorDataId,
- title: def.title || def.id,
- featureTitle: features[titleKey]
- };
- metaArray.push(obj);
- selectedIdArray.push(vectorDataId);
- selectedFeatures.push(feature.ol_uid);
- defs.push(def);
- }
- });
- if (metaArray.length) {
- //https://stackoverflow.com/a/23600960/4589331
- const dialogObject = groupBy(metaArray, 'id');
- defs.forEach((def) => {
- setStyleFunction(def, def.vectorStyle.id, vectorStyles, null, state, { id: selectedIdArray, features: selectedFeatures });
- })
-
- store.dispatch(openCustomContent('Vector-dialog' + e.pixel[0] + e.pixel[1],
- {
- backdrop: false,
- clickableBehindModal: true,
- desktopOnly: true,
- isDraggable: true,
- wrapClassName: 'vector-modal-wrap',
- modalClassName: 'vector-modal light',
- CompletelyCustomModal: vectorDialog,
- customProps: { vectorMetaObject: dialogObject },
- onClose: () => {
- defs.forEach((def) => {
- setStyleFunction(def, def.vectorStyle.id, vectorStyles, null, state, { id: selectedIdArray, features: selectedFeatures, reset: true })
- })
- }
- }
- ));
- };
- });
+ map.on('click', (e) => onMapClickGetVectorFeatures(e, map, store));
measureTools[proj.crs] = measure(map, self.events, store);
return map;
diff --git a/web/js/modules/vector-styles/actions.js b/web/js/modules/vector-styles/actions.js
index bd37956fd..4d98aa961 100644
--- a/web/js/modules/vector-styles/actions.js
+++ b/web/js/modules/vector-styles/actions.js
@@ -1,7 +1,9 @@
import {
SET_FILTER_RANGE,
CLEAR_VECTORSTYLE,
- SET_VECTORSTYLE
+ SET_VECTORSTYLE,
+ SET_SELECTED_VECTORS,
+
} from './constants';
import {
setRange as setRangeSelector,
@@ -34,6 +36,7 @@ export function setFilterRange(layerId, props, index, groupName) {
});
};
}
+
/**
* Action to set custom vectorStyle
*
@@ -89,3 +92,14 @@ export function clearStyle(layer, vectorStyleId, groupName) {
});
};
}
+/**
+ * Action to select Vectors
+ *
+ * @param {Object} payload Keys are layer ids that contain arrays of feature ids
+ */
+export function selectVectorFeatures(payload) {
+ return {
+ type: SET_SELECTED_VECTORS,
+ payload
+ }
+}
\ No newline at end of file
diff --git a/web/js/modules/vector-styles/constants.js b/web/js/modules/vector-styles/constants.js
index 693e1ab69..7d793ae2c 100644
--- a/web/js/modules/vector-styles/constants.js
+++ b/web/js/modules/vector-styles/constants.js
@@ -2,3 +2,4 @@ export const VECTORSTYLE_CHANGE = 'VECTORSTYLES/CHANGE_OF_ACTIVE_VECTORSTYLE';
export const SET_FILTER_RANGE = 'VECTORSTYLES/SET_FILTER_RANGE';
export const CLEAR_VECTORSTYLE = 'VECTORSTYLES/CLEAR_VECTORSTYLE';
export const SET_VECTORSTYLE = 'VECTORSTYLES/SET_VECTORSTYLE';
+export const SET_SELECTED_VECTORS = 'VECTORSTYLES/SET_SELECTED';
diff --git a/web/js/modules/vector-styles/reducers.js b/web/js/modules/vector-styles/reducers.js
index aa5b9f4ef..d263d9a15 100644
--- a/web/js/modules/vector-styles/reducers.js
+++ b/web/js/modules/vector-styles/reducers.js
@@ -2,12 +2,14 @@ import { assign as lodashAssign, get as lodashGet } from 'lodash';
import {
CLEAR_VECTORSTYLE,
SET_VECTORSTYLE,
- SET_FILTER_RANGE
+ SET_FILTER_RANGE,
+ SET_SELECTED_VECTORS
} from './constants';
export const defaultVectorStyleState = {
custom: {},
active: {},
- activeB: {}
+ activeB: {},
+ selected: {}
};
export function getInitialVectorStyleState(config) {
const custom = lodashGet(config, 'vectorStyles') || {};
@@ -31,6 +33,10 @@ export function vectorStyleReducer(state = defaultVectorStyleState, action) {
return lodashAssign({}, state, {
[groupName]: action.vectorStyles
});
+ case SET_SELECTED_VECTORS:
+ return lodashAssign({}, state, {
+ selected: action.payload
+ });
default:
return state;
}
diff --git a/web/js/modules/vector-styles/selectors.js b/web/js/modules/vector-styles/selectors.js
index d69702fed..c8ce0dc82 100644
--- a/web/js/modules/vector-styles/selectors.js
+++ b/web/js/modules/vector-styles/selectors.js
@@ -73,8 +73,8 @@ export function setRange(layerId, props, index, palettes, state) {
return (layerId, props, index, palettes, state);
}
-export function setStyleFunction(def, vectorStyleId, vectorStyles, layer, state, selected) {
- selected = selected || {};
+export function setStyleFunction(def, vectorStyleId, vectorStyles, layer, state, interactionsObject) {
+ interactionsObject = interactionsObject || {};
var styleFunction;
var layerId = def.id;
var glStyle = vectorStyles[layerId];
@@ -126,6 +126,7 @@ export function setStyleFunction(def, vectorStyleId, vectorStyles, layer, state,
// Apply mapbox-gl styles
styleFunction = stylefunction(layer, glStyle, vectorStyleId);
// Filter Orbit Tracks
+ console.log('lol')
if (glStyle.name === 'Orbit Tracks') {
// Filter time by 5 mins
layer.setStyle(function (feature, resolution) {
@@ -138,11 +139,12 @@ export function setStyleFunction(def, vectorStyleId, vectorStyles, layer, state,
return styleFunction(feature, resolution);
}
});
- } else if (glStyle.name === 'SEDAC' && selected.id && selected.id.includes(def.vectorData.id)) {
- const selectedFeatures = selected.features;
+ } else if (glStyle.name === 'SEDAC' && interactionsObject.type === 'selection' && interactionsObject.features.length) {
+ const selectedFeatures = interactionsObject.features;
+
layer.setStyle(function (feature, resolution) {
const selectedFeature = selectedFeatures.includes(feature.ol_uid);
- if (selectedFeature && !selected.reset) {
+ if (selectedFeature) {
return selectedStyleFunction(feature, styleFunction(feature, resolution));
} else {
return styleFunction(feature, resolution);
diff --git a/web/js/modules/vector-styles/util.js b/web/js/modules/vector-styles/util.js
index c919f28b7..54732dedb 100644
--- a/web/js/modules/vector-styles/util.js
+++ b/web/js/modules/vector-styles/util.js
@@ -1,7 +1,14 @@
import {
- assign as lodashAssign
+ assign as lodashAssign,
+ find as lodashFind,
+ get as lodashGet,
+ groupBy as lodashGroupBy
} from 'lodash';
import { Stroke, Style, Fill, Circle } from 'ol/style';
+import vectorDialog from '../../containers/vector-dialog';
+import { setStyleFunction } from './selectors';
+import { openCustomContent } from '../modal/actions';
+import { selectVectorFeatures } from './actions';
export function getVectorStyleAttributeArray(layer) {
var isCustomActive = false;
@@ -83,4 +90,76 @@ export function selectedStyleFunction(feature, styleArray) {
return style;
}
});
+}
+export function onMapClickGetVectorFeatures(e, map, store) {
+ let metaArray = [];
+ let selected = {};
+ let defs = [];
+ const state = store.getState();
+ const lastSelection = state.vectorStyles.selected;
+ const config = state.config;
+ // const vectorStyles = config.vectorStyles;
+ map.forEachFeatureAtPixel(e.pixel, function (feature, layer) {
+ const def = lodashGet(layer, 'wv.def');
+ if (!def) return;
+
+ if (def.vectorData && def.vectorData.id && def.title) {
+ const layerId = def.id;
+ if (!selected[layerId]) selected[layerId] = [];
+ let features = feature.getProperties();
+ const vectorDataId = def.vectorData.id;
+ const data = config.vectorData[vectorDataId];
+ const properties = data.mvt_properties;
+ const titleKey = lodashFind(properties, { Function: 'Identify' })['Identifier'];
+
+ const obj = {
+ legend: properties,
+ features: features,
+ id: vectorDataId,
+ title: def.title || def.id,
+ featureTitle: features[titleKey]
+ };
+ metaArray.push(obj);
+ selected[layerId].push(feature.ol_uid);
+ }
+ });
+ if (Object.entries(selected).length || Object.entries(lastSelection).length) {
+ store.dispatch(selectVectorFeatures(selected));
+ }
+ if (metaArray.length) {
+ store.dispatch(openCustomContent('Vector-dialog' + e.pixel[0] + e.pixel[1],
+ {
+ backdrop: false,
+ clickableBehindModal: true,
+ desktopOnly: true,
+ isDraggable: true,
+ wrapClassName: 'vector-modal-wrap',
+ modalClassName: 'vector-modal light',
+ CompletelyCustomModal: vectorDialog,
+ customProps: { vectorMetaObject: lodashGroupBy(metaArray, 'id') },
+ onClose: () => {
+ store.dispatch(selectVectorFeatures({}));
+ }
+ }
+ ));
+ };
+}
+export function updateVectorSelection(selectionObj, lastSelection, layers, type, state) {
+ const vectorStyles = state.config.vectorStyles
+ for (let [key, featureIdArray] of Object.entries(selectionObj)) {
+ const def = lodashFind(layers, { id: key });
+ setStyleFunction(def, def.vectorStyle.id, vectorStyles, null, state, { type, features: featureIdArray });
+ if (lastSelection[key]) delete lastSelection[key];
+ }
+ for (let [key] of Object.entries(lastSelection)) {
+ const def = lodashFind(layers, { id: key });
+ setStyleFunction(
+ def,
+ def.vectorStyle.id,
+ vectorStyles,
+ null,
+ state
+ );
+ }
+
}
\ No newline at end of file