Skip to content

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

[Feat] Auto detect h3 layer from h3 field data (#53) (#1218)

Shan Heb91785ec6bd2
diff --git a/src/layers/h3-hexagon-layer/h3-hexagon-layer.js b/src/layers/h3-hexagon-layer/h3-hexagon-layer.js
index b59c11aa3..a6d9875a2 100644
--- a/src/layers/h3-hexagon-layer/h3-hexagon-layer.js
+++ b/src/layers/h3-hexagon-layer/h3-hexagon-layer.js
@@ -22,7 +22,7 @@ import Layer from '../base-layer';
import {GeoJsonLayer} from '@deck.gl/layers';
import {H3HexagonLayer} from '@deck.gl/geo-layers';
import EnhancedColumnLayer from 'deckgl-layers/column-layer/enhanced-column-layer';
-import {getCentroid, idToPolygonGeo, h3IsValid} from './h3-utils';
+import {getCentroid, idToPolygonGeo, h3IsValid, getHexFields} from './h3-utils';
import H3HexagonLayerIcon from './h3-hexagon-layer-icon';
import {CHANNEL_SCALES, HIGHLIGH_COLOR_3D} from 'constants/default-settings';
import {hexToRgb} from 'utils/color-utils';
@@ -91,18 +91,32 @@ export default class HexagonIdLayer extends Layer {
};
}
- static findDefaultLayerProps({fields = []}) {
+ static findDefaultLayerProps({fields = [], allData = []}) {
const foundColumns = this.findDefaultColumnField(HEXAGON_ID_FIELDS, fields);
- if (!foundColumns || !foundColumns.length) {
+ const hexFields = getHexFields(fields, allData);
+ if ((!foundColumns || !foundColumns.length) && !hexFields.length) {
return {props: []};
}
return {
- props: foundColumns.map(columns => ({
- isVisible: true,
- label: 'H3 Hexagon',
- columns
- }))
+ props: (foundColumns || [])
+ .map(columns => ({
+ isVisible: true,
+ label: 'H3 Hexagon',
+ columns
+ }))
+ .concat(
+ (hexFields || []).map(f => ({
+ isVisible: true,
+ label: f.name,
+ columns: {
+ hex_id: {
+ value: f.name,
+ fieldIdx: fields.findIndex(fid => fid.name === f.name)
+ }
+ }
+ }))
+ )
};
}
diff --git a/src/layers/h3-hexagon-layer/h3-utils.js b/src/layers/h3-hexagon-layer/h3-utils.js
index 5bce7b742..faa2c7e75 100644
--- a/src/layers/h3-hexagon-layer/h3-utils.js
+++ b/src/layers/h3-hexagon-layer/h3-utils.js
@@ -19,6 +19,9 @@
// THE SOFTWARE.
import {h3GetResolution, h3IsValid, h3ToGeo, h3ToGeoBoundary, geoToH3} from 'h3-js';
+import {ALL_FIELD_TYPES} from 'constants/default-settings';
+import {notNullorUndefined} from 'utils/data-utils';
+
export {h3GetResolution, h3IsValid};
// get vertices should return [lon, lat]
@@ -170,3 +173,13 @@ function getDistortions(vts, origs) {
return distortions;
}
+
+export const isHexField = (field, fieldIdx, allData) => {
+ if (!field.type === ALL_FIELD_TYPES.string) {
+ return false;
+ }
+ const firstDP = allData.find(d => notNullorUndefined(d[fieldIdx]));
+ return firstDP && h3IsValid(firstDP[fieldIdx]);
+};
+
+export const getHexFields = (fields, allData) => fields.filter((f, i) => isHexField(f, i, allData));
diff --git a/src/utils/index.js b/src/utils/index.js
new file mode 100644
index 000000000..e80d2d6d1
--- /dev/null
+++ b/src/utils/index.js
@@ -0,0 +1,24 @@
+// 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.
+
+export {maybeToDate, roundValToStep} from './data-utils';
+export {updateAllLayerDomainData} from '../reducers/vis-state-updaters';
+export {findPointFieldPairs} from '../utils/dataset-utils';
+export {getHexFields} from '../layers/h3-hexagon-layer/h3-utils';
diff --git a/src/utils/layer-utils.js b/src/utils/layer-utils.js
index 6371ba27a..42c972b1f 100644
--- a/src/utils/layer-utils.js
+++ b/src/utils/layer-utils.js
@@ -47,7 +47,7 @@ export function findDefaultLayer(dataset, layerClasses = {}) {
// go through all layerProps to create layer
return layerProps.map(props => {
const layer = new layerClasses[props.type](props);
- return typeof layer.setInitialLayerConfig === 'function'
+ return typeof layer.setInitialLayerConfig === 'function' && Array.isArray(dataset.allData)
? layer.setInitialLayerConfig(dataset.allData)
: layer;
});