Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

add hover capabilities #2144

Benjamin Kingd37392d02956
diff --git a/web/js/map/ui.js b/web/js/map/ui.js
index db26c9799..5c82f46f6 100644
--- a/web/js/map/ui.js
+++ b/web/js/map/ui.js
@@ -51,7 +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';
+import { onMapClickGetVectorFeatures, updateVectorSelection, onMapHoverGetVectorFeatures } from '../modules/vector-styles/util';
export function mapui(models, config, store, ui) {
var layerBuilder, createLayer;
@@ -90,6 +90,7 @@ export function mapui(models, config, store, ui) {
createLayer = self.createLayer = layerBuilder.createLayer;
self.promiseDay = precache.promiseDay;
self.selectedVectors = {};
+ self.hoveredVectors = {};
/**
* Suscribe to redux store and listen for
* specific action types
@@ -140,13 +141,15 @@ export function mapui(models, config, store, ui) {
case CALCULATE_RESPONSIVE_STATE:
return onResize();
case vectorStyleConstants.SET_SELECTED_VECTORS:
+ case vectorStyleConstants.SET_HOVERED_VECTORS:
+ const type = action.type === vectorStyleConstants.SET_SELECTED_VECTORS ? 'selection' : 'hovered';
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;
+ let lastSelectionStr = type === 'selection' ? 'selectedVectors' : 'hoveredVectors';
+ updateVectorSelection(action.payload, self[lastSelectionStr], layers[activeLayerStr], type, state);
+ self[lastSelectionStr] = newSelection;
return;
case CHANGE_UNITS:
return toggleMeasurementUnits(action.value);
@@ -1127,6 +1130,7 @@ export function mapui(models, config, store, ui) {
return;
}
pixels = map.getEventPixel(e.originalEvent);
+ onMapHoverGetVectorFeatures(pixels, map, store)
coords = map.getCoordinateFromPixel(pixels);
if (!coords) return;
diff --git a/web/js/modules/vector-styles/actions.js b/web/js/modules/vector-styles/actions.js
index 4d98aa961..01c92642e 100644
--- a/web/js/modules/vector-styles/actions.js
+++ b/web/js/modules/vector-styles/actions.js
@@ -3,7 +3,7 @@ import {
CLEAR_VECTORSTYLE,
SET_VECTORSTYLE,
SET_SELECTED_VECTORS,
-
+ SET_HOVERED_VECTORS
} from './constants';
import {
setRange as setRangeSelector,
@@ -102,4 +102,10 @@ export function selectVectorFeatures(payload) {
type: SET_SELECTED_VECTORS,
payload
}
+}
+export function hoverVectorFeatures(payload) {
+ return {
+ type: SET_HOVERED_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 7d793ae2c..d5eed196d 100644
--- a/web/js/modules/vector-styles/constants.js
+++ b/web/js/modules/vector-styles/constants.js
@@ -3,3 +3,4 @@ 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';
+export const SET_HOVERED_VECTORS = 'VECTORSTYLES/SET_HOVERED_VECTORS';
\ No newline at end of file
diff --git a/web/js/modules/vector-styles/reducers.js b/web/js/modules/vector-styles/reducers.js
index d263d9a15..894fac2b8 100644
--- a/web/js/modules/vector-styles/reducers.js
+++ b/web/js/modules/vector-styles/reducers.js
@@ -3,13 +3,15 @@ import {
CLEAR_VECTORSTYLE,
SET_VECTORSTYLE,
SET_FILTER_RANGE,
- SET_SELECTED_VECTORS
+ SET_SELECTED_VECTORS,
+ SET_HOVERED_VECTORS
} from './constants';
export const defaultVectorStyleState = {
custom: {},
active: {},
activeB: {},
- selected: {}
+ selected: {},
+ hovered: {}
};
export function getInitialVectorStyleState(config) {
const custom = lodashGet(config, 'vectorStyles') || {};
@@ -37,6 +39,10 @@ export function vectorStyleReducer(state = defaultVectorStyleState, action) {
return lodashAssign({}, state, {
selected: action.payload
});
+ case SET_HOVERED_VECTORS:
+ return lodashAssign({}, state, {
+ hovered: action.payload
+ });
default:
return state;
}
diff --git a/web/js/modules/vector-styles/selectors.js b/web/js/modules/vector-styles/selectors.js
index 2c678724e..a6643dcbb 100644
--- a/web/js/modules/vector-styles/selectors.js
+++ b/web/js/modules/vector-styles/selectors.js
@@ -74,14 +74,15 @@ export function setRange(layerId, props, index, palettes, state) {
return (layerId, props, index, palettes, state);
}
-export function setStyleFunction(def, vectorStyleId, vectorStyles, layer, state, interactionsObject) {
- interactionsObject = interactionsObject || {};
+export function setStyleFunction(def, vectorStyleId, vectorStyles, layer, state) {
var styleFunction;
var layerId = def.id;
var glStyle = vectorStyles[layerId];
var olMap = lodashGet(state, 'map.ui.selected');
var layerState = state.layers;
const activeLayerStr = state.compare.activeString;
+ const selected = state.vectorStyles.selected;
+ const hovered = state.vectorStyles.hovered;
var activeLayers = getLayers(
layerState[activeLayerStr],
{},
@@ -139,16 +140,19 @@ export function setStyleFunction(def, vectorStyleId, vectorStyles, layer, state,
return styleFunction(feature, resolution);
}
});
- } else if (glStyle.name === 'SEDAC' && interactionsObject.type === 'selection' && interactionsObject.features.length) {
- const selectedFeatures = interactionsObject.features;
-
+ } else if (glStyle.name === 'SEDAC' &&
+ ((selected[layerId] && selected[layerId].length) ||
+ (hovered[layerId] && hovered[layerId].length))) {
+ const selectedFeatures = selected[layerId];
+ const hoveredFeatures = hovered[layerId];
layer.setStyle(function (feature, resolution) {
const data = state.config.vectorData[def.vectorData.id];
const properties = data.mvt_properties;
const titleKey = lodashFind(properties, { Function: 'Identify' })['Identifier'];
const id = feature.getProperties()[titleKey];
- const selectedFeature = selectedFeatures.includes(id);
- if (selectedFeature) {
+ if (selectedFeatures && selectedFeatures.includes(id)) {
+ return selectedStyleFunction(feature, styleFunction(feature, resolution));
+ } else if (hoveredFeatures && hoveredFeatures.includes(id)) {
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 96e67dc91..53026340b 100644
--- a/web/js/modules/vector-styles/util.js
+++ b/web/js/modules/vector-styles/util.js
@@ -2,13 +2,14 @@ import {
assign as lodashAssign,
find as lodashFind,
get as lodashGet,
- groupBy as lodashGroupBy
+ groupBy as lodashGroupBy,
+ isEqual as lodashIsEqual
} 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';
+import { selectVectorFeatures, hoverVectorFeatures } from './actions';
export function getVectorStyleAttributeArray(layer) {
var isCustomActive = false;
@@ -94,7 +95,6 @@ export function selectedStyleFunction(feature, styleArray) {
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;
@@ -144,11 +144,36 @@ export function onMapClickGetVectorFeatures(e, map, store) {
));
};
}
+export function onMapHoverGetVectorFeatures(pixels, map, store) {
+ let selected = {};
+ const state = store.getState()
+ const lastSelection = state.vectorStyles.hovered;
+ const config = state.config;
+ map.forEachFeatureAtPixel(pixels, 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 title = features[titleKey];
+ if (!selected[layerId].includes(title)) selected[layerId].push(title);
+ }
+ });
+ if (lodashIsEqual(lastSelection, selected)) return;
+ if (Object.entries(selected).length || Object.entries(lastSelection).length) {
+ store.dispatch(hoverVectorFeatures(selected));
+ }
+}
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 });
+ setStyleFunction(def, def.vectorStyle.id, vectorStyles, null, state);
if (lastSelection[key]) delete lastSelection[key];
}
for (let [key] of Object.entries(lastSelection)) {