Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix(trip): align filtered coordinates and timestamps (#3915)
Igor Dykhta69bcbc8a674f
diff --git a/src/layers/src/trip-layer/trip-layer.ts b/src/layers/src/trip-layer/trip-layer.tsindex 990700582..896a7b5c5 100644--- a/src/layers/src/trip-layer/trip-layer.ts+++ b/src/layers/src/trip-layer/trip-layer.ts@@ -59,6 +59,7 @@ import {DataContainerInterface} from '@kepler.gl/utils';import {notNullorUndefined} from '@kepler.gl/common-utils';import {ScenegraphLayer as DeckScenegraphLayer} from '@deck.gl/mesh-layers';import {fetchGltf} from '../scenegraph-layer/scenegraph-layer';+import type {Feature, LineString} from 'geojson';export type TripLayerVisConfigSettings = {opacity: VisConfigNumber;@@ -608,13 +609,28 @@ export default class TripLayer extends Layer {});}+ private _getTimestamps(feature: Feature<LineString, {index: number}>) {+ const {index} = feature.properties;+ const timestamps = this.dataToTimeStamp[index];+ const originalFeature = this.dataToFeature[index] as Feature<LineString, {index: number}>;+ if (this.config.columnMode !== COLUMN_MODE_TABLE || feature === originalFeature) {+ return timestamps;+ }++ // Table filtering retains coordinate references but removes vertices from the trip.+ const retainedCoordinates = new Set(feature.geometry.coordinates);+ return timestamps?.filter((_, i) =>+ retainedCoordinates.has(originalFeature.geometry.coordinates[i])+ );+ }+// Called from gpu-filter-utils.getFilterValueAccessor(), which passes// (dataContainer, feature, fieldIndex). The trip carries its own materialised// rows in properties.values, so the data container is not needed here._getColumnModeValueAccessor = (dc, feature) => {return field => {if (field.fieldIdx === this.config.columns.timestamp?.fieldIdx) {- return this.dataToTimeStamp[feature.properties.index];+ return this._getTimestamps(feature);}return feature.properties.values.map(v => field.valueAccessor(v));};@@ -680,7 +696,7 @@ export default class TripLayer extends Layer {data,getFilterValue,getPath: d => d.geometry.coordinates,- getTimestamps: d => this.dataToTimeStamp[d.properties.index],+ getTimestamps: d => this._getTimestamps(d),textLabels,...accessors};diff --git a/test/browser/layer-tests/trip-layer-specs.js b/test/browser/layer-tests/trip-layer-specs.jsindex 8c525a88b..439ddf5b1 100644--- a/test/browser/layer-tests/trip-layer-specs.js+++ b/test/browser/layer-tests/trip-layer-specs.js@@ -13,6 +13,10 @@ import {} from '@kepler.gl/layers';import {copyTableAndUpdate} from '@kepler.gl/table';+import {processCsvData} from '@kepler.gl/processors';+import {INITIAL_MAP_STATE, INITIAL_VIS_STATE, visStateReducer} from '@kepler.gl/reducers';+import {VisStateActions} from '@kepler.gl/actions';+import {applyActions} from 'test/helpers/mock-state';const {TripLayer} = KeplerGlLayers;import {@@ -303,6 +307,108 @@ test('#TripLayer -> formatLayerData -> table column mode with gpu filter', t =>t.end();});+test('#TripLayer -> table vertex filters keep rendered and GPU timestamps aligned', t => {+ const start = Date.UTC(2024, 0, 1);+ const csv = `id,lat,lng,timestamp,status+dropped,9,19,2024-01-01 00:00:00,skip+retained,13,23,2024-01-01 00:00:30,last+retained,10,20,2024-01-01 00:00:00,skip+retained,11,21,2024-01-01 00:00:10,keep+retained,12,22,2024-01-01 00:00:20,skip+retained,,,2024-01-01 00:00:40,keep+retained,14,24,,keep`;+ let state = applyActions(visStateReducer, INITIAL_VIS_STATE, [+ {+ action: VisStateActions.updateVisData,+ payload: [{info: {id: dataId}, data: processCsvData(csv)}]+ },+ {action: VisStateActions.addFilter, payload: [dataId]},+ {action: VisStateActions.setFilter, payload: [0, 'name', 'timestamp']}+ ]);++ testFormatLayerDataCases(t, TripLayer, [+ {+ name: 'filtered trip timestamps',+ layer: {+ type: 'trip',+ config: {+ dataId,+ columnMode: 'table',+ columns: {id: 'id', lat: 'lat', lng: 'lng', timestamp: 'timestamp'}+ }+ },+ datasets: state.datasets,+ assert: ({layer, layerData}) => {+ t.deepEqual(+ layerData.getTimestamps(layerData.data[1]),+ [start, start + 10000, start + 20000, start + 30000],+ 'unfiltered timestamps are sorted and omit rows missing coordinates or time'+ );+ state = visStateReducer(state, VisStateActions.addFilter(dataId));+ state = visStateReducer(state, VisStateActions.setFilter(1, 'name', 'status'));++ [+ {+ value: ['keep', 'last'],+ coordinates: [+ [21, 11],+ [23, 13]+ ],+ times: [10000, 30000]+ },+ {value: ['last'], coordinates: [[23, 13]], times: [30000]},+ {value: ['absent'], coordinates: [], times: []},+ {+ value: ['keep', 'last', 'skip'],+ coordinates: [+ [20, 10],+ [21, 11],+ [22, 12],+ [23, 13]+ ],+ times: [0, 10000, 20000, 30000]+ }+ ].forEach(({value, coordinates, times}) => {+ state = visStateReducer(state, VisStateActions.setFilter(1, 'value', value));+ layerData = layer.formatLayerData(state.datasets, layerData);+ const feature = layerData.data.find(d => d.properties.index === 1);+ t.deepEqual(+ feature ? layerData.getPath(feature).map(c => c.slice(0, 2)) : [],+ coordinates,+ `categorical filter ${value} retains the expected sorted coordinates`+ );+ if (!feature) {+ t.equal(layerData.data.length, 0, 'filtering every vertex leaves no trips');+ return;+ }+ t.deepEqual(+ layerData.getTimestamps(feature),+ times.map(time => start + time),+ `categorical filter ${value} retains the matching normalized timestamps`+ );+ t.deepEqual(+ layerData.getFilterValue(feature).map(vertex => vertex[0]),+ times,+ 'GPU timestamp values stay aligned with retained vertices'+ );+ const [trails] = layer.renderLayer({+ data: layerData,+ gpuFilter: state.datasets[dataId].gpuFilter,+ mapState: INITIAL_MAP_STATE,+ animationConfig: {domain: [start, start + 40000], currentTime: start + 15000}+ });+ t.deepEqual(+ trails.props.getTimestamps(feature),+ times,+ 'rendered timestamps retain the same coordinate order after domain subtraction'+ );+ });+ }+ }+ ]);+ t.end();+});+test('#TripLayer -> renderLayer', t => {const filteredIndex = [0, 2, 4];