Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

fix: refresh heatmap weights when the scale changes (#3956)

Igor Dykhta8dda107c97da
diff --git a/src/layers/src/heatmap-layer/heatmap-layer.ts b/src/layers/src/heatmap-layer/heatmap-layer.ts
index e3ee23c88..e38c584a2 100644
--- a/src/layers/src/heatmap-layer/heatmap-layer.ts
+++ b/src/layers/src/heatmap-layer/heatmap-layer.ts
@@ -654,7 +654,9 @@ class HeatmapLayer extends Layer {
const updateTriggers = {
getPosition: {columns: this.config.columns, columnMode: this.config.columnMode},
getWeight: {
- weightField: this.config.weightField
+ weightField: this.config.weightField,
+ weightScale: this.config.weightScale,
+ weightDomain: this.config.weightDomain
}
};
diff --git a/test/browser/layer-tests/heatmap-layer-specs.js b/test/browser/layer-tests/heatmap-layer-specs.js
index cb89c408d..0d6fe11bc 100644
--- a/test/browser/layer-tests/heatmap-layer-specs.js
+++ b/test/browser/layer-tests/heatmap-layer-specs.js
@@ -26,6 +26,7 @@ import {
import {LayerManager, MapView} from '@deck.gl/core';
import {device} from '@deck.gl/test-utils';
import {convertGeometryToWKB} from '@loaders.gl/gis';
+import KeplerHeatmapLayer from '../../../src/layers/src/heatmap-layer/deck-heatmap-layer';
const {HeatmapLayer, PointLayer} = KeplerGlLayers;
@@ -34,6 +35,159 @@ const columns = {
lng: 'lng'
};
+// Exercise deck.gl's attribute lifecycle without allocating GPU textures or drawing.
+class AttributeTestHeatmapLayer extends KeplerHeatmapLayer {
+ updatedAttributes = new Set();
+ _setupResources() {}
+ updateShaders() {}
+ _updateHeatmapState() {}
+ updateAttributes(changedAttributes) {
+ super.updateAttributes(changedAttributes);
+ Object.keys(changedAttributes).forEach(name => this.updatedAttributes.add(name));
+ }
+ renderLayers() {
+ return [];
+ }
+}
+
+test('#HeatmapLayer -> config actions invalidate cached weights only when needed', t => {
+ const table = new KeplerTable({info: {id: 'weights'}, color: [1, 2, 3]});
+ table.importData({
+ data: {
+ fields: ['lat', 'lng', 'value', 'other'].map(name => ({name, type: 'real'})),
+ rows: [
+ [37.75, -122.4, 1, 9],
+ [37.76, -122.41, 4, 4],
+ [37.77, -122.42, 9, 1]
+ ]
+ }
+ });
+ const layer = validateLayerWithData(
+ table,
+ {
+ type: 'heatmap',
+ config: {dataId: 'weights', columns, weightField: table.fields[2], weightScale: 'linear'}
+ },
+ LayerClasses
+ );
+ const datasets = {weights: table};
+ layer.updateLayerDomain(datasets);
+ let state = {
+ ...INITIAL_VIS_STATE,
+ datasets,
+ layers: [layer],
+ layerData: [layer.formatLayerData(datasets)]
+ };
+ const viewport = new MapView().makeViewport({
+ width: INITIAL_MAP_STATE.width,
+ height: INITIAL_MAP_STATE.height,
+ viewState: INITIAL_MAP_STATE
+ });
+ const manager = new LayerManager(device, {viewport});
+ const apply = action => {
+ state = visStateReducer(state, action);
+ };
+ const configure = config => apply(VisStateActions.layerConfigChange(state.layers[0], config));
+ const check = (label, expected, weightsChanged, positionsChanged = false) => {
+ const [rendered] = state.layers[0].renderLayer({
+ data: state.layerData[0],
+ gpuFilter: state.datasets.weights.gpuFilter,
+ mapState: INITIAL_MAP_STATE,
+ idx: 0,
+ visible: true,
+ layerCallbacks: {},
+ interactionConfig: INITIAL_VIS_STATE.interactionConfig
+ });
+ const [deckLayer] = testRenderDeckLayer(
+ t,
+ 'heatmap attributes',
+ new AttributeTestHeatmapLayer(rendered.props),
+ {layerManager: manager}
+ );
+ const {weights} = deckLayer.getAttributeManager().getAttributes();
+ const actual = weights.isConstant
+ ? rendered.props.data.map(() => weights.value[0])
+ : Array.from(weights.value.slice(0, rendered.props.data.length));
+ t.equal(actual.length, expected.length, `${label}: row count`);
+ actual.forEach((value, i) =>
+ t.ok(Math.abs(value - expected[i]) < 1e-6, `${label}: cached weight ${i}`)
+ );
+ t.equal(
+ deckLayer.updatedAttributes.has('weights'),
+ weightsChanged,
+ `${label}: weight invalidation`
+ );
+ t.equal(
+ deckLayer.updatedAttributes.has('positions'),
+ positionsChanged,
+ `${label}: position invalidation`
+ );
+ };
+ try {
+ check('linear', [0, 3 / 8, 1], true, true);
+ const originalData = state.layerData[0].data;
+ configure({weightScale: 'sqrt'});
+ t.equal(state.layerData[0].data, originalData, 'scale change reuses the data array');
+ t.equal(
+ state.layerData[0].getWeight(originalData[1]),
+ 0.5,
+ 'configuration action updates the weight accessor'
+ );
+ check('sqrt', [0, 0.5, 1], true);
+ configure({weightScale: 'sqrt'});
+ check('same scale', [0, 0.5, 1], false);
+ apply(VisStateActions.layerVisConfigChange(state.layers[0], {opacity: 0.4}));
+ check('opacity', [0, 0.5, 1], false);
+ configure({weightScale: 'log'});
+ check('log', [0, Math.log(4) / Math.log(9), 1], true);
+ configure({weightScale: 'linear'});
+ check('back to linear', [0, 3 / 8, 1], true);
+ configure({weightDomain: [1, 17]});
+ check('domain', [0, 3 / 16, 0.5], true);
+ apply(
+ VisStateActions.layerVisualChannelConfigChange(
+ state.layers[0],
+ {weightField: table.fields[3]},
+ 'weight'
+ )
+ );
+ check('other field', [1, 3 / 8, 0], true);
+ apply(
+ VisStateActions.layerVisualChannelConfigChange(state.layers[0], {weightField: null}, 'weight')
+ );
+ check('cleared field', [1, 1, 1], true);
+ apply(
+ VisStateActions.layerVisualChannelConfigChange(
+ state.layers[0],
+ {weightField: table.fields[2]},
+ 'weight'
+ )
+ );
+ check('restored field', [0, 3 / 8, 1], true);
+ apply(VisStateActions.addFilter('weights'));
+ apply(VisStateActions.setFilter(0, 'name', 'value'));
+ apply(VisStateActions.setFilter(0, 'value', [4, 9]));
+ const filtered = state.layerData[0];
+ check('filtered data', filtered.data.map(filtered.getWeight), true, true);
+ apply(VisStateActions.removeFilter(0));
+ check('restored data', [0, 3 / 8, 1], true, true);
+ apply(
+ VisStateActions.updateDataset('weights', {
+ fields: ['lat', 'lng', 'value', 'other'].map(name => ({name, type: 'real'})),
+ rows: [
+ [37.75, -122.4, 1, 9],
+ [37.76, -122.41, 5, 4],
+ [37.77, -122.42, 17, 1]
+ ]
+ })
+ );
+ check('replaced data', [0, 0.25, 1], true, true);
+ } finally {
+ manager.finalize();
+ }
+ t.end();
+});
+
test('#HeatmapLayer -> contructor', t => {
const TEST_CASES = {
CREATE: [