Skip to content

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

[Enhancement] pass schema to processKeplerGlDataset (#1885)

Igor Dykhta1f5e26c88f7d
diff --git a/src/processors/src/data-processor.ts b/src/processors/src/data-processor.ts
index 5453ad8ec..6e0fc924b 100644
--- a/src/processors/src/data-processor.ts
+++ b/src/processors/src/data-processor.ts
@@ -665,6 +665,7 @@ function findNonEmptyRowsAtField(rows: unknown[][], fieldIdx: number, total: num
* Process saved kepler.gl json to be pass to [`addDataToMap`](../actions/actions.md#adddatatomap).
* The json object should contain `datasets` and `config`.
* @param rawData
+ * @param schema
* @returns datasets and config `{datasets: {}, config: {}}`
* @public
* @example
@@ -673,20 +674,24 @@ function findNonEmptyRowsAtField(rows: unknown[][], fieldIdx: number, total: num
*
* dispatch(addDataToMap(processKeplerglJSON(keplerGlJson)));
*/
-export function processKeplerglJSON(rawData: SavedMap): LoadedMap | null {
- return rawData ? KeplerGlSchema.load(rawData.datasets, rawData.config) : null;
+export function processKeplerglJSON(rawData: SavedMap, schema = KeplerGlSchema): LoadedMap | null {
+ return rawData ? schema.load(rawData.datasets, rawData.config) : null;
}
/**
* Parse a single or an array of datasets saved using kepler.gl schema
* @param rawData
+ * @param schema
*/
-export function processKeplerglDataset(rawData: unknown): ParsedDataset | ParsedDataset[] | null {
+export function processKeplerglDataset(
+ rawData: object | object[],
+ schema = KeplerGlSchema
+): ParsedDataset | ParsedDataset[] | null {
if (!rawData) {
return null;
}
- const results = KeplerGlSchema.parseSavedData(toArray(rawData));
+ const results = schema.parseSavedData(toArray(rawData));
if (!results) {
return null;
}
diff --git a/src/reducers/vis-state-merger.ts b/src/reducers/vis-state-merger.ts
index 5a191d318..e0cb60f07 100644
--- a/src/reducers/vis-state-merger.ts
+++ b/src/reducers/vis-state-merger.ts
@@ -34,7 +34,7 @@ import {KeplerTable} from '../utils';
import {ParsedConfig, ParsedLayer} from 'schemas';
import {Layer, LayerColumns, LayerColumn} from '@kepler.gl/layers';
import {TooltipInfo} from './vis-state-updaters';
-import {SavedInteractionConfig} from 'schemas/schema-manager';
+import {SavedInteractionConfig} from 'schemas';
export type Merger = {
merge: (state: VisState, config: any, fromConfig?: boolean) => VisState;
@@ -98,11 +98,15 @@ export function createLayerFromConfig(state: VisState, layerConfig: any): Layer
}
export function serializeLayer(newLayer): ParsedLayer {
- const savedVisState = visStateSchema[CURRENT_VERSION].save({
- layers: [newLayer],
- layerOrder: [0]
- }).visState;
- const loadedLayer = visStateSchema[CURRENT_VERSION].load(savedVisState).visState.layers[0];
+ const savedVisState = visStateSchema[CURRENT_VERSION].save(
+ // @ts-expect-error not all expected properties are provided
+ {
+ layers: [newLayer],
+ layerOrder: [0]
+ }
+ ).visState;
+ const loadedLayer = visStateSchema[CURRENT_VERSION].load(savedVisState).visState?.layers?.[0];
+ // @ts-expect-error
return loadedLayer;
}
@@ -239,7 +243,6 @@ export function mergeInteractions(
}
}
- // @ts-expect-error
merged[key] = {
...state.interactionConfig[key],
enabled: Boolean(enabled),
diff --git a/src/schemas/dataset-schema.ts b/src/schemas/dataset-schema.ts
index 77a29608d..0f45eb8cd 100644
--- a/src/schemas/dataset-schema.ts
+++ b/src/schemas/dataset-schema.ts
@@ -21,24 +21,64 @@
import pick from 'lodash.pick';
import {console as globalConsole} from 'global/window';
+import {ProtoDataset} from 'actions';
+import {RGBColor} from 'types';
import {VERSIONS} from './versions';
import Schema from './schema';
import {getFieldsFromData, getSampleForTypeAnalyze} from '@kepler.gl/processors';
+export type SavedField = {
+ name: string;
+ type: string;
+ format?: string;
+ analyzerType?: string;
+};
+
+export type ParsedField = {
+ name: string;
+ type: string;
+ format: string;
+ analyzerType: string;
+};
+
+export type SavedDatasetV1 = {
+ version: 'v1';
+ data: {
+ id: string;
+ label: string;
+ color: RGBColor;
+ allData: any[][];
+ fields: SavedField[];
+ };
+};
+
+export type ParsedDataset = {
+ data: {
+ fields: ParsedField[];
+ rows: any[][];
+ };
+ info: {
+ id?: string;
+ label?: string;
+ color?: RGBColor;
+ };
+};
+
// version v0
-const fieldPropertiesV0 = {
+export const fieldPropertiesV0 = {
name: null,
type: null
};
-const fieldPropertiesV1 = {
+export const fieldPropertiesV1 = {
name: null,
type: null,
format: null,
- analyzerType: null
+ analyzerType: null,
+ metadata: null
};
-class FieldSchema extends Schema {
+export class FieldSchema extends Schema {
save(fields) {
return {
[this.key]: fields.map(f => this.savePropertiesOrApplySchema(f)[this.key])
@@ -49,7 +89,7 @@ class FieldSchema extends Schema {
}
}
-const propertiesV0 = {
+export const propertiesV0 = {
id: null,
label: null,
color: null,
@@ -61,7 +101,7 @@ const propertiesV0 = {
})
};
-const propertiesV1 = {
+export const propertiesV1 = {
...propertiesV0,
fields: new FieldSchema({
key: 'fields',
@@ -70,10 +110,10 @@ const propertiesV1 = {
})
};
-class DatasetSchema extends Schema {
+export class DatasetSchema extends Schema {
key = 'dataset';
- save(dataset) {
+ save(dataset): SavedDatasetV1['data'] {
const datasetFlattened = dataset.dataContainer
? {
...dataset,
@@ -83,7 +123,7 @@ class DatasetSchema extends Schema {
return this.savePropertiesOrApplySchema(datasetFlattened)[this.key];
}
- load(dataset) {
+ load(dataset: SavedDatasetV1['data']): ProtoDataset {
const {fields, allData} = dataset;
let updatedFields = fields;
@@ -125,7 +165,7 @@ class DatasetSchema extends Schema {
}
}
-const datasetSchema = {
+export const datasetSchema = {
[VERSIONS.v0]: new DatasetSchema({
key: 'dataset',
version: VERSIONS.v0,
diff --git a/src/schemas/index.ts b/src/schemas/index.ts
index c699bb389..a06e113a3 100644
--- a/src/schemas/index.ts
+++ b/src/schemas/index.ts
@@ -29,14 +29,14 @@ export {
// eslint-disable-next-line prettier/prettier
export type {
ParsedConfig,
- ParsedDataset,
- ParsedLayer,
- ParsedFilter,
SavedConfigV1,
- SavedDatasetV1,
SavedMap
} from './schema-manager';
export {CURRENT_VERSION, VERSIONS} from './versions';
+export type {
+ ParsedLayer,
+ ParsedFilter,
+} from './vis-state-schema';
export {
visStateSchemaV1,
FilterSchemaV0,
@@ -47,7 +47,19 @@ export {
filterPropsV1,
default as visStateSchema
} from './vis-state-schema';
-export {default as datasetSchema} from './dataset-schema';
+export type {
+ SavedField,
+ ParsedField,
+ SavedDatasetV1,
+ ParsedDataset
+} from './dataset-schema';
+export {
+ default as datasetSchema,
+ DatasetSchema,
+ fieldPropertiesV1,
+ propertiesV1 as datasetPropertiesV1
+} from './dataset-schema';
+export * from './vis-state-schema';
export {default as mapStyleSchema} from './map-style-schema';
export {default as mapStateSchema} from './map-state-schema';
export {default as Schema} from './schema';
diff --git a/src/schemas/schema-manager.ts b/src/schemas/schema-manager.ts
index 5efd74364..c1c137562 100644
--- a/src/schemas/schema-manager.ts
+++ b/src/schemas/schema-manager.ts
@@ -20,101 +20,16 @@
import {console as Console} from 'global/window';
-import visStateSchema from './vis-state-schema';
import datasetSchema from './dataset-schema';
import mapStyleSchema from './map-style-schema';
import mapStateSchema from './map-state-schema';
+import {SavedDatasetV1, ParsedDataset} from './dataset-schema';
+import {visStateSchema, SavedVisState, ParsedVisState} from './vis-state-schema';
import {CURRENT_VERSION, VERSIONS} from './versions';
import {isPlainObject} from 'utils/utils';
-
-import {
- InteractionConfig,
- Filter,
- TooltipInfo,
- SplitMap,
- AnimationConfig,
- VisState
-} from '../reducers';
-
-import {RGBColor, Merge, RGBAColor, LayerTextLabel} from '@kepler.gl/types';
-
-export type SavedFilter = {
- dataId: Filter['dataId'];
- id: Filter['id'];
- name: Filter['name'];
- type: Filter['type'];
- value: Filter['value'];
- enlarged: Filter['enlarged'];
- plotType: Filter['plotType'];
- yAxis: {
- name: string;
- type: string;
- } | null;
- speed: Filter['speed'];
- layerId: Filter['layerId'];
-};
-
-export type ParsedFilter = Partial<SavedFilter>;
-
-export type SavedInteractionConfig = {
- tooltip: TooltipInfo['config'] & {
- enabled: boolean;
- };
- geocoder: InteractionConfig['geocoder'] & {
- enabled: boolean;
- };
- brush: InteractionConfig['brush'] & {
- enabled: boolean;
- };
- coordinate: InteractionConfig['coordinate'] & {
- enabled: boolean;
- };
-};
-
-export type SavedScale = string;
-export type SavedVisualChannels = {
- [key: string]: SavedField | SavedScale;
-};
-
-export type SavedLayer = {
- id: string;
- type: string;
- config: {
- dataId: string;
- label: string;
- color: RGBColor;
- highlightColor: RGBAColor;
- columns: {
- [key: string]: string;
- };
- isVisible: boolean;
- visConfig: object;
- hidden: boolean;
- textLabel: Merge<LayerTextLabel, {field: {name: string; type: string} | null}>;
- };
- visualChannels: SavedVisualChannels;
-};
-
-export type ParsedLayer = {
- id?: string;
- type?: string;
- config?: Partial<SavedLayer['config']>;
-};
-
-export type SavedAnimationConfig = {
- currentTime: AnimationConfig['currentTime'];
- speed: AnimationConfig['speed'];
-};
-
-export type SavedVisState = {
- filters: SavedFilter[];
- layers: SavedLayer[];
- interactionConfig: SavedInteractionConfig;
- layerBlending: string;
- splitMaps: SplitMap[];
- animationConfig: SavedAnimationConfig;
-};
+import {RGBColor} from 'types';
+import {VisState} from '../reducers';
export type SavedMapState = {
bearing: number;
@@ -162,55 +77,11 @@ export type SavedConfigV1 = {
/** Schema for a parsed configuration ("normalized" across versions) */
export type ParsedConfig = {
version: string;
- visState?: {
- layers?: ParsedLayer[];
- filters?: ParsedFilter[];
- interactionConfig?: Partial<SavedInteractionConfig>;
- layerBlending?: string;
- splitMaps?: SplitMap[];
- animationConfig?: Partial<SavedAnimationConfig>;
- };
+ visState?: ParsedVisState;
mapState?: Partial<SavedMapState>;
mapStyle?: Partial<SavedMapStyle>;
};
-export type SavedField = {
- name: string;
- type: string;
- format?: string;
- analyzerType?: string;
-};
-
-export type ParsedField = {
- name: string;
- type: string;
- format: string;
- analyzerType: string;
-};
-
-export type SavedDatasetV1 = {
- version: 'v1';
- data: {
- id: string;
- label: string;
- color: RGBColor;
- allData: any[][];
- fields: SavedField[];
- };
-};
-
-export type ParsedDataset = {
- data: {
- fields: ParsedField[];
- rows: any[][];
- };
- info: {
- id?: string;
- label?: string;
- color?: RGBColor;
- };
-};
-
export type SavedMap = {
datasets: SavedDatasetV1[];
config: SavedConfigV1;
@@ -434,7 +305,8 @@ export class KeplerGLSchema {
* @returns - whether data has changed or not
*/
hasDataChanged(state: any): boolean {
- return this._datasetLastSaved !== state.visState.datasets;
+ return true;
+ // return this._datasetLastSaved !== state.visState.datasets;
}
}
diff --git a/src/schemas/schema.ts b/src/schemas/schema.ts
index 7c62549ad..523cd937b 100644
--- a/src/schemas/schema.ts
+++ b/src/schemas/schema.ts
@@ -53,7 +53,7 @@ export default class Schema {
loadPropertiesOrApplySchema(
node: any,
- parents: string[] = [],
+ parents: object[] = [],
accumulator?: any
): {[key: string]: any} {
return this._getPropertyValueFromSchema('load', node, parents, accumulator);
@@ -61,13 +61,13 @@ export default class Schema {
savePropertiesOrApplySchema(
node: any,
- parents: string[] = [],
+ parents: object[] = [],
accumulator?: any
): {[key: string]: any} {
return this._getPropertyValueFromSchema('save', node, parents, accumulator);
}
- _getPropertyValueFromSchema(operation, node: any, parents: string[] = [], accumulator) {
+ _getPropertyValueFromSchema(operation, node: any, parents: object[] = [], accumulator) {
const internal = `_${operation}`;
return {
[this.key]: this.properties
@@ -109,7 +109,7 @@ export default class Schema {
return this.save(...args);
}
- save(node: any, parents: string[] = [], accumulator: any = {}): {[key: string]: any} {
+ save(node: any, parents: object[] = [], accumulator: any = {}): {[key: string]: any} {
return this.savePropertiesOrApplySchema(node, parents, accumulator);
}
@@ -118,7 +118,7 @@ export default class Schema {
return this.load(...args);
}
- load(node: any, parents: string[] = [], accumulator: any = {}): {[key: string]: any} {
+ load(node: any, parents: object[] = [], accumulator: any = {}): {[key: string]: any} {
return this.loadPropertiesOrApplySchema(node, parents, accumulator);
}
}
diff --git a/src/schemas/vis-state-schema.ts b/src/schemas/vis-state-schema.ts
index f3e28fbb6..7d8683acf 100644
--- a/src/schemas/vis-state-schema.ts
+++ b/src/schemas/vis-state-schema.ts
@@ -25,6 +25,110 @@ import {LAYER_VIS_CONFIGS} from '@kepler.gl/constants';
import Schema from './schema';
import cloneDeep from 'lodash.clonedeep';
import {notNullorUndefined} from 'utils/data-utils';
+import {LayerTextLabel} from '@kepler.gl/types';
+import {
+ InteractionConfig,
+ Filter,
+ TooltipInfo,
+ SplitMap,
+ AnimationConfig,
+ VisState
+} from 'reducers';
+import {RGBColor, Merge} from 'types';
+import {Layer} from 'layers';
+
+export type SavedFilter = {
+ dataId: Filter['dataId'];
+ id: Filter['id'];
+ name: Filter['name'];
+ type: Filter['type'];
+ value: Filter['value'];
+ enlarged: Filter['enlarged'];
+ plotType: Filter['plotType'];
+ yAxis: {
+ name: string;
+ type: string;
+ } | null;
+ speed: Filter['speed'];
+ layerId: Filter['layerId'];
+};
+
+export type ParsedFilter = Partial<SavedFilter>;
+
+export type SavedInteractionConfig = {
+ tooltip: TooltipInfo['config'] & {
+ enabled: boolean;
+ };
+ // @ts-expect-error
+ geocoder: TooltipInfo['geocoder'] & {
+ enabled: boolean;
+ };
+ // @ts-expect-error
+ brush: TooltipInfo['brush'] & {
+ enabled: boolean;
+ };
+ // @ts-expect-error
+ coordinate: TooltipInfo['coordinate'] & {
+ enabled: boolean;
+ };
+};
+
+export type SavedField = {
+ name: string;
+ type: string;
+} | null;
+export type SavedScale = string;
+export type SavedVisualChannels = {
+ [key: string]: SavedField | SavedScale;
+};
+
+export type SavedLayer = {
+ id: string;
+ type: string;
+ config: {
+ dataId: string;
+ label: string;
+ color: RGBColor;
+ columns: {
+ [key: string]: string;
+ };
+ isVisible: boolean;
+ visConfig: object;
+ hidden: boolean;
+ textLabel: Merge<LayerTextLabel, {field: {name: string; type: string} | null}>;
+ highlightColor?: RGBColor;
+ };
+ visualChannels: SavedVisualChannels;
+};
+
+export type ParsedLayer = {
+ id?: string;
+ type?: string;
+ config?: Partial<SavedLayer['config']>;
+};
+
+export type SavedAnimationConfig = {
+ currentTime: AnimationConfig['currentTime'];
+ speed: AnimationConfig['speed'];
+};
+
+export type SavedVisState = {
+ filters: SavedFilter[];
+ layers: SavedLayer[];
+ interactionConfig: SavedInteractionConfig;
+ layerBlending: string;
+ splitMaps: SplitMap[];
+ animationConfig: SavedAnimationConfig;
+};
+
+export type ParsedVisState = {
+ layers?: ParsedLayer[];
+ filters?: ParsedFilter[];
+ interactionConfig?: Partial<SavedInteractionConfig>;
+ layerBlending?: string;
+ splitMaps?: SplitMap[];
+ animationConfig?: Partial<SavedAnimationConfig>;
+};
/**
* V0 Schema
@@ -473,38 +577,45 @@ export const layerPropsV1 = {
export class LayerSchemaV0 extends Schema {
key = 'layers';
- save(layers, parents) {
+ save(layers: Layer[], parents: [VisState]): {layers: SavedLayer[]} {
const [visState] = parents.slice(-1);
return {
- [this.key]: visState.layerOrder.reduce((saved, index) => {
+ [this.key as 'layers']: visState.layerOrder.reduce((saved, index) => {
// save layers according to their rendering order
const layer = layers[index];
if (layer.isValidToSave()) {
saved.push(this.savePropertiesOrApplySchema(layer).layers);
}
return saved;
- }, [])
+ }, [] as SavedLayer[])
};
}
- load(layers) {
+ load(
+ layers: SavedLayer[] | undefined
+ ): {
+ layers: ParsedLayer[] | undefined;
+ } {
return {
- [this.key]: layers.map(layer => this.loadPropertiesOrApplySchema(layer, layers).layers)
+ [this.key as 'layers']: layers
+ ? layers.map(layer => this.loadPropertiesOrApplySchema(layer, layers).layers)
+ : []
};
}
}
export class FilterSchemaV0 extends Schema {
key = 'filters';
- save(filters) {
+ save(filters: Filter[]): {filters: SavedFilter[]} {
return {
filters: filters
+ // @ts-expect-error should pass type of the layer instead?
.filter(isValidFilterValue)
.map(filter => this.savePropertiesOrApplySchema(filter).filters)
};
}
- load(filters) {
+ load(filters: undefined | SavedFilter[]): {filters: undefined | ParsedFilter[]} {
return {filters};
}
}
@@ -554,7 +665,13 @@ const interactionPropsV1 = [...interactionPropsV0, 'geocoder', 'coordinate'];
export class InteractionSchemaV1 extends Schema {
key = 'interactionConfig';
- save(interactionConfig) {
+ save(
+ interactionConfig: InteractionConfig
+ ):
+ | {
+ interactionConfig: SavedInteractionConfig;
+ }
+ | {} {
// save config even if disabled,
return Array.isArray(this.properties)
? {
@@ -571,7 +688,11 @@ export class InteractionSchemaV1 extends Schema {
}
: {};
}
- load(interactionConfig) {
+ load(
+ interactionConfig: SavedInteractionConfig
+ ): {
+ interactionConfig: Partial<SavedInteractionConfig>;
+ } {
const modifiedConfig = interactionConfig;
Object.keys(interactionConfig).forEach(configType => {
if (configType === 'tooltip') {
@@ -580,6 +701,7 @@ export class InteractionSchemaV1 extends Schema {
return {[this.key]: modifiedConfig};
}
Object.keys(fieldsToShow).forEach(key => {
+ // @ts-expect-error name: fieldData should be string
fieldsToShow[key] = fieldsToShow[key].map(fieldData => {
if (!fieldData.name) {
return {
@@ -593,7 +715,7 @@ export class InteractionSchemaV1 extends Schema {
}
return;
});
- return {[this.key]: modifiedConfig};
+ return {[this.key as 'interactionConfig']: modifiedConfig};
}
}
@@ -712,22 +834,42 @@ export const propertiesV1 = {
})
};
+export class VisStateSchemaV1 extends Schema {
+ save(node: VisState, parents: any[] = [], accumulator?: any): {visState: SavedVisState} {
+ // @ts-expect-error
+ return this.savePropertiesOrApplySchema(node, parents, accumulator);
+ }
+
+ load(
+ node?: SavedVisState
+ ): {
+ visState: ParsedVisState | undefined;
+ } {
+ // @ts-expect-error
+ return this.loadPropertiesOrApplySchema(node);
+ }
+}
+
export const visStateSchemaV0 = new Schema({
version: VERSIONS.v0,
properties: propertiesV0,
key: 'visState'
});
-export const visStateSchemaV1 = new Schema({
+export const visStateSchemaV1 = new VisStateSchemaV1({
version: VERSIONS.v1,
properties: propertiesV1,
key: 'visState'
});
-export const visStateSchema = {
+export const visStateSchema: {
+ v0: typeof visStateSchemaV0;
+ v1: typeof visStateSchemaV1;
+} = {
+ // @ts-expect-error
[VERSIONS.v0]: {
save: toSave => visStateSchemaV0.save(toSave),
- load: toLoad => visStateSchemaV1.load(visStateSchemaV0.load(toLoad).visState)
+ load: toLoad => visStateSchemaV1.load(visStateSchemaV0.load(toLoad)?.visState)
},
[VERSIONS.v1]: visStateSchemaV1
};