Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[feat] Adding applyLayerConfig action (#2337)

Igor D85fa66f3ecfd
diff --git a/src/actions/src/action-types.ts b/src/actions/src/action-types.ts
index 039b9ee52..05278648d 100644
--- a/src/actions/src/action-types.ts
+++ b/src/actions/src/action-types.ts
@@ -60,6 +60,7 @@ export const ActionTypes = {
ADD_DATA: `${ACTION_PREFIX}ADD_DATA`,
ADD_FILTER: `${ACTION_PREFIX}ADD_FILTER`,
ADD_LAYER: `${ACTION_PREFIX}ADD_LAYER`,
+ APPLY_LAYER_CONFIG: `${ACTION_PREFIX}APPLY_LAYER_CONFIG`,
DUPLICATE_LAYER: `${ACTION_PREFIX}DUPLICATE_LAYER`,
INTERACTION_CONFIG_CHANGE: `${ACTION_PREFIX}INTERACTION_CONFIG_CHANGE`,
LAYER_CONFIG_CHANGE: `${ACTION_PREFIX}LAYER_CONFIG_CHANGE`,
diff --git a/src/actions/src/vis-state-actions.ts b/src/actions/src/vis-state-actions.ts
index 48b04b8fa..c82a5e437 100644
--- a/src/actions/src/vis-state-actions.ts
+++ b/src/actions/src/vis-state-actions.ts
@@ -35,11 +35,40 @@ import {
FeatureSelectionContext,
InteractionConfig,
Filter,
- ParsedConfig
+ ParsedConfig,
+ ParsedLayer
} from '@kepler.gl/types';
// TODO - import LoaderObject type from @loaders.gl/core when supported
// TODO - import LoadOptions type from @loaders.gl/core when supported
+export type ApplyLayerConfigUpdaterAction = {
+ oldLayerId: string;
+ newLayerConfig: ParsedLayer;
+ layerIndex?: number;
+};
+
+/**
+ * Update layer base config: dataId, label, column, isVisible
+ * @param oldLayerId - layer id to be updated
+ * @param newLayerConfig - new layer config
+ * @param layerIndex - (Optional) Index of the layer to be updated (can be useful in some cases, because
+ * the layer id might change during update, e.g. when the type of the layer changes)
+ * @returns action
+ * @public
+ */
+export function applyLayerConfig(
+ oldLayerId: string,
+ newLayerConfig: ParsedLayer,
+ layerIndex?: number
+): Merge<ApplyLayerConfigUpdaterAction, {type: typeof ActionTypes.APPLY_LAYER_CONFIG}> {
+ return {
+ type: ActionTypes.APPLY_LAYER_CONFIG,
+ oldLayerId,
+ newLayerConfig,
+ layerIndex
+ };
+}
+
export type LayerConfigChangeUpdaterAction = {
oldLayer: Layer;
newConfig: Partial<LayerBaseConfig>;
diff --git a/src/components/src/side-panel/add-by-dataset-button.tsx b/src/components/src/side-panel/add-by-dataset-button.tsx
index bf339861d..a4ffe57ae 100644
--- a/src/components/src/side-panel/add-by-dataset-button.tsx
+++ b/src/components/src/side-panel/add-by-dataset-button.tsx
@@ -29,6 +29,7 @@ import {Button, DatasetSquare} from '../common/styled-components';
import Typeahead from '../common/item-selector/typeahead';
import Accessor from '../common/item-selector/accessor';
import {useIntl} from 'react-intl';
+import {RootContext} from '../context';
const DropdownContainer = styled.div.attrs({
className: 'add-layer-menu-dropdown'
@@ -159,40 +160,44 @@ const AddByDatasetButton: React.FC<AddByDatasetButtonProps> = ({
return options.length === 1 ? (
buttonRendered
) : (
- <Tippy
- trigger="click"
- arrow={false}
- interactive
- placement="bottom"
- appendTo="parent"
- // @ts-ignore
- onCreate={setTippyInstance}
- duration={0}
- content={
- <DropdownMenu>
- <DropdownContainer>
- <Typeahead
- className={TYPEAHEAD_CLASS}
- customClasses={{
- results: 'list-selector',
- input: TYPEAHEAD_INPUT_CLASS,
- listItem: 'list__item'
- }}
- placeholder={intl ? intl.formatMessage({id: 'placeholder.search'}) : 'Search'}
- selectedItems={null}
- options={options}
- displayOption={Accessor.generateOptionToStringFor('label')}
- filterOption={'label'}
- searchable
- onOptionSelected={onOptionSelected}
- customListItemComponent={ListItem}
- />
- </DropdownContainer>
- </DropdownMenu>
- }
- >
- {buttonRendered}
- </Tippy>
+ <RootContext.Consumer>
+ {context => (
+ <Tippy
+ trigger="click"
+ arrow={false}
+ interactive
+ placement="bottom"
+ appendTo={context?.current || 'parent'}
+ // @ts-ignore
+ onCreate={setTippyInstance}
+ duration={0}
+ content={
+ <DropdownMenu>
+ <DropdownContainer>
+ <Typeahead
+ className={TYPEAHEAD_CLASS}
+ customClasses={{
+ results: 'list-selector',
+ input: TYPEAHEAD_INPUT_CLASS,
+ listItem: 'list__item'
+ }}
+ placeholder={intl ? intl.formatMessage({id: 'placeholder.search'}) : 'Search'}
+ selectedItems={null}
+ options={options}
+ displayOption={Accessor.generateOptionToStringFor('label')}
+ filterOption={'label'}
+ searchable
+ onOptionSelected={onOptionSelected}
+ customListItemComponent={ListItem}
+ />
+ </DropdownContainer>
+ </DropdownMenu>
+ }
+ >
+ {buttonRendered}
+ </Tippy>
+ )}
+ </RootContext.Consumer>
);
};
diff --git a/src/reducers/src/vis-state-merger.ts b/src/reducers/src/vis-state-merger.ts
index 89789083c..3ee810edd 100644
--- a/src/reducers/src/vis-state-merger.ts
+++ b/src/reducers/src/vis-state-merger.ts
@@ -554,6 +554,7 @@ export function mergeAnimationConfig<S extends VisState>(
* @param fields
* @param savedCols
* @param emptyCols
+ * @param options
* @return - validated columns or null
*/
@@ -562,7 +563,8 @@ export function validateSavedLayerColumns(
savedCols: {
[key: string]: string;
} = {},
- emptyCols: LayerColumns
+ emptyCols: LayerColumns,
+ options: {throwOnError?: boolean} = {}
) {
// Prepare columns for the validator
const columns: typeof emptyCols = {};
@@ -586,11 +588,21 @@ export function validateSavedLayerColumns(
validateColumn(columns[key], columns, fields)
);
- if (allColFound) {
- return columns;
+ const rv = allColFound ? columns : null;
+ if (options.throwOnError) {
+ const requiredColumns = Object.keys(emptyCols).filter(k => !emptyCols[k].optional);
+ const missingColumns = requiredColumns.filter(k => !columns?.[k].value);
+ if (missingColumns.length) {
+ throw new Error(`Layer has missing or invalid columns: ${missingColumns.join(', ')}`);
+ }
+ const configColumns = Object.keys(savedCols);
+ const invalidColumns = configColumns.filter(k => !columns?.[k]?.value);
+ if (invalidColumns.length) {
+ throw new Error(`Layer has invalid columns: ${invalidColumns.join(', ')}`);
+ }
}
- return null;
+ return rv;
}
export function validateColumn(
@@ -613,9 +625,15 @@ export function validateColumn(
*
* @param {Array<Object>} fields
* @param {Object} savedTextLabel
+ * @param {Object} options
* @return {Object} - validated textlabel
*/
-export function validateSavedTextLabel(fields, [layerTextLabel], savedTextLabel) {
+export function validateSavedTextLabel(
+ fields,
+ [layerTextLabel],
+ savedTextLabel,
+ options: {throwOnError?: boolean} = {}
+) {
const savedTextLabels = Array.isArray(savedTextLabel) ? savedTextLabel : [savedTextLabel];
// validate field
@@ -626,6 +644,10 @@ export function validateSavedTextLabel(fields, [layerTextLabel], savedTextLabel)
)
: null;
+ if (field === undefined && options.throwOnError) {
+ throw new Error(`Layer has invalid text label field: ${JSON.stringify(textLabel.field)}`);
+ }
+
return Object.keys(layerTextLabel).reduce(
(accu, key) => ({
...accu,
@@ -643,7 +665,8 @@ export function validateSavedTextLabel(fields, [layerTextLabel], savedTextLabel)
export function validateSavedVisualChannels(
fields: KeplerTable['fields'],
newLayer: Layer,
- savedLayer: ParsedLayer
+ savedLayer: ParsedLayer,
+ options: {throwOnError?: boolean} = {}
): null | Layer {
Object.values(newLayer.visualChannels).forEach(({field, scale, key}) => {
let foundField;
@@ -663,6 +686,12 @@ export function validateSavedVisualChannels(
}
newLayer.validateVisualChannel(key);
+ if (options.throwOnError) {
+ const fieldName = savedLayer.config?.[field]?.name;
+ if (fieldName && fieldName !== newLayer.config[field]?.name) {
+ throw new Error(`Layer has invalid visual channel field: ${field}`);
+ }
+ }
}
});
return newLayer;
@@ -670,6 +699,7 @@ export function validateSavedVisualChannels(
type ValidateLayerOption = {
allowEmptyColumn?: boolean;
+ throwOnError?: boolean;
};
export function validateLayersByDatasets(
@@ -710,6 +740,7 @@ export function validateLayersByDatasets(
* Validate saved layer config with new data,
* update fieldIdx based on new fields
*/
+// eslint-disable-next-line complexity
export function validateLayerWithData(
{fields, id: dataId}: KeplerTable,
savedLayer: ParsedLayer,
@@ -717,8 +748,12 @@ export function validateLayerWithData(
options: ValidateLayerOption = {}
): Layer | null {
const {type} = savedLayer;
+ const {throwOnError} = options;
// layer doesnt have a valid type
if (!type || !layerClasses.hasOwnProperty(type) || !savedLayer.config) {
+ if (throwOnError) {
+ throw new Error(`Layer has invalid type "${type}" or config is missing`);
+ }
return null;
}
@@ -734,8 +769,13 @@ export function validateLayerWithData(
// find column fieldIdx
const columnConfig = newLayer.getLayerColumns();
- if (Object.keys(columnConfig).length) {
- const columns = validateSavedLayerColumns(fields, savedLayer.config.columns, columnConfig);
+ if (Object.keys(columnConfig)) {
+ const columns = validateSavedLayerColumns(
+ fields,
+ savedLayer.config.columns,
+ columnConfig,
+ options
+ );
if (columns) {
newLayer.updateLayerConfig({columns});
} else if (!options.allowEmptyColumn) {
@@ -746,11 +786,16 @@ export function validateLayerWithData(
// visual channel field is saved to be {name, type}
// find visual channel field by matching both name and type
// refer to vis-state-schema.js VisualChannelSchemaV1
- newLayer = validateSavedVisualChannels(fields, newLayer, savedLayer);
+ newLayer = validateSavedVisualChannels(fields, newLayer, savedLayer, options);
const textLabel =
savedLayer.config.textLabel && newLayer.config.textLabel
- ? validateSavedTextLabel(fields, newLayer.config.textLabel, savedLayer.config.textLabel)
+ ? validateSavedTextLabel(
+ fields,
+ newLayer.config.textLabel,
+ savedLayer.config.textLabel,
+ options
+ )
: newLayer.config.textLabel;
// copy visConfig over to emptyLayer to make sure it has all the props
@@ -765,6 +810,12 @@ export function validateLayerWithData(
textLabel
});
+ if (throwOnError) {
+ if (!newLayer.isValidToSave()) {
+ throw new Error(`Layer is not valid to save: ${newLayer.id}`);
+ }
+ }
+
return newLayer;
}
diff --git a/src/reducers/src/vis-state-updaters.ts b/src/reducers/src/vis-state-updaters.ts
index d87c66cf1..d4871763f 100644
--- a/src/reducers/src/vis-state-updaters.ts
+++ b/src/reducers/src/vis-state-updaters.ts
@@ -24,12 +24,19 @@ import cloneDeep from 'lodash.clonedeep';
import uniq from 'lodash.uniq';
import get from 'lodash.get';
import xor from 'lodash.xor';
+import pick from 'lodash.pick';
+import isEqual from 'lodash.isequal';
import copy from 'copy-to-clipboard';
import deepmerge from 'deepmerge';
// Tasks
import {LOAD_FILE_TASK, UNWRAP_TASK, PROCESS_FILE_DATA, DELAY_TASK} from '@kepler.gl/tasks';
// Actions
import {
+ applyLayerConfig,
+ layerConfigChange,
+ layerTypeChange,
+ layerVisConfigChange,
+ layerVisualChannelConfigChange,
loadFilesErr,
loadFilesSuccess,
loadFileStepSuccess,
@@ -77,7 +84,8 @@ import {
validateLayerWithData,
createLayerFromConfig,
serializeLayer,
- serializeVisState
+ serializeVisState,
+ parseLayerConfig
} from './vis-state-merger';
import {mergeStateFromMergers, isValidMerger} from './merger-handler';
import {Layer, LayerClasses, LAYER_ID_LENGTH} from '@kepler.gl/layers';
@@ -327,11 +335,130 @@ export function updateStateOnLayerVisibilityChange<S extends VisState>(state: S,
return newState;
}
+/**
+ * Compares two objects (or arrays) and returns a new object with only the
+ * properties that have changed between the two objects.
+ */
+function pickChangedProps<T>(prev: T, next: T): Partial<T> {
+ const changedProps: Partial<T> = {};
+ const pickPropsOf = obj => {
+ Object.keys(obj).forEach(key => {
+ if (!changedProps.hasOwnProperty(key) && !isEqual(prev[key], next[key])) {
+ changedProps[key] = next[key];
+ }
+ });
+ };
+ pickPropsOf(prev);
+ pickPropsOf(next);
+ return changedProps;
+}
+
+const VISUAL_CHANNEL_PROP_TYPES = ['field', 'scale', 'domain', 'aggregation'];
+
+/**
+ * Apply layer config
+ * @memberof visStateUpdaters
+ * @returns nextState
+ */
+// eslint-disable-next-line complexity, max-statements
+export function applyLayerConfigUpdater(
+ state: VisState,
+ action: VisStateActions.ApplyLayerConfigUpdaterAction
+): VisState {
+ const {oldLayerId, newLayerConfig, layerIndex} = action;
+ const newParsedLayer =
+ // will move visualChannels to the config prop
+ parseLayerConfig(state.schema, newLayerConfig);
+ const oldLayer = state.layers.find(l => l.id === oldLayerId);
+ if (!oldLayer || !newParsedLayer) {
+ return state;
+ }
+ if (layerIndex !== null && layerIndex !== undefined && state.layers[layerIndex] !== oldLayer) {
+ // layerIndex is provided, but it doesn't match the oldLayer
+ return state;
+ }
+ const dataset = state.datasets[newParsedLayer.config.dataId];
+ if (!dataset) {
+ return state;
+ }
+ // Make sure the layer is valid and convert it to Layer
+ const newLayer = validateLayerWithData(dataset, newParsedLayer, state.layerClasses);
+ if (!newLayer) {
+ return state;
+ }
+
+ let nextState = state;
+
+ if (newLayer.type && newLayer.type !== oldLayer.type) {
+ const oldLayerIndex = state.layers.findIndex(l => l.id === oldLayerId);
+ if (oldLayerIndex >= 0) {
+ nextState = layerTypeChangeUpdater(nextState, layerTypeChange(oldLayer, newLayer.type));
+ // layerTypeChangeUpdater changes the id of the layer, so we need to obtain the new id
+ // but first make sure that the layer was not removed
+ if (nextState.layers.length === state.layers.length) {
+ const newLayerId = nextState.layers[oldLayerIndex].id;
+ nextState = applyLayerConfigUpdater(
+ nextState,
+ applyLayerConfig(newLayerId, {...newLayerConfig, id: newLayerId})
+ );
+ }
+ }
+ return nextState;
+ }
+
+ const serializedOldLayer = serializeLayer(oldLayer, state.schema);
+ const serializedNewLayer = serializeLayer(newLayer, state.schema);
+ if (!serializedNewLayer || !serializedOldLayer) {
+ return state;
+ }
+ if (!isEqual(serializedOldLayer, serializedNewLayer)) {
+ const changed = pickChangedProps(serializedOldLayer.config, serializedNewLayer.config);
+
+ if ('visConfig' in changed) {
+ if (changed.visConfig) {
+ nextState = layerVisConfigChangeUpdater(
+ nextState,
+ layerVisConfigChange(oldLayer, changed.visConfig)
+ );
+ }
+ delete changed.visConfig;
+ }
+
+ Object.keys(oldLayer.visualChannels).forEach(channelName => {
+ const channel = oldLayer.visualChannels[channelName];
+ const channelPropNames = VISUAL_CHANNEL_PROP_TYPES.map(prop => channel[prop]);
+ if (channelPropNames.some(prop => prop in changed)) {
+ nextState = layerVisualChannelChangeUpdater(
+ nextState,
+ layerVisualChannelConfigChange(
+ oldLayer,
+ pick(newLayer.config, channelPropNames),
+ channelName
+ )
+ );
+ for (const prop of channelPropNames) {
+ delete changed[prop];
+ }
+ }
+ });
+
+ if (Object.keys(changed).length > 0) {
+ nextState = layerConfigChangeUpdater(
+ nextState,
+ layerConfigChange(oldLayer, pick(newLayer.config, Object.keys(changed)))
+ );
+ }
+ }
+
+ return nextState;
+}
+
/**
* Update layer base config: dataId, label, column, isVisible
* @memberof visStateUpdaters
* @returns nextState
*/
+// eslint-disable-next-line complexity
export function layerConfigChangeUpdater(
state: VisState,
action: VisStateActions.LayerConfigChangeUpdaterAction
@@ -339,7 +466,10 @@ export function layerConfigChangeUpdater(
const {oldLayer} = action;
const idx = state.layers.findIndex(l => l.id === oldLayer.id);
const props = Object.keys(action.newConfig);
- if (typeof action.newConfig.dataId === 'string') {
+ if (
+ typeof action.newConfig.dataId === 'string' &&
+ action.newConfig.dataId !== oldLayer.config.dataId
+ ) {
const {dataId, ...restConfig} = action.newConfig;
const stateWithDataId = layerDataIdChangeUpdater(state, {
oldLayer,
diff --git a/src/reducers/src/vis-state.ts b/src/reducers/src/vis-state.ts
index be1754448..1d48cd17f 100644
--- a/src/reducers/src/vis-state.ts
+++ b/src/reducers/src/vis-state.ts
@@ -31,6 +31,8 @@ const actionHandler = {
[ActionTypes.ADD_LAYER]: visStateUpdaters.addLayerUpdater,
+ [ActionTypes.APPLY_LAYER_CONFIG]: visStateUpdaters.applyLayerConfigUpdater,
+
[ActionTypes.DUPLICATE_LAYER]: visStateUpdaters.duplicateLayerUpdater,
[ActionTypes.SET_FILTER_VIEW]: visStateUpdaters.setFilterViewUpdater,
diff --git a/test/node/reducers/vis-state-test.js b/test/node/reducers/vis-state-test.js
index a01dd8c8a..d24725ca6 100644
--- a/test/node/reducers/vis-state-test.js
+++ b/test/node/reducers/vis-state-test.js
@@ -32,6 +32,7 @@ import {
INITIAL_VIS_STATE,
DEFAULT_ANIMATION_CONFIG,
serializeLayer,
+ validateLayerWithData,
defaultInteractionConfig,
prepareStateForDatasetReplace
} from '@kepler.gl/reducers';
@@ -5323,3 +5324,159 @@ test('VisStateUpdater -> addLayer with empty column', t => {
t.end();
});
+
+test('VisStateUpdater -> applyLayerConfig', t => {
+ const initialState = StateWFiles.visState;
+ const oldLayers = initialState.layers;
+
+ const oldLayerIndex = 0;
+ const oldLayer = oldLayers[oldLayerIndex];
+ const oldLayerId = oldLayer.id;
+ const {schema} = initialState;
+ const layerToJson = layer =>
+ schema.getConfigToSave({
+ visState: {layers: [layer], layerOrder: [layer.id]}
+ }).config.visState.layers?.[0];
+ const oldLayerDataset = initialState.datasets[oldLayer.config.dataId];
+ const transformConfig = transform => transform(layerToJson(oldLayer, schema));
+ const getUpdatedLayerJson = state => layerToJson(state.layers[oldLayerIndex], schema);
+
+ let nextState;
+
+ nextState = reducer(
+ initialState,
+ VisStateActions.applyLayerConfig(
+ oldLayerId,
+ transformConfig(layer => {
+ layer.config.isVisible = false;
+ layer.config.label = 'New label';
+ return layer;
+ })
+ )
+ );
+ t.equal(getUpdatedLayerJson(nextState).config.isVisible, false, 'should change isVisible');
+ t.equal(getUpdatedLayerJson(nextState).config.label, 'New label', 'should change label');
+
+ nextState = reducer(
+ initialState,
+ VisStateActions.applyLayerConfig(
+ oldLayerId,
+ transformConfig(layer => {
+ layer.config.columns = {
+ lng: 'gps_data.lat',
+ lat: 'gps_data.lng'
+ };
+ return layer;
+ })
+ )
+ );
+ t.equal(
+ getUpdatedLayerJson(nextState).config.columns.lng,
+ 'gps_data.lat',
+ 'should change lng column'
+ );
+ t.equal(
+ getUpdatedLayerJson(nextState).config.columns.lat,
+ 'gps_data.lng',
+ 'should change lat column'
+ );
+
+ nextState = reducer(
+ initialState,
+ VisStateActions.applyLayerConfig(
+ oldLayerId,
+ transformConfig(layer => {
+ layer.config.textLabel = {field: {name: 'gps_data.types', type: 'string'}};
+ return layer;
+ })
+ )
+ );
+ t.equal(
+ getUpdatedLayerJson(nextState).config.textLabel?.[0]?.field?.name,
+ 'gps_data.types',
+ 'should change text label'
+ );
+
+ nextState = reducer(
+ initialState,
+ VisStateActions.applyLayerConfig(
+ oldLayerId,
+ transformConfig(layer => {
+ layer.visualChannels.colorField = {name: 'gps_data.lat', type: 'real'};
+ layer.visualChannels.colorScale = 'quantize';
+ return layer;
+ })
+ )
+ );
+ t.equal(
+ getUpdatedLayerJson(nextState).visualChannels.colorField?.name,
+ 'gps_data.lat',
+ 'should change visualChannel colorField'
+ );
+ t.equal(
+ getUpdatedLayerJson(nextState).visualChannels.colorScale,
+ 'quantize',
+ 'should change visualChannel colorScale'
+ );
+
+ t.throws(() => {
+ validateLayerWithData(
+ oldLayerDataset,
+ transformConfig(layer => {
+ layer.config.textLabel = {field: {name: 'INVALID', type: 'string'}};
+ return layer;
+ }),
+ initialState.layerClasses,
+ {throwOnError: true}
+ );
+ }, 'validation should throw error for invalid text label');
+
+ t.throws(() => {
+ validateLayerWithData(
+ oldLayerDataset,
+ transformConfig(layer => {
+ layer.config.columns = {
+ lng: 'INVALID COLUMN'
+ };
+ return layer;
+ }),
+ initialState.layerClasses,
+ {throwOnError: true}
+ );
+ }, 'validation should throw error for invalid columns');
+
+ nextState = reducer(
+ initialState,
+ VisStateActions.applyLayerConfig(
+ oldLayerId,
+ transformConfig(layer => {
+ layer.config.dataId = 'INVALID DATA ID';
+ return layer;
+ })
+ )
+ );
+ t.equal(
+ getUpdatedLayerJson(nextState).config.dataId,
+ oldLayer.config.dataId,
+ 'should not change data id to invalid one'
+ );
+ t.deepEqual(
+ getUpdatedLayerJson(nextState),
+ layerToJson(oldLayer, schema),
+ 'should not change on invalid data id'
+ );
+
+ nextState = reducer(
+ initialState,
+ VisStateActions.applyLayerConfig(
+ oldLayerId,
+ transformConfig(layer => {
+ layer.type = '3D';
+ return layer;
+ })
+ )
+ );
+ t.equal(getUpdatedLayerJson(nextState).type, '3D', 'should change layer type');
+
+ t.end();
+});