Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[Feat] allow changing dataset in layer config (#1312)
Shan Hebdbea2641120
diff --git a/src/components/side-panel/layer-panel/layer-configurator.js b/src/components/side-panel/layer-panel/layer-configurator.jsindex 35c76e3d2..29d4effd8 100644--- a/src/components/side-panel/layer-panel/layer-configurator.js+++ b/src/components/side-panel/layer-panel/layer-configurator.js@@ -918,28 +918,25 @@ export default function LayerConfiguratorFactory(layerTypeOptions={layerTypeOptions}onSelect={updateLayerType}/>- <ConfigGroupCollapsibleContent>- {Object.keys(datasets).length > 1 && (- <SourceDataSelector- datasets={datasets}- id={layer.id}- disabled={layer.type && config.columns}- dataId={config.dataId}- onSelect={value => updateLayerConfig({dataId: value})}- />- )}- <LayerColumnConfig- columnPairs={layer.columnPairs}- columns={layer.config.columns}- assignColumnPairs={layer.assignColumnPairs.bind(layer)}- assignColumn={layer.assignColumn.bind(layer)}- columnLabels={layer.columnLabels}- fields={fields}- fieldPairs={fieldPairs}- updateLayerConfig={updateLayerConfig}- updateLayerType={this.props.updateLayerType}+ {Object.keys(datasets).length > 1 && (+ <SourceDataSelector+ datasets={datasets}+ id={layer.id}+ dataId={config.dataId}+ onSelect={value => updateLayerConfig({dataId: value})}/>- </ConfigGroupCollapsibleContent>+ )}+ <LayerColumnConfig+ columnPairs={layer.columnPairs}+ columns={layer.config.columns}+ assignColumnPairs={layer.assignColumnPairs.bind(layer)}+ assignColumn={layer.assignColumn.bind(layer)}+ columnLabels={layer.columnLabels}+ fields={fields}+ fieldPairs={fieldPairs}+ updateLayerConfig={updateLayerConfig}+ updateLayerType={this.props.updateLayerType}+ /></LayerConfigGroup>{this[renderTemplate] &&this[renderTemplate]({diff --git a/src/reducers/vis-state-merger.d.ts b/src/reducers/vis-state-merger.d.tsindex 8b9bcf651..e374c4456 100644--- a/src/reducers/vis-state-merger.d.ts+++ b/src/reducers/vis-state-merger.d.ts@@ -38,7 +38,16 @@ export type Merger = {};export function validateSavedVisualChannels(fields, newLayer, savedLayer);-export function validateLayerWithData({fields, id: dataId}, savedLayer, layerClasses);+export function validateLayerWithData(+ dataset: {fields, id: dataId},+ savedLayer: savedLayer,+ layerClasses: {+ [key: string]: LayerClass+ },+ option?: {+ allowEmptyColumn?: boolean;+ }+): Layer | null;export type VisStateMergers = Merger[];diff --git a/src/reducers/vis-state-merger.js b/src/reducers/vis-state-merger.jsindex dac034096..e81a79805 100644--- a/src/reducers/vis-state-merger.js+++ b/src/reducers/vis-state-merger.js@@ -510,14 +510,14 @@ export function validateSavedVisualChannels(fields, newLayer, savedLayer) {/*** Validate saved layer config with new data,* update fieldIdx based on new fields- * @param {object} dataset- * @param {Array<Object>} dataset.fields- * @param {string} dataset.id- * @param {Object} savedLayer- * @param {Object} layerClasses- * @return {null | Object} - validated layer or null+ * @type {typeof import('./vis-state-merger').validateLayerWithData}*/-export function validateLayerWithData({fields, id: dataId}, savedLayer, layerClasses) {+export function validateLayerWithData(+ {fields, id: dataId},+ savedLayer,+ layerClasses,+ options = {}+) {const {type} = savedLayer;// layer doesnt have a valid typeif (!layerClasses.hasOwnProperty(type) || !savedLayer.config || !savedLayer.config.columns) {@@ -537,10 +537,11 @@ export function validateLayerWithData({fields, id: dataId}, savedLayer, layerClaconst columnConfig = newLayer.getLayerColumns();if (Object.keys(columnConfig).length) {const columns = validateSavedLayerColumns(fields, savedLayer.config.columns, columnConfig);- if (!columns) {+ if (columns) {+ newLayer.updateLayerConfig({columns});+ } else if (!options.allowEmptyColumn) {return null;}- newLayer.updateLayerConfig({columns});}// visual channel field is saved to be {name, type}diff --git a/src/reducers/vis-state-updaters.d.ts b/src/reducers/vis-state-updaters.d.tsindex 2f41c2495..5028771b2 100644--- a/src/reducers/vis-state-updaters.d.ts+++ b/src/reducers/vis-state-updaters.d.ts@@ -310,7 +310,7 @@ export type VisState = {loadOptions: object;initialState?: Partial<VisState>;mergers: VisStateMergers;- schema: KeplerGLSchema,+ schema: KeplerGLSchema;preserveLayerOrder?: number[];};@@ -337,6 +337,15 @@ export function layerTextLabelChangeUpdater(state: VisState,action: VisStateActions.LayerTextLabelChangeUpdaterAction): VisState;+export function layerDataIdChangeUpdater(+ state: VisState,+ action: {+ oldLayer: Layer;+ newConfig: {+ dataId: string;+ };+ }+): VisState;export function layerTypeChangeUpdater(state: VisState,action: VisStateActions.LayerTypeChangeUpdaterAction@@ -473,9 +482,7 @@ export function loadFilesUpdater(state: VisState,action: VisStateActions.LoadFilesUpdaterAction): VisState;-export function loadNextFileUpdater(- state: VisState-): VisState;+export function loadNextFileUpdater(state: VisState): VisState;export function loadFilesSuccessUpdater(state: VisState,action: VisStateActions.loadFilesSuccessUpdaterActiondiff --git a/src/reducers/vis-state-updaters.js b/src/reducers/vis-state-updaters.jsindex 772961e26..1e1217476 100644--- a/src/reducers/vis-state-updaters.js+++ b/src/reducers/vis-state-updaters.js@@ -59,7 +59,7 @@ import {set, toArray} from 'utils/utils';import {calculateLayerData, findDefaultLayer} from 'utils/layer-utils';-import {isValidMerger, VIS_STATE_MERGERS} from './vis-state-merger';+import {isValidMerger, VIS_STATE_MERGERS, validateLayerWithData} from './vis-state-merger';import {addNewLayersToSplitMap,@@ -73,7 +73,7 @@ import {EDITOR_MODES, SORT_ORDER} from 'constants/default-settings';import {pick_, merge_} from './composer-helpers';import {processFileContent} from 'actions/vis-state-actions';-import KeplerGLSchema from 'schemas';+import KeplerGLSchema, {CURRENT_VERSION, visStateSchema} from 'schemas';// type imports/** @typedef {import('./vis-state-updaters').Field} Field */@@ -255,6 +255,16 @@ export function layerConfigChangeUpdater(state, action) {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') {+ const {dataId, ...restConfig} = action.newConfig;+ const stateWithDataId = layerDataIdChangeUpdater(state, {+ oldLayer,+ newConfig: {dataId}+ });+ const nextLayer = stateWithDataId.layers.find(l => l.id === oldLayer.id);+ return layerConfigChangeUpdater(state, {oldLayer: nextLayer, newConfig: restConfig});+ }+let newLayer = oldLayer.updateLayerConfig(action.newConfig);let layerData;@@ -350,6 +360,57 @@ export function layerTextLabelChangeUpdater(state, action) {});}+function validateExistingLayerWithData(dataset, layerClasses, layer) {+ let newLayer = layer;++ const savedVisState = visStateSchema[CURRENT_VERSION].save({+ layers: [newLayer],+ layerOrder: [0]+ }).visState;+ const loadedLayer = visStateSchema[CURRENT_VERSION].load(savedVisState).visState.layers[0];+ newLayer = validateLayerWithData(dataset, loadedLayer, layerClasses, {+ allowEmptyColumn: true+ });++ return newLayer;+}++/**+ * Update layer config dataId+ * @memberof visStateUpdaters+ * @type {typeof import('./vis-state-updaters').layerDataIdChangeUpdater}+ * @returns nextState+ */+export function layerDataIdChangeUpdater(state, action) {+ const {oldLayer, newConfig} = action;+ const {dataId} = newConfig;++ if (!oldLayer || !state.datasets[dataId]) {+ return state;+ }+ const idx = state.layers.findIndex(l => l.id === oldLayer.id);++ let newLayer = oldLayer.updateLayerConfig({dataId});+ // this may happen when a layer is new (type: null and no columns) but it's not ready to be saved+ if (newLayer.isValidToSave()) {+ newLayer = validateExistingLayerWithData(state.datasets[dataId], state.layerClasses, newLayer);+ // if cant validate it with data create a new one+ if (!newLayer) {+ newLayer = new state.layerClasses[oldLayer.type]({dataId, id: oldLayer.id});+ }+ }++ newLayer = newLayer.updateLayerConfig({+ isVisible: oldLayer.config.isVisible,+ isConfigActive: true+ });++ newLayer.updateLayerDomain(state.datasets);+ const {layerData, layer} = calculateLayerData(newLayer, state, undefined);++ return updateStateWithLayerAndData(state, {layerData, layer, idx});+}+/*** Update layer type. Previews layer config will be copied if applicable.* @memberof visStateUpdaters@@ -376,10 +437,6 @@ export function layerTypeChangeUpdater(state, action) {newLayer.assignConfigToLayer(oldLayer.config, oldLayer.visConfigSettings);- // if (newLayer.config.dataId) {- // const dataset = state.datasets[newLayer.config.dataId];- // newLayer.updateLayerDomain(dataset);- // }newLayer.updateLayerDomain(state.datasets);const {layerData, layer} = calculateLayerData(newLayer, state);let newState = updateStateWithLayerAndData(state, {layerData, layer, idx});diff --git a/test/browser.js b/test/browser.jsindex a52ff851a..e4a52e92c 100644--- a/test/browser.js+++ b/test/browser.js@@ -29,8 +29,8 @@ test.onFinish(window.browserTestDriver_finish);test.onFailure(window.browserTestDriver_fail);test('Browser tests', t => {- // require('./node/index.js');- require('./browser/index.js');+ require('./node/index.js');+ // require('./browser/index.js');t.end();});diff --git a/test/node/reducers/vis-state-merger-test.js b/test/node/reducers/vis-state-merger-test.jsindex 2747d285d..81640e320 100644--- a/test/node/reducers/vis-state-merger-test.js+++ b/test/node/reducers/vis-state-merger-test.js@@ -257,7 +257,6 @@ test('VisStateMerger.current -> mergeLayers -> toEmptyState', t => {const oldVisState = oldState.visState;const parsedLayers = configParsed.visState.layers;-// mergeLayersconst mergedState = mergeLayers(oldState.visState, parsedLayers, true);diff --git a/test/node/reducers/vis-state-test.js b/test/node/reducers/vis-state-test.jsindex b4db4f1d0..eaadf2486 100644--- a/test/node/reducers/vis-state-test.js+++ b/test/node/reducers/vis-state-test.js@@ -575,6 +575,112 @@ test('#visStateReducer -> LAYER_CONFIG_CHANGE -> isVisible -> splitMaps', t => {t.end();});+test('visStateReducer -> layerDataIdChangeUpdater', t => {+ const initialState = CloneDeep(StateWFilesFiltersLayerColor).visState;+ const pointLayer = initialState.layers[0];++ const nextState = reducer(+ initialState,+ VisStateActions.layerConfigChange(pointLayer, {+ dataId: testGeoJsonDataId+ })+ );+ const updatedLayer = nextState.layers[0];++ t.equal(updatedLayer.config.dataId, testGeoJsonDataId, 'should update point layer dataId');+ t.deepEqual(+ updatedLayer.config.columns,+ {+ altitude: {value: null, fieldIdx: -1, optional: true},+ lat: {value: null, fieldIdx: -1},+ lng: {value: null, fieldIdx: -1}+ },+ 'should not update point layer column'+ );+ t.equal(updatedLayer.config.colorField, null, 'should not update point layer colorField');++ // add layer+ const nextState1 = reducer(nextState, VisStateActions.addLayer());+ const newLayer = nextState1.layers[nextState1.layers.length - 1];++ const nextState2 = reducer(+ nextState1,+ VisStateActions.layerConfigChange(newLayer, {+ dataId: testGeoJsonDataId,+ color: [1, 2, 3]+ })+ );++ t.equal(+ nextState2.layers[nextState1.layers.length - 1].config.dataId,+ testGeoJsonDataId,+ 'should update new layer dataId'+ );+ t.deepEqual(+ nextState2.layers[nextState1.layers.length - 1].config.color,+ [1, 2, 3],+ 'should update new layer color'+ );++ t.end();+});++test('visStateReducer -> layerDataIdChangeUpdater -> validation', t => {+ const initialState = CloneDeep(StateWFilesFiltersLayerColor).visState;+ const pointLayer = initialState.layers[0];+ const textField = pointLayer.config.textLabel[0].field;++ const newDataInfo = {+ id: 'new-data-csv',+ label: 'new Data'+ };+ const fieldIdx = testFields.findIndex(f => f.name === textField.name);++ // remove 1 field from sample data+ const fields = testFields.filter(f => f.name !== textField.name);+ const rows = testAllData.map(row => [+ ...row.slice(0, fieldIdx),+ ...row.slice(fieldIdx + 1, row.length)+ ]);+ // add another dataset+ const nextState = reducer(+ initialState,+ VisStateActions.updateVisData([{info: newDataInfo, data: {fields, rows}}])+ );++ const nextState1 = reducer(+ nextState,+ VisStateActions.layerConfigChange(pointLayer, {+ dataId: 'new-data-csv'+ })+ );++ const updatedLayer = nextState1.layers[0];+ t.equal(updatedLayer.config.dataId, 'new-data-csv', 'should update point layer dataId');+ t.equal(+ updatedLayer.config.colorField.name,+ 'gps_data.types',+ 'should update point layer colorField'+ );++ t.deepEqual(+ updatedLayer.config.columns,+ {+ altitude: {value: null, fieldIdx: -1, optional: true},+ lat: {value: 'gps_data.lat', fieldIdx: 1},+ lng: {value: 'gps_data.lng', fieldIdx: 2}+ },+ 'should update point layer column'+ );++ t.equal(+ updatedLayer.config.textLabel[0].field,+ undefined,+ 'should assign fields not exists to undefined'+ );+ t.end();+});+test('#visStateReducer -> LAYER_VIS_CONFIG_CHANGE -> opacity', t => {const initialState = StateWFiles.visState;const layer = initialState.layers[0];