Skip to content

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

[Bug] fix cluster point count, cluster layer failed to render on export image (#855)

Shan He7f7971727129
diff --git a/src/deckgl-layers/cluster-layer/cluster-layer.js b/src/deckgl-layers/cluster-layer/cluster-layer.js
index fdca93aba..c93a020f7 100644
--- a/src/deckgl-layers/cluster-layer/cluster-layer.js
+++ b/src/deckgl-layers/cluster-layer/cluster-layer.js
@@ -104,7 +104,7 @@ export default class ClusterLayer extends CompositeLayer {
needsReclusterPoints(oldProps, props) {
return (
- Math.round(oldProps.zoom) !== Math.round(props.zoom)
+ Math.round(oldProps.mapState.zoom) !== Math.round(props.mapState.zoom)
);
}
@@ -125,20 +125,16 @@ export default class ClusterLayer extends CompositeLayer {
getClusters() {
const {geoJSON} = this.state;
- const {clusterRadius} = this.props;
- const {
- viewport,
- viewport: {longitude, latitude, height, width}
- } = this.context;
-
+ const {clusterRadius, mapState} = this.props;
+ const {longitude, latitude, zoom, width, height} = mapState;
// zoom needs to be an integer for the different map utils. Also helps with cache key.
- const zoom = Math.round(viewport.zoom);
- const bbox = geoViewport.bounds([longitude, latitude], zoom, [
+ const zoomRound = Math.round(zoom);
+ const bbox = geoViewport.bounds([longitude, latitude], zoomRound, [
width,
height
]);
- const clusters = clustersAtZoom({bbox, clusterRadius, geoJSON, zoom});
+ const clusters = clustersAtZoom({bbox, clusterRadius, geoJSON, zoom: zoomRound});
this.setState({clusters});
}
diff --git a/src/deckgl-layers/layer-utils/cluster-utils.js b/src/deckgl-layers/layer-utils/cluster-utils.js
index 1cd4292b7..6649c56fa 100644
--- a/src/deckgl-layers/layer-utils/cluster-utils.js
+++ b/src/deckgl-layers/layer-utils/cluster-utils.js
@@ -45,15 +45,9 @@ const getClusterer = memoize(({clusterRadius, geoJSON}) => {
maxZoom: 20,
radius: clusterRadius,
reduce: (accumulated, props) => {
- if (props.points) {
- // avoid using spread to prevent max call stack exceeded error
- props.points.forEach(p => {
- accumulated.points.push(p);
- });
- } else {
- accumulated.points.push(props);
- }
- }
+ accumulated.points = [...accumulated.points, ...props.points]
+ },
+ map: props => ({points: [props.data]})
}).load(geoJSON);
}, clusterResolver);
diff --git a/src/layers/cluster-layer/cluster-layer.js b/src/layers/cluster-layer/cluster-layer.js
index 72ff420c9..e93aec6ae 100644
--- a/src/layers/cluster-layer/cluster-layer.js
+++ b/src/layers/cluster-layer/cluster-layer.js
@@ -87,8 +87,7 @@ export default class ClusterLayer extends AggregationLayer {
autoHighlight: true,
highlightColor: this.config.highlightColor,
opacity: visConfig.opacity,
- zoom: mapState.zoom,
-
+ mapState,
// parameters
parameters: {depthTest: mapState.dragRotate},
diff --git a/src/layers/layer-factory.js b/src/layers/layer-factory.js
index f3caf69e8..795a9f80e 100644
--- a/src/layers/layer-factory.js
+++ b/src/layers/layer-factory.js
@@ -132,7 +132,7 @@ export const LAYER_VIS_CONFIGS = {
},
clusterRadius: {
type: 'number',
- label: 'Cluster Size (m)',
+ label: 'Cluster Radius in pixels',
defaultValue: 40,
isRanged: false,
range: [1, 500],
@@ -142,7 +142,7 @@ export const LAYER_VIS_CONFIGS = {
},
clusterRadiusRange: {
type: 'number',
- label: 'Radius Range (m)',
+ label: 'Radius Range in pixels',
defaultValue: [1, 40],
isRanged: true,
range: [1, 150],
diff --git a/test/helpers/layer-utils.js b/test/helpers/layer-utils.js
index fefdfeb4d..79e4ac51a 100644
--- a/test/helpers/layer-utils.js
+++ b/test/helpers/layer-utils.js
@@ -108,26 +108,31 @@ export function testFormatLayerDataCases(t, LayerClass, testCases) {
export function testRenderLayerCases(t, LayerClass, testCases) {
testCases.forEach(tc => {
const layer = testCreateLayer(t, LayerClass, tc.props);
+ let result;
if (layer) {
- const result = testFormatLayerData(t, layer, tc.data);
-
- if (result) {
- let deckLayers;
-
- t.doesNotThrow(() => {
- deckLayers = layer.renderLayer({
- data: result,
- idx: 0,
- layerInteraction: {},
- mapState: INITIAL_MAP_STATE,
- interactionConfig: INITIAL_VIS_STATE.interactionConfig,
- ...(tc.renderArgs || {})
- });
- }, `${layer.type}.renderLayer should not fail`);
-
- if (deckLayers) {
- testInitializeDeckLayer(t, layer.type, deckLayers);
+ result = testFormatLayerData(t, layer, tc.data);
+ }
+
+ if (result) {
+ let deckLayers;
+
+ t.doesNotThrow(() => {
+ deckLayers = layer.renderLayer({
+ data: result,
+ idx: 0,
+ layerInteraction: {},
+ mapState: INITIAL_MAP_STATE,
+ interactionConfig: INITIAL_VIS_STATE.interactionConfig,
+ ...(tc.renderArgs || {})
+ });
+ }, `${layer.type}.renderLayer should not fail`);
+
+ if (deckLayers) {
+ const initialDeckLayers = testInitializeDeckLayer(t, layer.type, deckLayers);
+
+ if (tc.assert) {
+ tc.assert(initialDeckLayers, layer, result);
}
}
}
@@ -152,7 +157,7 @@ export function testInitializeDeckLayer(t, layerType, layers) {
);
spy.restore();
- return null;
+ return layerManager.layers;
}
/**
diff --git a/test/node/layer-tests/cluster-layer-specs.js b/test/node/layer-tests/cluster-layer-specs.js
new file mode 100644
index 000000000..434dacba9
--- /dev/null
+++ b/test/node/layer-tests/cluster-layer-specs.js
@@ -0,0 +1,183 @@
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import test from 'tape';
+import {
+ testCreateCases,
+ testFormatLayerDataCases,
+ testRenderLayerCases
+} from 'test/helpers/layer-utils';
+import csvData from 'test/fixtures/test-csv-data';
+import {KeplerGlLayers} from 'layers';
+const {ClusterLayer} = KeplerGlLayers;
+
+import {processCsvData} from 'processors/data-processor';
+
+test('#ClusterLayer -> constructor', t => {
+ const TEST_CASES = {
+ CREATE: [
+ {
+ props: {
+ dataId: 'smoothie',
+ isVisible: true,
+ label: 'test cluster layer'
+ },
+ test: layer => {
+ t.ok(
+ layer.config.dataId === 'smoothie',
+ 'ClusterLayer dataId should be correct'
+ );
+ t.ok(layer.type === 'cluster', 'type should be cluster');
+ t.ok(layer.isAggregated === true, 'ClusterLayer is aggregated');
+ }
+ }
+ ]
+ };
+
+ testCreateCases(t, ClusterLayer, TEST_CASES.CREATE);
+ t.end();
+});
+
+test('#ClusterLayer -> formatLayerData', t => {
+ const {rows} = processCsvData(csvData);
+
+ const filteredIndex = [0, 2, 4];
+ const data = [rows[0], rows[2], rows[4]];
+
+ const expectedLayerMeta = {
+ bounds: [31.2148748, 29.9870074, 31.2590542, 30.0614122]
+ };
+
+ const TEST_CASES = [
+ {
+ props: {
+ dataId: '0dj3h',
+ label: 'gps point',
+ columns: {
+ lat: {
+ value: 'gps_data.lat',
+ fieldIdx: 1
+ },
+ lng: {
+ value: 'gps_data.lng',
+ fieldIdx: 2
+ }
+ }
+ },
+ data: [data, rows, filteredIndex, undefined],
+ test: result => {
+ const {layerData, layer} = result;
+ const expectedLayerData = {
+ data: [rows[0], rows[2], rows[4]],
+ getPosition: () => {}
+ };
+
+ t.deepEqual(
+ Object.keys(layerData).sort(),
+ Object.keys(expectedLayerData).sort(),
+ 'layerData should have 6 keys'
+ );
+ t.deepEqual(
+ layerData.data,
+ expectedLayerData.data,
+ 'should format correct cluster layerData'
+ );
+ t.ok(
+ typeof layerData.getPosition === 'function',
+ 'should have getPosition accessor as function'
+ );
+ t.deepEqual(
+ layerData.getPosition(layerData.data[0]),
+ [31.2590542, 29.9900937],
+ 'getPosition should return correct lat lng'
+ );
+ t.deepEqual(
+ layer.meta,
+ expectedLayerMeta,
+ 'should format correct cluster layer meta'
+ );
+ }
+ }
+ ];
+
+ testFormatLayerDataCases(t, ClusterLayer, TEST_CASES);
+ t.end();
+});
+
+test('#PointLayer -> renderLayer', t => {
+ const {rows} = processCsvData(csvData);
+
+ const filteredIndex = rows.map((d, i) => i);
+ const data = rows;
+
+ const TEST_CASES = [
+ {
+ props: {
+ dataId: '0dj3h',
+ label: 'gps point',
+ id: 'test',
+ columns: {
+ lat: {
+ value: 'gps_data.lat',
+ fieldIdx: 1
+ },
+ lng: {
+ value: 'gps_data.lng',
+ fieldIdx: 2
+ }
+ }
+ },
+ renderArgs: {
+ mapState: {
+ height: 914,
+ latitude: 30.05057028781603,
+ longitude: 31.235150464409074,
+ width: 1136,
+ zoom: 11.447706282002736
+ },
+ layerCallbacks: {
+ onSetLayerDomain: domain => {}
+ }
+ },
+ data: [data, rows, filteredIndex, undefined],
+ assert: (deckLayers, layer, result) => {
+ t.equal(deckLayers.length, 2, 'should render 2 layers');
+
+ t.deepEqual(
+ deckLayers.map(l => l.id),
+ ['test', 'test-cluster'],
+ 'should render 1 cluster, 1 point layer'
+ );
+
+ const clusterLayer = deckLayers[0];
+ const {clusters} = clusterLayer.state;
+
+ t.equal(clusters.length, 7, 'should create 7 clusters');
+ clusters.forEach(cluster => {
+ t.ok(
+ cluster.properties.point_count === cluster.properties.points.length,
+ 'point count should be correct'
+ );
+ });
+ }
+ }
+ ];
+
+ testRenderLayerCases(t, ClusterLayer, TEST_CASES);
+ t.end();
+});
diff --git a/test/node/layer-tests/index.js b/test/node/layer-tests/index.js
index fae0a55c4..36966d079 100644
--- a/test/node/layer-tests/index.js
+++ b/test/node/layer-tests/index.js
@@ -25,3 +25,5 @@ import './point-layer-specs';
import './hexagon-layer-specs';
import './heatmap-layer-specs';
import './scenegraph-layer-specs';
+import './cluster-layer-specs';
+