Skip to content

Commit 5430705

Browse files
committed
speed color written to numeric props
1 parent 7405a4a commit 5430705

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ function distanceBetweenPoints([lon1, lat1, lon2, lat2]: number[]): number {
2121

2222

2323
/**
24-
* Calculate speeds and apply colors to trajectory geometry using a color accessor
24+
* Calculate speeds and write to numericProps attribute for trajectory geometry
2525
*/
2626
export function applyTrajectoryColors(
2727
geometry: ProcessedGeometry,
28-
getVertexColor: (vertexData: {speed: number}, info: {index: number; data: any[]; target: any[]}) => [number, number, number, number]
28+
getVertexColor?: (vertexData: {speed: number}, info: {index: number; data: any[]; target: any[]}) => [number, number, number, number]
2929
): void {
30-
// Add new attribute for per-vertex colors
31-
const {positions, attributes} = geometry;
30+
const {positions, attributes, numericProps} = geometry;
3231
const n = positions.value.length / positions.size;
33-
attributes.getColor = { value: new Uint8Array(4 * n), size: 4, normalized: true };
3432

35-
// Calculate speed and apply colors
33+
numericProps.speed = { value: new Float32Array(n), size: 1 };
34+
35+
// Calculate speed and write to numericProps
3636
let previousSpeed = 0;
3737

3838
for (let i = 0; i < n; i++) {
@@ -68,8 +68,7 @@ export function applyTrajectoryColors(
6868
speed = 100; // fallback speed
6969
}
7070

71-
// Use the provided color accessor
72-
const color = getVertexColor({speed}, {index: i, data: [], target: []});
73-
attributes.getColor.value.set(color, 4 * i);
71+
// Write speed to numericProps
72+
numericProps.speed.value[i] = speed;
7473
}
7574
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ export default class TrajectoryTileLayer<
121121
if (!lines) return null;
122122

123123
// Optionally apply vertex colors
124-
if (this.props.getVertexColor) {
124+
//if (this.props.getVertexColor) {
125125
applyTrajectoryColors(lines, this.props.getVertexColor);
126-
}
126+
//}
127127

128128
return { ...createEmptyBinary(), lines }
129129
}

test/apps/carto-trajectories/app.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,14 @@ export default function App() {
322322
return [255 * Math.random(), 255 * Math.random(), 255 * Math.random(), 255];
323323
},
324324
getFillColor: colorContinuous({
325-
attr: 'ts',
326-
domain: [0, 200, 300, 500, 700],
325+
attr: 'speed',
326+
domain: [0, 5, 10, 15, 20, 25],
327327
colors: palette
328328
}),
329329

330330
// Alternative API, dedicated accessor: Color each vertex by speed
331331
_getVertexColor: colorContinuous({
332-
attr: (d: any) => d.speed,
332+
attr: 'speed',
333333
domain: [5, 7, 9, 11, 13, 15],
334334
colors: palette
335335
}),

0 commit comments

Comments
 (0)