Skip to content

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

[Chore]: Technical: Translate mapboxgl-layer (#1755)

Maksim Suslov18342926faa1
diff --git a/src/layers/base-layer.ts b/src/layers/base-layer.ts
index 2850afac0..019a1056e 100644
--- a/src/layers/base-layer.ts
+++ b/src/layers/base-layer.ts
@@ -116,6 +116,8 @@ export type LayerWeightConfig = {
weightField: VisualChannelField;
};
+export type VisualChannels = {[key: string]: VisualChannel};
+
export type VisualChannel = {
property: string;
field: string;
@@ -241,7 +243,7 @@ class Layer {
return ['label', 'opacity', 'thickness', 'isVisible', 'hidden'];
}
- get visualChannels(): {[key: string]: VisualChannel} {
+ get visualChannels(): VisualChannels {
return {
color: {
property: 'color',
@@ -1007,7 +1009,8 @@ class Layer {
this.meta = {...this.meta, ...meta};
}
- getDataUpdateTriggers({filteredIndex, id, allData}) {
+ // @ts-expect-error
+ getDataUpdateTriggers({filteredIndex, id, allData}: KeplerTable): any {
const {columns} = this.config;
return {
@@ -1310,7 +1313,7 @@ class Layer {
}, []);
}
- calculateDataAttribute(keplerTable: KeplerTable, getPosition) {
+ calculateDataAttribute(keplerTable: KeplerTable, getPosition): any {
// implemented in subclasses
return [];
}
diff --git a/src/layers/heatmap-layer/heatmap-layer.js b/src/layers/heatmap-layer/heatmap-layer.js
index ace3e8bcd..d612d0e4c 100644
--- a/src/layers/heatmap-layer/heatmap-layer.js
+++ b/src/layers/heatmap-layer/heatmap-layer.js
@@ -195,15 +195,6 @@ class HeatmapLayer extends MapboxGLLayer {
}
);
- getGeometry(position) {
- return position.every(Number.isFinite)
- ? {
- type: 'Point',
- coordinates: position
- }
- : null;
- }
-
formatLayerData(datasets, oldLayerData) {
const {weightField} = this.config;
const {dataContainer} = datasets[this.config.dataId];
diff --git a/src/layers/mapbox-utils.js b/src/layers/mapbox-utils.ts
similarity index 78%
rename from src/layers/mapbox-utils.js
rename to src/layers/mapbox-utils.ts
index f0ffb6d43..b29fb9a7c 100644
--- a/src/layers/mapbox-utils.js
+++ b/src/layers/mapbox-utils.ts
@@ -18,25 +18,23 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-import {OVERLAY_TYPE} from './base-layer';
-
-/** @typedef {import("geojson").FeatureCollection} FeatureCollection */
-/** @typedef {import("geojson").Feature} Feature */
+import Layer, {OVERLAY_TYPE} from './base-layer';
+import {Feature} from 'geojson';
/**
* This function will convert layers to mapbox layers
- * @param {Array<Object>} layers the layers to be converted
- * @param {Array<Object>} layerData extra layer information
- * @param {Array<Number>} layerOrder the order by which we should convert layers
- * @param {Object} layersToRender {[id]: true | false} object whether each layer should be rendered
- * @returns {Object} {[id]: layer}
+ * @param layers the layers to be converted
+ * @param layerData extra layer information
+ * @param layerOrder the order by which we should convert layers
+ * @param layersToRender {[id]: true | false} object whether each layer should be rendered
+ * @returns
*/
export function generateMapboxLayers(
- layers = [],
- layerData = [],
- layerOrder = [],
- layersToRender = {}
-) {
+ layers: Layer[] = [],
+ layerData: any[] = [],
+ layerOrder: number[] = [],
+ layersToRender: {[key: string]: boolean} = {}
+): {[key: string]: Layer} {
if (layerData.length > 0) {
return layerOrder
.slice()
@@ -63,14 +61,22 @@ export function generateMapboxLayers(
return {};
}
+type newLayersType = {
+ [key: string]: Layer & Partial<{data: any; sourceId: any; isVisible: boolean}>;
+};
+type oldLayersType = {[key: string]: Layer & {data?: any}};
/**
* Update mapbox layers on the given map
- * @param {Object} map
- * @param {Object} newLayers Map of new mapbox layers to be displayed
- * @param {Object} oldLayers Map of the old layers to be compare with the current ones to detect deleted layers
+ * @param map
+ * @param newLayers Map of new mapbox layers to be displayed
+ * @param oldLayers Map of the old layers to be compare with the current ones to detect deleted layers
* {layerId: sourceId}
*/
-export function updateMapboxLayers(map, newLayers = {}, oldLayers = null) {
+export function updateMapboxLayers(
+ map,
+ newLayers: newLayersType = {},
+ oldLayers: oldLayersType | null = null
+) {
// delete no longer existed old layers
if (oldLayers) {
checkAndRemoveOldLayers(map, oldLayers, newLayers);
@@ -96,7 +102,7 @@ export function updateMapboxLayers(map, newLayers = {}, oldLayers = null) {
});
}
-function checkAndRemoveOldLayers(map, oldLayers, newLayers) {
+function checkAndRemoveOldLayers(map, oldLayers: oldLayersType, newLayers: newLayersType) {
Object.keys(oldLayers).forEach(layerId => {
if (!newLayers[layerId]) {
map.removeLayer(layerId);
@@ -137,10 +143,13 @@ function updateSourceData(map, sourceId, data) {
* @param getProperties {({index: number}) => any}
* @returns FeatureCollection
*/
-export function geoJsonFromData(filteredIndex = [], getGeometry, getProperties = d => {}) {
- const geojson = {
+export function geoJsonFromData(
+ filteredIndex = [],
+ getGeometry: {({index: number}): any},
+ getProperties: {({index: number}): any} = d => {}
+) {
+ const geojson: {type: string; features: Feature[]} = {
type: 'FeatureCollection',
- /** @type {Feature[]} */
features: []
};
@@ -180,7 +189,7 @@ export function gpuFilterToMapboxFilter(gpuFilter) {
// [">=", key, value]
// ["<=", key, value]
const expressions = Object.values(filterValueUpdateTriggers).reduce(
- (accu, name, i) =>
+ (accu: any[], name, i) =>
name
? [
...accu,
diff --git a/src/layers/mapboxgl-layer.js b/src/layers/mapboxgl-layer.ts
similarity index 76%
rename from src/layers/mapboxgl-layer.js
rename to src/layers/mapboxgl-layer.ts
index ff637657e..98f9794a7 100644
--- a/src/layers/mapboxgl-layer.js
+++ b/src/layers/mapboxgl-layer.ts
@@ -18,16 +18,28 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-import Layer, {OVERLAY_TYPE} from './base-layer';
+import Layer, {LayerBaseConfig, LayerColumn, OVERLAY_TYPE, VisualChannels} from './base-layer';
import {createSelector} from 'reselect';
import {geoJsonFromData, prefixGpuField, gpuFilterToMapboxFilter} from './mapbox-utils';
+import KeplerTable from '../utils/table-utils/kepler-table';
+import {Merge} from '../reducers';
-export const mapboxRequiredColumns = ['lat', 'lng'];
+type MapboxLayerGLColumns = {
+ lat: LayerColumn;
+ lng: LayerColumn;
+};
-export const pointColResolver = ({lat, lng}) => `${lat.fieldIdx}-${lng.fieldIdx}`;
+export type MapboxLayerGLConfig = Merge<LayerBaseConfig, {columns: MapboxLayerGLColumns}>;
+
+export const mapboxRequiredColumns: ['lat', 'lng'] = ['lat', 'lng'];
+
+export const pointColResolver = ({lat, lng}: MapboxLayerGLColumns) =>
+ `${lat.fieldIdx}-${lng.fieldIdx}`;
class MapboxLayerGL extends Layer {
+ declare config: MapboxLayerGLConfig;
+
get overlayType() {
return OVERLAY_TYPE.mapboxgl;
}
@@ -36,7 +48,7 @@ class MapboxLayerGL extends Layer {
return null;
}
- get isAggregated() {
+ get isAggregated(): true {
return true;
}
@@ -52,12 +64,13 @@ class MapboxLayerGL extends Layer {
return [];
}
- get visualChannels() {
+ get visualChannels(): VisualChannels {
return {};
}
- datasetSelector = config => config.dataId;
- gpuFilterSelector = (config, datasets) => (datasets[config.dataId] || {}).gpuFilter;
- columnsSelector = config => pointColResolver(config.columns);
+ datasetSelector = (config: MapboxLayerGLConfig) => config.dataId;
+ gpuFilterSelector = (config: MapboxLayerGLConfig, datasets) =>
+ (datasets[config.dataId] || {}).gpuFilter;
+ columnsSelector = (config: MapboxLayerGLConfig) => pointColResolver(config.columns);
sourceSelector = createSelector(
this.datasetSelector,
@@ -74,7 +87,7 @@ class MapboxLayerGL extends Layer {
return Array.isArray(filter) && filter.length;
}
- getDataUpdateTriggers({filteredIndex, gpuFilter, id}) {
+ getDataUpdateTriggers({filteredIndex, gpuFilter, id}: KeplerTable): any {
const {columns} = this.config;
const visualChannelFields = Object.values(this.visualChannels).reduce(
@@ -99,7 +112,16 @@ class MapboxLayerGL extends Layer {
return updateTriggers;
}
- calculateDataAttribute({dataContainer, filteredIndex, gpuFilter}, getPosition) {
+ getGeometry(position) {
+ return position.every(Number.isFinite)
+ ? {
+ type: 'Point',
+ coordinates: position
+ }
+ : null;
+ }
+
+ calculateDataAttribute({dataContainer, filteredIndex, gpuFilter}: KeplerTable, getPosition) {
const getGeometry = d => this.getGeometry(getPosition(d));
const vcFields = Object.values(this.visualChannels)
@@ -127,14 +149,14 @@ class MapboxLayerGL extends Layer {
? d => {
const filterValue = valueAccessor(d);
return Object.values(filterValueUpdateTriggers).reduce(
- (accu, name, i) => ({
+ (accu: any, name, i) => ({
...accu,
...(name ? {[prefixGpuField(name)]: filterValue[i]} : {})
}),
{}
- );
+ ) as any;
}
- : d => ({});
+ : d => ({} as any);
const getProperties = d => ({
...getPropertyFromVisualChanel(d),