Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[Feat] support create geojson path from point csv in polygon layer (#2664)
Igor Dykhta2bc593711396
diff --git a/package.json b/package.jsonindex 977f31019..33ac8e97c 100644--- a/package.json+++ b/package.json@@ -140,7 +140,7 @@"@testing-library/user-event": "^14.4.3","@types/d3-array": "^2.0.0","@types/d3-scale": "^3.2.2",- "@types/geojson": "^7946.0.7",+ "@types/geojson": "^7946.0.8","@types/jsdom": "^21.1.1","@types/redux-actions": "^2.6.2","@types/supercluster": "^7.1.0",diff --git a/src/components/src/side-panel/layer-panel/layer-column-config.tsx b/src/components/src/side-panel/layer-panel/layer-column-config.tsxindex 54c876e82..704011430 100644--- a/src/components/src/side-panel/layer-panel/layer-column-config.tsx+++ b/src/components/src/side-panel/layer-panel/layer-column-config.tsx@@ -53,7 +53,7 @@ function getValidFieldPairsSuggestionsForColumn(LayerColumnConfigFactory.deps = [ColumnSelectorFactory];function LayerColumnConfigFactory(ColumnSelector: ReturnType<typeof ColumnSelectorFactory>) {- const LayerColumnConfig: React.FC<LayerColumnConfigProps<MinimalField>> = ({+ const LayerColumnConfig: React.FC<LayerColumnConfigProps<MinimalField & {fieldIdx: number}>> = ({columnPairs,fieldPairs,columns,diff --git a/src/components/src/side-panel/layer-panel/layer-column-mode-config.tsx b/src/components/src/side-panel/layer-panel/layer-column-mode-config.tsxindex 910234dda..1ebc0294a 100644--- a/src/components/src/side-panel/layer-panel/layer-column-mode-config.tsx+++ b/src/components/src/side-panel/layer-panel/layer-column-mode-config.tsx@@ -76,7 +76,9 @@ const ConfigPanesContainer = styled.div`}`;-interface FieldOption extends MinimalField {}+interface FieldOption extends MinimalField {+ fieldIdx: number;+}export type ColumnModeConfigProps = {supportedColumnModes: SupportedColumnMode[] | null;@@ -237,9 +239,7 @@ function LayerColumnModeConfigFactory(columnPairs={layer.columnPairs}columns={cols}assignColumnPairs={layer.assignColumnPairs.bind(layer)}- assignColumn={- layer.assignColumn.bind(layer) as (key: string, field: FieldOption) => LayerColumns- }+ assignColumn={layer.assignColumn.bind(layer)}columnLabels={layer.columnLabels}fields={fields}fieldPairs={fieldPairs}diff --git a/src/deckgl-layers/package.json b/src/deckgl-layers/package.jsonindex fd8a03134..87c2068f8 100644--- a/src/deckgl-layers/package.json+++ b/src/deckgl-layers/package.json@@ -43,7 +43,7 @@"@mapbox/geo-viewport": "^0.4.1","@mapbox/vector-tile": "^1.3.1","@types/d3-array": "^2.0.0",- "@types/geojson": "^7946.0.7",+ "@types/geojson": "^7946.0.8","@types/lodash.memoize": "^4.1.7","@types/supercluster": "^7.1.0","d3-array": "^2.8.0",diff --git a/src/layers/package.json b/src/layers/package.jsonindex 10ca93362..7e00d6732 100644--- a/src/layers/package.json+++ b/src/layers/package.json@@ -56,7 +56,7 @@"@turf/boolean-within": "^6.0.1","@turf/center": "^6.0.1","@turf/helpers": "^6.1.4",- "@types/geojson": "^7946.0.7",+ "@types/geojson": "^7946.0.8","@types/keymirror": "^0.1.1","@types/lodash.memoize": "^4.1.7","@types/lodash.uniq": "^4.5.7",diff --git a/src/layers/src/base-layer.ts b/src/layers/src/base-layer.tsindex ac1248b00..831e0083f 100644--- a/src/layers/src/base-layer.ts+++ b/src/layers/src/base-layer.ts@@ -41,6 +41,7 @@ import {hexToRgb,getLatLngBounds,isPlainObject,+ toArray,notNullorUndefined,DataContainerInterface,getSampleContainerData@@ -355,8 +356,9 @@ class Layer {*/get defaultPointColumnPairs(): ColumnPairs {return {- lat: {pair: 'lng', fieldPairKey: 'lat'},- lng: {pair: 'lat', fieldPairKey: 'lng'}+ lat: {pair: ['lng', 'altitude'], fieldPairKey: 'lat'},+ lng: {pair: ['lat', 'altitude'], fieldPairKey: 'lng'},+ altitude: {pair: ['lng', 'lat'], fieldPairKey: 'altitude'}};}@@ -546,11 +548,8 @@ class Layer {/*** Assign a field to layer column, return column config- * @param key - Column Key- * @param field - Selected field- * @returns {{}} - Column config*/- assignColumn(key: string, field: Field): LayerColumns {+ assignColumn(key: string, field: {name: string; fieldIdx: number}): LayerColumns {// field value could be null for optional columnsconst update = field? {@@ -570,30 +569,41 @@ class Layer {/*** Assign a field pair to column config, return column config- * @param key - Column Key- * @param pair - field Pair- * @returns Column config*/- assignColumnPairs(key: string, pair: FieldPair): LayerColumns {+ assignColumnPairs(key: string, fieldPairs: FieldPair): LayerColumns {if (!this.columnPairs || !this.columnPairs?.[key]) {// should not end in this statereturn this.config.columns;}+ // key = 'lat'+ const {pair, fieldPairKey} = this.columnPairs?.[key] || {};- const {pair: partnerKey, fieldPairKey} = this.columnPairs?.[key] || {};-- if (!pair[fieldPairKey]) {+ if (typeof fieldPairKey === 'string' && !pair[fieldPairKey]) {// do not allow `key: undefined` to creep into the `updatedColumn` objectreturn this.config.columns;}- const {fieldPairKey: partnerFieldPairKey} = this.columnPairs?.[partnerKey] || {};-- return {+ // pair = ['lng', 'alt] | 'lng'+ const updatedColumn = {...this.config.columns,- [key]: pair[fieldPairKey],- [partnerKey]: pair[partnerFieldPairKey]+ // @ts-expect-error fieldPairKey can be string[] here?+ [key]: fieldPairs[fieldPairKey]};++ const partnerKeys = toArray(pair);+ for (const partnerKey of partnerKeys) {+ if (+ this.config.columns[partnerKey] &&+ this.columnPairs?.[partnerKey] &&+ // @ts-ignore+ fieldPairs[this.columnPairs?.[partnerKey].fieldPairKey]+ ) {+ // @ts-ignore+ updatedColumn[partnerKey] = fieldPairs[this.columnPairs?.[partnerKey].fieldPairKey];+ }+ }++ return updatedColumn;}/**diff --git a/src/layers/src/geojson-layer/geojson-info-modal.tsx b/src/layers/src/geojson-layer/geojson-info-modal.tsxnew file mode 100644index 000000000..d8fc09ac0--- /dev/null+++ b/src/layers/src/geojson-layer/geojson-info-modal.tsx@@ -0,0 +1,96 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import React from 'react';+import styled from 'styled-components';+import ReactMarkdown from 'react-markdown';+import {useIntl} from 'react-intl';++import {FormattedMessage} from '@kepler.gl/localization';++import Table from '../table';++const InfoModal = styled.div`+ font-size: 13px;+ color: ${props => props.theme.titleColorLT};++ pre {+ padding: 12px;+ background-color: #f8f8f9;+ }+`;++const StyledTitle = styled.div`+ font-size: 20px;+ letter-spacing: 1.25px;+ margin: 18px 0 14px 0;+ color: ${props => props.theme.titleColorLT};+`;++const StyledCode = styled.code`+ color: ${props => props.theme.titleColorLT};+`;++const exampleTableHeader = ['id', 'latitude', 'longitude', 'sort by'];+const exampleTabbleRows = [+ ['A', '40.81773', '-74.20986', '0'],+ ['A', '40.81765', '-74.20987', '1'],+ ['A', '40.81746', '-74.20998', '2'],+ ['B', '40.64375', '-74.33242', '0'],+ ['B', '40.64353', '-74.20987', '1'],+ ['B', '40.64222', '-74.33001', '2']+];++const ExampleTable = () => (+ <Table className="geojson-example-table">+ <thead>+ <tr>+ {exampleTableHeader.map(v => (+ <th key={v}>{v}</th>+ ))}+ </tr>+ </thead>+ <tbody>+ {exampleTabbleRows.map((row, i) => (+ <tr key={i}>+ {row.map((v, j) => (+ <td key={j}>+ <StyledCode>{v}</StyledCode>+ </td>+ ))}+ </tr>+ ))}+ </tbody>+ </Table>+);++const GeojsonInfoModalFactory = columnMode => {+ const GeojsonInfoModal = () => {+ const intl = useIntl();+ return (+ <InfoModal className="geojson-info-modal">+ <div className="geojson-info-modal__description">+ <ReactMarkdown+ children={intl.formatMessage({+ id:+ columnMode === 'geojson'+ ? 'modal.polygonInfo.description'+ : 'modal.polygonInfo.descriptionTable'+ })}+ />+ </div>+ {columnMode === 'table' ? (+ <div className="geojson-info-modal__example">+ <StyledTitle>+ <FormattedMessage id="modal.polygonInfo.exampleTable" />+ </StyledTitle>+ <ExampleTable />+ </div>+ ) : null}+ </InfoModal>+ );+ };+ return GeojsonInfoModal;+};++export default GeojsonInfoModalFactory;diff --git a/src/layers/src/geojson-layer/geojson-layer.ts b/src/layers/src/geojson-layer/geojson-layer.tsindex d5b75c930..0ad0eefcf 100644--- a/src/layers/src/geojson-layer/geojson-layer.ts+++ b/src/layers/src/geojson-layer/geojson-layer.ts@@ -9,6 +9,7 @@ import uniq from 'lodash.uniq';import {DATA_TYPES} from 'type-analyzer';import Layer, {colorMaker,+ defaultGetFieldValue,LayerBaseConfig,LayerBaseConfigPartial,LayerColorConfig,@@ -18,11 +19,18 @@ import Layer, {LayerStrokeColorConfig} from '../base-layer';import {GeoJsonLayer as DeckGLGeoJsonLayer} from '@deck.gl/layers';-import {getGeojsonLayerMeta, GeojsonDataMaps, DeckGlGeoTypes} from './geojson-utils';+import {+ getGeojsonLayerMeta,+ GeojsonDataMaps,+ DeckGlGeoTypes,+ detectTableColumns,+ COLUMN_MODE_GEOJSON+} from './geojson-utils';import {getGeojsonLayerMetaFromArrow,isLayerHoveredFromArrow,- getHoveredObjectFromArrow+ getHoveredObjectFromArrow,+ assignColumnsByColumnMode} from '../layer-utils';import GeojsonLayerIcon from './geojson-layer-icon';import {@@ -46,6 +54,7 @@ import {import {KeplerTable} from '@kepler.gl/table';import {DataContainerInterface, ArrowDataContainer} from '@kepler.gl/utils';import {FilterArrowExtension} from '@kepler.gl/deckgl-layers';+import GeojsonInfoModalFactory from './geojson-info-modal';const SUPPORTED_ANALYZER_TYPES = {[DATA_TYPES.GEOMETRY]: true,@@ -179,6 +188,21 @@ const geoColumnAccessor =(dc: DataContainerInterface): arrow.Vector | null =>dc.getColumn?.(geojson.fieldIdx) as arrow.Vector;+const getTableModeValueAccessor = feature => {+ // Called from gpu-filter-utils.getFilterValueAccessor()+ return field => feature.properties.values.map(v => field.valueAccessor(v));+};++const getTableModeFieldValue = (field, data) => {+ let rv;+ if (typeof data === 'function') {+ rv = data(field);+ } else {+ rv = defaultGetFieldValue(field, data);+ }+ return rv;+};+const geoFieldAccessor =({geojson}: GeoJsonLayerColumnsConfig) =>(dc: DataContainerInterface): Field | null =>@@ -189,6 +213,22 @@ export const defaultElevation = 500;export const defaultLineWidth = 1;export const defaultRadius = 1;+export const COLUMN_MODE_TABLE = 'table';+const SUPPORTED_COLUMN_MODES = [+ {+ key: COLUMN_MODE_GEOJSON,+ label: 'GeoJSON',+ requiredColumns: ['geojson']+ },+ {+ key: COLUMN_MODE_TABLE,+ label: 'Table columns',+ requiredColumns: ['id', 'lat', 'lng'],+ optionalColumns: ['altitude', 'sortBy']+ }+];+const DEFAULT_COLUMN_MODE = COLUMN_MODE_GEOJSON;+export default class GeoJsonLayer extends Layer {declare config: GeoJsonLayerConfig;declare visConfigSettings: GeoJsonVisConfigSettings;@@ -200,12 +240,21 @@ export default class GeoJsonLayer extends Layer {filteredIndexTrigger: number[] | null = null;centroids: Array<number[] | null> = [];+ _layerInfoModal: {+ [COLUMN_MODE_TABLE]: () => React.JSX.Element;+ [COLUMN_MODE_GEOJSON]: () => React.JSX.Element;+ };+constructor(props) {super(props);this.registerVisConfig(geojsonVisConfigs);this.getPositionAccessor = (dataContainer: DataContainerInterface) =>featureAccessor(this.config.columns)(dataContainer);+ this._layerInfoModal = {+ [COLUMN_MODE_TABLE]: GeojsonInfoModalFactory(COLUMN_MODE_TABLE),+ [COLUMN_MODE_GEOJSON]: GeojsonInfoModalFactory(COLUMN_MODE_GEOJSON)+ };}get type() {@@ -223,8 +272,38 @@ export default class GeoJsonLayer extends Layer {return GeojsonLayerIcon;}- get requiredLayerColumns() {- return geoJsonRequiredColumns;+ get columnPairs() {+ return this.defaultPointColumnPairs;+ }++ get supportedColumnModes() {+ return SUPPORTED_COLUMN_MODES;+ }++ get layerInfoModal() {+ return {+ [COLUMN_MODE_GEOJSON]: {+ id: 'iconInfo',+ template: this._layerInfoModal[COLUMN_MODE_GEOJSON],+ modalProps: {+ title: 'modal.polygonInfo.title'+ }+ },+ [COLUMN_MODE_TABLE]: {+ id: 'iconInfo',+ template: this._layerInfoModal[COLUMN_MODE_TABLE],+ modalProps: {+ title: 'modal.polygonInfo.titleTable'+ }+ }+ };+ }++ accessVSFieldValue(field, indexKey) {+ if (this.config.columnMode === COLUMN_MODE_GEOJSON) {+ return defaultGetFieldValue;+ }+ return getTableModeFieldValue;}get visualChannels() {@@ -322,7 +401,7 @@ export default class GeoJsonLayer extends Layer {getDefaultLayerConfig(props: LayerBaseConfigPartial) {return {...super.getDefaultLayerConfig(props),-+ columnMode: props?.columnMode ?? DEFAULT_COLUMN_MODE,// add height visual channelheightField: null,heightDomain: [0, 1],@@ -385,12 +464,17 @@ export default class GeoJsonLayer extends Layer {const {gpuFilter, dataContainer} = datasets[this.config.dataId];const {data} = this.updateData(datasets, oldLayerData);- const customFilterValueAccessor = (dc, d, fieldIndex) => {- return dc.valueAt(d.properties.index, fieldIndex);- };- const indexAccessor = f => f.properties.index;+ let filterValueAccessor;+ let dataAccessor;+ if (this.config.columnMode === COLUMN_MODE_GEOJSON) {+ filterValueAccessor = (dc, d, fieldIndex) => dc.valueAt(d.properties.index, fieldIndex);+ dataAccessor = dc => d => ({index: d.properties.index});+ } else {+ filterValueAccessor = getTableModeValueAccessor;+ dataAccessor = dc => d => ({index: d.properties.index});+ }- const dataAccessor = () => d => ({index: d.properties.index});+ const indexAccessor = f => f.properties.index;const accessors = this.getAttributeAccessors({dataAccessor, dataContainer});const isFilteredAccessor = d => {@@ -401,7 +485,7 @@ export default class GeoJsonLayer extends Layer {data,getFilterValue: gpuFilter.filterValueAccessor(dataContainer)(indexAccessor,- customFilterValueAccessor+ filterValueAccessor),getFiltered: isFilteredAccessor,...accessors@@ -437,6 +521,7 @@ export default class GeoJsonLayer extends Layer {if (this.dataToFeature.length < dataContainer.numChunks()) {// for incrementally loading data, we only load and render the latest batch; otherwise, we will load and render all batchesconst isIncrementalLoad = dataContainer.numChunks() - this.dataToFeature.length === 1;+ // TODO: add support for COLUMN_MODE_TABLE in getGeojsonLayerMetaFromArrowconst {dataToFeature, bounds, fixedRadius, featureTypes, centroids} =getGeojsonLayerMetaFromArrow({dataContainer,@@ -451,7 +536,8 @@ export default class GeoJsonLayer extends Layer {} else if (this.dataToFeature.length === 0) {const {dataToFeature, bounds, fixedRadius, featureTypes, centroids} = getGeojsonLayerMeta({dataContainer,- getFeature+ getFeature,+ config: this.config});if (centroids) this.centroids = centroids;this.dataToFeature = dataToFeature;@@ -459,10 +545,31 @@ export default class GeoJsonLayer extends Layer {}}- setInitialLayerConfig({dataContainer}) {+ setInitialLayerConfig(dataset) {+ const {dataContainer} = dataset;if (!dataContainer.numRows()) {return this;}++ // defefaultLayerProps will automatically find geojson column+ // if not found, we try to set it to id / lat /lng /ts+ if (!this.config.columns.geojson.value) {+ // find columns from lat, lng, id, and ts+ const columnConfig = detectTableColumns(dataset, this.config.columns, 'sortBy');+ if (columnConfig) {+ const columns = assignColumnsByColumnMode({+ columns: columnConfig.columns,+ supportedColumnModes: this.supportedColumnModes,+ columnMode: COLUMN_MODE_TABLE+ });+ this.updateLayerConfig({+ ...columnConfig,+ columns,+ columnMode: COLUMN_MODE_TABLE+ });+ }+ }+this.updateLayerMeta(dataContainer);const {featureTypes} = this.meta;diff --git a/src/layers/src/geojson-layer/geojson-utils.ts b/src/layers/src/geojson-layer/geojson-utils.tsindex ce41d041c..d728b9194 100644--- a/src/layers/src/geojson-layer/geojson-utils.ts+++ b/src/layers/src/geojson-layer/geojson-utils.ts@@ -1,9 +1,10 @@// SPDX-License-Identifier: MIT// Copyright contributors to the kepler.gl project-import {Feature, BBox} from 'geojson';+import {Feature, Position, BBox} from 'geojson';import normalize from '@mapbox/geojson-normalize';import bbox from '@turf/bbox';+import {ascending} from 'd3-array';import center from '@turf/center';import {AllGeoJSON} from '@turf/helpers';import {parseSync} from '@loaders.gl/core';@@ -11,12 +12,18 @@ import {WKBLoader, WKTLoader} from '@loaders.gl/wkt';import {binaryToGeometry} from '@loaders.gl/gis';import {BinaryFeatureCollection} from '@loaders.gl/schema';import {DataContainerInterface, getSampleData} from '@kepler.gl/utils';+import {ALL_FIELD_TYPES} from '@kepler.gl/constants';+import {LayerColumns} from '@kepler.gl/types';+import {KeplerTable} from '@kepler.gl/table';-import {GeojsonLayerMetaProps} from '../layer-utils';+import {LayerBaseConfig} from '../base-layer';+import {GeojsonLayerMetaProps, assignPointPairToLayerColumn} from '../layer-utils';export type GetFeature = (d: any) => Feature;export type GeojsonDataMaps = (Feature | BinaryFeatureCollection | null)[];+export const COLUMN_MODE_GEOJSON = 'geojson';+/* eslint-disable */// TODO: Re-enable eslint when we upgrade to handle enums and type mapsexport enum FeatureTypes {@@ -28,6 +35,11 @@ export enum FeatureTypes {MultiPolygon = 'MultiPolygon'}+// @ts-expect-error return type of getGeojsonFeatureTypes ?+type FeatureTypeMap = {+ [key in FeatureTypes]: boolean;+};+/* eslint-enable */export function parseGeoJsonRawFeature(rawFeature: unknown): Feature | null {@@ -61,12 +73,19 @@ export function parseGeoJsonRawFeature(rawFeature: unknown): Feature | null {export function getGeojsonLayerMeta({dataContainer,- getFeature+ getFeature,+ config}: {dataContainer: DataContainerInterface;getFeature: GetFeature;+ config: LayerBaseConfig;}): GeojsonLayerMetaProps {- const dataToFeature = getGeojsonDataMaps(dataContainer, getFeature);+ const dataToFeature =+ config.columnMode === COLUMN_MODE_GEOJSON+ ? getGeojsonDataMaps(dataContainer, getFeature)+ : // COLUMN_MODE_TABLE+ groupColumnsAsGeoJson(dataContainer, config.columns, 'sortBy');+// get bounds from featuresconst bounds = getGeojsonBounds(dataToFeature);// if any of the feature has properties.radius set to be true@@ -102,11 +121,11 @@ export function getGeojsonLayerMeta({/*** Parse raw data to GeoJson feature- * @param dataContainer- * @param getFeature- * @returns {{}}*/-export function getGeojsonDataMaps(dataContainer: any, getFeature: GetFeature): GeojsonDataMaps {+export function getGeojsonDataMaps(+ dataContainer: DataContainerInterface,+ getFeature: GetFeature+): GeojsonDataMaps {const acceptableTypes = ['Point','MultiPoint',@@ -146,7 +165,7 @@ export function getGeojsonDataMaps(dataContainer: any, getFeature: GetFeature):* @param {String} geoString* @returns {null | Object} geojson object or null if failed*/-export function parseGeometryFromString(geoString: string): Feature | null {+function parseGeometryFromString(geoString: string): Feature | null {let parsedGeo;// try parse as geojson string@@ -192,6 +211,9 @@ export function parseGeometryFromString(geoString: string): Feature | null {return normalized.features[0];}+/**+ * Get geojson bounds+ */export function getGeojsonBounds(features: GeojsonDataMaps = []): BBox | null {// 70 ms for 10,000 polygons// here we only pick couple@@ -229,8 +251,6 @@ export type DeckGlGeoTypes = {/*** Parse geojson from string- * @param {Array<Object>} allFeatures- * @returns {Object} mapping of feature type existence*/export function getGeojsonFeatureTypes(allFeatures: GeojsonDataMaps): DeckGlGeoTypes {// @ts-expect-error some test cases only have 1 geotype@@ -247,3 +267,114 @@ export function getGeojsonFeatureTypes(allFeatures: GeojsonDataMaps): DeckGlGeoTreturn featureTypes;}++type CoordsType = [number, number, number, number | null] & {+ datumIndex: number;+ datum: number[];+};++export function groupColumnsAsGeoJson(+ dataContainer: DataContainerInterface,+ columns: LayerColumns,+ sortByColumn = 'timestamp'+): Feature[] {+ const groupedById: {[key: string]: CoordsType[]} = {};+ const sortByFieldIdx = columns[sortByColumn].fieldIdx;+ const sortByRequired = !columns[sortByColumn].optional;+ for (let index = 0; index < dataContainer.numRows(); index++) {+ // note: can materialize the row+ const datum = dataContainer.rowAsArray(index);+ const id = datum[columns.id.fieldIdx];+ const lat = datum[columns.lat.fieldIdx];+ const lon = datum[columns.lng.fieldIdx];+ const altitude = columns.altitude ? datum[columns.altitude.fieldIdx] : 0;+ const sortBy = sortByFieldIdx > -1 ? datum[sortByFieldIdx] : null;+ // @ts-expect-error+ const coords: CoordsType = [lon, lat, altitude, sortBy];+ // Adding references to the original data to the coordinates array+ coords.datumIndex = index;+ coords.datum = datum;+ if (!groupedById[id]) groupedById[id] = [];++ if (+ Number.isFinite(lon) &&+ Number.isFinite(lat) &&+ (!sortByRequired || (sortByRequired && sortBy))+ ) {+ // only push points if lat,lng,and sortby exist+ groupedById[id].push(coords);+ }+ }++ const result: Feature[] = Object.entries(groupedById).map(+ ([id, items]: [string, CoordsType[]], index) => ({+ type: 'Feature' as 'Feature',+ geometry: {+ type: 'LineString' as 'LineString',+ // Sort by columns if has sortByField+ // TODO: items are expected in Position[] format?+ coordinates: (sortByFieldIdx > -1+ ? items.sort((a, b) => ascending(a[3] as any, b[3] as any))+ : items) as Position[]+ },+ properties: {+ index,+ // values are used for valueAccessor in TripLayer.formatLayerData()+ // Note: this can cause row materialization in case of non-row based containers+ values: items.map(item => dataContainer.rowAsArray(item.datumIndex))+ }+ })+ );+ return result;+}++/**+ * Find id / ts / lat / lng columns from a table and assign it to layer columns+ */+export function detectTableColumns(+ dataset: KeplerTable,+ layerColumns: LayerColumns,+ sortBy: string = 'timestamp'+) {+ const {fields, fieldPairs} = dataset;+ if (!fieldPairs.length || !fields.length) {+ return null;+ }+ // find sort by field+ const sortByFieldIdx = fields.findIndex(f => f.type === ALL_FIELD_TYPES.timestamp);+ // find id column+ const idFieldIdx = fields.findIndex(f => f.name?.toLowerCase().match(/^(id|uuid)$/g));++ if (sortByFieldIdx > -1 && idFieldIdx > -1) {+ const pointColumns = assignPointPairToLayerColumn(fieldPairs[0], true);+ return {+ columns: {+ ...Object.keys(layerColumns).reduce(+ (accu, col) => ({+ ...accu,+ [col]: pointColumns[col] ?? layerColumns[col]+ }),+ {}+ ),+ geojson: {+ value: null,+ fieldIdx: -1+ // optional: true+ },+ id: {+ value: fields[idFieldIdx].name,+ fieldIdx: idFieldIdx+ // optional: false+ },+ [sortBy]: {+ value: fields[sortByFieldIdx].name,+ fieldIdx: sortByFieldIdx+ // optional: false+ }+ },+ label: fieldPairs[0].defaultName+ };+ }++ return null;+}diff --git a/src/layers/src/index.ts b/src/layers/src/index.tsindex b0e53d0f1..47aeb3e2f 100644--- a/src/layers/src/index.ts+++ b/src/layers/src/index.ts@@ -15,7 +15,7 @@ import {default as GridLayer} from './grid-layer/grid-layer';export {pointToPolygonGeo} from './grid-layer/grid-utils';import {default as HexagonLayer} from './hexagon-layer/hexagon-layer';import {default as GeojsonLayer} from './geojson-layer/geojson-layer';-export {defaultElevation, defaultLineWidth, defaultRadius} from './geojson-layer/geojson-layer';+export {defaultElevation, defaultLineWidth, defaultRadius, COLUMN_MODE_TABLE} from './geojson-layer/geojson-layer';import {default as ClusterLayer} from './cluster-layer/cluster-layer';import {default as IconLayer} from './icon-layer/icon-layer';import {default as HeatmapLayer} from './heatmap-layer/heatmap-layer';diff --git a/src/layers/src/layer-utils.ts b/src/layers/src/layer-utils.tsindex 9ca9a0912..380ee758e 100644--- a/src/layers/src/layer-utils.ts+++ b/src/layers/src/layer-utils.ts@@ -14,7 +14,7 @@ import {import {DeckGlGeoTypes, GeojsonDataMaps} from './geojson-layer/geojson-utils';export function assignPointPairToLayerColumn(pair: FieldPair, hasAlt: boolean) {- const {lat, lng, alt} = pair.pair;+ const {lat, lng, altitude} = pair.pair;if (!hasAlt) {return {lat, lng};}@@ -24,7 +24,7 @@ export function assignPointPairToLayerColumn(pair: FieldPair, hasAlt: boolean) {return {lat,lng,- altitude: alt ? {...defaultAltColumn, ...alt} : defaultAltColumn+ altitude: altitude ? {...defaultAltColumn, ...altitude} : defaultAltColumn};}@@ -154,6 +154,7 @@ export function assignColumnsByColumnMode({columnMode: string | undefined;}): LayerColumns {const requiredColumns = getColumnModeRequiredColumns(supportedColumnModes, columnMode);+if (requiredColumns) {return Object.entries(columns).reduce((acc, [columnKey, column]) => ({diff --git a/src/layers/src/trip-layer/trip-info-modal.tsx b/src/layers/src/trip-layer/trip-info-modal.tsxindex 131468442..f63380431 100644--- a/src/layers/src/trip-layer/trip-info-modal.tsx+++ b/src/layers/src/trip-layer/trip-info-modal.tsx@@ -3,10 +3,20 @@import React from 'react';import styled from 'styled-components';+import ReactMarkdown from 'react-markdown';+import {useIntl} from 'react-intl';+import {FormattedMessage} from '@kepler.gl/localization';+import {Table} from '@kepler.gl/layers';-const StyledCode = styled.code`+const InfoModal = styled.div`+ font-size: 13px;color: ${props => props.theme.titleColorLT};++ pre {+ padding: 12px;+ background-color: #f8f8f9;+ }`;const StyledTitle = styled.div`@@ -15,50 +25,100 @@ const StyledTitle = styled.div`margin: 18px 0 14px 0;color: ${props => props.theme.titleColorLT};`;+const StyledCode = styled.code`+ color: ${props => props.theme.titleColorLT};+`;++const codeExampleGeojson = `+${'```json'}+{+ "type": "FeatureCollection",+ "features": [+ {+ "type": "Feature",+ "properties": {+ "vendor": "A",+ "vol":20+ },+ "geometry": {+ "type": "LineString",+ "coordinates": [+ [-74.20986, 40.81773, 0, 1564184363],+ [-74.20987, 40.81765, 0, 1564184396],+ [-74.20998, 40.81746, 0, 1564184409]+ ]+ }+ }+ ]+}+${'```'}+`;++const exampleTableHeader = ['id', 'latitude', 'longitude', 'timestamp'];+const exampleTabbleRows = [+ ['A', '40.81773', '-74.20986', '1564184363'],+ ['A', '40.81765', '-74.20987', '1564184396'],+ ['A', '40.81746', '-74.20998', '1564184409'],+ ['B', '40.64375', '-74.33242', '1565578213'],+ ['B', '40.64353', '-74.20987', '1565578217'],+ ['B', '40.64222', '-74.33001', '1565578243']+];++const ExampleTable = () => (+ <Table className="trip-example-table">+ <thead>+ <tr>+ {exampleTableHeader.map(v => (+ <th key={v}>{v}</th>+ ))}+ </tr>+ </thead>+ <tbody>+ {exampleTabbleRows.map((row, i) => (+ <tr key={i}>+ {row.map((v, j) => (+ <td key={j}>+ <StyledCode>{v}</StyledCode>+ </td>+ ))}+ </tr>+ ))}+ </tbody>+ </Table>+);-const TripInfoModalFactory = () => {- const TripInfoModal = () => (- <div className="trip-info-modal">- <div className="trip-info-modal__description">- <p>- <FormattedMessage id={'modal.tripInfo.description1'} />- <code>- <FormattedMessage id={'modal.tripInfo.code'} />- </code>- <FormattedMessage id={'modal.tripInfo.description2'} />- </p>- </div>- <div className="trip-info-modal__example">- <StyledTitle>- <FormattedMessage id={'modal.tripInfo.example'} />- </StyledTitle>- <pre>- <StyledCode>- {`- {- "type": "FeatureCollection",- "features": [- {- "type": "Feature",- "properties": { "vendor": "A",- "vol":20},- "geometry": {- "type": "LineString",- "coordinates": [- [-74.20986, 40.81773, 0, 1564184363],- [-74.20987, 40.81765, 0, 1564184396],- [-74.20998, 40.81746, 0, 1564184409]- ]- }- }- ]+const TripInfoModalFactory = columnMode => {+ const TripInfoModal = () => {+ const intl = useIntl();+ return (+ <InfoModal className="trip-info-modal">+ <div className="trip-info-modal__description">+ <ReactMarkdown+ children={intl.formatMessage({+ id:+ columnMode === 'geojson'+ ? 'modal.tripInfo.description1'+ : 'modal.tripInfo.descriptionTable1'+ })}+ />+ </div>+ <div className="trip-info-modal__example">+ <StyledTitle>+ <FormattedMessage+ id={+ columnMode === 'geojson' ? 'modal.tripInfo.example' : 'modal.tripInfo.exampleTable'}- `}- </StyledCode>- </pre>- </div>- </div>- );+ />+ </StyledTitle>+ {columnMode === 'geojson' ? (+ <ReactMarkdown children={codeExampleGeojson} />+ ) : (+ <ExampleTable />+ )}+ </div>+ </InfoModal>+ );+ };return TripInfoModal;};diff --git a/src/layers/src/trip-layer/trip-layer.ts b/src/layers/src/trip-layer/trip-layer.tsindex cf3a6287d..0a6fc30b3 100644--- a/src/layers/src/trip-layer/trip-layer.ts+++ b/src/layers/src/trip-layer/trip-layer.ts@@ -7,18 +7,20 @@ import uniq from 'lodash.uniq';import Layer, {LayerBaseConfig, defaultGetFieldValue} from '../base-layer';import {TripsLayer as DeckGLTripsLayer} from '@deck.gl/geo-layers';-import {GEOJSON_FIELDS, ColorRange, ALL_FIELD_TYPES} from '@kepler.gl/constants';+import {GEOJSON_FIELDS, ColorRange} from '@kepler.gl/constants';import TripLayerIcon from './trip-layer-icon';import {getGeojsonDataMaps,getGeojsonBounds,getGeojsonFeatureTypes,- GeojsonDataMaps+ GeojsonDataMaps,+ detectTableColumns,+ groupColumnsAsGeoJson} from '../geojson-layer/geojson-utils';-import {groupColumnsAsGeoJson, isTripGeoJsonField, parseTripGeoJsonTimestamp} from './trip-utils';-import {assignPointPairToLayerColumn} from '../layer-utils';+import {isTripGeoJsonField, parseTripGeoJsonTimestamp} from './trip-utils';+import {assignColumnsByColumnMode} from '../layer-utils';import TripInfoModalFactory from './trip-info-modal';import {bisectRight} from 'd3-array';import {@@ -108,8 +110,8 @@ const getTableModeFieldValue = (field, data) => {return rv;};-const COLUMN_MODE_GEOJSON = 'geojson';-const COLUMN_MODE_TABLE = 'table';+export const COLUMN_MODE_GEOJSON = 'geojson';+export const COLUMN_MODE_TABLE = 'table';const SUPPORTED_COLUMN_MODES = [{key: COLUMN_MODE_GEOJSON,@@ -134,7 +136,7 @@ export default class TripLayer extends Layer {dataToFeature: GeojsonDataMaps;dataToTimeStamp: any[];getFeature: (columns: TripLayerColumnsConfig) => (dataContainer: DataContainerInterface) => any;- _layerInfoModal: () => JSX.Element;+ _layerInfoModal: Record<string, () => JSX.Element>;constructor(props) {super(props);@@ -144,7 +146,10 @@ export default class TripLayer extends Layer {this.dataContainer = null;this.registerVisConfig(tripVisConfigs);this.getFeature = memoize(featureAccessor, featureResolver);- this._layerInfoModal = TripInfoModalFactory();+ this._layerInfoModal = {+ [COLUMN_MODE_TABLE]: TripInfoModalFactory(COLUMN_MODE_TABLE),+ [COLUMN_MODE_GEOJSON]: TripInfoModalFactory(COLUMN_MODE_GEOJSON)+ };}get supportedColumnModes() {@@ -209,10 +214,17 @@ export default class TripLayer extends Layer {return {[COLUMN_MODE_GEOJSON]: {id: 'iconInfo',- template: this._layerInfoModal,+ template: this._layerInfoModal[COLUMN_MODE_GEOJSON],modalProps: {title: 'modal.tripInfo.title'}+ },+ [COLUMN_MODE_TABLE]: {+ id: 'iconInfo',+ template: this._layerInfoModal[COLUMN_MODE_TABLE],+ modalProps: {+ title: 'modal.tripInfo.titleTable'+ }}};}@@ -259,36 +271,6 @@ export default class TripLayer extends Layer {};}- // find columns from lat, lng, id, and ts- if (fieldPairs.length) {- // find time column- const timeFieldIdx = fields.findIndex(f => f.type === ALL_FIELD_TYPES.timestamp);- // find id column- const idFieldIdx = fields.findIndex(f => f.name?.toLowerCase().match(/^(id|uuid)$/g));-- if (timeFieldIdx > -1 && idFieldIdx > -1) {- const layerProp = {- columns: {- ...assignPointPairToLayerColumn(fieldPairs[0], true),- id: {- value: fields[idFieldIdx],- fieldIdx: idFieldIdx- },- timestamp: {- value: fields[timeFieldIdx],- fieldIdx: timeFieldIdx- }- },- label: fieldPairs[0].defaultName,- columnMode: COLUMN_MODE_TABLE- };-- return {- props: [layerProp]- };- }- }-return {props: []};}@@ -358,6 +340,7 @@ export default class TripLayer extends Layer {return {};}// to-do: parse segment from dataContainer+const {dataContainer, gpuFilter} = datasets[this.config.dataId];const {data} = this.updateData(datasets, oldLayerData);@@ -406,7 +389,7 @@ export default class TripLayer extends Layer {this.dataToFeature = getGeojsonDataMaps(dataContainer, getFeature);} else {this.dataContainer = dataContainer;- this.dataToFeature = groupColumnsAsGeoJson(dataContainer, this.config.columns);+ this.dataToFeature = groupColumnsAsGeoJson(dataContainer, this.config.columns, 'timestamp');}const {dataToTimeStamp, animationDomain} = parseTripGeoJsonTimestamp(this.dataToFeature);@@ -423,10 +406,31 @@ export default class TripLayer extends Layer {this.updateMeta({bounds, featureTypes, getFeature});}- setInitialLayerConfig({dataContainer}) {+ setInitialLayerConfig(dataset) {+ const {dataContainer} = dataset;if (!dataContainer.numRows()) {return this;}++ // defefaultLayerProps will automatically find geojson column+ // if not found, we try to set it to id / lat /lng /ts+ if (!this.config.columns.geojson.value) {+ // find columns from lat, lng, id, and ts+ const columnConfig = detectTableColumns(dataset, this.config.columns);+ if (columnConfig) {+ const columns = assignColumnsByColumnMode({+ columns: columnConfig.columns,+ supportedColumnModes: this.supportedColumnModes,+ columnMode: COLUMN_MODE_TABLE+ });+ this.updateLayerConfig({+ ...columnConfig,+ columns,+ columnMode: COLUMN_MODE_TABLE+ });+ }+ }+this.updateLayerMeta(dataContainer);return this;}diff --git a/src/layers/src/trip-layer/trip-utils.ts b/src/layers/src/trip-layer/trip-utils.tsindex 2116cb291..201eaedbc 100644--- a/src/layers/src/trip-layer/trip-utils.ts+++ b/src/layers/src/trip-layer/trip-utils.ts@@ -2,9 +2,8 @@// Copyright contributors to the kepler.gl projectimport {Analyzer, DATA_TYPES} from 'type-analyzer';-import {ascending} from 'd3-array';-import {Field, LayerColumns} from '@kepler.gl/types';+import {Field} from '@kepler.gl/types';import {parseGeoJsonRawFeature, getGeojsonFeatureTypes} from '../geojson-layer/geojson-utils';import {@@ -167,50 +166,3 @@ export function getAnimationDomainFromTimestamps(dataToTimeStamp: number[][] = [[Infinity, -Infinity]);}--type GeoJsonFeature = any;-type CoordsType = number[] & {- datumIndex: number;- datum: number[];-};--export function groupColumnsAsGeoJson(- dataContainer: DataContainerInterface,- columns: LayerColumns-): GeoJsonFeature[] {- const groupedById: {[key: string]: CoordsType[]} = {};- for (let index = 0; index < dataContainer.numRows(); index++) {- // Note: this can cause row materialization in case of non-row based containers- const datum = dataContainer.rowAsArray(index) as number[];- const id = datum[columns.id.fieldIdx];- const lat = datum[columns.lat.fieldIdx];- const lon = datum[columns.lng.fieldIdx];- const altitude = columns.altitude ? datum[columns.altitude.fieldIdx] : 0;- const time = datum[columns.timestamp.fieldIdx];- // @ts-expect-error- const coords: CoordsType = [lon, lat, altitude, time];- // Adding references to the original data to the coordinates array- coords.datumIndex = index;- coords.datum = datum;- if (!groupedById[id]) groupedById[id] = [];- if (Number.isFinite(lon) && Number.isFinite(lat) && time) {- groupedById[id].push(coords);- }- }- const result = Object.entries(groupedById).map(([id, items]: [string, CoordsType[]], index) => ({- type: 'Feature',- geometry: {- type: 'LineString',- coordinates:- // Sort by time- items.sort((a, b) => ascending(a[3], b[3]))- },- properties: {- index,- // values are used for valueAccessor in TripLayer.formatLayerData()- // Note: this can cause row materialization in case of non-row based containers- values: items.map(item => dataContainer.rowAsArray(item.datumIndex))- }- }));- return result;-}diff --git a/src/localization/src/translations/en.ts b/src/localization/src/translations/en.tsindex 04dc90f33..64b1e6949 100644--- a/src/localization/src/translations/en.ts+++ b/src/localization/src/translations/en.ts@@ -381,13 +381,90 @@ export default {storage: 'Load from Storage'},tripInfo: {- title: 'How to enable trip animation',- description1:- 'In order to animate the path, the geoJSON data needs to contain `LineString` in its feature geometry, and the coordinates in the LineString need to have 4 elements in the formats of',- code: ' [longitude, latitude, altitude, timestamp] ',- description2:- 'with the last element being a timestamp. Valid timestamp formats include unix in seconds such as `1564184363` or in milliseconds such as `1564184363000`.',- example: 'Example:'+ title: 'Create trips from GeoJson',+ titleTable: 'Create trips from a list of points',+ description1: `To animate the path, the GeoJSON data needs to contain \`LineString\` in its feature geometry, and the coordinates in the LineString need to have 4 elements in the formats of+${'```json'}+[longitude, latitude, altitude, timestamp]+${'```'}+The 3rd element is a timestamp. Valid timestamp formats include unix in seconds such as \`1564184363\` or in milliseconds such as \`1564184363000\`.`,+ descriptionTable1:+ 'Trips can be created by joining a list of points from latitude and longitude, sort by timestamps and group by uniq ids.',+ example: 'Example GeoJSON',+ exampleTable: 'Example Csv'+ },+ polygonInfo: {+ title: 'Create polygon layer from GeoJSON feature',+ titleTable: 'Create path from points',+ description: `Polygon can be created from+__1 .A GeoJSON Feature Collection__+__2. A Csv contains geometry column__++### 1. Create polygon from GeoJSON file++When upload a GeoJSON file contains FeatureCollection, a polygon layer will be auto-created++Example GeoJSON+${'```json'}+{+ "type": "FeatureCollection",+ "features": [{+ "type": "Feature",+ "geometry": {+ "type": "Point",+ "coordinates": [102.0, 0.5]+ },+ "properties": {+ "prop0": "value0"+ }+ }, {+ "type": "Feature",+ "geometry": {+ "type": "LineString",+ "coordinates": [+ [102.0, 0.0],+ [103.0, 1.0],+ [104.0, 0.0],+ [105.0, 1.0]+ ]+ },+ "properties": {+ "prop0": "value0"+ }+ }]+}+${'```'}++### 2. Create polygon from a Geometry column in Csv table+Geometries (Polygons, Points, LindStrings etc) can be embedded into CSV as a \`GeoJSON\` or \`WKT\` formatted string.++#### 2.1 \`GeoJSON\` string+Example data.csv with \`GeoJSON\` string+${'```txt'}+id,_geojson+1,"{""type"":""Polygon"",""coordinates"":[[[-74.158491,40.835947],[-74.157914,40.83902]]]}"+${'```'}++#### 2.2 \`WKT\` string+Example data.csv with \`WKT\` string+[The Well-Known Text (WKT)](https://dev.mysql.com/doc/refman/5.7/en/gis-data-formats.html#gis-wkt-format) representation of geometry values is designed for exchanging geometry data in ASCII form.++Example data.csv with WKT+${'```txt'}+id,_geojson+1,"POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))"+${'```'}+`,+ descriptionTable: `Paths can be created by joining a list of points from latitude and longitude, sort by an index field (e.g. timestamp) and group by uniq ids.++ ### Layer columns:+ - **id**: - *required* - A \`id\` column is used to group by points. Points with the same id will be joined into a single path.+ - **lat**: - *required* - The latitude of the point+ - **lon**: - *required* - The longitude of the point+ - **alt**: - *optional* - The altitude of the point+ - **sort by**: - *optional* - A \`sort by\` column is used to sort the points, if not specified, points will be sorted by row index.+`,+ exampleTable: 'Example CSV'},iconInfo: {title: 'How to draw icons',@@ -446,6 +523,7 @@ export default {icon: 'icon',geojson: 'geojson',token: 'token',+ sortBy: 'sort by',arc: {lat0: 'source lat',lng0: 'source lng',diff --git a/src/reducers/src/vis-state-merger.ts b/src/reducers/src/vis-state-merger.tsindex 4c724d591..beab0b7c1 100644--- a/src/reducers/src/vis-state-merger.ts+++ b/src/reducers/src/vis-state-merger.ts@@ -800,7 +800,6 @@ export function validateLayerWithData(color: savedLayer.config.color,isVisible: savedLayer.config.isVisible,hidden: savedLayer.config.hidden,- // columns: savedLayer.config.columns,columnMode: savedLayer.config.columnMode,highlightColor: savedLayer.config.highlightColor});diff --git a/src/reducers/src/vis-state-updaters.ts b/src/reducers/src/vis-state-updaters.tsindex 9342cc5c1..105e8bd7d 100644--- a/src/reducers/src/vis-state-updaters.ts+++ b/src/reducers/src/vis-state-updaters.ts@@ -710,13 +710,10 @@ export function setInitialLayerConfig(layer, datasets, layerClasses): Layer {dataId: newLayer.config.dataId,isConfigActive: newLayer.config.isConfigActive});-- return typeof newLayer.setInitialLayerConfig === 'function'- ? newLayer.setInitialLayerConfig(dataset)- : newLayer;}-- return newLayer;+ return typeof newLayer.setInitialLayerConfig === 'function'+ ? newLayer.setInitialLayerConfig(dataset)+ : newLayer;}/*** Update layer type. Previews layer config will be copied if applicable.diff --git a/src/table/src/kepler-table.ts b/src/table/src/kepler-table.tsindex f4253427d..09171fd94 100644--- a/src/table/src/kepler-table.ts+++ b/src/table/src/kepler-table.ts@@ -541,7 +541,7 @@ export function findPointFieldPairs(fields: Field[]): FieldPair[] {},...(altIdx > -1? {- alt: {+ altitude: {fieldIdx: altIdx,value: fields[altIdx].name}diff --git a/src/types/layers.d.ts b/src/types/layers.d.tsindex 921127926..c2c3f47dc 100644--- a/src/types/layers.d.ts+++ b/src/types/layers.d.ts@@ -38,8 +38,8 @@ export type LayerColumns = {};export type ColumnPair = {- pair: string;- fieldPairKey: string;+ pair: string | string[];+ fieldPairKey: string | string[];};export type ColumnPairs = {[key: string]: ColumnPair};@@ -114,7 +114,15 @@ export type Field = {export type FieldPair = {defaultName: string;pair: {- [key: string]: {+ lat: {+ fieldIdx: number;+ value: string;+ };+ lng: {+ fieldIdx: number;+ value: string;+ };+ altitude?: {fieldIdx: number;value: string;};diff --git a/test/browser/components/side-panel/layer-configurator-test.js b/test/browser/components/side-panel/layer-configurator-test.jsindex 5421c1c69..3242e88cd 100644--- a/test/browser/components/side-panel/layer-configurator-test.js+++ b/test/browser/components/side-panel/layer-configurator-test.js@@ -138,7 +138,7 @@ test('Components -> LayerConfigurator.mount -> default prop 1', t => {t.end();});-test('Components -> LayerConfigurator.mount -> defaut prop 2', t => {+test('Components -> LayerConfigurator.mount -> LayerColumnConfig', t => {// mountconst updateLayerConfigSpy = sinon.spy();@@ -298,11 +298,6 @@ test('Components -> LayerConfigurator.mount -> collapsed / expand config group ''LayerColumnModeConfig should be expanded');- // t.equal(- // wrapper.find(LayerColumnModeConfig).length,- // 1,- // 'LayerColumnModeConfig should be expanded'- // );t.end();});diff --git a/test/browser/layer-tests/point-layer-specs.js b/test/browser/layer-tests/point-layer-specs.jsindex c49bd0731..56ad5d876 100644--- a/test/browser/layer-tests/point-layer-specs.js+++ b/test/browser/layer-tests/point-layer-specs.js@@ -40,8 +40,9 @@ test('#PointLayer -> constructor', t => {t.deepEqual(layer.columnPairs,{- lat: {pair: 'lng', fieldPairKey: 'lat'},- lng: {pair: 'lat', fieldPairKey: 'lng'}+ lat: {pair: ['lng', 'altitude'], fieldPairKey: 'lat'},+ lng: {pair: ['lat', 'altitude'], fieldPairKey: 'lng'},+ altitude: {pair: ['lng', 'lat'], fieldPairKey: 'altitude'}},'columnPairs should be correct');diff --git a/test/fixtures/state-saved-v0.js b/test/fixtures/state-saved-v0.jsindex b1d418c67..a1c7fb694 100644--- a/test/fixtures/state-saved-v0.js+++ b/test/fixtures/state-saved-v0.js@@ -1277,9 +1277,16 @@ mergedLayer4.config = {columns: {geojson: {value: '_geojson',- fieldIdx: 0- }+ fieldIdx: 0,+ optional: false+ },+ id: {value: null, fieldIdx: -1, optional: true},+ lat: {value: null, fieldIdx: -1, optional: true},+ lng: {value: null, fieldIdx: -1, optional: true},+ altitude: {value: null, fieldIdx: -1, optional: true},+ sortBy: {value: null, fieldIdx: -1, optional: true}},+ columnMode: 'geojson',highlightColor: [252, 242, 26, 255],isConfigActive: false,hidden: false,diff --git a/test/fixtures/state-saved-v1-1.js b/test/fixtures/state-saved-v1-1.jsindex 5c69f2fd1..e8002ab24 100644--- a/test/fixtures/state-saved-v1-1.js+++ b/test/fixtures/state-saved-v1-1.js@@ -857,9 +857,16 @@ mergedLayer0.config = {columns: {geojson: {fieldIdx: 1,- value: 'simplified_shape_v2'- }+ value: 'simplified_shape_v2',+ optional: false+ },+ id: {value: null, fieldIdx: -1, optional: true},+ lat: {value: null, fieldIdx: -1, optional: true},+ lng: {value: null, fieldIdx: -1, optional: true},+ altitude: {value: null, fieldIdx: -1, optional: true},+ sortBy: {value: null, fieldIdx: -1, optional: true}},+ columnMode: 'geojson',hidden: false,isVisible: true,isConfigActive: false,@@ -2685,8 +2692,14 @@ mergedLayer1.config = {label: 'stroke by pop',color: [221, 178, 124],columns: {- geojson: {value: 'simplified_shape', fieldIdx: 2}+ geojson: {value: 'simplified_shape', fieldIdx: 2, optional: false},+ id: {value: null, fieldIdx: -1, optional: true},+ lat: {value: null, fieldIdx: -1, optional: true},+ lng: {value: null, fieldIdx: -1, optional: true},+ altitude: {value: null, fieldIdx: -1, optional: true},+ sortBy: {value: null, fieldIdx: -1, optional: true}},+ columnMode: 'geojson',isVisible: true,isConfigActive: false,highlightColor: [252, 242, 26, 255],diff --git a/test/fixtures/test-trip-csv-data.js b/test/fixtures/test-trip-csv-data.jsnew file mode 100644index 000000000..70a094790--- /dev/null+++ b/test/fixtures/test-trip-csv-data.js@@ -0,0 +1,98 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {DEFAULT_TEXT_LABEL, DEFAULT_COLOR_RANGE, DEFAULT_LAYER_OPACITY} from '@kepler.gl/constants';++const gps = `timestamp,location-lng,location-lat,ground-speed,heading,name,location-alt+2014-08-01 00:00:23.000,90.2266981,27.6162803,0.22,0.0,Thuub,3217.0+2014-08-01 00:10:07.000,,,0.27,140.9,Thuub,3212.3+2014-08-01 00:20:07.000,90.2267115,27.6162102,0.27,110.1,Thuub,3209.1+2014-08-01 00:30:07.000,90.2267409,27.6162514,0.32,0.0,Thuub,3206.2+2014-08-01 00:40:07.000,90.2265545,27.6163481,0.46,0.0,Thuub,3231.3+2014-08-01 00:50:07.000,90.2269491,27.6161884,0.51,0.0,Thuub,3178.8+2014-08-01 01:00:07.000,90.2265336,27.6163122,0.3,99.61,Thuub,3257.2+2014-08-01 01:10:07.000,90.2267568,27.6162647,0.33,0.0,Thuub,3192.5+2014-08-01 01:20:07.000,90.2267296,27.6163003,0.43,0.0,Thuub,3199.2+2014-08-01 06:42:41.000,82.1138274,27.4378974,15.95,64.23,Ngang Ka,966.3+,82.1139649,27.4379582,14.9,62.91,Ngang Ka,969.9+2014-08-01 06:42:43.000,82.1140881,27.4380211,13.67,58.33,Ngang Ka,972.8+2014-08-01 06:42:44.000,82.1141907,27.4380927,12.54,48.5,Ngang Ka,974.6+2014-08-01 06:42:45.000,82.114263,27.4381765,11.44,32.11,Ngang Ka,975.2+2014-08-01 06:42:46.000,82.1143013,27.438272,11.35,13.76,Ngang Ka,975.8+2014-08-01 06:42:47.000,82.11431,27.4383692,10.8,0.66,Ngang Ka,977.7+2014-08-01 06:42:48.000,82.1142962,27.4384518,8.76,346.69,Ngang Ka,980.6+2014-08-01 06:42:49.000,82.1142637,27.4385091,6.63,325.71,Ngang Ka,983.6+2014-08-01 06:42:50.000,82.1142176,27.4385368,5.34,292.95,Ngang Ka,985.9`;++export const tripCsvDataInfo = {+ id: 'trip_csv_data',+ label: 'Trip Csv Data',+ color: [100, 100, 100]+};++// test first 2+export const expectedCoordinates = [+ [90.2266981, 27.6162803, 3217, '2014-08-01 00:00:23.000'],+ [90.2267115, 27.6162102, 3209.1, '2014-08-01 00:20:07.000']+];+expectedCoordinates[0].datumIndex = 0;+expectedCoordinates[0].datum = [+ '2014-08-01 00:00:23.000',+ 90.2266981,+ 27.6162803,+ 0.22,+ 0,+ 'Thuub',+ 3217+];++expectedCoordinates[1].datumIndex = 2;+expectedCoordinates[1].datum = [+ '2014-08-01 00:20:07.000',+ 90.2267115,+ 27.6162102,+ 0.27,+ 110.1,+ 'Thuub',+ 3209.1+];++export const expectedTripLayerConfig = {+ id: 'dont_test_me',+ type: 'trip',+ config: {+ dataId: 'trip_csv_data',+ columnMode: 'table',+ label: 'location',+ color: [130, 154, 227],+ columns: {+ id: 'name',+ lat: 'location-lat',+ lng: 'location-lng',+ timestamp: 'timestamp',+ altitude: 'location-alt'+ },+ isVisible: true,+ visConfig: {+ opacity: DEFAULT_LAYER_OPACITY,+ thickness: 2,+ colorRange: DEFAULT_COLOR_RANGE,+ trailLength: 180,+ fadeTrail: true,+ billboard: false,+ sizeRange: [0, 10]+ },+ hidden: false,+ textLabel: [DEFAULT_TEXT_LABEL]+ },+ visualChannels: {+ colorField: {+ name: 'ground-speed',+ type: 'real'+ },+ colorScale: 'quantile',+ sizeField: null,+ sizeScale: 'linear'+ }+};+export default gps;diff --git a/test/helpers/layer-utils.js b/test/helpers/layer-utils.jsindex 57c3c7fce..ead7075c1 100644--- a/test/helpers/layer-utils.js+++ b/test/helpers/layer-utils.js@@ -30,6 +30,7 @@ import csvData, {wktCsv} from '../fixtures/test-csv-data';import testLayerData, {bounds, fieldDomain, iconGeometry} from '../fixtures/test-layer-data';import {geojsonData} from '../fixtures/geojson';import tripGeoJson from '../fixtures/trip-geojson';+import {IntlWrapper} from './component-utils';import {logStep} from '../../scripts/log';@@ -85,17 +86,29 @@ export function testCreateCases(t, LayerClass, testCases) {mount(<layer.layerIcon />);}, 'layer icon should be mountable');- /*if (layer.layerInfoModal) {- t.doesNotThrow(() => {- mount(- <IntlWrapper>- <layer.layerInfoModal.template />- </IntlWrapper>- );- }, 'layer info modal should be mountable');+ if (layer.layerInfoModal.template) {+ t.doesNotThrow(() => {+ mount(+ <IntlWrapper>+ <layer.layerInfoModal.template />+ </IntlWrapper>+ );+ }, 'layer info modal should be mountable');+ } else if (Object.keys(layer.layerInfoModal).length) {+ // layerInfoModal is based on columnMode+ Object.keys(layer.layerInfoModal).forEach(mode => {+ const Template = layer.layerInfoModal[mode].template;+ t.doesNotThrow(() => {+ mount(+ <IntlWrapper>+ <Template />+ </IntlWrapper>+ );+ }, 'layer info modal should be mountable');+ });+ }}- */}if (layer && tc.test) {tc.test(layer);diff --git a/test/helpers/mock-state.js b/test/helpers/mock-state.jsindex cf69506d7..dde2f32ff 100644--- a/test/helpers/mock-state.js+++ b/test/helpers/mock-state.js@@ -37,6 +37,10 @@ import {config as tripConfig,dataId as tripDataId} from '../fixtures/test-trip-data';+import tripCsvData, {+ tripCsvDataInfo,+ expectedTripLayerConfig+} from '../fixtures/test-trip-csv-data';import tripGeojson, {tripDataInfo} from '../fixtures/trip-geojson';import {processCsvData, processGeojson} from '@kepler.gl/processors';import {MOCK_MAP_STYLE} from './mock-map-styles';@@ -145,7 +149,37 @@ function mockStateWithTripGeojson() {return updatedState;}-function mockStateWithFilters(state) {+export function mockStateWithTripTable(state) {+ const initialState = cloneDeep(state || InitialState);+ const updatedState = applyActions(keplerGlReducer, initialState, [+ {+ action: addDataToMap,+ payload: [+ {+ datasets: {info: tripCsvDataInfo, data: processCsvData(tripCsvData)},+ config: {+ version: 'v1',+ config: {+ visState: {+ layers: [expectedTripLayerConfig]+ }+ }+ }+ }+ ]+ }+ ]);++ test(t => {+ t.equal(updatedState.visState.layers.length, 1, 'should create 1 trip layer');+ t.equal(updatedState.visState.layers[0].type, 'trip', 'should create trip layer');+ t.end();+ });++ return updatedState;+}++export function mockStateWithFilters(state) {const initialState = state || mockStateWithFileUpload();const prepareState = applyActions(keplerGlReducer, initialState, [@@ -804,6 +838,7 @@ export const expectedSavedLayer2 = {columns: {geojson: '_geojson'},+ columnMode: 'geojson',hidden: false,isVisible: true,visConfig: {@@ -851,6 +886,7 @@ export const expectedLoadedLayer2 = {columns: {geojson: '_geojson'},+ columnMode: 'geojson',hidden: false,isVisible: true,visConfig: {@@ -902,6 +938,7 @@ export const StateWH3Layer = mockStateWithH3Layer();export const StateWMultiH3Layers = mockStateWithMultipleH3Layers();export const StateWithGeocoderDataset = mockStateWithGeocoderDataset();export const StateWLayerStyle = mockStateWithLayerStyling();+export const StateWTripTable = mockStateWithTripTable();export const expectedSavedTripLayer = {id: 'trip-0',@@ -914,14 +951,6 @@ export const expectedSavedTripLayer = {columnMode: 'geojson',columns: {geojson: '_geojson'- // TODO Uncomment once fixed- /*- id: null,- lat: null,- lng: null,- timestamp: null,- altitude: null- */},hidden: false,isVisible: true,diff --git a/test/node/reducers/vis-state-test.js b/test/node/reducers/vis-state-test.jsindex 73053af27..1e556d54a 100644--- a/test/node/reducers/vis-state-test.js+++ b/test/node/reducers/vis-state-test.js@@ -9,6 +9,7 @@ import {console as Console} from 'global/window';import {drainTasksForTesting, succeedTaskInTest, errorTaskInTest} from 'react-palm/tasks';import CloneDeep from 'lodash.clonedeep';+import SchemaManager from '@kepler.gl/schemas';import {VisStateActions, MapStateActions} from '@kepler.gl/actions';import {visStateReducer as reducer,@@ -21,7 +22,12 @@ import {} from '@kepler.gl/reducers';import {processCsvData, processGeojson} from '@kepler.gl/processors';-import {Layer, KeplerGlLayers} from '@kepler.gl/layers';+import {+ Layer,+ KeplerGlLayers,+ COLUMN_MODE_TABLE,+ assignColumnsByColumnMode+} from '@kepler.gl/layers';import {KeplerTable, createNewDataEntry, maybeToDate} from '@kepler.gl/table';import {createDataContainer, getDefaultFilter} from '@kepler.gl/utils';import {@@ -51,7 +57,7 @@ import {fields as geojsonFields,rows as geojsonRows} from 'test/fixtures/geojson';-+import tripCsvData, {tripCsvDataInfo, expectedCoordinates} from 'test/fixtures/test-trip-csv-data';import tripGeojson, {timeStampDomain, tripDataInfo} from 'test/fixtures/trip-geojson';import {mockPolygonFeature, mockPolygonFeature2, mockPolygonData} from 'test/fixtures/polygon';@@ -79,6 +85,7 @@ import {InitialState} from 'test/helpers/mock-state';import {getNextColorMakerValue} from 'test/helpers/layer-utils';+import {expectedTripLayerConfig} from '../../fixtures/test-trip-csv-data';const mockData = {fields: [@@ -657,6 +664,97 @@ test('#visStateReducer -> LAYER_CONFIG_CHANGE -> isVisible -> splitMaps', t => {t.end();});+test('#visStateReducer -> LAYER_CONFIG_CHANGE -> columnMode', t => {+ const initialState = InitialState.visState;+ // const initialState = cloneDeep(state || InitialState);+ const updatedState = reducer(+ initialState,+ VisStateActions.updateVisData({info: tripCsvDataInfo, data: processCsvData(tripCsvData)})+ );++ const pointLayer = updatedState.layers[0];+ // change layer type to trip+ const updatedState2 = reducer(updatedState, VisStateActions.layerTypeChange(pointLayer, 'trip'));+ // trip Layer+ const tripLayer = updatedState2.layers[0];++ const updatedColumns = assignColumnsByColumnMode({+ columns: tripLayer.config.columns,+ supportedColumnModes: tripLayer.supportedColumnModes,+ columnMode: COLUMN_MODE_TABLE+ });++ // update trip layer column mode and columns+ const nextState = reducer(+ updatedState2,+ VisStateActions.layerConfigChange(tripLayer, {+ columnMode: COLUMN_MODE_TABLE,+ columns: updatedColumns+ })+ );++ const expectedLayerConfigColumns = {+ geojson: {value: null, fieldIdx: -1, optional: true},+ id: {value: null, fieldIdx: -1, optional: false},+ lat: {value: 'location-lat', fieldIdx: 2, optional: false},+ lng: {value: 'location-lng', fieldIdx: 1, optional: false},+ timestamp: {value: null, fieldIdx: -1, optional: false},+ altitude: {value: 'location-alt', fieldIdx: 6, optional: true}+ };+ t.deepEqual(+ nextState.layers[0].config.columns,+ expectedLayerConfigColumns,+ 'should update layer columns'+ );+ t.equal(+ nextState.layers[0].config.columnMode,+ COLUMN_MODE_TABLE,+ 'should update layer columnMode'+ );++ t.deepEqual(nextState.layerData[0], {}, 'should not format layer data without all columns');+ // update trip layer column mode and columns id, timestap+ const nextState1 = reducer(+ nextState,+ VisStateActions.layerConfigChange(nextState.layers[0], {+ columns: {+ ...updatedColumns,+ timestamp: {value: 'timestamp', fieldIdx: 0, optional: true},+ id: {value: 'name', fieldIdx: 5, optional: true}+ }+ })+ );+ t.ok(nextState1.layerData[0].data, 'should format layer data with columns');+ t.equal(nextState1.layerData[0].data.length, 2, 'Should format 2 geojson features');++ t.deepEqual(+ nextState1.layerData[0].data[0].geometry.coordinates.slice(0, 2),+ expectedCoordinates,+ 'feature[0] coordinates should be correct'+ );+ t.deepEqual(+ nextState1.layerData[0].data[0].properties.index,+ 0,+ 'feature[0] properties index should be correct'+ );+ t.deepEqual(+ nextState1.layerData[0].data[0].properties.values.length,+ 8,+ 'feature[0] properties values should have correct length'+ );+ const stateToSave = SchemaManager.save({visState: nextState1});+ const savedTripLayer = stateToSave.config.config.visState.layers[0];+ t.equal(savedTripLayer.config.columnMode, COLUMN_MODE_TABLE, 'should save columnMode');+ t.deepEqual(+ savedTripLayer.config.columns,+ expectedTripLayerConfig.config.columns,+ 'should save trip layer config columns'+ );++ // console.log(JSON.stringify(stateToSave.config.config.visState.layers[0], null, 2));+ t.end();+});+test('visStateReducer -> layerDataIdChangeUpdater', t => {const initialState = CloneDeep(StateWFilesFiltersLayerColor).visState;const pointLayer = initialState.layers[0];diff --git a/test/node/utils/kepler-table-test.js b/test/node/utils/kepler-table-test.jsindex e62fccb02..833d7b0fe 100644--- a/test/node/utils/kepler-table-test.js+++ b/test/node/utils/kepler-table-test.js@@ -332,7 +332,7 @@ test('KeplerTable -> findPointFieldPairs', t => {fieldIdx: 1,value: 'point.long'},- alt: {+ altitude: {fieldIdx: 2,value: 'point.altitude'}@@ -390,7 +390,7 @@ test('KeplerTable -> findPointFieldPairs', t => {fieldIdx: 1,value: 'point_lng'},- alt: {+ altitude: {fieldIdx: 2,value: 'point_alt'}diff --git a/yarn.lock b/yarn.lockindex e32006fc2..5c032a69b 100644--- a/yarn.lock+++ b/yarn.lock@@ -2965,7 +2965,7 @@ __metadata:"@mapbox/geo-viewport": "npm:^0.4.1""@mapbox/vector-tile": "npm:^1.3.1""@types/d3-array": "npm:^2.0.0"- "@types/geojson": "npm:^7946.0.7"+ "@types/geojson": "npm:^7946.0.8""@types/lodash.memoize": "npm:^4.1.7""@types/supercluster": "npm:^7.1.0"d3-array: "npm:^2.8.0"@@ -3021,7 +3021,7 @@ __metadata:"@turf/boolean-within": "npm:^6.0.1""@turf/center": "npm:^6.0.1""@turf/helpers": "npm:^6.1.4"- "@types/geojson": "npm:^7946.0.7"+ "@types/geojson": "npm:^7946.0.8""@types/keymirror": "npm:^0.1.1""@types/lodash.memoize": "npm:^4.1.7""@types/lodash.uniq": "npm:^4.5.7"@@ -15479,7 +15479,7 @@ __metadata:"@testing-library/user-event": "npm:^14.4.3""@types/d3-array": "npm:^2.0.0""@types/d3-scale": "npm:^3.2.2"- "@types/geojson": "npm:^7946.0.7"+ "@types/geojson": "npm:^7946.0.8""@types/jsdom": "npm:^21.1.1""@types/mapbox__geo-viewport": "npm:^0.4.1""@types/redux-actions": "npm:^2.6.2"