Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix: keep Trip models on filtered trajectories (#3944)
Igor Dykhtad8cabb78dcfb
diff --git a/src/layers/src/trip-layer/trip-layer-color.spec.ts b/src/layers/src/trip-layer/trip-layer-color.spec.tsindex a16653c18..fdf1d2c16 100644--- a/src/layers/src/trip-layer/trip-layer-color.spec.ts+++ b/src/layers/src/trip-layer/trip-layer-color.spec.ts@@ -115,6 +115,326 @@ describe('TripLayer TABLE mode color by field', () => {});});+// The discarded middle vertex bends east; the retained trail runs due north.+async function makeFilteredModelTrip() {+ const table = new KeplerTable({color: [255, 0, 0]} as any);+ await table.importData({+ data: {+ rows: [+ ['removed', 0, 50, '2024-01-01 00:00:00', 'Uber', 0, 45],+ ['trip', 0, 0, '2024-01-01 00:00:00', 'Uber', 0, 45],+ ['trip', 10, 0, '2024-01-01 00:00:10', 'Lyft', 0, 30],+ ['trip', 15, 10, '2024-01-01 00:00:15', 'Uber', 0, 45],+ ['trip', 20, 0, '2024-01-01 00:00:20', 'Waymo', 0, 60]+ ],+ fields: [+ {name: 'id', type: ALL_FIELD_TYPES.string},+ {name: 'latitude', type: ALL_FIELD_TYPES.real},+ {name: 'longitude', type: ALL_FIELD_TYPES.real},+ {name: 'timestamp', type: ALL_FIELD_TYPES.timestamp},+ {name: 'company', type: ALL_FIELD_TYPES.string},+ {name: 'altitude', type: ALL_FIELD_TYPES.real},+ {name: 'heading', type: ALL_FIELD_TYPES.real}+ ]+ } as any+ });+ const layer = makeTripLayer(table);+ layer.updateLayerConfig({+ columns: {...layer.config.columns, altitude: {value: 'altitude', fieldIdx: 5}}+ });+ layer.updateLayerConfig({textLabel: [{...DEFAULT_TEXT_LABEL, field: table.fields[4]}]});+ layer.updateLayerVisConfig({+ scenegraphEnabled: true,+ scenegraph: 'custom',+ scenegraphCustomModelUrl: 'https://example.com/model.glb',+ scenegraphColorEnabled: true,+ scenegraphUseTrailColor: true+ });+ return {table, layer};+}++describe('TripLayer models follow filtered vertices', () => {+ test('positions, headings, labels, hover rows and vertex colors share the retained trajectory', async () => {+ const {table, layer} = await makeFilteredModelTrip();+ table.filteredIndex = [2, 4];+ const data = layer.formatLayerData({[table.id]: table}, null);+ const feature = data.data[0];+ expect(feature.properties.index).toBe(1);+ expect(data.getPath(feature).map(c => c.slice(0, 2))).toEqual([+ [0, 10],+ [0, 20]+ ]);+ const start = Date.UTC(2024, 0, 1);+ for (const seconds of [15, 10, 20]) {+ const animationConfig = {+ domain: layer.config.animation.domain,+ currentTime: start + seconds * 1000+ };+ const [, model, label] = layer.renderLayer({+ data,+ gpuFilter: table.gpuFilter,+ mapState: {zoom: 4, latitude: 0, longitude: 0},+ animationConfig,+ visible: true,+ layerCallbacks: {},+ idx: 0+ });+ expect(model.props.getPosition(feature)).toEqual([0, seconds, 0]);+ expect(label.props.getPosition(feature)).toEqual([0, seconds, 0]);+ expect(label.props.getText(feature)).toBe('Waymo');+ expect(+ layer.getHoverData(feature, table.dataContainer, table.fields, animationConfig)+ ).toEqual(table.dataContainer.rowAsArray(4));+ // Both retained vertices head north. Removing the eastward vertex must also remove its yaw.+ const matrix = model.props.getTransformMatrix(feature);+ expect(Array.from(matrix).every(Number.isFinite)).toBe(true);+ expect(matrix[0]).toBeCloseTo(0);+ // Also retain the supported per-vertex color accessor behavior.+ const getColor = layer._getColorGetter(+ {+ getColor: () => [+ [10, 20, 30],+ [40, 50, 60]+ ]+ },+ animationConfig+ );+ if (typeof getColor !== 'function') {+ throw new Error('Expected a per-vertex color accessor');+ }+ expect(getColor(feature)).toEqual([40, 50, 60]);+ }+ for (const seconds of [0, 9, 21]) {+ const animationConfig = {+ domain: layer.config.animation.domain,+ currentTime: start + seconds * 1000+ };+ const [, model] = layer.renderLayer({+ data,+ gpuFilter: table.gpuFilter,+ mapState: {zoom: 4, latitude: 0, longitude: 0},+ animationConfig+ });+ expect(model.props.getPosition(feature)).toEqual([0, 0, -Number.MAX_VALUE]);+ }+ });++ test.each([[[4]], [[]], [[0, 1, 2, 3, 4]]])(+ 'handles one, zero and all retained vertices (%j)',+ async indices => {+ const {table, layer} = await makeFilteredModelTrip();+ table.filteredIndex = indices;+ const data = layer.formatLayerData({[table.id]: table}, null);+ const animationConfig = {+ domain: layer.config.animation.domain,+ currentTime: Date.UTC(2024, 0, 1, 0, 0, 20)+ };+ const [, model] = layer.renderLayer({+ data,+ gpuFilter: table.gpuFilter,+ mapState: {zoom: 4, latitude: 0, longitude: 0},+ animationConfig+ });+ expect(data.data).toHaveLength(indices.length === 0 ? 0 : indices.length === 1 ? 1 : 2);+ if (indices.length) {+ const feature = data.data.find(f => f.properties.index === 1);+ expect(model.props.getPosition(feature)).toEqual([0, 20, 0]);+ expect(Array.from(model.props.getTransformMatrix(feature)).every(Number.isFinite)).toBe(+ true+ );+ }+ }+ );++ test.each([false, true])(+ 'keeps the configured yaw for a single retained vertex (invert=%s)',+ async invertYaw => {+ const {table, layer} = await makeFilteredModelTrip();+ table.filteredIndex = [4];+ layer.config.yawField = table.fields[6];+ layer.updateLayerVisConfig({invertYaw});+ const data = layer.formatLayerData({[table.id]: table}, null);+ const [, model, label] = layer.renderLayer({+ data,+ gpuFilter: table.gpuFilter,+ mapState: {zoom: 4, latitude: 0, longitude: 0},+ animationConfig: {+ domain: layer.config.animation.domain,+ currentTime: Date.UTC(2024, 0, 1, 0, 0, 20)+ }+ });+ const feature = data.data[0];+ const matrix = model.props.getTransformMatrix(feature);+ expect((Math.atan2(matrix[1], matrix[0]) * 180) / Math.PI).toBeCloseTo(invertYaw ? 150 : 30);+ expect(model.props.getPosition(feature)).toEqual([0, 20, 0]);+ expect(label.props.getText(feature)).toBe('Waymo');+ }+ );++ test.each([+ [false, false],+ [false, true],+ [true, false],+ [true, true]+ ])('interpolates configured yaw (filtered=%s, invert=%s)', async (filtered, invertYaw) => {+ const {table, layer} = await makeFilteredModelTrip();+ await table.update({+ fields: table.fields,+ rows: [+ ['trip', 0, 0, '2024-01-01 00:00:00', 'Uber', 0, 0],+ ['trip', 10, 0, '2024-01-01 00:00:10', 'Lyft', 0, 20],+ ['trip', 20, 0, '2024-01-01 00:00:20', 'Waymo', 0, 80]+ ]+ });+ if (filtered) table.filteredIndex = [1, 2];+ layer.config.yawField = table.fields[6];+ layer.updateLayerVisConfig({invertYaw});+ const data = layer.formatLayerData({[table.id]: table}, null);+ const feature = data.data[0];+ const cases = filtered+ ? [+ [10, 20],+ [15, 50],+ [20, 80]+ ]+ : [+ [0, 0],+ [5, 10],+ [10, 20],+ [15, 50],+ [20, 80]+ ];++ for (const [seconds, heading] of cases) {+ const options = {+ data,+ gpuFilter: table.gpuFilter,+ mapState: {zoom: 4, latitude: 0, longitude: 0},+ animationConfig: {+ domain: layer.config.animation.domain,+ currentTime: Date.UTC(2024, 0, 1) + seconds * 1000+ }+ };+ const [, model, label] = layer.renderLayer(options);+ const matrix = model.props.getTransformMatrix(feature);+ const expectedYaw = 90 - (invertYaw ? -heading : heading);+ expect((Math.atan2(matrix[1], matrix[0]) * 180) / Math.PI).toBeCloseTo(expectedYaw);+ expect(model.props.getPosition(feature)).toEqual([0, seconds, 0]);+ expect(label.props.getPosition(feature)).toEqual([0, seconds, 0]);++ // Geometry-derived direction uses the retained path, independently of yaw-field inversion.+ layer.config.yawField = null;+ const [, geometricModel] = layer.renderLayer(options);+ expect(geometricModel.props.getTransformMatrix(feature)[0]).toBeCloseTo(0);+ layer.config.yawField = table.fields[6];+ }+ });++ test.each([+ [undefined, 80, false],+ [null, 80, false],+ [NaN, 80, false],+ [Infinity, 80, false],+ [20, null, false],+ [20, 80, true]+ ])(+ 'keeps endpoint yaw when the first segment cannot interpolate (%s, %s, duplicate=%s)',+ async (firstHeading, lastHeading, duplicateTime) => {+ const {table, layer} = await makeFilteredModelTrip();+ await table.update({+ fields: table.fields,+ rows: [+ ['trip', 10, 0, '2024-01-01 00:00:10', 'Uber', 0, firstHeading],+ [+ 'trip',+ 20,+ 0,+ duplicateTime ? '2024-01-01 00:00:10' : '2024-01-01 00:00:20',+ 'Waymo',+ 0,+ lastHeading+ ]+ ]+ });+ layer.config.yawField = table.fields[6];+ const data = layer.formatLayerData({[table.id]: table}, null);+ for (const invertYaw of [false, true]) {+ layer.updateLayerVisConfig({invertYaw});+ const [, model] = layer.renderLayer({+ data,+ gpuFilter: table.gpuFilter,+ mapState: {zoom: 4, latitude: 0, longitude: 0},+ animationConfig: {+ domain: layer.config.animation.domain,+ currentTime: Date.UTC(2024, 0, 1, 0, 0, duplicateTime ? 10 : 15)+ }+ });+ const matrix = model.props.getTransformMatrix(data.data[0]);+ const expectedYaw = 90 - (invertYaw ? -1 : 1) * Number(lastHeading);+ expect((Math.atan2(matrix[1], matrix[0]) * 180) / Math.PI).toBeCloseTo(expectedYaw);+ }+ }+ );++ test('reuses filtered timestamps across frames and refreshes them after filters or data change', async () => {+ const {table, layer} = await makeFilteredModelTrip();+ const start = Date.UTC(2024, 0, 1);+ table.filteredIndex = [2, 4];+ let data = layer.formatLayerData({[table.id]: table}, null);+ let feature = data.data[0];+ const firstTimes = data.getTimestamps(feature);+ expect(firstTimes).toEqual([start + 10000, start + 20000]);+ for (const seconds of [11, 15, 19]) {+ const [, model, label] = layer.renderLayer({+ data,+ gpuFilter: table.gpuFilter,+ mapState: {zoom: 4, latitude: 0, longitude: 0},+ animationConfig: {+ domain: layer.config.animation.domain,+ currentTime: start + seconds * 1000+ }+ });+ expect(model.props.getPosition(feature)).toEqual([0, seconds, 0]);+ expect(label.props.getText(feature)).toBe('Waymo');+ expect(Array.from(model.props.getTransformMatrix(feature)).every(Number.isFinite)).toBe(true);+ expect(data.getTimestamps(feature)).toBe(firstTimes);+ }++ table.filteredIndex = [3, 4];+ data = layer.formatLayerData({[table.id]: table}, data);+ feature = data.data[0];+ const nextTimes = data.getTimestamps(feature);+ expect(nextTimes).toEqual([start + 15000, start + 20000]);+ expect(nextTimes).not.toBe(firstTimes);++ const rows = Array.from({length: table.dataContainer.numRows()}, (_, i) => [+ ...table.dataContainer.rowAsArray(i)+ ]);+ rows[3][3] = '2024-01-01 00:00:30';+ rows[4][3] = '2024-01-01 00:00:40';+ await table.update({rows, fields: table.fields});+ table.filteredIndex = [3, 4];+ data = layer.formatLayerData({[table.id]: table}, data);+ feature = data.data[0];+ expect(data.getTimestamps(feature)).toEqual([start + 30000, start + 40000]);+ expect(data.getTimestamps(feature)).not.toBe(nextTimes);+ const [, model] = layer.renderLayer({+ data,+ gpuFilter: table.gpuFilter,+ mapState: {zoom: 4, latitude: 0, longitude: 0},+ animationConfig: {domain: layer.config.animation.domain, currentTime: start + 35000}+ });+ expect(model.props.getPosition(feature)).toEqual([5, 17.5, 0]);++ table.filteredIndex = [];+ data = layer.formatLayerData({[table.id]: table}, data);+ expect(data.data).toEqual([]);+ table.filteredIndex = [2, 4];+ data = layer.formatLayerData({[table.id]: table}, data);+ expect(data.getTimestamps(data.data[0])).toEqual([start + 10000, start + 40000]);+ });+});+describe('TripLayer text label collision', () => {test('renders moving labels with collision off, on, and off again', async () => {const table = await makeTripTable();diff --git a/src/layers/src/trip-layer/trip-layer.ts b/src/layers/src/trip-layer/trip-layer.tsindex f4c02e339..45de1f35f 100644--- a/src/layers/src/trip-layer/trip-layer.ts+++ b/src/layers/src/trip-layer/trip-layer.ts@@ -308,6 +308,7 @@ export default class TripLayer extends Layer {dataToFeature: GeojsonDataMaps;dataToTimeStamp: any[];+ private _filteredTimestamps = new WeakMap<Feature, number[]>();getFeature: (columns: TripLayerColumnsConfig) => (dataContainer: DataContainerInterface) => any;_layerInfoModal: Record<string, () => JSX.Element>;@@ -556,7 +557,7 @@ export default class TripLayer extends Layer {// index for dataContainer is saved to feature.propertiesreturn dataContainer.row(object.properties.index);}- return this._findDatumForFeatureByTime(object.properties.index, animationConfig)?.datum;+ return this._findDatumForFeatureByTime(object, animationConfig)?.datum;}calculateDataAttribute(dataset: KeplerTable) {@@ -596,7 +597,7 @@ export default class TripLayer extends Layer {}const getText = (f, animationConfig) => {- const {datum} = this._findDatumForFeatureByTime(f.properties.index, animationConfig);+ const {datum} = this._findDatumForFeatureByTime(f, animationConfig);return getSingleTextLabelValue({field, format: ''}, datum);};@@ -617,15 +618,25 @@ export default class TripLayer extends Layer {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) {+ if (+ !timestamps ||+ this.config.columnMode !== COLUMN_MODE_TABLE ||+ feature === originalFeature+ ) {return timestamps;}- // Table filtering retains coordinate references but removes vertices from the trip.+ const cached = this._filteredTimestamps.get(feature);+ if (cached) return cached;++ // Filtering creates new features; cache their timestamps across animation frames.+ // Coordinate references preserve the ordering of the normalized source timestamps.const retainedCoordinates = new Set(feature.geometry.coordinates);- return timestamps?.filter((_, i) =>+ const filteredTimestamps = timestamps.filter((_, i) =>retainedCoordinates.has(originalFeature.geometry.coordinates[i]));+ this._filteredTimestamps.set(feature, filteredTimestamps);+ return filteredTimestamps;}// Called from gpu-filter-utils.getFilterValueAccessor(), which passes@@ -747,6 +758,7 @@ export default class TripLayer extends Layer {const {dataToTimeStamp, animationDomain} = timestamps;this.dataToTimeStamp = dataToTimeStamp;+ this._filteredTimestamps = new WeakMap();this.updateAnimationDomain(animationDomain);// get bounds from features@@ -851,7 +863,7 @@ export default class TripLayer extends Layer {const getPixelOffset = getTextOffsetByRadius(10, () => 1, mapState);const getPosition = f => {- const {coords} = this._findDatumForFeatureByTime(f.properties.index, animationConfig);+ const {coords} = this._findDatumForFeatureByTime(f, animationConfig);if (!coords) {return [0, 0, -Number.MAX_VALUE];}@@ -966,10 +978,7 @@ export default class TripLayer extends Layer {}return feature => {- const dataForTime = this._findDatumForFeatureByTime(- feature.properties.index,- animationConfig- );+ const dataForTime = this._findDatumForFeatureByTime(feature, animationConfig);const {idx} = dataForTime;const {coordinates} = feature.geometry;let roll = 0;@@ -1045,9 +1054,15 @@ export default class TripLayer extends Layer {prevYaw = idx > 1 ? getYawFromSpeedVector(coordinates[idx - 2], coordinates[idx - 1]) : 0;nextYaw = getYawFromSpeedVector(coordinates[idx - 1], coordinates[idx]);}- if (idx > 1) {+ if (+ idx > 1 ||+ (yawField &&+ Number.isFinite(prevDatum?.[yawField.fieldIdx]) &&+ Number.isFinite(datum?.[yawField.fieldIdx]) &&+ Number.isFinite(advancement))+ ) {yaw = interpolateAngle(prevYaw ?? 0, nextYaw ?? 0, advancement);- } else if (idx > 0) {+ } else if (idx > 0 || (idx === 0 && yawField && datum)) {yaw = nextYaw ?? 0;}return yaw;@@ -1064,10 +1079,7 @@ export default class TripLayer extends Layer {return feature => {const colors = data.getColor(feature);if (this.config.colorField && Array.isArray(colors[0])) {- const {idx} = this._findDatumForFeatureByTime(- feature.properties.index,- animationConfig- );+ const {idx} = this._findDatumForFeatureByTime(feature, animationConfig);return idx >= 0 ? colors[idx] : SCENEGRAPH_DEFAULT_COLOR;}return colors;@@ -1083,18 +1095,16 @@ export default class TripLayer extends Layer {}private _findDatumForFeatureByTime(- featureIndex: number,+ feature,animationConfig: AnimationConfig,interpolateCoords = true): DatumForFeatureByTime {const {currentTime} = animationConfig ?? {};if (notNullorUndefined(currentTime)) {- const object = this.dataToFeature[featureIndex];- const timestamps = this.dataToTimeStamp[featureIndex];+ const timestamps = this._getTimestamps(feature) ?? [];let idx = bisectRight(timestamps, currentTime);- // @ts-expect-error type geometry?- const coordinates = object?.geometry?.coordinates;- if (!coordinates) {+ const coordinates = feature?.geometry?.coordinates;+ if (!coordinates?.length || !timestamps.length) {return {idx: -1, coords: null, datum: null, prevDatum: null, advancement: 0};}if (idx >= coordinates.length) idx = coordinates.length - 1;@@ -1138,8 +1148,8 @@ export default class TripLayer extends Layer {keyframes: {startDatum: any; endDatum: any; factor: number};}[] = [];for (let i = 0; i < this.dataToFeature.length; i++) {- const df = this._findDatumForFeatureByTime(i, animationConfig, true);const feature = this.dataToFeature[i];+ const df = this._findDatumForFeatureByTime(feature, animationConfig, true);data.push({position: df.coords,orientation: {