Skip to content

Commit 3fd2bb5

Browse files
authored
fix: HeatmapLayer working with binary data (#9787)
1 parent c3b8e28 commit 3fd2bb5

File tree

3 files changed

+80
-14
lines changed

3 files changed

+80
-14
lines changed

modules/aggregation-layers/src/heatmap-layer/heatmap-layer.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ export default class HeatmapLayer<
236236
// Update weight map immediately
237237
clearTimeout(this.state.updateTimer);
238238
this.setState({isWeightMapDirty: true});
239+
240+
if (changeFlags.dataChanged) {
241+
// Recreate weights transform if data changed, as buffer layout may have changed,
242+
// happens when binary attibutes passed.
243+
const weightsTransformShaders = this.getShaders({vs: weightsVs, fs: weightsFs});
244+
this._createWeightsTransform(weightsTransformShaders);
245+
}
239246
} else if (changeFlags.viewportZoomChanged) {
240247
// Update weight map when zoom stops
241248
this._debouncedUpdateWeightmap();
@@ -558,18 +565,13 @@ export default class HeatmapLayer<
558565
let {colorTexture} = this.state;
559566
const colors = colorRangeToFlatArray(colorRange, false, Uint8Array as any);
560567

561-
if (colorTexture && colorTexture?.width === colorRange.length) {
562-
// TODO(v9): Unclear whether `setSubImageData` is a public API, or what to use if not.
563-
(colorTexture as any).setTexture2DData({data: colors});
564-
} else {
565-
colorTexture?.destroy();
566-
colorTexture = this.context.device.createTexture({
567-
...TEXTURE_PROPS,
568-
data: colors,
569-
width: colorRange.length,
570-
height: 1
571-
});
572-
}
568+
colorTexture?.destroy();
569+
colorTexture = this.context.device.createTexture({
570+
...TEXTURE_PROPS,
571+
data: colors,
572+
width: colorRange.length,
573+
height: 1
574+
});
573575
this.setState({colorTexture});
574576
}
575577

test/modules/aggregation-layers/heatmap-layer/heatmap-layer.spec.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ test('HeatmapLayer', t => {
6060
t.end();
6161
});
6262

63-
test('HeatmapLayer#updates', t => {
63+
test.skip('HeatmapLayer#updates', t => {
6464
testLayer({
6565
Layer: HeatmapLayer,
6666
onError: t.notOk,
@@ -188,3 +188,67 @@ test('HeatmapLayer#updates', t => {
188188

189189
t.end();
190190
});
191+
192+
test('HeatmapLayer#binaryData', t => {
193+
const pointCount = 2;
194+
const positions = new Float32Array(pointCount * 2);
195+
const weights = new Float32Array(pointCount);
196+
197+
// Generate test data
198+
positions[0] = -122.4;
199+
positions[1] = 37.8; // San Francisco
200+
positions[2] = -122.3;
201+
positions[3] = 37.7; // Nearby point
202+
203+
weights[0] = 100;
204+
weights[1] = 50;
205+
206+
const binaryData = {
207+
length: pointCount,
208+
attributes: {
209+
getPosition: {
210+
value: positions,
211+
size: 2
212+
},
213+
getWeight: {
214+
value: weights,
215+
size: 1
216+
}
217+
}
218+
};
219+
220+
testLayer({
221+
Layer: HeatmapLayer,
222+
onError: t.notOk,
223+
viewport: viewport0,
224+
testCases: [
225+
{
226+
props: {
227+
data: binaryData,
228+
radiusPixels: 30
229+
},
230+
onAfterUpdate({layer, subLayer}) {
231+
t.ok(layer, 'HeatmapLayer should render with binary data');
232+
t.ok(subLayer instanceof TriangleLayer, 'Should create TriangleLayer sublayer');
233+
t.equal(
234+
layer.getNumInstances(),
235+
pointCount,
236+
'Should correctly count binary data instances'
237+
);
238+
239+
// Verify weightsTransform was created properly
240+
t.ok(layer.state.weightsTransform, 'Should have weightsTransform');
241+
t.ok(layer.state.weightsTexture, 'Should have weightsTexture');
242+
243+
const positionAttribute = layer.state.weightsTransform.model.bufferLayout.find(
244+
a => a.name === 'positions'
245+
).attributes[0];
246+
t.ok(positionAttribute, 'Should have position attribute');
247+
t.equal(positionAttribute.format, 'float32x2', 'bufferLayout should match binary data');
248+
}
249+
}
250+
]
251+
});
252+
253+
t.end();
254+
});

test/modules/aggregation-layers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import './aggregation-layer.spec';
66
import './contour-layer/contour-layer.spec';
77
import './contour-layer/marching-squares.spec';
88
import './grid-layer.spec';
9-
// import './heatmap-layer/heatmap-layer.spec';
9+
import './heatmap-layer/heatmap-layer.spec';
1010
import './heatmap-layer/heatmap-layer-utils.spec';
1111
import './hexagon-layer.spec';
1212
import './hexbin.spec';

0 commit comments

Comments
 (0)