Skip to content

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

feat: an option to show vector tile features with not set attributes (#3676)

Igor Dykhtaf6b29bdf5995
diff --git a/src/components/src/side-panel/layer-panel/vector-tile-layer-configurator.tsx b/src/components/src/side-panel/layer-panel/vector-tile-layer-configurator.tsx
index a343525a5..5745bd7d2 100644
--- a/src/components/src/side-panel/layer-panel/vector-tile-layer-configurator.tsx
+++ b/src/components/src/side-panel/layer-panel/vector-tile-layer-configurator.tsx
@@ -60,7 +60,11 @@ function VectorTileLayerConfiguratorFactory(
return (
<StyledLayerConfigurator>
{/* Fill Color */}
- <LayerConfigGroup {...visConfiguratorProps} label="layer.fillColor">
+ <LayerConfigGroup
+ {...visConfiguratorProps}
+ label="layer.fillColor"
+ collapsible={Boolean(layer.config.colorField)}
+ >
{layerChannelConfigProps.fields ? (
<ChannelByValueSelector
{...layerChannelConfigProps}
@@ -77,6 +81,9 @@ function VectorTileLayerConfiguratorFactory(
{...visConfiguratorProps}
channel={layer.visualChannels.color}
/>
+ <ConfigGroupCollapsibleContent>
+ <VisConfigSwitch {...visConfiguratorProps} {...layer.visConfigSettings.hideNulls} />
+ </ConfigGroupCollapsibleContent>
</>
) : (
<LayerColorSelector {...layerConfiguratorProps} />
diff --git a/src/constants/src/default-settings.ts b/src/constants/src/default-settings.ts
index 8cf9c89a7..d57c2cfb5 100644
--- a/src/constants/src/default-settings.ts
+++ b/src/constants/src/default-settings.ts
@@ -943,6 +943,8 @@ export const DEFAULT_LAYER_COLOR_PALETTE_STEPS = 6;
export const DEFAULT_TOOLTIP_FIELDS: any[] = [];
export const NO_VALUE_COLOR: RGBAColor = [0, 0, 0, 0];
+/** Visible gray used when a color field has no value and missing features should stay on the map. */
+export const MISSING_VALUE_COLOR: RGBAColor = [128, 128, 128, 255];
export const DEFAULT_PICKING_RADIUS = 3;
diff --git a/src/layers/src/base-layer.ts b/src/layers/src/base-layer.ts
index e334f153a..647dc0a88 100644
--- a/src/layers/src/base-layer.ts
+++ b/src/layers/src/base-layer.ts
@@ -1147,6 +1147,8 @@ class Layer implements KeplerLayer {
);
const getFieldValue = this.accessVSFieldValue(this.config[field], indexKey);
+ const resolveNullValue = () =>
+ typeof nullValue === 'function' ? nullValue(this.config) : nullValue;
if (scaleFunction) {
attributeAccessors[accessor] = scaleFunction.byZoom
@@ -1157,7 +1159,7 @@ class Layer implements KeplerLayer {
scaleFunc,
dataAccessor(dataContainer)(d),
this.config[field],
- nullValue,
+ resolveNullValue(),
getFieldValue
);
})
@@ -1166,7 +1168,7 @@ class Layer implements KeplerLayer {
scaleFunction,
dataAccessor(dataContainer)(d),
this.config[field],
- nullValue,
+ resolveNullValue(),
getFieldValue
);
diff --git a/src/layers/src/vector-tile/vector-tile-layer.ts b/src/layers/src/vector-tile/vector-tile-layer.ts
index 2d2af9c87..1f560842a 100644
--- a/src/layers/src/vector-tile/vector-tile-layer.ts
+++ b/src/layers/src/vector-tile/vector-tile-layer.ts
@@ -21,7 +21,9 @@ import {
CHANNEL_SCALES,
DEFAULT_COLOR_UI,
LAYER_VIS_CONFIGS,
- CULL_MODE
+ CULL_MODE,
+ NO_VALUE_COLOR,
+ MISSING_VALUE_COLOR
} from '@kepler.gl/constants';
import {
getTileUrl,
@@ -40,6 +42,7 @@ import {
BindedLayerCallbacks,
VisConfigRange,
VisConfigNumber,
+ VisConfigBoolean,
DomainStops
} from '@kepler.gl/types';
import {DataContainerInterface} from '@kepler.gl/utils';
@@ -63,11 +66,7 @@ import AbstractTileLayer, {
AbstractTileLayerVisConfigSettings
} from './abstract-tile-layer';
import TileDataset from './common-tile/tile-dataset';
-import {
- isDomainStops,
- isDomainQuantiles,
- isIndexedField
-} from './common-tile/tile-utils';
+import {isDomainStops, isDomainQuantiles, isIndexedField} from './common-tile/tile-utils';
export {getNumVectorTilesBeingLoaded} from './loading-counter';
@@ -164,7 +163,16 @@ export const vectorTileVisConfigs = {
isRanged: true,
range: [0, 1],
step: 0.01
- } as VisConfigRange
+ } as VisConfigRange,
+
+ hideNulls: {
+ type: 'boolean',
+ defaultValue: false,
+ label: 'layer.hideNulls',
+ group: '',
+ property: 'hideNulls',
+ description: 'layer.hideNullsDescription'
+ } as VisConfigBoolean
};
export type VectorTileLayerConfig = Merge<
@@ -190,6 +198,7 @@ export type VectorTileLayerVisConfigSettings = Merge<
{
sizeRange: VisConfigRange;
strokeWidth: VisConfigNumber;
+ hideNulls: VisConfigBoolean;
}
>;
@@ -271,10 +280,18 @@ export default class VectorTileLayer extends AbstractTileLayer<VectorTile, Featu
return [DatasetType.VECTOR_TILE];
}
+ get noneLayerDataAffectingProps() {
+ return [...super.noneLayerDataAffectingProps, 'hideNulls'];
+ }
+
get visualChannels(): Record<string, VisualChannel> {
const visualChannels = super.visualChannels;
return {
...visualChannels,
+ color: {
+ ...visualChannels.color,
+ nullValue: config => (config.visConfig.hideNulls ? NO_VALUE_COLOR : MISSING_VALUE_COLOR)
+ },
strokeColor: {
property: 'strokeColor',
field: 'strokeColorField',
@@ -285,7 +302,7 @@ export default class VectorTileLayer extends AbstractTileLayer<VectorTile, Featu
channelScaleType: CHANNEL_SCALES.color,
accessor: 'getLineColor',
condition: config => config.visConfig.stroked,
- nullValue: visualChannels.color.nullValue,
+ nullValue: NO_VALUE_COLOR,
getAttributeValue: config => config.visConfig.strokeColor || config.color
},
size: {
@@ -371,9 +388,7 @@ export default class VectorTileLayer extends AbstractTileLayer<VectorTile, Featu
if (this.config.visConfig.dynamicColor && visualChannel.key === 'color') {
if (scale === SCALE_TYPES.quantile) {
const current = this.config.colorDomain;
- return Array.isArray(current) && current.length > 2
- ? (current as number[])
- : defaultDomain;
+ return Array.isArray(current) && current.length > 2 ? (current as number[]) : defaultDomain;
}
if (scale === SCALE_TYPES.quantize) {
const current = this.config.colorDomain;
@@ -640,6 +655,7 @@ export default class VectorTileLayer extends AbstractTileLayer<VectorTile, Featu
colorScale: this.config.colorScale,
colorDomain: this.config.colorDomain,
colorRange: visConfig.colorRange,
+ hideNulls: visConfig.hideNulls,
currentTime: isIndexedField(colorField) ? animationConfig.currentTime : null
},
getElevation: {
diff --git a/src/localization/src/translations/ca.ts b/src/localization/src/translations/ca.ts
index 5b7ae0fe1..5bbd99004 100644
--- a/src/localization/src/translations/ca.ts
+++ b/src/localization/src/translations/ca.ts
@@ -102,6 +102,9 @@ export default {
layer: 'Capa',
appearance: 'Aparença',
uniqueIdField: 'Camp ID únic',
+ hideNulls: 'Amaga els valors nuls',
+ hideNullsDescription:
+ "Quan està activat, les entitats sense valor de color de fons s'amaguen. Quan està desactivat, es mostren en gris.",
type: {
point: 'punt',
arc: 'arc',
diff --git a/src/localization/src/translations/cn.ts b/src/localization/src/translations/cn.ts
index c0e5583c0..100f1cd7f 100644
--- a/src/localization/src/translations/cn.ts
+++ b/src/localization/src/translations/cn.ts
@@ -102,6 +102,8 @@ export default {
layer: '图层',
appearance: '外观',
uniqueIdField: '唯一ID字段',
+ hideNulls: '隐藏空值',
+ hideNullsDescription: '开启后,没有填充颜色值的要素将被隐藏。关闭时,这些要素显示为灰色。',
type: {
point: 'point',
arc: 'arc',
diff --git a/src/localization/src/translations/en.ts b/src/localization/src/translations/en.ts
index 45869490a..c1ba3d63b 100644
--- a/src/localization/src/translations/en.ts
+++ b/src/localization/src/translations/en.ts
@@ -117,6 +117,9 @@ export default {
imageSource: 'Image Source',
alignment: 'Alignment',
uniqueIdField: 'Unique ID Field',
+ hideNulls: 'Hide Null Values',
+ hideNullsDescription:
+ 'When on, features with no fill-color value are hidden. When off, they are shown in gray.',
type: {
point: 'point',
arc: 'arc',
diff --git a/src/localization/src/translations/es.ts b/src/localization/src/translations/es.ts
index cb7c364fa..491385734 100644
--- a/src/localization/src/translations/es.ts
+++ b/src/localization/src/translations/es.ts
@@ -102,6 +102,9 @@ export default {
layer: 'Capa',
appearance: 'Apariencia',
uniqueIdField: 'Campo ID único',
+ hideNulls: 'Ocultar valores nulos',
+ hideNullsDescription:
+ 'Cuando está activado, las entidades sin valor de color de relleno se ocultan. Cuando está desactivado, se muestran en gris.',
type: {
point: 'punto',
arc: 'arco',
diff --git a/src/localization/src/translations/fi.ts b/src/localization/src/translations/fi.ts
index 090004bad..fa56b72bb 100644
--- a/src/localization/src/translations/fi.ts
+++ b/src/localization/src/translations/fi.ts
@@ -101,6 +101,9 @@ export default {
layer: 'Taso',
appearance: 'Ulkoasu',
uniqueIdField: 'Yksilöivä ID-kenttä',
+ hideNulls: 'Piilota tyhjät arvot',
+ hideNullsDescription:
+ 'Kun asetus on päällä, kohteet ilman täyttöväriarvoa piilotetaan. Kun se on pois päältä, ne näytetään harmaana.',
type: {
point: 'piste',
arc: 'kaari',
diff --git a/src/localization/src/translations/ja.ts b/src/localization/src/translations/ja.ts
index e6e581629..225590d03 100644
--- a/src/localization/src/translations/ja.ts
+++ b/src/localization/src/translations/ja.ts
@@ -102,6 +102,9 @@ export default {
layer: 'レイヤ',
appearance: '外観',
uniqueIdField: '一意IDフィールド',
+ hideNulls: 'Null値を非表示',
+ hideNullsDescription:
+ 'オンにすると、塗りつぶし色の値がないフィーチャは非表示になります。オフの場合は灰色で表示されます。',
type: {
point: 'point',
arc: 'arc',
diff --git a/src/localization/src/translations/pt.ts b/src/localization/src/translations/pt.ts
index 49fe5b434..203bb3fce 100644
--- a/src/localization/src/translations/pt.ts
+++ b/src/localization/src/translations/pt.ts
@@ -102,6 +102,9 @@ export default {
layer: 'Camada',
appearance: 'Aparência',
uniqueIdField: 'Campo ID único',
+ hideNulls: 'Ocultar valores nulos',
+ hideNullsDescription:
+ 'Quando ativado, entidades sem valor de cor de preenchimento são ocultadas. Quando desativado, elas são exibidas em cinza.',
type: {
point: 'ponto',
arc: 'arco',
diff --git a/src/localization/src/translations/ru.ts b/src/localization/src/translations/ru.ts
index 3de4d3db6..a08dd2b89 100644
--- a/src/localization/src/translations/ru.ts
+++ b/src/localization/src/translations/ru.ts
@@ -102,6 +102,9 @@ export default {
layer: 'Слой',
appearance: 'Внешний вид',
uniqueIdField: 'Поле уникального ID',
+ hideNulls: 'Скрывать пустые значения',
+ hideNullsDescription:
+ 'Если включено, объекты без значения цвета заливки скрываются. Если выключено, они отображаются серым.',
type: {
point: 'точки',
arc: 'дуги',
diff --git a/test/browser/layer-tests/index.js b/test/browser/layer-tests/index.js
index 3dfe0a1d6..81241c967 100644
--- a/test/browser/layer-tests/index.js
+++ b/test/browser/layer-tests/index.js
@@ -20,5 +20,6 @@ import './s2-geometry-layer-specs';
import './a5-layer-specs';
import './geohash-layer-specs';
import './wms-layer-specs';
+import './vector-tile-layer-specs';
import './flow-layer-specs';
import './aggregation-layer-geojson-specs';
diff --git a/test/browser/layer-tests/vector-tile-layer-specs.js b/test/browser/layer-tests/vector-tile-layer-specs.js
new file mode 100644
index 000000000..59f1ad8e1
--- /dev/null
+++ b/test/browser/layer-tests/vector-tile-layer-specs.js
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import test from 'tape-catch';
+import {KeplerGlLayers} from '@kepler.gl/layers';
+import {MISSING_VALUE_COLOR, NO_VALUE_COLOR} from '@kepler.gl/constants';
+
+const {VectorTileLayer} = KeplerGlLayers;
+
+test('#VectorTileLayer -> hideNulls visConfig defaults', t => {
+ const layer = new VectorTileLayer({dataId: 'test'});
+
+ t.equal(layer.config.visConfig.hideNulls, false, 'hideNulls is off by default');
+ t.ok(layer.visConfigSettings.hideNulls, 'hideNulls visConfig setting is registered');
+ t.notOk(
+ layer.shouldCalculateLayerData(['hideNulls']),
+ 'toggling hideNulls should not recreate tile data'
+ );
+
+ const nullValue = layer.visualChannels.color.nullValue;
+ t.equal(typeof nullValue, 'function', 'fill color nullValue is resolved from visConfig');
+ t.deepEqual(
+ nullValue(layer.config),
+ MISSING_VALUE_COLOR,
+ 'missing fill values render gray when hideNulls is off'
+ );
+
+ layer.updateLayerVisConfig({hideNulls: true});
+ t.deepEqual(
+ layer.visualChannels.color.nullValue(layer.config),
+ NO_VALUE_COLOR,
+ 'missing fill values are transparent when hideNulls is on'
+ );
+
+ t.end();
+});
+
+test('#VectorTileLayer -> getEncodedChannelValue respects hideNulls', t => {
+ const layer = new VectorTileLayer({dataId: 'test'});
+ const scale = () => [255, 0, 0, 255];
+ const getNull = () => null;
+
+ t.deepEqual(
+ layer.getEncodedChannelValue(
+ scale,
+ {},
+ {name: 'metric'},
+ layer.visualChannels.color.nullValue(layer.config),
+ getNull
+ ),
+ MISSING_VALUE_COLOR,
+ 'null fill values use gray when hideNulls is off'
+ );
+
+ layer.updateLayerVisConfig({hideNulls: true});
+ t.deepEqual(
+ layer.getEncodedChannelValue(
+ scale,
+ {},
+ {name: 'metric'},
+ layer.visualChannels.color.nullValue(layer.config),
+ getNull
+ ),
+ NO_VALUE_COLOR,
+ 'null fill values are transparent when hideNulls is on'
+ );
+
+ t.end();
+});