Skip to content

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

[Feat] replaceDataInMap action - save colorsByDataId (#2859)

Igor Dykhta6bc594602b12
diff --git a/examples/demo-app/src/app.tsx b/examples/demo-app/src/app.tsx
index 7fea93c76..329cd5cff 100644
--- a/examples/demo-app/src/app.tsx
+++ b/examples/demo-app/src/app.tsx
@@ -48,7 +48,13 @@ import sampleGeojsonPoints from './data/sample-geojson-points';
import sampleGeojsonConfig from './data/sample-geojson-config';
import sampleH3Data, {config as h3MapConfig} from './data/sample-hex-id-csv';
import sampleS2Data, {config as s2MapConfig, dataId as s2DataId} from './data/sample-s2-data';
-import sampleAnimateTrip, {animateTripDataId} from './data/sample-animate-trip-data';
+import sampleAnimateTrip, {
+ pointData,
+ pointDataId,
+ animateTripDataId,
+ replacePointData,
+ config as syncedTripConfig
+} from './data/sample-animate-trip-data';
import sampleIconCsv from './data/sample-icon-csv';
import sampleGpsData from './data/sample-gps-data';
import sampleRowData, {config as rowDataConfig} from './data/sample-row-data';
@@ -367,7 +373,7 @@ const App = props => {
const _loadGeojsonData = useCallback(() => {
// load geojson
const geojsonPoints = processGeojson(sampleGeojsonPoints);
- const geojsonZip = processGeojson(sampleGeojson);
+ const geojsonZip = null; // processGeojson(sampleGeojson);
dispatch(
addDataToMap({
datasets: [
@@ -392,6 +398,45 @@ const App = props => {
);
}, [dispatch]);
+ const _loadSyncedFilterWTripLayer = useCallback(() => {
+ dispatch(
+ addDataToMap({
+ datasets: [
+ {
+ info: {label: 'Trip animation', id: animateTripDataId},
+ data: processGeojson(sampleAnimateTrip)
+ },
+ {
+ info: {
+ label: 'Sample Taxi Trips',
+ id: pointDataId,
+ color: [255, 0, 0]
+ },
+ data: pointData
+ }
+ ],
+ config: syncedTripConfig,
+ options: {
+ centerMap: true
+ }
+ })
+ );
+ }, [dispatch]);
+
+ const _replaceSyncedFilterWTripLayer = useCallback(() => {
+ window.setTimeout(() => {
+ dispatch(
+ replaceDataInMap({
+ datasetToReplaceId: pointDataId,
+ datasetToUse: {
+ info: {label: 'Sample Taxi Trips Replaced', id: `${pointDataId}-2`},
+ data: replacePointData
+ }
+ })
+ );
+ }, 1000);
+ }, [dispatch]);
+
const _replaceData = useCallback(() => {
// add geojson data
const sliceData = processGeojson({
@@ -498,6 +543,8 @@ const App = props => {
// _loadGpsData();
// _loadRowData();
// _loadVectorTileData();
+ _loadSyncedFilterWTripLayer();
+ _replaceSyncedFilterWTripLayer();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
_loadPointData,
@@ -510,7 +557,9 @@ const App = props => {
_loadGpsData,
_loadRowData,
_replaceData,
- _loadVectorTileData
+ _loadVectorTileData,
+ _loadSyncedFilterWTripLayer,
+ _replaceSyncedFilterWTripLayer
]);
return (
diff --git a/examples/demo-app/src/data/sample-animate-trip-data.js b/examples/demo-app/src/data/sample-animate-trip-data.js
index 18884c0d1..e6b897646 100644
--- a/examples/demo-app/src/data/sample-animate-trip-data.js
+++ b/examples/demo-app/src/data/sample-animate-trip-data.js
@@ -455,3 +455,86 @@ export const replacePointData = {
['2019-08-12 2:10:30', -73.97627258, 40.75893402]
]
};
+
+export const config = {
+ version: 'v1',
+ config: {
+ visState: {
+ filters: [
+ {
+ dataId: [pointDataId],
+ id: '5tbxclf5g',
+ name: ['tpep_pickup_datetime'],
+ type: 'timeRange',
+ value: [1565576828000, 1565577141000],
+ animationWindow: 'free',
+ view: 'enlarged',
+ syncedWithLayerTimeline: true,
+ syncTimelineMode: 1,
+ enabled: true
+ }
+ ],
+ layers: [
+ {
+ id: 'point-layer',
+ type: 'point',
+ config: {
+ dataId: pointDataId,
+ label: 'pickup',
+ color: [221, 178, 124],
+ columns: {
+ lat: 'pickup_latitude',
+ lng: 'pickup_longitude'
+ },
+ isVisible: true
+ }
+ },
+ {
+ id: 'trip-layer',
+ type: 'trip',
+ config: {
+ dataId: animateTripDataId,
+ columnMode: 'geojson',
+ label: 'Trip animation',
+ color: [18, 147, 154],
+ columns: {
+ geojson: '_geojson'
+ }
+ }
+ }
+ ],
+ interactionConfig: {
+ tooltip: {
+ fieldsToShow: {
+ [animateTripDataId]: [
+ {
+ name: 'value',
+ format: null
+ }
+ ],
+ [pointDataId]: [
+ {
+ name: 'tpep_pickup_datetime',
+ format: 'L LT'
+ }
+ ]
+ },
+ compareMode: true,
+ compareType: 'relative',
+ enabled: true
+ }
+ },
+ layerBlending: 'normal',
+ overlayBlending: 'normal',
+ splitMaps: [],
+ animationConfig: {
+ currentTime: 1565577141000,
+ speed: 1
+ },
+ editor: {
+ features: [],
+ visible: true
+ }
+ }
+ }
+};
diff --git a/src/actions/src/actions.ts b/src/actions/src/actions.ts
index ad4b7c8cb..cf3b1fe76 100644
--- a/src/actions/src/actions.ts
+++ b/src/actions/src/actions.ts
@@ -204,7 +204,7 @@ export type ReplaceDataInMapPayload = {
/**
* Initialize kepler.gl reducer. It is used to pass in `mapboxApiAccessToken` to `mapStyle` reducer.
* @memberof main
- * @param {object} payload
+ * @param payload
* @param payload.datasetToReplaceId - mapboxApiAccessToken to be saved to mapStyle reducer
* @param payload.datasetToUse - mapboxApiUrl to be saved to mapStyle reducer.
* @public
diff --git a/src/reducers/src/vis-state-merger.ts b/src/reducers/src/vis-state-merger.ts
index da17611b1..6afcc97ba 100644
--- a/src/reducers/src/vis-state-merger.ts
+++ b/src/reducers/src/vis-state-merger.ts
@@ -92,9 +92,23 @@ export function replaceFilterDatasetIds(
savedFilter.forEach(filter => {
if (filter.dataId.includes(dataId)) {
const newDataId = filter.dataId.map(d => (d === dataId ? dataIdToUse : d));
+ let plotType;
+ // TODO: more generic approach to save plotType.colorsByDataId
+ if (filter.plotType?.colorsByDataId?.[dataId]) {
+ // replace colorByDataId in filter.plotType
+ const {[dataId]: color, ...rest} = filter.plotType?.colorsByDataId || {};
+ plotType = {
+ ...filter.plotType,
+ colorsByDataId: {
+ ...rest,
+ [dataIdToUse]: color
+ }
+ };
+ }
replaced.push({
...filter,
- dataId: newDataId
+ dataId: newDataId,
+ ...(plotType ? {plotType} : {})
});
}
});
diff --git a/src/table/src/kepler-table.ts b/src/table/src/kepler-table.ts
index 7d6c83efc..742888b5c 100644
--- a/src/table/src/kepler-table.ts
+++ b/src/table/src/kepler-table.ts
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
-import {console as Console} from 'global/console';
+import Console from 'global/console';
import {ascending, descending} from 'd3-array';
import {
diff --git a/src/utils/src/index.ts b/src/utils/src/index.ts
index fa39a2c98..e730e1ed8 100644
--- a/src/utils/src/index.ts
+++ b/src/utils/src/index.ts
@@ -100,7 +100,7 @@ export {
unobserveDimensions
} from './observe-dimensions';
export type {Dimensions} from './observe-dimensions';
-export {snapToMarks} from './plot';
+export {snapToMarks, getTimeBins} from './plot';
export * from './projection-utils';
export * from './split-map-utils';
export * from './utils';
diff --git a/test/fixtures/synced-filter-with-trip-layer.js b/test/fixtures/synced-filter-with-trip-layer.js
index 9db9ded36..3c70ad21a 100644
--- a/test/fixtures/synced-filter-with-trip-layer.js
+++ b/test/fixtures/synced-filter-with-trip-layer.js
@@ -12,8 +12,8 @@ export const syncedFilterWithTripLayerMap = {
{
version: 'v1',
data: {
- id: 'ku6sngc2',
- label: 'un_2573-few-points.csv',
+ id: 'points-dataset',
+ label: 'few-points.csv',
color: [143, 47, 191],
allData: [
[
@@ -168,8 +168,8 @@ export const syncedFilterWithTripLayerMap = {
{
version: 'v1',
data: {
- id: '5l94bqp',
- label: 'un_2573-trips.json',
+ id: 'trip-dataset',
+ label: 'trips.json',
color: [0, 92, 255],
allData: [
[
@@ -222,7 +222,7 @@ export const syncedFilterWithTripLayerMap = {
visState: {
filters: [
{
- dataId: ['ku6sngc2'],
+ dataId: ['points-dataset'],
id: 'vxwjjz1sf',
name: ['DateTime'],
type: 'timeRange',
@@ -239,7 +239,7 @@ export const syncedFilterWithTripLayerMap = {
id: 'proypi',
type: 'point',
config: {
- dataId: 'ku6sngc2',
+ dataId: 'points-dataset',
label: 'Point',
color: [255, 203, 153],
highlightColor: [252, 242, 26, 255],
@@ -292,7 +292,7 @@ export const syncedFilterWithTripLayerMap = {
id: 'p4jyzm7',
type: 'trip',
config: {
- dataId: '5l94bqp',
+ dataId: 'trip-dataset',
label: 'un_2573-trips',
color: [248, 149, 112],
highlightColor: [252, 242, 26, 255],
@@ -333,14 +333,14 @@ export const syncedFilterWithTripLayerMap = {
interactionConfig: {
tooltip: {
fieldsToShow: {
- ku6sngc2: [
+ 'point-dataset': [
{name: 'DateTime', format: null},
{name: 'Latitude', format: null},
{name: 'Longitude', format: null},
{name: 'Depth', format: null},
{name: 'Magnitude', format: null}
],
- '5l94bqp': [{name: 'vendor', format: null}]
+ 'trip-dataset': [{name: 'vendor', format: null}]
},
compareMode: false,
compareType: 'absolute',
diff --git a/test/node/reducers/composer-state-test.js b/test/node/reducers/composer-state-test.js
index cab648aac..0f1d8ad9c 100644
--- a/test/node/reducers/composer-state-test.js
+++ b/test/node/reducers/composer-state-test.js
@@ -3,7 +3,11 @@
/* eslint-disable max-statements */
+import {drainTasksForTesting, succeedTaskWithValues} from 'react-palm/tasks';
import test from 'tape';
+
+import {registerEntry} from '@kepler.gl/actions';
+import {processCsvData} from '@kepler.gl/processors';
import keplerGlReducer, {
addDataToMapUpdater,
replaceDataInMapUpdater,
@@ -12,9 +16,7 @@ import keplerGlReducer, {
visStateReducer,
mapStateReducer
} from '@kepler.gl/reducers';
-import {processCsvData} from '@kepler.gl/processors';
-import {registerEntry} from '@kepler.gl/actions';
-import {drainTasksForTesting, succeedTaskWithValues} from 'react-palm/tasks';
+import {getTimeBins} from '@kepler.gl/utils';
import testCsvData, {sampleConfig, dataWithNulls} from 'test/fixtures/test-csv-data';
import testHexIdData, {
@@ -26,6 +28,9 @@ import testHexIdData, {
import {cmpLayers, cmpFilters, cmpDataset, cmpInteraction} from 'test/helpers/comparison-utils';
import {applyExistingDatasetTasks} from 'test/helpers/mock-state';
+import {StateWSyncedTimeFilter} from '../../helpers/mock-state';
+import {testCsvDataSlice1Id, testCsvDataSlice2Id} from '../../fixtures/test-csv-data';
+
const mockRawData = {
fields: [
{
@@ -579,3 +584,141 @@ test('#composerStateReducer - replaceDataInMapUpdater: same dataId', t => {
t.deepEqual(nextSavedConfig.visState.layers, expectedLayers, 'should replace layer dataId');
t.end();
});
+
+test('#composerStateReducer - replaceDataInMapUpdater: syncedTimeFilter & match', t => {
+ const oldState = StateWSyncedTimeFilter;
+ const dataIdToReplace = 'dataset_to_replace';
+
+ const datasetToUse = {
+ data: processCsvData(dataWithNulls),
+ info: {
+ id: dataIdToReplace
+ }
+ };
+
+ let nextState = replaceDataInMapUpdater(oldState, {
+ payload: {
+ datasetToReplaceId: testCsvDataSlice1Id,
+ datasetToUse
+ }
+ });
+ // create datasets from existing tasks, trigger auto create layers
+ nextState = {
+ ...nextState,
+ visState: applyExistingDatasetTasks(visStateReducer, nextState.visState)
+ };
+
+ const oldFilter = oldState.visState.filters[0];
+ const expectedFilter = {
+ ...oldState.visState.filters[0],
+ dataId: [dataIdToReplace, testCsvDataSlice2Id],
+ // union [147407 1056000, 147407 1677000]; [147407 1301000, 1474072208000];
+ domain: [1474071056000, 1474072208000],
+ // value [1474071116000, 1474072188000] is still within so it doesnt change
+ value: oldState.visState.filters[0].value,
+ timeBins: {},
+ plotType: {
+ ...oldState.visState.filters[0].plotType,
+ colorsByDataId: {
+ [dataIdToReplace]: '#FF0000',
+ 'test-csv-data-2': '#00FF00'
+ }
+ }
+ };
+
+ // should recalculate timeBins based on new datasets
+ expectedFilter.timeBins = getTimeBins(
+ expectedFilter,
+ nextState.visState.datasets,
+ oldFilter.plotType.interval
+ );
+
+ t.deepEqual(
+ nextState.visState.filters,
+ [expectedFilter],
+ 'should replace dataset with syncd filter'
+ );
+ const expectedDomain0 = 1474071056000;
+ const expectedGpuFilter = {
+ filterRange: [
+ [expectedFilter.value[0] - expectedDomain0, expectedFilter.value[1] - expectedDomain0],
+ [0, 0],
+ [0, 0],
+ [0, 0]
+ ],
+ filterValueUpdateTriggers: {
+ gpuFilter_0: {name: 'gps_data.utc_timestamp', domain0: expectedDomain0},
+ gpuFilter_1: null,
+ gpuFilter_2: null,
+ gpuFilter_3: null
+ },
+ filterValueAccessor: 'dont test me'
+ };
+ t.deepEqual(
+ nextState.visState.datasets[dataIdToReplace].gpuFilter.filterRange,
+ expectedGpuFilter.filterRange,
+ 'gpu filterRange should be correct'
+ );
+ t.deepEqual(
+ nextState.visState.datasets[dataIdToReplace].gpuFilter.filterValueUpdateTriggers,
+ expectedGpuFilter.filterValueUpdateTriggers,
+ 'gpu filterValueUpdateTriggers should be correct'
+ );
+ t.deepEqual(
+ nextState.visState.datasets[testCsvDataSlice2Id].gpuFilter.filterRange,
+ expectedGpuFilter.filterRange,
+ 'gpu filterRange should be correct'
+ );
+ t.deepEqual(
+ nextState.visState.datasets[testCsvDataSlice2Id].gpuFilter.filterValueUpdateTriggers,
+ expectedGpuFilter.filterValueUpdateTriggers,
+ 'gpu filterValueUpdateTriggers should be correct'
+ );
+
+ t.end();
+});
+
+test('#composerStateReducer - replaceDataInMapUpdater: syncedTimeFilter & no match', t => {
+ const oldState = StateWSyncedTimeFilter;
+ const dataIdToReplace = 'dataset_to_replace';
+
+ const datasetToUse = {
+ // a different dataset with no match
+ data: processCsvData(testHexIdData),
+ info: {
+ id: dataIdToReplace
+ }
+ };
+
+ let nextState = replaceDataInMapUpdater(oldState, {
+ payload: {
+ datasetToReplaceId: testCsvDataSlice1Id,
+ datasetToUse
+ }
+ });
+ // create datasets from existing tasks, trigger auto create layers
+ nextState = {
+ ...nextState,
+ visState: applyExistingDatasetTasks(visStateReducer, nextState.visState)
+ };
+
+ t.equal(nextState.visState.filters.length, 0, 'should not merge filter if no match');
+ t.equal(
+ nextState.visState.layers.length,
+ 1,
+ 'should only keep 1 layer (not able to merge the other)'
+ );
+
+ t.deepEqual(oldState.visState.layers[1], nextState.visState.layers[0], 'should keep 1 layer');
+
+ t.equal(nextState.visState.layerToBeMerged.length, 1, 'should keep unmerged in layerToBeMerged');
+ t.equal(
+ nextState.visState.filterToBeMerged.length,
+ 1,
+ 'should keep unmerged in filterToBeMerged'
+ );
+
+ t.deepEqual(nextState.visState.interactionToBeMerged, {}, 'should reset interactionToBeMerged');
+ t.deepEqual(nextState.visState.splitMapsToBeMerged, [], 'should reset splitMapsToBeMerged');
+ t.end();
+});