Skip to content

Commit 6c21193

Browse files
committed
autocomputeSpeed prop
1 parent 5430705 commit 6c21193

File tree

5 files changed

+21
-52
lines changed

5 files changed

+21
-52
lines changed

modules/carto/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ export {default as colorContinuous} from './style/color-continuous-style';
6868
export {fetchMap, LayerFactory} from './api/fetch-map';
6969
export {fetchBasemapProps} from './api/basemap';
7070
export {normalizeTimestamp} from './layers/trajectory-utils';
71-
export {applyTrajectoryColors} from './layers/trajectory-speed-utils';
7271
export type {
7372
FetchMapOptions,
7473
FetchMapResult,

modules/carto/src/layers/trajectory-speed-utils.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ function distanceBetweenPoints([lon1, lat1, lon2, lat2]: number[]): number {
2323
/**
2424
* Calculate speeds and write to numericProps attribute for trajectory geometry
2525
*/
26-
export function applyTrajectoryColors(
27-
geometry: ProcessedGeometry,
28-
getVertexColor?: (vertexData: {speed: number}, info: {index: number; data: any[]; target: any[]}) => [number, number, number, number]
29-
): void {
30-
const {positions, attributes, numericProps} = geometry;
26+
export function autocomputeSpeed(geometry: ProcessedGeometry): void {
27+
const {positions, attributes} = geometry;
3128
const n = positions.value.length / positions.size;
32-
33-
numericProps.speed = { value: new Float32Array(n), size: 1 };
29+
geometry.numericProps.speed = { value: new Float32Array(n), size: 1 };
3430

3531
// Calculate speed and write to numericProps
3632
let previousSpeed = 0;
@@ -69,6 +65,6 @@ export function applyTrajectoryColors(
6965
}
7066

7167
// Write speed to numericProps
72-
numericProps.speed.value[i] = speed;
68+
geometry.numericProps.speed!.value[i] = speed;
7369
}
7470
}

modules/carto/src/layers/trajectory-tile-layer.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import {GeoJsonLayer} from '@deck.gl/layers';
99
import type {TilejsonResult} from '@carto/api-client';
1010
import VectorTileLayer from './vector-tile-layer';
1111
import {transformTrajectoryData, type TileBounds, normalizeTimestamp} from './trajectory-utils';
12-
import {applyTrajectoryColors} from './trajectory-speed-utils';
12+
import {autocomputeSpeed} from './trajectory-speed-utils';
1313
import {createBinaryProxy, createEmptyBinary} from '../utils';
1414

1515
const defaultProps: DefaultProps<TrajectoryTileLayerProps> = {
1616
...TripsLayer.defaultProps,
1717
data: VectorTileLayer.defaultProps.data,
18+
autocomputeSpeed: false,
1819
renderMode: 'trips',
19-
palette: 'Sunset',
2020
fadeTrail: true,
2121
currentTime: 0,
2222
trailLength: 1000
@@ -30,31 +30,17 @@ export type TrajectoryTileLayerProps<FeaturePropertiesT = unknown> = _Trajectory
3030
type _TrajectoryTileLayerProps = {
3131
data: null | TilejsonResult | Promise<TilejsonResult>;
3232

33+
/**
34+
* Set to true to automatically compute speed for each vertex and store in properties.speed
35+
*/
36+
autocomputeSpeed?: boolean;
37+
3338
/**
3439
* Rendering mode for trajectories.
3540
* - 'paths': Static path rendering
3641
* - 'trips': Animated trip rendering with time controls
3742
*/
3843
renderMode?: 'paths' | 'trips';
39-
40-
/**
41-
* Color palette for trajectory visualization (deprecated - use getVertexColor instead)
42-
*/
43-
palette?: string;
44-
45-
/**
46-
* Accessor function to get color for each vertex based on calculated attributes
47-
* Receives an object with calculated properties (e.g., speed) and should return RGBA color array
48-
* @param vertexData - Object containing calculated attributes like {speed: number}
49-
* @param info - Additional info object with index, data, target properties
50-
* @returns RGBA color array [r, g, b, a] where values are 0-255
51-
*/
52-
getVertexColor?: (vertexData: {speed: number}, info: {index: number; data: any[]; target: any[]}) => [number, number, number, number];
53-
54-
/**
55-
* Outline color for the trajectory
56-
*/
57-
outline?: 'white' | 'black' | 'none';
5844
};
5945

6046
// @ts-ignore
@@ -120,10 +106,9 @@ export default class TrajectoryTileLayer<
120106

121107
if (!lines) return null;
122108

123-
// Optionally apply vertex colors
124-
//if (this.props.getVertexColor) {
125-
applyTrajectoryColors(lines, this.props.getVertexColor);
126-
//}
109+
if (this.props.autocomputeSpeed) {
110+
autocomputeSpeed(lines);
111+
}
127112

128113
return { ...createEmptyBinary(), lines }
129114
}
@@ -157,7 +142,6 @@ export default class TrajectoryTileLayer<
157142
lineJointRounded: props.jointRounded !== undefined ? props.jointRounded : true,
158143
capRounded: props.capRounded !== undefined ? props.capRounded : true,
159144
_pathType: props._pathType || 'open',
160-
outline: props.outline || 'none'
161145
};
162146

163147
const {minTime, maxTime} = this.state;

modules/carto/src/layers/trajectory-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export type ProcessedGeometry = BinaryLineFeature & {
4040
getColor?: {value: Uint8Array; size: number; normalized: boolean};
4141
getTimestamps: {value: Float32Array; size: number};
4242
};
43+
numericProps: {
44+
speed?: {value: Float32Array; size: number};
45+
[key: string]: {value: Float32Array; size: number} | undefined;
46+
};
4347
}
4448

4549
function isPointInTileBounds(

test/apps/carto-trajectories/app.tsx

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {MapboxOverlay} from '@deck.gl/mapbox';
66

77
import {DeckProps } from '@deck.gl/core';
88
import {trajectoryQuerySource, trajectoryTableSource} from '@carto/api-client';
9-
import {TrajectoryTileLayer, VectorTileLayer, colorCategories, colorContinuous} from '@deck.gl/carto';
9+
import {TrajectoryTileLayer, VectorTileLayer, colorContinuous} from '@deck.gl/carto';
1010
import {normalizeTimestamp} from '@deck.gl/carto/layers/trajectory-utils';
1111

1212
import RangeInput from './range-input';
@@ -317,28 +317,14 @@ export default function App() {
317317
colors: palette
318318
}),
319319

320-
// Color each vertex, will be invoked once per vertex
321-
_getFillColor: (d: any, info: any) => {
322-
return [255 * Math.random(), 255 * Math.random(), 255 * Math.random(), 255];
323-
},
320+
// Color each vertex by speed
321+
autocomputeSpeed: true, // Will add 'speed' attribute to geometry
324322
getFillColor: colorContinuous({
325323
attr: 'speed',
326324
domain: [0, 5, 10, 15, 20, 25],
327325
colors: palette
328326
}),
329327

330-
// Alternative API, dedicated accessor: Color each vertex by speed
331-
_getVertexColor: colorContinuous({
332-
attr: 'speed',
333-
domain: [5, 7, 9, 11, 13, 15],
334-
colors: palette
335-
}),
336-
337-
// Alternative examples:
338-
// getVertexColor: (d: any) => {
339-
340-
// return d.speed > 10 ? [255, 0, 0, 255] : [0, 255, 0, 255]; // Red/Green based on speed
341-
//},
342328
currentTime,
343329
trailLength,
344330
getWidth: lineWidth,

0 commit comments

Comments
 (0)