Skip to content

Commit 7405a4a

Browse files
committed
Copy across numeric props and use binary proxy
1 parent 412c071 commit 7405a4a

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {TilejsonResult} from '@carto/api-client';
1010
import VectorTileLayer from './vector-tile-layer';
1111
import {transformTrajectoryData, type TileBounds, normalizeTimestamp} from './trajectory-utils';
1212
import {applyTrajectoryColors} from './trajectory-speed-utils';
13-
import {createEmptyBinary} from '../utils';
13+
import {createBinaryProxy, createEmptyBinary} from '../utils';
1414

1515
const defaultProps: DefaultProps<TrajectoryTileLayerProps> = {
1616
...TripsLayer.defaultProps,
@@ -168,8 +168,11 @@ export default class TrajectoryTileLayer<
168168
const startIndex = data.startIndices[index];
169169
const endIndex = data.startIndices[index + 1];
170170
const nVertices = endIndex - startIndex;
171-
const colors = new Array(nVertices).fill(0).map((_, index) => {
172-
return props.getFillColor!(undefined, {index, data});
171+
const colors = new Array(nVertices).fill(0).map((_, i) => {
172+
const index = startIndex + i;
173+
const properties = createBinaryProxy(data, index);
174+
const d = {properties};
175+
return props.getFillColor!(d, {index, data});
173176
});
174177

175178
return colors;

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (c) vis.gl contributors
44

55
import { BinaryLineFeature } from '@loaders.gl/schema';
6+
import { NumericProps } from './schema/spatialjson-utils';
67

78

89

@@ -175,7 +176,7 @@ export function rebuildGeometry(
175176
validPoints: TrajectoryPoint[],
176177
pathIndices: number[],
177178
): ProcessedGeometry {
178-
const {positions, properties} = originalGeometry;
179+
const {positions, numericProps, properties} = originalGeometry;
179180
const newN = validPoints.length;
180181

181182
if (newN === 0) {
@@ -195,25 +196,43 @@ export function rebuildGeometry(
195196
}
196197

197198
const size = 2;
198-
199+
const newNumericProps: NumericProps = {};
200+
const numericPropKeys = Object.keys(numericProps);
201+
for (const prop of numericPropKeys) {
202+
const size = numericProps[prop].size || 1;
203+
newNumericProps[prop] = {value: new Float32Array(newN), size};
204+
}
205+
199206
// Properties are per-line.
200207
const newFeatureIds = new Uint16Array(newN);
208+
let dst = 0;
201209
for (let i = 0; i < pathIndices.length - 1; i++) {
202210
const startIndex = pathIndices[i];
203211
const endIndex = pathIndices[i + 1];
204-
const point = validPoints[startIndex];
212+
const point = validPoints[startIndex]; // is this correct??? validPoints[i]????
213+
const srcIndex = point.index;
214+
const numVertices = endIndex - startIndex;
215+
205216
const trajectoryId = point.trajectoryId;
206-
newProperties[i] = {trajectoryId, ...properties[point.index]}
217+
newProperties[i] = {trajectoryId, ...properties[srcIndex]}
207218

208219
for (let j = startIndex; j < endIndex; j++) {
209220
newFeatureIds[j] = i; // Each vertex is marked with featureId of the line
210221
}
222+
223+
for (const prop of numericPropKeys) {
224+
const {value: originalValue} = originalGeometry.numericProps[prop];
225+
const {value, size} = newNumericProps[prop];
226+
const dataSlice = originalValue.subarray(srcIndex * size, (srcIndex + numVertices) * size)
227+
value.set(dataSlice, dst * size);
228+
}
229+
dst += numVertices;
211230
}
212231

213232
return {
214233
positions: {value: newPositions, size},
215234
properties: newProperties,
216-
numericProps: {}, // TODO add numericProps
235+
numericProps: newNumericProps,
217236
featureIds: {value: newFeatureIds, size: 1},
218237
globalFeatureIds: {value: newFeatureIds, size: 1},
219238
pathIndices: {value: new Uint16Array(pathIndices), size: 1},

test/apps/carto-trajectories/app.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,14 @@ export default function App() {
318318
}),
319319

320320
// Color each vertex, will be invoked once per vertex
321-
getFillColor: (d: any, info: any) => {
321+
_getFillColor: (d: any, info: any) => {
322322
return [255 * Math.random(), 255 * Math.random(), 255 * Math.random(), 255];
323323
},
324-
325-
324+
getFillColor: colorContinuous({
325+
attr: 'ts',
326+
domain: [0, 200, 300, 500, 700],
327+
colors: palette
328+
}),
326329

327330
// Alternative API, dedicated accessor: Color each vertex by speed
328331
_getVertexColor: colorContinuous({

0 commit comments

Comments
 (0)