Interactive interface for browsing global, full-resolution satellite imagery
import {
get as lodashGet,
isUndefined as lodashIsUndefined,
each as lodashEach,
find as lodashFind,
cloneDeep as lodashCloneDeep,
} from 'lodash';
import update from 'immutability-helper';
import { containsCoordinate } from 'ol/extent';
import { stylefunction } from 'ol-mapbox-style';
import {
getMinValue, getMaxValue, selectedStyleFunction,
} from './util';
import {
isActive as isPaletteActive,
getLookup as getPaletteLookup,
} from '../palettes/selectors';
import util from '../../util/util';
import { fetchVectorstyle } from '../layers/util';
// Array of vector layer IDs to have their vectorstyle live fetched from `web/config`
// * For use in local dev environment only *
const liveFetchLayers = [];
/**
* Get OpenLayers layers from state that were created from WV vector
* layer definitions. NOTE: This currently also will include the associate WMS
* breakpoint layers as well.
*
* @param {*} state
* @returns
*/
export function getVectorLayers(state) {
const { map: { ui: { selected } } } = state;
const layerGroups = selected.getLayers().getArray();
return layerGroups.reduce((prev, layerGroup) => {
const isVector = lodashGet(layerGroup, 'wv.def.type') === 'vector';
if (!isVector) return prev;
const layers = layerGroup.getLayersArray ? layerGroup.getLayersArray() : layerGroup;
return [...prev, ...layers];
}, []);
}
export function getAllVectorStyles(layerId, index, state) {
const { config, vectorStyles } = state;
const name = lodashGet(config, `layers.${layerId}.vectorStyle.id`);
let vectorStyle = vectorStyles.custom[name];
if (!vectorStyle) {
throw new Error(`${name} Is not a rendered vectorStyle`);
}
if (!lodashIsUndefined(index)) {
if (vectorStyle.layers) {
vectorStyle = vectorStyle.layers[index];
}
}
return vectorStyle;
}
/**
* Gets a single colormap (entries / legend combo)
*
* @param layerId {string} The ID of the layer
* @param index {Number} The index of the colormap for this layer, default 0
* object.
* @return {object} object including the entries and legend
*/
export function getVectorStyle(layerId, indexInt, state) {
const index = lodashIsUndefined(indexInt) ? 0 : indexInt;
const renderedVectorStyle = lodashGet(
state,
`vectorStyles.${layerId}.layers.${index}`,
);
if (renderedVectorStyle) {
return renderedVectorStyle;
}
return getAllVectorStyles(layerId, index, state);
}
export function findIndex(layerId, type, value, indexInt, state) {
let index = indexInt;
index = index || 0;
const { values } = getVectorStyle(layerId, index, state).entries;
let result;
lodashEach(values, (check, indexArg) => {
const min = getMinValue(check);
const max = getMaxValue(check);
if (type === 'min' && value === min) {
result = indexArg;
return false;
}
if (type === 'max' && value === max) {
result = indexArg;
return false;
}
return true;
});
return result;
}
export function setRange(layerId, props, index, palettes, state) {
// Placeholder filter range function
return {
layerId, props, index, palettes, state,
};
}
const updateGlStylePalette = (glStyle, rgbPalette) => {
for (let i = 0; i < glStyle.layers.length; i += 1) {
const thisPaintObj = glStyle.layers[i].paint;
if (Object.prototype.hasOwnProperty.call(thisPaintObj, 'line-color')) {
thisPaintObj['line-color'] = rgbPalette;
}
if (Object.prototype.hasOwnProperty.call(thisPaintObj, 'circle-color')) {
thisPaintObj['circle-color'] = rgbPalette;
}
if (Object.prototype.hasOwnProperty.call(thisPaintObj, 'fill-color')) {
thisPaintObj['fill-color'] = rgbPalette;
}
if (Object.prototype.hasOwnProperty.call(thisPaintObj, 'line-width')) {
thisPaintObj['line-width'] = 2;
}
}
return glStyle;
};
const updateDisabled = (glStyle, lookup) => {
for (let i = 0; i < glStyle.layers.length; i += 1) {
const thisCircleColor = glStyle.layers[i].paint['circle-color'];
thisCircleColor.forEach((color, index) => {
const regex = /rgba?\(.*\)/;
if (regex.test(color)) {
const colors = color.split('(')[1].split(')')[0].split(/,\s?/);
if (colors.length < 4) {
colors.push('255');
}
const colorStr = colors.join(',');
if (lookup[colorStr]) {
thisCircleColor[index] = `rgba(${lookup[colorStr].r}, ${lookup[colorStr].g}, ${lookup[colorStr].b}, ${lookup[colorStr].a})`;
}
}
});
}
return glStyle;
};
const shouldRenderFeature = (feature, acceptableExtent) => {
if (!acceptableExtent) return true;
const midpoint = feature.getFlatCoordinates
? feature.getFlatCoordinates()
: feature.getGeometry().getFlatCoordinates();
if (containsCoordinate(acceptableExtent, midpoint)) return true;
return false;
};
/** Sets the Style Function for the layer (this styles vector features)
*
* @param {Object} def | Layer definition
* @param {String} vectorStyleId | ID to lookup the vector style in the state
* @param {Object} vectorStyles | Contains styles of all vector products
* @param {Object} layer | OL layer object
* @param {Object} options | Layer options object
* @param {Object} state | The entire state of the application
* @param {Boolean} styleSelection | Indicates if the request is triggered by user interaction with
* vector feature
*/
export function setStyleFunction(opts) {
let { layer } = opts;
const {
def,
vectorStyleId,
vectorStyles,
options,
state,
vectorStyleSource,
styleSelection = false,
manualStyle,
} = opts;
const map = lodashGet(state, 'map.ui.selected');
if (!map) return undefined;
const { proj } = state;
const { selected } = state.vectorStyles;
const resolutions = opts.resolutions || proj.selected.resolutions;
const layerId = def.id;
const styleId = lodashGet(def, `vectorStyle.${proj.id}.id`) || vectorStyleId || lodashGet(def, 'vectorStyle.id') || layerId;
const customPalette = def.custom;
const disabledPalette = def.disabled;
let glStyle = vectorStyles[styleId];
if (customPalette && Object.prototype.hasOwnProperty.call(state, 'palettes')) {
const hexColor = state.palettes.custom[customPalette].colors[0];
const rgbPalette = util.hexToRGBA(hexColor);
glStyle = updateGlStylePalette(glStyle, rgbPalette);
} else if (!styleSelection) {
const customDefaultStyle = state.vectorStyles.customDefault[def.vectorStyle.id];
if (customDefaultStyle !== undefined) {
glStyle = customDefaultStyle;
}
}
if (manualStyle) {
glStyle = manualStyle;
}
// De-reference the glState object prior to applying the palette to the layer
glStyle = lodashCloneDeep(glStyle);
let lookup;
if (isPaletteActive(def.id, options.group, state)) {
lookup = getPaletteLookup(def.id, options.group, state);
}
if (disabledPalette) {
glStyle = updateDisabled(glStyle, lookup);
}
if (!layer || layer.isWMS || glStyle === undefined) {
return undefined;
}
// This is required to bust the openlayers functionCache
if (Object.prototype.hasOwnProperty.call(glStyle, 'id')) {
delete glStyle.id;
}
layer = layer.getLayers
? lodashFind(layer.getLayers().getArray(), 'isVector')
: layer;
const source = vectorStyleSource || layerId;
const styleFunction = stylefunction(layer, glStyle, source, resolutions);
const selectedFeatures = selected[layerId];
// Process style of feature selected/clicked in UI
if ((glStyle.name !== 'Orbit Tracks') && selectedFeatures) {
const extentStartX = layer.getExtent()[0];
const fallbackExtent = extentStartX === -250
? [110, -90, 180, 90]
: null;
const acceptableExtent = extentStartX === 180
? [-180, -90, -110, 90]
: fallbackExtent;
layer.setStyle((feature, resolution) => {
const data = state.config.vectorData[def.vectorData.id];
const properties = data.mvt_properties;
const features = feature.getProperties();
const idKey = lodashFind(properties, { Function: 'Identify' }).Identifier;
const uniqueIdentifier = features[idKey];
if (shouldRenderFeature(feature, acceptableExtent)) {
if (uniqueIdentifier && selectedFeatures && selectedFeatures.includes(uniqueIdentifier)) {
return selectedStyleFunction(feature, styleFunction(feature, resolution));
}
return styleFunction(feature, resolution);
}
return undefined;
});
}
// Modify Sea Ice Motion points to symbols (triangle) indicating the direction of motion
if (glStyle.name === 'AMSRU2_Sea_Ice_Motion_12km') {
const targetColor = glStyle.layers[0].paint?.['circle-color'] || glStyle.layers[0].paint?.['line-color'];
glStyle.layers = glStyle.layers.flatMap(layer => {
if (layer.id.includes('_Point')) {
layer.type = 'symbol';
layer.paint = { 'text-color': targetColor };
layer.layout = {
'text-field': '\u25B4', // triangle pointing right
'text-size': 35,
};
layer.filter = ['==', ['get', 'type'], 'endpoint'];
}
return [layer];
});
layer.setStyle((feature, resolution) => {
const styles = styleFunction(feature, resolution);
if (feature.getGeometry().getType() === 'Point') {
const props = feature.getProperties();
const dx = props.end_x_projected - props.start_x_projected;
const dy = props.end_y_projected - props.start_y_projected;
const rotation = Math.atan2(dx, dy);
if (styles) {
const stylesArray = Array.isArray(styles) ? styles : [styles];
stylesArray.forEach(style => {
const text = style.getText();
if (text && typeof text.setRotation === 'function') {
text.setRotation(rotation);
}
});
return stylesArray;
}
}
return styles;
});
};
return vectorStyleId;
}
export function isActive(layerId, groupObj, state) {
let group = groupObj;
group = group || state.compare.activeString;
if (state.vectorStyles.custom[layerId]) {
return state.vectorStyles[group][layerId];
}
return undefined;
}
export function getKey(layerId, group, state) {
let groupStr = group;
groupStr = groupStr || state.compare.activeString;
if (!isActive(layerId, groupStr, state)) {
return '';
}
const def = getVectorStyle(layerId, undefined, state);
const keys = [];
if (def.custom) {
keys.push(`style=${def.custom}`);
}
if (def.min) {
keys.push(`min=${def.min}`);
}
if (def.max) {
keys.push(`max=${def.max}`);
}
return keys.join(',');
}
export function clearStyleFunction(def, vectorStyleId, vectorStyles, layerObj, state) {
let layer = layerObj;
const layerId = def.id;
const glStyle = vectorStyles[layerId];
const olMap = lodashGet(state, 'legacy.map.ui.selected');
if (olMap) {
lodashEach(olMap.getLayers().getArray(), (subLayer) => {
if (subLayer.wv.id === layerId) {
layer = subLayer;
}
});
}
const styleFunction = stylefunction(layer, glStyle, vectorStyleId);
if (glStyle.name === 'Orbit Tracks') {
// Filter time by 5 mins
layer.setStyle((feature, resolution) => {
let minute;
const minutes = feature.get('label');
if (minutes) {
minute = minutes.split(':');
}
if ((minute && minute[1] % 5 === 0) || feature.getGeometry().getType() === 'LineString') {
return styleFunction(feature, resolution);
}
return undefined;
});
}
return update(vectorStyles, { layerId: { maps: { $unset: ['custom'] } } });
}
/** Apply style to new layer
*
* @param {Object} def
* @param {Object} olVectorLayer
* @param {Object} state
* @param {Object} options
*/
export const applyStyle = async (def, olVectorLayer, state, options, resolutions) => {
const { config } = state;
const { vectorStyles } = config;
const vectorStyleId = def.vectorStyle.id;
const vectorStyleSource = def.vectorStyle.source;
if (!vectorStyles || !vectorStyleId) {
return;
}
const opts = {
def,
vectorStyleId,
vectorStyles,
layer: olVectorLayer,
options,
state,
vectorStyleSource,
styleSelection: false,
...resolutions && { resolutions },
};
// Fetch live vectorstyle if layer is in liveFetchLayers
if (liveFetchLayers.includes(def.id)) {
const fetchedStyle = await fetchVectorstyle(def.id);
if (fetchedStyle) {
opts.manualStyle = fetchedStyle;
}
}
setStyleFunction(opts);
};