Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[Enhancement] save and load text label config (#242)
Shan He8254abc1e32e
diff --git a/package.json b/package.jsonindex 091276618..4118c2f4c 100644--- a/package.json+++ b/package.json@@ -28,6 +28,7 @@"fast-test": "NODE_ENV=test babel-tape-runner ./test/node.js && NODE_ENV=test babel-tape-runner -r ./test/setup-browser-env.js ./test/browser.js","test-browser": "NODE_ENV=test babel-tape-runner -r ./test/setup-browser-env.js ./test/browser.js","test-node": "NODE_ENV=test babel-tape-runner ./test/node.js | tap-spec",+ "test": "npm run test-node && npm run test-browser","cover": "nyc --reporter html --reporter cobertura --reporter json-summary npm test && nyc report","start": "npm run install-and-start -- examples/demo-app start-local","start:custom-layer": "npm run install-and-start -- examples/custom-layer start-local",diff --git a/src/reducers/vis-state-merger.js b/src/reducers/vis-state-merger.jsindex ffe5f1c09..689e55478 100644--- a/src/reducers/vis-state-merger.js+++ b/src/reducers/vis-state-merger.js@@ -291,6 +291,29 @@ export function validateSavedLayerColumns(fields, savedCols, emptyCols) {return allColFound && colFound;}+/**+ * Validate saved text label config with new data+ * refer to vis-state-schema.js TextLabelSchemaV1+ *+ * @param {Object[]} fields+ * @param {Object} savedTextLabel+ * @return {Object} - validated textlabel+ */+export function validateSavedTextLabel(fields, layerTextLabel, savedTextLabel) {++ // validate field+ const field = savedTextLabel.field ? fields.find(fd =>+ Object.keys(savedTextLabel.field).every(+ key => savedTextLabel.field[key] === fd[key]+ )+ ) : null;++ return Object.keys(layerTextLabel).reduce((accu, key) => ({+ ...accu,+ [key]: key === 'field' ? field : (savedTextLabel[key] || layerTextLabel[key])+ }), {});+}+/*** Validate saved visual channels config with new data,* refer to vis-state-schema.js VisualChannelSchemaV1@@ -372,6 +395,12 @@ export function validateLayerWithData({fields, id: dataId}, savedLayer, layerClasavedLayer);+ const textLabel = savedLayer.config.textLabel && newLayer.config.textLabel ? validateSavedTextLabel(+ fields,+ newLayer.config.textLabel,+ savedLayer.config.textLabel+ ) : newLayer.config.textLabel;+// copy visConfig over to emptyLayer to make sure it has all the propsconst visConfig = newLayer.copyLayerConfig(newLayer.config.visConfig,@@ -382,6 +411,7 @@ export function validateLayerWithData({fields, id: dataId}, savedLayer, layerClanewLayer.updateLayerConfig({columns,visConfig,+ textLabel,...foundVisualChannelConfigs});diff --git a/src/schemas/vis-state-schema.js b/src/schemas/vis-state-schema.jsindex 8b39556a5..6868a40ff 100644--- a/src/schemas/vis-state-schema.js+++ b/src/schemas/vis-state-schema.js@@ -290,6 +290,21 @@ class ColumnSchemaV1 extends Schema {}}+class TextLabelSchemaV1 extends Schema {+ save(textLabel) {+ return {+ [this.key]: {+ ...textLabel,+ field: textLabel.field ? pick(textLabel.field, ['name', 'type']) : null+ }+ }+ }++ load(textLabel) {+ return {textLabel};+ }+}+/*** V1: save [field]: {name, type}, [scale]: '' for each channel*/@@ -337,7 +352,11 @@ export const layerPropsV1 = {key: 'columns'}),isVisible: null,- visConfig: null+ visConfig: null,+ textLabel: new TextLabelSchemaV1({+ version: VERSIONS.v1,+ key: 'textLabel'+ })}}),visualChannels: new VisualChannelSchemaV1({diff --git a/test/fixtures/state-saved-v1-4.js b/test/fixtures/state-saved-v1-4.jsnew file mode 100644index 000000000..d3123e50b--- /dev/null+++ b/test/fixtures/state-saved-v1-4.js@@ -0,0 +1,267 @@+// Copyright (c) 2018 Uber Technologies, Inc.+//+// Permission is hereby granted, free of charge, to any person obtaining a copy+// of this software and associated documentation files (the "Software"), to deal+// in the Software without restriction, including without limitation the rights+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+// copies of the Software, and to permit persons to whom the Software is+// furnished to do so, subject to the following conditions:+//+// The above copyright notice and this permission notice shall be included in+// all copies or substantial portions of the Software.+//+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+// THE SOFTWARE.+import {KeplerGlLayers} from 'layers';+const {PointLayer} = KeplerGlLayers;++export const stateSavedV1 = {+ datasets: [+ {+ version: 'v1',+ data: {+ id: '8ppj5gfrs',+ label: 'eats_res_radius.csv',+ color: [143, 47, 191],+ allData: [+ [1000, 38.4163786, -121.7922809, false, true, 1, false, 'Hello'],+ [1000, 37.75159991, -122.4761712, false, false, 2, false, 'Smoothie'],+ [2000, 37.8323825, -122.2736475, false, true, 3, false, 'Milkshake'],+ [2000, 37.77565501, -122.4403984, false, true, 4, false, 'Amsterdam'],+ [3000, 37.79659543, -122.4219072, false, true, 0, false, 'Smoothie'],+ [3000, 37.7980847, -122.4050548, false, false, 1, false, 'Smoothie'],+ [3000, 37.79242329, -122.3986584, false, false, 0, false, 'World'],+ [3000, 37.79669585, -122.4219416, false, false, 1, false, 'yo'],+ [2000, 37.6169644, -122.384047, false, false, 0, false, 'really']+ ],+ fields: [+ {+ name: 'radius',+ type: 'integer',+ format: ''+ },+ {+ name: 'point_lat',+ type: 'real',+ format: ''+ },+ {+ name: 'point_lng',+ type: 'real',+ format: ''+ },+ {+ name: 'boolean',+ type: 'boolean',+ format: ''+ },+ {+ name: 'num_boolean',+ type: 'boolean',+ format: ''+ },+ {+ name: 'int_num',+ type: 'integer',+ format: ''+ },+ {+ name: 'boolean_1',+ type: 'boolean',+ format: ''+ },+ {+ name: 'name',+ type: 'string',+ format: ''+ }+ ]+ }+ }+ ],+ config: {+ version: 'v1',+ config: {+ visState: {+ filters: [],+ layers: [+ {+ id: 'k2ghkng',+ type: 'point',+ config: {+ dataId: '8ppj5gfrs',+ label: 'point',+ color: [23, 184, 190, 255],+ columns: {+ lat: 'point_lat',+ lng: 'point_lng',+ altitude: null+ },+ isVisible: true,+ visConfig: {+ radius: 12.5,+ fixedRadius: false,+ opacity: 0.8,+ outline: false,+ thickness: 2,+ colorRange: {+ name: 'Global Warming',+ type: 'sequential',+ category: 'Uber',+ colors: [+ '#5A1846',+ '#900C3F',+ '#C70039',+ '#E3611C',+ '#F1920E',+ '#FFC300'+ ]+ },+ radiusRange: [0, 50],+ 'hi-precision': false+ },+ textLabel: {+ field: {+ name: 'name',+ type: 'string'+ },+ color: [184, 15, 135, 255],+ size: 27,+ offset: [-10, 0],+ anchor: 'end'+ }+ },+ visualChannels: {+ colorField: null,+ colorScale: 'quantile',+ sizeField: null,+ sizeScale: 'linear'+ }+ }+ ],+ interactionConfig: {+ tooltip: {+ fieldsToShow: {+ '8ppj5gfrs': [+ 'radius',+ 'boolean',+ 'num_boolean',+ 'int_num',+ 'boolean_1'+ ]+ },+ enabled: true+ },+ brush: {+ size: 0.5,+ enabled: false+ }+ },+ layerBlending: 'normal',+ splitMaps: []+ },+ mapState: {+ bearing: 0,+ dragRotate: false,+ latitude: 37.871422572545065,+ longitude: -122.32925002428057,+ pitch: 0,+ zoom: 10.386420916638542,+ isSplit: false+ },+ mapStyle: {+ styleType: 'dark',+ topLayerGroups: {},+ visibleLayerGroups: {+ label: true,+ road: true,+ border: false,+ building: true,+ water: true,+ land: true+ },+ mapStyles: {}+ }+ }+ },+ info: {+ app: 'kepler.gl',+ created_at: 'Tue Oct 02 2018 16:11:32 GMT-0700 (Pacific Daylight Time)'+ }+};++const mergedLayer0 = new PointLayer({+ id: 'k2ghkng'+});++mergedLayer0.config = {+ dataId: '8ppj5gfrs',+ label: 'point',+ color: [23, 184, 190, 255],+ columns: {+ lat: {+ value: 'point_lat',+ fieldIdx: 1+ },+ lng: {+ value: 'point_lng',+ fieldIdx: 2+ },+ altitude: {+ fieldIdx: -1,+ optional: true,+ value: null+ }+ },+ isVisible: true,+ colorField: null,+ colorDomain: [0, 1],+ highlightColor: [252, 242, 26, 255],+ isConfigActive: false,+ colorScale: 'quantile',+ sizeField: null,+ sizeDomain: [0, 1],+ sizeScale: 'linear',+ textLabel: {+ field: {+ name: 'name',+ type: 'string',+ format: '',+ id: 'name',+ tableFieldIndex: 8+ },+ color: [184, 15, 135, 255],+ size: 27,+ offset: [-10, 0],+ anchor: 'end'+ },+ visConfig: {+ radius: 12.5,+ fixedRadius: false,+ opacity: 0.8,+ outline: false,+ thickness: 2,+ colorRange: {+ name: 'Global Warming',+ type: 'sequential',+ category: 'Uber',+ colors: [+ '#5A1846',+ '#900C3F',+ '#C70039',+ '#E3611C',+ '#F1920E',+ '#FFC300'+ ]+ },+ radiusRange: [0, 50],+ 'hi-precision': false+ },+};++export const mergedLayers = [mergedLayer0];diff --git a/test/helpers/mock-state.js b/test/helpers/mock-state.jsindex 79fcfd668..61d56c216 100644--- a/test/helpers/mock-state.js+++ b/test/helpers/mock-state.js@@ -158,6 +158,22 @@ function mockStateWithLayerDimensions(state) {'color'];+ const textLabelField = initialState.visState.datasets['190vdll3di'].fields.find(+ f => f.name === 'date'+ );++ const textLabelPayload = [+ layer0,+ {+ textLabel: {+ field: textLabelField,+ color: [255, 0, 0],+ size: 100,+ offset: [10, 0],+ anchor: 'start'+ }+ }+ ]// layers = [ 'point', 'geojson', 'hexagon' ]const reorderPayload = [[2, 0, 1]];@@ -171,6 +187,9 @@ function mockStateWithLayerDimensions(state) {// change colorRange{action: VisStateActions.layerVisConfigChange, payload: colorRangePayload},+ // change textLabel+ {action: VisStateActions.layerConfigChange, payload: textLabelPayload},+// add layer{action: VisStateActions.addLayer,@@ -298,6 +317,13 @@ export const expectedSavedLayer0 = {colorAggregation: 'count',sizeAggregation: 'count',enable3d: false+ },+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'}},visualChannels: {@@ -305,7 +331,7 @@ export const expectedSavedLayer0 = {colorScale: 'quantile',sizeField: null,sizeScale: 'linear'- }+ },};export const expectedLoadedLayer0 = {@@ -350,7 +376,14 @@ export const expectedLoadedLayer0 = {colorField: null,colorScale: 'quantile',sizeField: null,- sizeScale: 'linear'+ sizeScale: 'linear',+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ }}};@@ -366,6 +399,16 @@ export const expectedSavedLayer1 = {lng: 'gps_data.lng',altitude: null},+ textLabel: {+ field: {+ name: 'date',+ type: 'date'+ },+ color: [255, 0, 0],+ size: 100,+ offset: [10, 0],+ anchor: 'start'+ },isVisible: true,visConfig: {radius: 10,@@ -428,7 +471,17 @@ export const expectedLoadedLayer1 = {},colorScale: 'ordinal',sizeField: null,- sizeScale: 'linear'+ sizeScale: 'linear',+ textLabel: {+ field: {+ name: 'date',+ type: 'date'+ },+ color: [255, 0, 0],+ size: 100,+ offset: [10, 0],+ anchor: 'start'+ }}};@@ -469,6 +522,13 @@ export const expectedSavedLayer2 = {filled: false,enable3d: false,wireframe: false+ },+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'}},visualChannels: {@@ -528,6 +588,13 @@ export const expectedLoadedLayer2 = {heightField: null,heightScale: 'linear',radiusField: null,- radiusScale: 'linear'+ radiusScale: 'linear',+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ }}};diff --git a/test/node/reducers/vis-state-merger-test.js b/test/node/reducers/vis-state-merger-test.jsindex 80c8a435d..14d5367ea 100644--- a/test/node/reducers/vis-state-merger-test.js+++ b/test/node/reducers/vis-state-merger-test.js@@ -52,6 +52,10 @@ import {mergedLayers as mergedLayersV1Split,mergedSplitMaps as mergedSplitMapsV1} from 'test/fixtures/state-saved-v1-3';+import {+ stateSavedV1 as savedStateV1Label,+ mergedLayers as mergedLayersV1Label+} from 'test/fixtures/state-saved-v1-4';/*import {mergedFiltersV0,@@ -335,6 +339,43 @@ test('VisStateMerger.v1 -> mergeLayers -> toEmptyState', t => {t.end();});+test('VisStateMerger.v1.label -> mergeLayers -> toEmptyState', t => {+ const savedConfig = cloneDeep(savedStateV1Label);+ const parsedConfig = SchemaManager.parseSavedConfig(savedConfig.config);++ const oldState = InitialState.toJS();+ const oldVisState = oldState.visState;++ const parsedLayers = parsedConfig.visState.layers;++ // mergeLayers+ const mergedState = mergeLayers(oldState.visState, parsedLayers);++ Object.keys(oldVisState).forEach(key => {+ if (key === 'layerToBeMerged') {+ t.deepEqual(+ mergedState.layerToBeMerged,+ parsedLayers,+ 'Should save layers to layerToBeMerged before data loaded'+ );+ } else {+ t.deepEqual(+ mergedState[key],+ oldVisState[key],+ 'Should keep the rest of state same'+ );+ }+ });+ const parsedData = SchemaManager.parseSavedData(savedStateV1Label.datasets);++ // load data into reducer+ const stateWData = visStateReducer(mergedState, updateVisData(parsedData));++ // test parsed layers+ cmpLayers(t, mergedLayersV1Label, stateWData.layers, {id: true});+ t.end();+});+test('VisStateMerger.v1.split -> mergeLayers -> toEmptyState', t => {const savedConfig = cloneDeep(savedStateV1Split);const parsedConfig = SchemaManager.parseSavedConfig(savedConfig.config);diff --git a/test/node/reducers/vis-state-test.js b/test/node/reducers/vis-state-test.jsindex fd35afe17..67876c3ae 100644--- a/test/node/reducers/vis-state-test.js+++ b/test/node/reducers/vis-state-test.js@@ -85,29 +85,25 @@ const expectedFields = [name: 'start_point_lat',id: 'start_point_lat',type: 'real',- tableFieldIndex: 1,- format: ''+ tableFieldIndex: 1},{name: 'start_point_lng',id: 'start_point_lng',type: 'real',- tableFieldIndex: 2,- format: ''+ tableFieldIndex: 2},{name: 'end_point_lat',id: 'end_point_lat',type: 'real',- tableFieldIndex: 3,- format: ''+ tableFieldIndex: 3},{name: 'end_point_lng',id: 'end_point_lng',type: 'real',- tableFieldIndex: 4,- format: ''+ tableFieldIndex: 4}];