Interactive interface for browsing global, full-resolution satellite imagery
WV-2617 Ability to change palette of single-color vector products (#4349)
Tom Cariello8d8c9667fac1
diff --git a/web/js/components/layer/settings/layer-settings.js b/web/js/components/layer/settings/layer-settings.jsindex f1bb12df7..b4f182ffb 100644--- a/web/js/components/layer/settings/layer-settings.js+++ b/web/js/components/layer/settings/layer-settings.js@@ -340,13 +340,16 @@ class LayerSettings extends React.Component {} = this.props;const hasAssociatedLayers = layer.associatedLayers && layer.associatedLayers.length;const hasTracks = layer.orbitTracks && layer.orbitTracks.length;+ const layerGroup = layer.layergroup;if (layer.type !== 'vector') {renderCustomizations = customPalettesIsActive && palettedAllowed && layer.palette? this.renderCustomPalettes(): '';- } else {- renderCustomizations = ''; // this.renderVectorStyles(); for future+ } else if (layerGroup !== 'Orbital Track' && layerGroup !== 'Reference') {+ // Orbital Tracks palette swap looks bad at WMS zoom levels (white text stamps)+ // Reference (MGRS/HLS Grid) has no need for palettes+ renderCustomizations = this.renderCustomPalettes();}if (!layer.id) return '';diff --git a/web/js/modules/vector-styles/reducers.js b/web/js/modules/vector-styles/reducers.jsindex 014a5c7a7..a3e166127 100644--- a/web/js/modules/vector-styles/reducers.js+++ b/web/js/modules/vector-styles/reducers.js@@ -1,4 +1,4 @@-import { assign as lodashAssign, get as lodashGet } from 'lodash';+import { assign as lodashAssign, get as lodashGet, cloneDeep as lodashCloneDeep } from 'lodash';import {CLEAR_VECTORSTYLE,SET_VECTORSTYLE,@@ -14,9 +14,9 @@ export const defaultVectorStyleState = {};export function getInitialVectorStyleState(config) {const custom = lodashGet(config, 'vectorStyles') || {};- return lodashAssign({}, defaultVectorStyleState, {- custom,- });+ const customDefault = lodashCloneDeep(custom);++ return lodashAssign({}, defaultVectorStyleState, { custom, customDefault });}export function vectorStyleReducer(state = defaultVectorStyleState, action) {diff --git a/web/js/modules/vector-styles/selectors.js b/web/js/modules/vector-styles/selectors.jsindex 43f073adc..331f0eb4e 100644--- a/web/js/modules/vector-styles/selectors.js+++ b/web/js/modules/vector-styles/selectors.js@@ -3,6 +3,7 @@ import {isUndefined as lodashIsUndefined,each as lodashEach,find as lodashFind,+ cloneDeep as lodashCloneDeep,} from 'lodash';import update from 'immutability-helper';@@ -11,13 +12,12 @@ import { stylefunction } from 'ol-mapbox-style';import {getMinValue, getMaxValue, selectedStyleFunction,} from './util';-import {- getActiveLayers,-} from '../layers/selectors';+import util from '../../util/util';+/*** Get OpenLayers layers from state that were created from WV vector- * layer definiteions. NOTE: This currently also will include the associate WMS+ * layer definitions. NOTE: This currently also will include the associate WMS* breakpoint layers as well.** @param {*} state@@ -96,7 +96,16 @@ export function setRange(layerId, props, index, palettes, state) {};}-export function setStyleFunction(def, vectorStyleId, vectorStyles, layer, state) {+/** 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} 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(def, vectorStyleId, vectorStyles, layer, state, styleSelection = false) {const map = lodashGet(state, 'map.ui.selected');if (!map) return;const { proj } = state;@@ -104,20 +113,39 @@ export function setStyleFunction(def, vectorStyleId, vectorStyles, layer, state)const { resolutions } = proj.selected;const layerId = def.id;const styleId = lodashGet(def, `vectorStyle.${proj.id}.id`) || vectorStyleId || lodashGet(def, 'vectorStyle.id') || layerId;- const glStyle = vectorStyles[styleId];+ const customPalette = def.custom;++ 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 (!layer || layer.isWMS || glStyle === undefined) {+ return;+ }- if (!layer || layer.isWMS) {- return; // WMS breakpoint tile+ // 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;+ // De-reference the glState object prior to applying the palette to the layer+ glStyle = lodashCloneDeep(glStyle);const styleFunction = stylefunction(layer, glStyle, layerId, resolutions);const selectedFeatures = selected[layerId];- // Handle selected feature style+ // Process style of feature selected/clicked in UIif ((glStyle.name !== 'Orbit Tracks') && selectedFeatures) {const extentStartX = layer.getExtent()[0];const acceptableExtent = extentStartX === 180@@ -153,6 +181,25 @@ const shouldRenderFeature = (feature, acceptableExtent) => {return false;};+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;+};+export function getKey(layerId, groupStr, state) {groupStr = groupStr || state.compare.activeString;if (!isActive(layerId, groupStr, state)) {@@ -216,18 +263,11 @@ export function clearStyleFunction(def, vectorStyleId, vectorStyles, layer, statexport const applyStyle = (def, olVectorLayer, state) => {const { config } = state;const { vectorStyles } = config;- const activeLayers = getActiveLayers(state) || [];- const layerName = def.layer || def.id;- let vectorStyleId = def.vectorStyle.id;+ const vectorStyleId = def.vectorStyle.id;if (!vectorStyles || !vectorStyleId) {return;}- activeLayers.forEach((layer) => {- if (layer.id === layerName && layer.custom) {- vectorStyleId = layer.custom;- }- });setStyleFunction(def, vectorStyleId, vectorStyles, olVectorLayer, state);};diff --git a/web/js/modules/vector-styles/util.js b/web/js/modules/vector-styles/util.jsindex 3ce36470d..46f77d08f 100644--- a/web/js/modules/vector-styles/util.js+++ b/web/js/modules/vector-styles/util.js@@ -319,13 +319,13 @@ export function updateVectorSelection(selectionObj, lastSelection, layers, type,const def = lodashFind(layers, { id: key });if (!def) return;const olLayer = vectorLayers.find((layer) => layer.wv.id === key);- setStyleFunction(def, def.vectorStyle.id, vectorStyles, olLayer, state);+ setStyleFunction(def, def.vectorStyle.id, vectorStyles, olLayer, state, true);if (lastSelection[key]) delete lastSelection[key];}for (const [key] of Object.entries(lastSelection)) {const def = lodashFind(layers, { id: key });if (!def) return;const olLayer = vectorLayers.find((layer) => layer.wv.id === key);- setStyleFunction(def, def.vectorStyle.id, vectorStyles, olLayer, state);+ setStyleFunction(def, def.vectorStyle.id, vectorStyles, olLayer, state, true);}}diff --git a/web/js/ol/lookupimagetile.js b/web/js/ol/lookupimagetile.jsindex 8a3eeafb9..aefece4f0 100644--- a/web/js/ol/lookupimagetile.js+++ b/web/js/ol/lookupimagetile.js@@ -1,5 +1,6 @@import OlImageTile from 'ol/ImageTile';import OlTileState from 'ol/TileState';+import { cloneDeep as lodashCloneDeep } from 'lodash';class LookupImageTile extends OlImageTile {constructor(lookup, tileCoord, state, src, crossOrigin, tileLoadFunction, sourceOptions) {@@ -32,20 +33,49 @@ LookupImageTile.prototype.load = function() {that.canvas_.height,);const pixels = imageData.data;+ const colorLookupObj = lodashCloneDeep(that.lookup_);+ const defaultColor = Object.keys(that.lookup_)[0];+ const paletteColor = that.lookup_[Object.keys(that.lookup_)[0]];++ // Load black/transparent into the lookup+ colorLookupObj['0,0,0,0'] = {+ r: 0,+ g: 0,+ b: 0,+ a: 0,+ };for (let i = 0; i < octets; i += 4) {- const source = `${pixels[i + 0]},${+ const pixelColor = `${pixels[i + 0]},${pixels[i + 1]},${pixels[i + 2]},${pixels[i + 3]}`;- const target = that.lookup_[source];- if (target) {- pixels[i + 0] = target.r;- pixels[i + 1] = target.g;- pixels[i + 2] = target.b;- pixels[i + 3] = target.a;+ if (!colorLookupObj[pixelColor]) {+ // Handle non-transparent pixels that do not match the palette exactly+ const defaultColorArr = defaultColor.split(',');+ const pixelColorArr = pixelColor.split(',');++ // Determine difference of pixel from default to replicate anti-aliasing+ const rDifference = pixelColorArr[0] - defaultColorArr[0];+ const gDifference = pixelColorArr[1] - defaultColorArr[1];+ const bDifference = pixelColorArr[2] - defaultColorArr[2];+ const alphaValue = pixelColorArr[3];++ // Store the resulting pair of pixel color & anti-aliased adjusted color+ colorLookupObj[pixelColor] = {+ r: paletteColor.r + rDifference,+ g: paletteColor.g + gDifference,+ b: paletteColor.b + bDifference,+ a: alphaValue,+ };}++ // set the pixel color+ pixels[i + 0] = colorLookupObj[pixelColor].r;+ pixels[i + 1] = colorLookupObj[pixelColor].g;+ pixels[i + 2] = colorLookupObj[pixelColor].b;+ pixels[i + 3] = colorLookupObj[pixelColor].a;}g.putImageData(imageData, 0, 0);