Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[Bug] fix hexagon layer hover crash (#964)
Shan He212849ddbbb4
diff --git a/src/layers/aggregation-layer.js b/src/layers/aggregation-layer.jsindex 05661891c..e1f2c62f9 100644--- a/src/layers/aggregation-layer.js+++ b/src/layers/aggregation-layer.js@@ -33,7 +33,10 @@ export const pointPosResolver = ({lat, lng}) => `${lat.fieldIdx}-${lng.fieldIdx}export const getValueAggrFunc = (field, aggregation) => {return points => {return field- ? aggregate(points.map(p => p.data[field.tableFieldIndex - 1]), aggregation)+ ? aggregate(+ points.map(p => p.data[field.tableFieldIndex - 1]),+ aggregation+ ): points.length;};};@@ -273,7 +276,7 @@ export default class AggregationLayer extends Layer {}getDefaultAggregationLayerProp(opts) {- const {gpuFilter, mapState, layerCallbacks} = opts;+ const {gpuFilter, mapState, layerCallbacks = {}} = opts;const {visConfig} = this.config;const eleZoomFactor = this.getElevationZoomFactor(mapState);diff --git a/src/layers/hexagon-layer/hexagon-layer.js b/src/layers/hexagon-layer/hexagon-layer.jsindex 38a200db3..3e3dd5d79 100644--- a/src/layers/hexagon-layer/hexagon-layer.js+++ b/src/layers/hexagon-layer/hexagon-layer.js@@ -80,7 +80,9 @@ export default class HexagonLayer extends AggregationLayer {new GeoJsonLayer({...this.getDefaultHoverLayerProps(),wrapLongitude: false,- data: [hexagonToPolygonGeo(objectHovered, {}, radius * visConfig.coverage, mapState)],+ data: [+ hexagonToPolygonGeo(objectHovered, {}, radius * visConfig.coverage, mapState)+ ].filter(d => d),getLineColor: this.config.highlightColor,lineWidthScale: clamp([1, 100], radius * 0.1 * zoomFactor)})diff --git a/src/layers/hexagon-layer/hexagon-utils.js b/src/layers/hexagon-layer/hexagon-utils.jsindex 38aea5634..1f171ad30 100644--- a/src/layers/hexagon-layer/hexagon-utils.js+++ b/src/layers/hexagon-layer/hexagon-utils.js@@ -19,13 +19,23 @@// THE SOFTWARE.import {WebMercatorViewport} from '@deck.gl/core';+import Console from 'global/console';export function hexagonToPolygonGeo({object}, properties, radius, mapState) {const viewport = new WebMercatorViewport(mapState);+ if (!Array.isArray(object.position)) {+ return null;+ }const screenCenter = viewport.projectFlat(object.position);- const {pixelsPerMeter} = viewport.getDistanceScales();- const pixRadius = radius * pixelsPerMeter[0];+ const {unitsPerMeter} = viewport.getDistanceScales(object.position);++ if (!Array.isArray(unitsPerMeter)) {+ Console.warn(`unitsPerMeter is undefined`);+ return null;+ }++ const pixRadius = radius * unitsPerMeter[0];const coordinates = [];diff --git a/test/browser/layer-tests/heatmap-layer-specs.js b/test/browser/layer-tests/heatmap-layer-specs.jsindex acde324cb..a8b820fd4 100644--- a/test/browser/layer-tests/heatmap-layer-specs.js+++ b/test/browser/layer-tests/heatmap-layer-specs.js@@ -47,25 +47,12 @@ test('#HeatmapLayer -> contructor', t => {},test: layer => {// test constructor- t.equal(- layer.config.visConfig.radius,- 20,- 'Heatmap default radius should be 20'- );- t.ok(- layer.config.dataId === 'taro',- 'heatmaplayer dataId should be correct'- );+ t.equal(layer.config.visConfig.radius, 20, 'Heatmap default radius should be 20');+ t.ok(layer.config.dataId === 'taro', 'heatmaplayer dataId should be correct');t.ok(layer.type === 'heatmap', 'type should be heatmap');t.ok(layer.isAggregated === true, 'heatmaplayer is aggregated');- t.ok(- layer.config.label === 'test heatmap layer',- 'label should be correct'- );- t.ok(- Object.keys(layer.columnPairs).length,- 'should have columnPairs'- );+ t.ok(layer.config.label === 'test heatmap layer', 'label should be correct');+ t.ok(Object.keys(layer.columnPairs).length, 'should have columnPairs');}}]@@ -84,21 +71,9 @@ test('#Heatmaplayer -> formatLayerData -> w/ GpuFilter', t => {source: `${dataId}-1-2`,layout: {visibility: 'visible'},maxzoom: 18,- filter: [- 'all',- ['>=', 'gpu:utc_timestamp', 39000],- ['<=', 'gpu:utc_timestamp', 552000]- ],+ filter: ['all', ['>=', 'gpu:utc_timestamp', 39000], ['<=', 'gpu:utc_timestamp', 552000]],paint: {- 'heatmap-weight': [- 'interpolate',- ['linear'],- ['get', 'id'],- 1,- 0,- 345,- 1- ],+ 'heatmap-weight': ['interpolate', ['linear'], ['get', 'id'], 1, 0, 345, 1],'heatmap-intensity': ['interpolate', ['linear'], ['zoom'], 0, 1, 18, 3],'heatmap-color': ['interpolate',@@ -211,11 +186,7 @@ test('#Heatmaplayer -> formatLayerData -> w/ GpuFilter', t => {);// test layer.meta- t.deepEqual(- layer.meta,- pointLayerMeta,- 'should format correct heatmap layer.meta'- );+ t.deepEqual(layer.meta, pointLayerMeta, 'should format correct heatmap layer.meta');}}];@@ -239,15 +210,7 @@ test('#Heatmaplayer -> formatLayerData -> w/o GpuFilter', t => {maxzoom: 18,paint: {'heatmap-weight': 1,- 'heatmap-intensity': [- 'interpolate',- ['linear'],- ['zoom'],- 0,- 1,- MAX_ZOOM_LEVEL,- 3- ],+ 'heatmap-intensity': ['interpolate', ['linear'], ['zoom'], 0, 1, MAX_ZOOM_LEVEL, 3],'heatmap-color': ['interpolate',['linear'],@@ -261,15 +224,7 @@ test('#Heatmaplayer -> formatLayerData -> w/o GpuFilter', t => {0.75,'rgb(3,3,3)'],- 'heatmap-radius': [- 'interpolate',- ['linear'],- ['zoom'],- 0,- 2,- MAX_ZOOM_LEVEL,- 100- ],+ 'heatmap-radius': ['interpolate', ['linear'], ['zoom'], 0, 2, MAX_ZOOM_LEVEL, 100],'heatmap-opacity': 0.2}};@@ -345,11 +300,7 @@ test('#Heatmaplayer -> formatLayerData -> w/o GpuFilter', t => {);// test layer.meta- t.deepEqual(- layer.meta,- expectedLayerMeta,- 'should format correct heatmap layer.meta'- );+ t.deepEqual(layer.meta, expectedLayerMeta, 'should format correct heatmap layer.meta');}}];diff --git a/test/browser/layer-tests/hexagon-layer-specs.js b/test/browser/layer-tests/hexagon-layer-specs.jsindex cc1854204..91a9ed666 100644--- a/test/browser/layer-tests/hexagon-layer-specs.js+++ b/test/browser/layer-tests/hexagon-layer-specs.js@@ -48,20 +48,11 @@ test('#HexagonLayer -> constructor', t => {label: 'test hexagon layer'},test: layer => {- t.ok(- layer.config.dataId === 'blue',- 'HexagonLayer dataId should be correct'- );+ t.ok(layer.config.dataId === 'blue', 'HexagonLayer dataId should be correct');t.ok(layer.type === 'hexagon', 'type should be hexagon');t.ok(layer.isAggregated === true, 'HexagonLayer is aggregated');- t.ok(- layer.config.label === 'test hexagon layer',- 'label should be correct'- );- t.ok(- Object.keys(layer.columnPairs).length,- 'should have columnPairs'- );+ t.ok(layer.config.label === 'test hexagon layer', 'label should be correct');+ t.ok(Object.keys(layer.columnPairs).length, 'should have columnPairs');}}]@@ -146,11 +137,7 @@ test('#HexagonLayer -> formatLayerData', t => {'_filterData should filter data correctly');// test layer.meta- t.deepEqual(- layer.meta,- pointLayerMeta,- 'should format correct hexagon layer meta'- );+ t.deepEqual(layer.meta, pointLayerMeta, 'should format correct hexagon layer meta');}},{@@ -340,11 +327,7 @@ test('#HexagonLayer -> renderLayer', t => {const expectedProps = {coverage: layer.config.visConfig.coverage,radius: layer.config.visConfig.worldUnitSize * 1000,- colorRange: [- [8, 8, 8],- [9, 9, 9],- [7, 7, 7]- ],+ colorRange: [[8, 8, 8], [9, 9, 9], [7, 7, 7]],colorScaleType: layer.config.colorScale,elevationScaleType: layer.config.sizeScale,elevationScale: layer.config.visConfig.elevationScale,@@ -352,14 +335,8 @@ test('#HexagonLayer -> renderLayer', t => {lowerPercentile: layer.config.visConfig.percentile[0]};- const expectedColorBins = [- {i: 2, value: 1, counts: 1},- {i: 1, value: 2, counts: 2}- ];- const expectedElevationBins = [- {i: 2, value: 1, counts: 1},- {i: 1, value: 2, counts: 2}- ];+ const expectedColorBins = [{i: 2, value: 1, counts: 1}, {i: 1, value: 2, counts: 2}];+ const expectedElevationBins = [{i: 2, value: 1, counts: 1}, {i: 1, value: 2, counts: 2}];t.deepEqual(hexCellLayer.props.data,@@ -372,13 +349,9 @@ test('#HexagonLayer -> renderLayer', t => {expectedHexCellData[i],'should pass correct data to hexagon cell layer');- })+ });Object.keys(expectedProps).forEach(key => {- t.deepEqual(- props[key],- expectedProps[key],- `should have correct props.${key}`- );+ t.deepEqual(props[key], expectedProps[key], `should have correct props.${key}`);});t.deepEqual(@@ -405,3 +378,110 @@ test('#HexagonLayer -> renderLayer', t => {testRenderLayerCases(t, HexagonLayer, TEST_CASES);t.end();});++function creatLayerObjectHovered({layerId, data, object}) {+ return {+ color: [],+ layer: {+ props: {id: layerId}+ },+ index: data.index,+ picked: true,+ // mock+ x: 200,+ y: 200,+ pixel: [200, 200],+ coordinate: [-117.986, 34.173],+ lngLat: [-117.986, 34.173],+ devicePixel: [1449, 759],+ pixelRatio: 2,+ object,+ handled: false+ };+}++test('#HexagonLayer -> renderHover', t => {+ const filteredIndex = [0, 1, 2, 4, 5, 7];+ const {allData} = preparedDataset;+ const testObjectHovered = creatLayerObjectHovered({+ layerId: 'test_layer_1',+ data: allData[0],+ object: {+ colorValue: 1,+ elevationValue: 1,+ position: [-122.56068191457787, 37.71853775731428],+ index: 0,+ points: [{}, {}]+ }+ });++ const TEST_CASES = [+ {+ name: 'Hexagon gps point.1',+ layer: {+ type: 'hexagon',+ id: 'test_layer_1',+ config: {+ dataId,+ label: 'some geometry file',+ columns,+ color: [1, 2, 3],+ visConfig: {+ worldUnitSize: 20,+ colorRange: {+ colors: ['#080808', '#090909', '#070707']+ }+ }+ }+ },+ datasets: {+ [dataId]: {+ ...preparedDataset,+ filteredIndex+ }+ },+ renderArgs: {+ objectHovered: testObjectHovered+ },+ assert: (deckLayers, layer) => {+ t.deepEqual(+ deckLayers.map(l => l.id),+ [+ 'test_layer_1',+ 'test_layer_1-hexagon-cell',+ 'test_layer_1-hovered',+ 'test_layer_1-hovered-line-strings'+ ],+ 'Should create 4 deck.gl layers'+ );+ const expectedHoverData = [+ {+ geometry: {+ coordinates: [+ [-122.36376320555154, 37.80841570626016],+ [-122.56068191457787, 37.898184393157855],+ [-122.75760062360423, 37.80841570626016],+ [-122.75760062360423, 37.628550634764665],+ [-122.56068191457787, 37.538454428239675],+ [-122.36376320555154, 37.628550634764665],+ [-122.36376320555154, 37.80841570626016]+ ],+ type: 'LineString'+ },+ properties: {}+ }+ ];++ const hoverLayer = deckLayers[2];+ t.deepEqual(+ hoverLayer.props.data,+ expectedHoverData,+ 'should send correct hover layer data'+ );+ }+ }+ ];++ testRenderLayerCases(t, HexagonLayer, TEST_CASES);+ t.end();+});diff --git a/test/node/utils/export-utils-test.js b/test/node/utils/export-utils-test.jsindex 0e3d23772..586ead9f2 100644--- a/test/node/utils/export-utils-test.js+++ b/test/node/utils/export-utils-test.js@@ -1,4 +1,24 @@-import test from 'tape';+// Copyright (c) 2020 Uber Technologies, Inc.+//+// 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 {registerEntry} from 'actions/identity-actions';import keplerGlReducer from 'reducers';import {getMapJSON, exportToJsonString} from 'utils/export-utils';@@ -7,14 +27,10 @@ test('exportUtils -> ExportJson', t => {const state = keplerGlReducer(undefined, registerEntry({id: 'test'})).test;const body = exportToJsonString(getMapJSON(state));- t.equal(- typeof body,- 'string',- 'Should validate the type of body to be a string'- );+ t.equal(typeof body, 'string', 'Should validate the type of body to be a string');t.doesNotThrow(() => {- JSON.parse(body)+ JSON.parse(body);}, 'Should not throw when trying to parse body');t.end();