Skip to content

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

[Bug] fix heatmap getBounds (#711)

Shan He2aa71e6da7b3
diff --git a/examples/demo-app/src/app.js b/examples/demo-app/src/app.js
index 59797a701..0a325a03d 100644
--- a/examples/demo-app/src/app.js
+++ b/examples/demo-app/src/app.js
@@ -45,7 +45,7 @@ const KeplerGl = require('kepler.gl/components').injectComponents([
// Sample data
/* eslint-disable no-unused-vars */
-import sampleTripData, {testCsvData} from './data/sample-trip-data';
+import sampleTripData, {testCsvData, sampleTripDataConfig} from './data/sample-trip-data';
import sampleGeojson from './data/sample-small-geojson';
import sampleGeojsonPoints from './data/sample-geojson-points';
import sampleGeojsonConfig from './data/sample-geojson-config';
@@ -181,19 +181,7 @@ class App extends Component {
centerMap: true,
readOnly: false
},
- config: {
- visState: {
- filters: [
- {
- id: 'me',
- dataId: 'test_trip_data',
- name: 'tpep_pickup_datetime',
- type: 'timeRange',
- enlarged: true
- }
- ]
- }
- }
+ config: sampleTripDataConfig
})
);
}
diff --git a/examples/demo-app/src/data/sample-trip-data.js b/examples/demo-app/src/data/sample-trip-data.js
index 8f747deb0..fb4010226 100644
--- a/examples/demo-app/src/data/sample-trip-data.js
+++ b/examples/demo-app/src/data/sample-trip-data.js
@@ -3974,3 +3974,57 @@ export default {
]
]
};
+
+export const sampleTripDataConfig = {
+ version: 'v1',
+ config: {
+ visState: {
+ layers: [
+ {
+ type: 'heatmap',
+ config: {
+ dataId: 'test_trip_data',
+ columns: {
+ lat: 'pickup_latitude',
+ lng: 'pickup_longitude'
+ },
+ isVisible: true
+ }
+ },
+ {
+ type: 'point',
+ config: {
+ dataId: 'test_trip_data',
+ columns: {
+ lat: 'pickup_latitude',
+ lng: 'pickup_longitude'
+ },
+ isVisible: true
+ }
+ },
+ {
+ type: 'arc',
+ config: {
+ dataId: 'test_trip_data',
+ columns: {
+ lat0: 'pickup_latitude',
+ lng0: 'pickup_longitude',
+ lat1: 'dropoff_latitude',
+ lng1: 'dropoff_longitude'
+ },
+ isVisible: true
+ }
+ }
+ ],
+ filters: [
+ {
+ id: 'me',
+ dataId: 'test_trip_data',
+ name: 'tpep_pickup_datetime',
+ type: 'timeRange',
+ enlarged: true
+ }
+ ]
+ }
+ }
+};
diff --git a/src/layers/heatmap-layer/heatmap-layer.js b/src/layers/heatmap-layer/heatmap-layer.js
index b25490e0a..75771808f 100644
--- a/src/layers/heatmap-layer/heatmap-layer.js
+++ b/src/layers/heatmap-layer/heatmap-layer.js
@@ -19,14 +19,27 @@
// THE SOFTWARE.
import {createSelector} from 'reselect';
-import {CHANNEL_SCALES, SCALE_FUNC, ALL_FIELD_TYPES} from 'constants/default-settings';
+import memoize from 'lodash.memoize';
+import {
+ CHANNEL_SCALES,
+ SCALE_FUNC,
+ ALL_FIELD_TYPES
+} from 'constants/default-settings';
import {hexToRgb} from 'utils/color-utils';
import {geojsonFromPoints} from '../mapbox-utils';
import MapboxGLLayer from '../mapboxgl-layer';
import HeatmapLayerIcon from './heatmap-layer-icon';
const MAX_ZOOM_LEVEL = 18;
-export const pointColResolver = ({lat, lng, altitude}) =>
+
+export const pointPosAccessor = ({lat, lng}) => d => [
+ // lng
+ d.data[lng.fieldIdx],
+ // lat
+ d.data[lat.fieldIdx]
+];
+
+export const pointColResolver = ({lat, lng}) =>
`${lat.fieldIdx}-${lng.fieldIdx}`;
export const heatmapVisConfigs = {
@@ -47,7 +60,7 @@ export const heatmapVisConfigs = {
* 1, "rgb(178,24,43)"
* ]
*/
-const heatmapDensity = (colorRange) => {
+const heatmapDensity = colorRange => {
const scaleFunction = SCALE_FUNC.quantize;
const colors = ['#000000', ...colorRange.colors];
@@ -62,7 +75,7 @@ const heatmapDensity = (colorRange) => {
...bands,
invert[0], // first value in the range
`rgb(${hexToRgb(level).join(',')})` // color
- ]
+ ];
}, []);
colorDensity[1] = 'rgba(0,0,0,0)';
return colorDensity;
@@ -74,6 +87,7 @@ class HeatmapLayer extends MapboxGLLayer {
constructor(props) {
super(props);
this.registerVisConfig(heatmapVisConfigs);
+ this.getPosition = memoize(pointPosAccessor, pointColResolver);
}
get type() {
@@ -102,17 +116,20 @@ class HeatmapLayer extends MapboxGLLayer {
}
getVisualChannelDescription(channel) {
- return channel === 'color' ? {
- label: 'color',
- measure: 'Density'
- } : {
- label: 'weight',
- measure: this.config.weightField ? this.config.weightField.name : 'Density'
- }
+ return channel === 'color'
+ ? {
+ label: 'color',
+ measure: 'Density'
+ }
+ : {
+ label: 'weight',
+ measure: this.config.weightField
+ ? this.config.weightField.name
+ : 'Density'
+ };
}
getDefaultLayerConfig(props = {}) {
-
// mapbox heatmap layer color is always based on density
// no need to set colorField, colorDomain and colorScale
/* eslint-disable no-unused-vars */
@@ -129,18 +146,15 @@ class HeatmapLayer extends MapboxGLLayer {
}
isSameData = ({allData, filteredIndex, oldLayerData, opt = {}}, config) => {
- return Boolean(oldLayerData && oldLayerData.columns === config.columns &&
- opt.sameData
+ return Boolean(
+ oldLayerData && oldLayerData.columns === config.columns && opt.sameData
);
};
isSameConfig = ({oldLayerData, config}) => {
// columns must use the same filedIdx
// this is a fast way to compare columns object
- const {
- columns,
- weightField
- } = config;
+ const {columns, weightField} = config;
if (!oldLayerData) {
return false;
@@ -154,9 +168,20 @@ class HeatmapLayer extends MapboxGLLayer {
datasetSelector = config => config.dataId;
columnsSelector = config => pointColResolver(config.columns);
visConfigSelector = config => config.visConfig;
- weightFieldSelector = config => config.weightField ? config.weightField.name : null;
+ weightFieldSelector = config =>
+ config.weightField ? config.weightField.name : null;
weightDomainSelector = config => config.weightDomain;
+ getPositionAccessor() {
+ return this.getPosition(this.config.columns);
+ }
+
+ updateLayerMeta(allData) {
+ const getPosition = this.getPositionAccessor();
+ const bounds = this.getPointsBounds(allData, d => getPosition({data: d}));
+ this.updateMeta({bounds});
+ }
+
computeHeatmapConfiguration = createSelector(
this.datasetSelector,
this.columnsSelector,
@@ -165,7 +190,6 @@ class HeatmapLayer extends MapboxGLLayer {
this.weightDomainSelector,
(datasetId, columns, visConfig, weightField, weightDomain) => {
-
return {
type: 'heatmap',
id: this.id,
@@ -175,19 +199,25 @@ class HeatmapLayer extends MapboxGLLayer {
},
maxzoom: MAX_ZOOM_LEVEL,
paint: {
- 'heatmap-weight': weightField ? [
- 'interpolate',
- ['linear'],
- ['get', weightField],
- weightDomain[0], 0,
- weightDomain[1], 1
- ] : 1,
+ 'heatmap-weight': weightField
+ ? [
+ 'interpolate',
+ ['linear'],
+ ['get', weightField],
+ weightDomain[0],
+ 0,
+ weightDomain[1],
+ 1
+ ]
+ : 1,
'heatmap-intensity': [
'interpolate',
['linear'],
['zoom'],
- 0, 1,
- MAX_ZOOM_LEVEL, 3
+ 0,
+ 1,
+ MAX_ZOOM_LEVEL,
+ 3
],
'heatmap-color': [
'interpolate',
@@ -199,8 +229,10 @@ class HeatmapLayer extends MapboxGLLayer {
'interpolate',
['linear'],
['zoom'],
- 0, 2,
- MAX_ZOOM_LEVEL, visConfig.radius // radius
+ 0,
+ 2,
+ MAX_ZOOM_LEVEL,
+ visConfig.radius // radius
],
'heatmap-opacity': visConfig.opacity
}
@@ -221,14 +253,20 @@ class HeatmapLayer extends MapboxGLLayer {
const isSameData = this.isSameData(options, this.config);
const isSameConfig = this.isSameConfig(options);
- const data = !shouldRebuild(isSameData, isSameConfig) ?
- oldLayerData.data :
- geojsonFromPoints(
- allData,
- filteredIndex,
- this.config.columns,
- weightField ? [weightField] : []
- );
+ const getPosition = this.getPositionAccessor();
+
+ if (!oldLayerData || oldLayerData.getPosition !== getPosition) {
+ this.updateLayerMeta(allData, getPosition);
+ }
+
+ const data = !shouldRebuild(isSameData, isSameConfig)
+ ? oldLayerData.data
+ : geojsonFromPoints(
+ allData,
+ filteredIndex,
+ this.config.columns,
+ weightField ? [weightField] : []
+ );
const newConfig = this.computeHeatmapConfiguration(this.config);
newConfig.id = this.id;
@@ -237,7 +275,8 @@ class HeatmapLayer extends MapboxGLLayer {
columns: this.config.columns,
config: newConfig,
data,
- weightField
+ weightField,
+ getPosition
};
}
}
diff --git a/test/node/layer-tests/heatmap-layer-specs.js b/test/node/layer-tests/heatmap-layer-specs.js
index 63649ac32..35b9536d1 100644
--- a/test/node/layer-tests/heatmap-layer-specs.js
+++ b/test/node/layer-tests/heatmap-layer-specs.js
@@ -23,7 +23,7 @@ import {
testCreateCases,
testFormatLayerDataCases
} from 'test/helpers/layer-utils';
-import {processCsvData} from "processors/data-processor";
+import {processCsvData} from 'processors/data-processor';
import csvData from 'test/fixtures/test-csv-data';
import HeatmapLayer from 'layers/heatmap-layer/heatmap-layer';
@@ -43,19 +43,21 @@ test('#HeatmapLayer -> contructor', t => {
layer.config.visConfig.radius,
20,
'Heatmap default radius should be 20'
- )
+ );
}
}
]
};
testCreateCases(t, HeatmapLayer, TEST_CASES.CREATE);
- t.end()
+ t.end();
});
test('#Heatmaplayer -> formatLayerData', t => {
const {rows} = processCsvData(csvData);
- const filteredIndex = Array(rows.length).fill().map((_, index) => index);
+ const filteredIndex = Array(rows.length)
+ .fill()
+ .map((_, index) => index);
const data = rows;
const columns = {
lat: {
@@ -67,6 +69,39 @@ test('#Heatmaplayer -> formatLayerData', t => {
fieldIdx: 2
}
};
+ const expectedConfig = {
+ type: 'heatmap',
+ id: 'assign_from_layer',
+ source: 'heatmap-1-2',
+ layout: {visibility: 'visible'},
+ maxzoom: 18,
+ paint: {
+ 'heatmap-weight': 1,
+ 'heatmap-intensity': ['interpolate', ['linear'], ['zoom'], 0, 1, 18, 3],
+ 'heatmap-color': [
+ 'interpolate',
+ ['linear'],
+ ['heatmap-density'],
+ 0,
+ 'rgba(0,0,0,0)',
+ 0.14285714285714285,
+ 'rgb(90,24,70)',
+ 0.2857142857142857,
+ 'rgb(144,12,63)',
+ 0.42857142857142855,
+ 'rgb(199,0,57)',
+ 0.5714285714285714,
+ 'rgb(227,97,28)',
+ 0.7142857142857143,
+ 'rgb(241,146,14)',
+ 0.8571428571428571,
+ 'rgb(255,195,0)'
+ ],
+ 'heatmap-radius': ['interpolate', ['linear'], ['zoom'], 0, 2, 18, 20],
+ 'heatmap-opacity': 0.8
+ }
+ };
+
const TEST_CASES = [
{
props: {
@@ -77,81 +112,187 @@ test('#Heatmaplayer -> formatLayerData', t => {
},
data: [data, rows, filteredIndex, undefined],
test: result => {
- const {layerData, layer} = result;
+ const {layerData, layer} = result;
+ const expectedLayerMeta = {
+ bounds: [31.2148748, 29.9870074, 31.2590542, 30.0614122]
+ };
const expectedLayerData = {
columns,
- config: {
- type: 'heatmap',
- source: 'heatmap-1-2',
- layout: {
- visibility: 'visible'
- },
- maxzoom: 18,
- paint: {
- 'heatmap-weight': 1,
- 'heatmap-intensity': [
- 'interpolate',
- ['linear'],
- ['zoom'],
- 0, 1,
- 18, 3
- ],
- 'heatmap-color': [
- 'interpolate',
- ['linear'],
- ['heatmap-density']
- ],
- 'heatmap-radius': [
- 'interpolate',
- ['linear'],
- ['zoom'],
- 0, 2,
- 18, 20 // radius
- ],
- 'heatmap-opacity': 0.8
- }
- },
+ config: expectedConfig,
data: {
type: 'FeatureCollection',
features: [
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2590542, 29.9900937] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2461142, 29.9927699] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2312742, 29.9907261] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2175827, 29.9870074] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2154899, 29.9923041] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2149361, 29.9968249] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2164035, 30.0037217] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2179346, 30.0116207] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2179556, 30.0208925] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2178842, 30.0218999] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2179138, 30.0229344] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2179415, 30.0264237] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2181809, 30.0292134] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2193991, 30.034391] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2181803, 30.0352752] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2195902, 30.0395918] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2174421, 30.0497387] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2165983, 30.0538936] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2148748, 30.060911] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2212278, 30.060334] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2288985, 30.0554663] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2187021, 30.0614122] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2191059, 30.0612697] } },
- { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [ 31.2194728, 30.0610977] } }
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2590542, 29.9900937]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2461142, 29.9927699]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2312742, 29.9907261]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2175827, 29.9870074]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2154899, 29.9923041]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2149361, 29.9968249]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2164035, 30.0037217]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2179346, 30.0116207]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2179556, 30.0208925]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2178842, 30.0218999]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2179138, 30.0229344]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2179415, 30.0264237]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2181809, 30.0292134]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2193991, 30.034391]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2181803, 30.0352752]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2195902, 30.0395918]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2174421, 30.0497387]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2165983, 30.0538936]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2148748, 30.060911]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2212278, 30.060334]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2288985, 30.0554663]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2187021, 30.0614122]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2191059, 30.0612697]}
+ },
+ {
+ type: 'Feature',
+ properties: {},
+ geometry: {type: 'Point', coordinates: [31.2194728, 30.0610977]}
+ }
]
},
- weightField: null
+ weightField: null,
+ getPosition: () => {}
};
- t.deepEqual(layerData.data, expectedLayerData.data,
- "should format correct geojson layerData")
+ t.deepEqual(
+ Object.keys(layerData).sort(),
+ Object.keys(expectedLayerData).sort(),
+ 'lheatmap ayerData should have correct keys'
+ );
+
+ // test data
+ t.deepEqual(
+ layerData.data,
+ expectedLayerData.data,
+ 'should format correct heatmap layerData.data'
+ );
+
+ // test columns,
+ t.deepEqual(
+ layerData.columns,
+ expectedLayerData.columns,
+ 'should format correct heatmap layerData.columns'
+ );
+ expectedLayerData.config.id = layer.id;
+ // test config
+ t.deepEqual(
+ layerData.config,
+ expectedLayerData.config,
+ 'should format correct heatmap layerData.config'
+ );
+ // test weightField
+ t.deepEqual(
+ layerData.weightField,
+ expectedLayerData.weightField,
+ 'should format correct heatmap weightField'
+ );
+ // test layer.meta
+ t.deepEqual(
+ layer.meta,
+ expectedLayerMeta,
+ 'should format correct heatmap layer.meta'
+ );
}
}
];
testFormatLayerDataCases(t, HeatmapLayer, TEST_CASES);
- t.end()
+ t.end();
});
// todo: make sure data is created correctly