Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[feat] vector tile layer (#2839)
Igor Dykhtae106c1927d39
diff --git a/docs/user-guides/c-types-of-layers/README.md b/docs/user-guides/c-types-of-layers/README.mdindex 5de039059..1b040c82b 100644--- a/docs/user-guides/c-types-of-layers/README.md+++ b/docs/user-guides/c-types-of-layers/README.md@@ -92,5 +92,13 @@ To use H3 layer, you need a `hex_id` in your dataset, which can be generated usiTo use S2 layer, you need to assign a column containing S2 tokens.+## Vector Tile Layer++Vector Tile Layer makes it possible to visualize very large datasets through MVTs (Mapbox Vector Tiles). To optimize performance, the layer only loads and renders tiles containing features that are visible within the current viewport.++Supported URL templates:++- MVT (https://api.mapbox.com/v4/mapbox.mapbox-streets-v8/{z}/{x}/{y}.mvt?access_token=your-mapbox-acceess-token)+- pmtiles (https://your-cdn/filename.pmtiles)[Back to table of contents](../README.md)diff --git a/examples/demo-app/src/actions.js b/examples/demo-app/src/actions.jsindex 52778ccd8..a55813dcd 100644--- a/examples/demo-app/src/actions.js+++ b/examples/demo-app/src/actions.js@@ -5,6 +5,7 @@ import {push} from 'react-router-redux';import {fetch} from 'global';import {loadFiles, toggleModal} from '@kepler.gl/actions';+import {parseUri} from '@kepler.gl/common-utils';import {load} from '@loaders.gl/core';import {CSVLoader} from '@loaders.gl/csv';import {GeoArrowLoader} from '@loaders.gl/arrow';@@ -16,7 +17,6 @@ import {LOADING_SAMPLE_LIST_ERROR_MESSAGE,MAP_CONFIG_URL} from './constants/default-settings';-import {parseUri} from './utils/url';// CONSTANTSexport const INIT = 'INIT';diff --git a/examples/demo-app/src/app.tsx b/examples/demo-app/src/app.tsxindex f1cb5e134..7fea93c76 100644--- a/examples/demo-app/src/app.tsx+++ b/examples/demo-app/src/app.tsx@@ -194,6 +194,76 @@ const App = props => {);}, [dispatch]);+ const _loadVectorTileData = useCallback(() => {+ dispatch(+ addDataToMap({+ datasets: [+ {+ info: {+ label: 'Railroads',+ id: 'railroads.pmtiles',+ color: [255, 0, 0],+ type: 'vectorTile'+ },+ data: {+ rows: [],+ fields: [+ {+ name: 'continent',+ type: 'string',+ format: '',+ analyzerType: 'STRING'+ }+ ]+ },+ metadata: {+ name: 'output.pmtiles',+ description: 'output.pmtiles',+ type: 'pmtiles',+ tilesetDataUrl:+ 'https://4sq-studio-public.s3.us-west-2.amazonaws.com/pmtiles-test/161727fe-7952-4e57-aa05-850b3086b0b2.pmtiles',+ tilesetMetadataUrl:+ 'https://4sq-studio-public.s3.us-west-2.amazonaws.com/pmtiles-test/161727fe-7952-4e57-aa05-850b3086b0b2.pmtiles',+ id: 'sz6uy1xtj',+ format: 'rows',+ label: 'output.pmtiles',+ metaJson: null,+ bounds: [-150.1122219, -51.8952777, 179.3577783, 69.6043747],+ center: [14.0625, 50.7026397, 6],+ maxZoom: 6,+ minZoom: 0,+ fields: [+ {+ name: 'continent',+ id: 'continent',+ format: '',+ filterProps: {+ domain: [+ 'Africa',+ 'Asia',+ 'Europe',+ 'North America',+ 'Oceania',+ 'South America'+ ],+ value: [],+ type: 'multiSelect',+ gpu: false+ },+ type: 'string',+ analyzerType: 'STRING'+ }+ ]+ }+ }+ ],+ options: {+ autoCreateLayers: true+ }+ })+ );+ }, [dispatch]);+const _loadPointData = useCallback(() => {dispatch(addDataToMap({@@ -427,6 +497,7 @@ const App = props => {// _loadScenegraphLayer();// _loadGpsData();// _loadRowData();+ // _loadVectorTileData();// eslint-disable-next-line react-hooks/exhaustive-deps}, [_loadPointData,@@ -438,7 +509,8 @@ const App = props => {_loadScenegraphLayer,_loadGpsData,_loadRowData,- _replaceData+ _replaceData,+ _loadVectorTileData]);return (diff --git a/examples/demo-app/src/factories/load-data-modal.js b/examples/demo-app/src/factories/load-data-modal.jsindex 3a964e4b6..110006ab3 100644--- a/examples/demo-app/src/factories/load-data-modal.js+++ b/examples/demo-app/src/factories/load-data-modal.js@@ -29,6 +29,7 @@ const CustomLoadDataModalFactory = (...deps) => {// add more loading methodsconst loadingMethods = [defaultLoadingMethods.find(lm => lm.id === 'upload'),+ defaultLoadingMethods.find(lm => lm.id === 'tileset'),additionalMethods.remote,defaultLoadingMethods.find(lm => lm.id === 'storage'),additionalMethods.samplediff --git a/examples/demo-app/src/utils/url.js b/examples/demo-app/src/utils/url.jsindex 35ad95c51..e0e380e6c 100644--- a/examples/demo-app/src/utils/url.js+++ b/examples/demo-app/src/utils/url.js@@ -1,57 +1,6 @@// SPDX-License-Identifier: MIT// Copyright contributors to the kepler.gl project-// from http://blog.stevenlevithan.com/archives/parseuri-/**- * Allows to break down a url into multiple params- * @param str- */-export function parseUri(str) {- const o = parseUri.options;- const m = o.parser[o.strictMode ? 'strict' : 'loose'].exec(str);- const uri = {};- let i = 14;-- while (i--) uri[o.key[i]] = m[i] || '';-- uri[o.q.name] = {};- uri[o.key[12]].replace(o.q.parser, ($0, $1, $2) => {- if ($1) uri[o.q.name][$1] = $2;- });-- return uri;-}--parseUri.options = {- strictMode: false,- key: [- 'source',- 'protocol',- 'authority',- 'userInfo',- 'user',- 'password',- 'host',- 'port',- 'relative',- 'path',- 'directory',- 'file',- 'query',- 'anchor'- ],- q: {- name: 'queryKey',- parser: /(?:^|&)([^&=]*)=?([^&]*)/g- },- parser: {- strict:- /^(?:([^:/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:/?#]*)(?::(\d*))?))?((((?:[^?#/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,- loose:- /^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#/]*\.[^?#/.]+(?:[?#]|$)))*\/?)?([^?#/]*))(?:\?([^#]*))?(?:#(.*))?)/- }-};-/*** Validates an url* @param strdiff --git a/src/common-utils/src/data.ts b/src/common-utils/src/data.tsindex 18bf7ba48..44c62dc27 100644--- a/src/common-utils/src/data.ts+++ b/src/common-utils/src/data.ts@@ -1,6 +1,10 @@// SPDX-License-Identifier: MIT// Copyright contributors to the kepler.gl project+import {Analyzer, DATA_TYPES} from 'type-analyzer';++import {Field} from '@kepler.gl/types';+export function notNullorUndefined<T extends NonNullable<any>>(d: T | null | undefined): d is T {return d !== undefined && d !== null;}@@ -45,3 +49,23 @@ export function toArray<T>(item: T | T[]): T[] {return [item];}++/**+ * Check whether geojson linestring's 4th coordinate is 1) not timestamp 2) unix time stamp 3) real date time+ * @param timestamps array to be tested if its elements are timestamp+ * @returns the type of timestamp: unix/datetime/invalid(not timestamp)+ */+export function containValidTime(timestamps: string[]): Field | null {+ const formattedTimeStamps = timestamps.map(ts => ({ts}));+ const ignoredDataTypes = Object.keys(DATA_TYPES).filter(+ type => ![DATA_TYPES.TIME, DATA_TYPES.DATETIME, DATA_TYPES.DATE].includes(type)+ );++ // ignore all types but TIME to improve performance+ const analyzedType = Analyzer.computeColMeta(formattedTimeStamps, [], {ignoredDataTypes})[0];++ if (!analyzedType || analyzedType.category !== 'TIME') {+ return null;+ }+ return analyzedType;+}diff --git a/src/common-utils/src/index.ts b/src/common-utils/src/index.tsindex 05510daf3..1f45e1e35 100644--- a/src/common-utils/src/index.ts+++ b/src/common-utils/src/index.ts@@ -4,6 +4,7 @@export * from './data';export * from './data-type';export * from './string';+export * from './url';export {getCentroid, getHexFields, h3IsValid, idToPolygonGeo} from './h3-utils';export type {Centroid} from './h3-utils';diff --git a/src/common-utils/src/url.ts b/src/common-utils/src/url.tsnew file mode 100644index 000000000..02c25e373--- /dev/null+++ b/src/common-utils/src/url.ts@@ -0,0 +1,54 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++/**+ * Allows to break down a url into multiple params+ * from http://blog.stevenlevithan.com/archives/parseuri+ */+export function parseUri(str: string): {[key: string]: any} {+ const o = parseUri.options;+ const m = o.parser[o.strictMode ? 'strict' : 'loose'].exec(str);+ const uri = {};+ let i = 14;++ while (i--) uri[o.key[i]] = m?.[i] || '';++ uri[o.q.name] = {};+ uri[o.key[12]].replace(o.q.parser, ($0, $1, $2) => {+ if ($1) uri[o.q.name][$1] = $2;+ });++ return uri;+}++parseUri.options = {+ strictMode: false,+ key: [+ 'source',+ 'protocol',+ 'authority',+ 'userInfo',+ 'user',+ 'password',+ 'host',+ 'port',+ 'relative',+ 'path',+ 'directory',+ 'file',+ 'query',+ 'anchor'+ ],+ q: {+ name: 'queryKey',+ parser: /(?:^|&)([^&=]*)=?([^&]*)/g+ },+ parser: {+ strict:+ // eslint-disable-next-line no-useless-escape+ /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,+ loose:+ // eslint-disable-next-line no-useless-escape+ /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/+ }+};diff --git a/src/components/src/hooks/use-fetch-vector-tile-metadata.ts b/src/components/src/hooks/use-fetch-vector-tile-metadata.tsnew file mode 100644index 000000000..574465649--- /dev/null+++ b/src/components/src/hooks/use-fetch-vector-tile-metadata.ts@@ -0,0 +1,90 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {useCallback, useEffect, useState} from 'react';++import {MVTSource, TileJSON} from '@loaders.gl/mvt';+import {PMTilesSource, PMTilesMetadata} from '@loaders.gl/pmtiles';++import {VectorTileMetadata, VectorTileType} from '@kepler.gl/layers';++type FetchVectorTileMetadataProps = {+ url: string | null;+ type: VectorTileType;+ process?: (json: PMTilesMetadata | TileJSON) => VectorTileMetadata | Error | null;+};++const DEFAULT_PROCESS_FUNCTION = (json: PMTilesMetadata | TileJSON): VectorTileMetadata => {+ return {+ metaJson: null,+ bounds: null,+ center: null,+ maxZoom: null,+ minZoom: null,+ fields: [],+ ...json+ };+};++type FetchVectorTileMetadataReturn = {+ data: VectorTileMetadata | null;+ loading: boolean;+ error: Error | null;+};++/** Hook to fetch and return mvt or pmtiles metadata. */+export default function useFetchVectorTileMetadata({+ type,+ url,+ process = DEFAULT_PROCESS_FUNCTION+}: FetchVectorTileMetadataProps): FetchVectorTileMetadataReturn {+ const [error, setError] = useState<Error | null>(null);+ const [data, setData] = useState<VectorTileMetadata | null>(null);+ const [loading, setLoading] = useState<boolean>(false);++ const setProcessedData = useCallback(+ (value: PMTilesMetadata | TileJSON | null) => {+ if (!value) {+ return setError(value);+ }+ const processedData = process(value);+ if (processedData instanceof Error) {+ setError(processedData);+ } else {+ setData(processedData);+ }+ },+ [setError, setData, process]+ );++ useEffect(() => {+ const getAndProcessMetadata = async () => {+ setError(null);+ setData(null);+ if (url) {+ setLoading(true);++ try {+ const tileSource =+ type === VectorTileType.MVT+ ? MVTSource.createDataSource('', {+ mvt: {+ metadataUrl: decodeURIComponent(url)+ }+ })+ : PMTilesSource.createDataSource(decodeURIComponent(url), {});++ const metadata = await tileSource.metadata;+ setProcessedData(metadata);+ } catch (metadataError) {+ setError(metadataError as any);+ }+ setLoading(false);+ }+ };++ getAndProcessMetadata();+ }, [url, type, setProcessedData]);++ return {data, loading, error};+}diff --git a/src/components/src/hooks/use-on-click-outside.tsx b/src/components/src/hooks/use-on-click-outside.tsxindex 61dd5548b..f9b15c912 100644--- a/src/components/src/hooks/use-on-click-outside.tsx+++ b/src/components/src/hooks/use-on-click-outside.tsx@@ -1,8 +1,6 @@// SPDX-License-Identifier: MIT// Copyright contributors to the kepler.gl project-// Copyright 2022 Foursquare Labs, Inc. All Rights Reserved.-import document from 'global/document';import {useCallback, useEffect, useRef, MutableRefObject} from 'react';export default function useOnClickOutside<T extends HTMLElement>(diff --git a/src/components/src/map-container.tsx b/src/components/src/map-container.tsxindex 7a84e01f2..3288aab3f 100644--- a/src/components/src/map-container.tsx+++ b/src/components/src/map-container.tsx@@ -465,9 +465,9 @@ export default function MapContainerFactory(this.props.visStateActions.onLayerHover(info, this.props.index);};- _onLayerSetDomain = (idx: number, colorDomain: VisualChannelDomain) => {+ _onLayerSetDomain = (idx: number, colorDomain: {domain: VisualChannelDomain}) => {this.props.visStateActions.layerConfigChange(this.props.visState.layers[idx], {- colorDomain+ colorDomain: colorDomain.domain} as Partial<LayerBaseConfig>);};diff --git a/src/components/src/modal-container.tsx b/src/components/src/modal-container.tsxindex ac80c4e3b..4c32c96bb 100644--- a/src/components/src/modal-container.tsx+++ b/src/components/src/modal-container.tsx@@ -6,8 +6,7 @@ import {css} from 'styled-components';import get from 'lodash.get';import document from 'global/document';-import ModalDialogFactory from './modals/modal-dialog';-import {exportHtml, exportMap, exportJson, exportImage} from '@kepler.gl/utils';+import {ALL_FIELD_TYPES} from '@kepler.gl/constants';import {exportData,getFileFormatNames,@@ -15,6 +14,9 @@ import {MapStyle,ProviderState} from '@kepler.gl/reducers';+import {exportHtml, exportMap, exportJson, exportImage} from '@kepler.gl/utils';++import ModalDialogFactory from './modals/modal-dialog';// modalsimport DeleteDatasetModalFactory from './modals/delete-data-modal';@@ -173,6 +175,31 @@ export default function ModalContainerFactory(this.props.visStateActions.loadFiles(fileList);};+ _onTilesetAdded = (+ tileset: {name: string; type: string; metadata: Record<string, any>},+ processedMetadata?: Record<string, any>+ ) => {+ this.props.visStateActions.updateVisData(+ {+ info: {label: tileset.name, type: tileset.type, format: 'rows'},+ data: {+ fields: processedMetadata?.fields || [],+ rows: []+ },+ metadata: {+ ...processedMetadata,+ ...tileset.metadata+ },+ // vector tile layer only supports gpu filtering for now+ supportedFilterTypes: [ALL_FIELD_TYPES.real, ALL_FIELD_TYPES.integer]+ },+ {+ autoCreateLayers: true+ }+ );+ this._closeModal();+ };+_onExportImage = () => {if (!this.props.uiState.exportImage.processing) {exportImage(this.props.uiState.exportImage, `${this.props.appName}.png`);@@ -327,6 +354,7 @@ export default function ModalContainerFactory({...providerState}onClose={this._closeModal}onFileUpload={this._onFileUpload}+ onTilesetAdded={this._onTilesetAdded}onLoadCloudMap={this._onLoadCloudMap}loadFiles={uiState.loadFiles}fileLoading={visState.fileLoading}diff --git a/src/components/src/modals/export-data-modal.tsx b/src/components/src/modals/export-data-modal.tsxindex 99711ec88..167cee317 100644--- a/src/components/src/modals/export-data-modal.tsx+++ b/src/components/src/modals/export-data-modal.tsx@@ -12,7 +12,9 @@ import {StyledType,CheckMark} from '../common/styled-components';+import {StyledWarning} from './export-map-modal/components';import {injectIntl, IntlShape} from 'react-intl';+import {DatasetType} from '@kepler.gl/layers';import {FormattedMessage} from '@kepler.gl/localization';import {Datasets} from '@kepler.gl/table';@@ -88,6 +90,14 @@ const ExportDataModalFactory = () => {intl} = this.props;+ const exportAllDatasets = selectedDataset ? !datasets[selectedDataset] : true;+ const showVectorTileWarning = Object.keys(datasets).some(datasetId => {+ return (+ datasets[datasetId].type === DatasetType.VECTOR_TILE &&+ (selectedDataset === datasetId || exportAllDatasets)+ );+ });+return (<StyledModalContent className="export-data-modal"><div>@@ -172,6 +182,13 @@ const ExportDataModalFactory = () => {</StyledFilteredOption></div></StyledExportSection>+ {showVectorTileWarning ? (+ <div className="title">+ <StyledWarning>+ <FormattedMessage id={'modal.exportData.vectorTileWarning'} />+ </StyledWarning>+ </div>+ ) : null}</div></StyledModalContent>);diff --git a/src/components/src/modals/load-data-modal.tsx b/src/components/src/modals/load-data-modal.tsxindex 51d418f05..7fb9148a2 100644--- a/src/components/src/modals/load-data-modal.tsx+++ b/src/components/src/modals/load-data-modal.tsx@@ -8,14 +8,13 @@ import {IntlShape, useIntl} from 'react-intl';import FileUploadFactory from '../common/file-uploader/file-upload';import LoadStorageMapFactory from './load-storage-map';+import LoadTilesetFactory from './tilesets-modals/load-tileset';import ModalTabsFactory from './modal-tabs';import LoadingDialog from './loading-dialog';import {LOADING_METHODS} from '@kepler.gl/constants';import {FileLoading, FileLoadingProgress, LoadFiles} from '@kepler.gl/types';-/** @typedef {import('./load-data-modal').LoadDataModalProps} LoadDataModalProps */-const StyledLoadDataModal = styled.div.attrs({className: 'load-data-modal'})`@@ -41,6 +40,10 @@ type LoadDataModalProps = {// call backsonFileUpload: (files: File[]) => void;onLoadCloudMap: (provider: any, vis: any) => void;+ onTilesetAdded: (+ tileset: {name: string; type: string; metadata: Record<string, any>},+ processedMetadata?: Record<string, any>+ ) => void;fileLoading: FileLoading | false;loadingMethods?: LoadingMethod[];/** A list of names of supported formats suitable to present to user */@@ -56,12 +59,18 @@ type LoadDataModalProps = {fileLoadingProgress: FileLoadingProgress;};-LoadDataModalFactory.deps = [ModalTabsFactory, FileUploadFactory, LoadStorageMapFactory];+LoadDataModalFactory.deps = [+ ModalTabsFactory,+ FileUploadFactory,+ LoadStorageMapFactory,+ LoadTilesetFactory+];export function LoadDataModalFactory(ModalTabs: ReturnType<typeof ModalTabsFactory>,FileUpload: ReturnType<typeof FileUploadFactory>,- LoadStorageMap: ReturnType<typeof LoadStorageMapFactory>+ LoadStorageMap: ReturnType<typeof LoadStorageMapFactory>,+ LoadTileset: ReturnType<typeof LoadTilesetFactory>) {const defaultLoadingMethods = [{@@ -69,6 +78,11 @@ export function LoadDataModalFactory(label: 'modal.loadData.upload',elementType: FileUpload},+ {+ id: LOADING_METHODS.tileset,+ label: 'modal.loadData.tileset',+ elementType: LoadTileset+ },{id: LOADING_METHODS.storage,label: 'modal.loadData.storage',@@ -80,6 +94,7 @@ export function LoadDataModalFactory(defaultLoadingMethods: LoadDataModalProps['loadingMethods'];} = ({onFileUpload = noop,+ onTilesetAdded = noop,fileLoading = false,loadingMethods = defaultLoadingMethods,isCloudMapLoading,@@ -89,6 +104,7 @@ export function LoadDataModalFactory(const currentModalProps = {...restProps,onFileUpload,+ onTilesetAdded,fileLoading,isCloudMapLoading};diff --git a/src/components/src/modals/tilesets-modals/common.tsx b/src/components/src/modals/tilesets-modals/common.tsxnew file mode 100644index 000000000..10dca23de--- /dev/null+++ b/src/components/src/modals/tilesets-modals/common.tsx@@ -0,0 +1,17 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {VectorTileMetadata} from '@kepler.gl/layers';++export type DatasetCreationAttributes = {+ name: string;+ type: string;+ metadata: Record<string, any>;+};++export type MetaResponse = {+ metadata?: VectorTileMetadata | null;+ dataset?: DatasetCreationAttributes | null;+ loading?: boolean;+ error?: Error | null;+};diff --git a/src/components/src/modals/tilesets-modals/load-data-footer.tsx b/src/components/src/modals/tilesets-modals/load-data-footer.tsxnew file mode 100644index 000000000..11cb971b8--- /dev/null+++ b/src/components/src/modals/tilesets-modals/load-data-footer.tsx@@ -0,0 +1,78 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import React from 'react';+import styled from 'styled-components';++import {Button} from '../../common';++const AddDataButton = styled(Button)<{isLoading?: boolean}>`+ position: relative;+ display: inline-flex;+ align-items: center;+ opacity: ${props => (props.disabled ? 0.6 : 1)};+ height: 40px;+ padding: 9px 32px;+ width: auto;++ svg {+ margin-right: ${props => (props.isLoading ? '0' : '6px')};+ }+`;++const LoadDataFooterContainer = styled.div.attrs({+ className: 'load-data-footer'+})`+ /* position: absolute; */+ width: 100%;+ bottom: 0;++ display: flex;+ justify-content: space-between;+ padding: 21px 21px 0px 72px;+ margin: 24px -72px 0;+ align-items: center;+`;++const ErrorContainer = styled.div`+ color: red;+ padding-right: 15px;+`;++type LoadDataFooterProps = {+ disabled?: boolean;+ isLoading?: boolean;+ onConfirm: () => void;+ confirmText: string;+ prependText?: string;+ errorText?: string;+};++const LoadDataFooter: React.FC<LoadDataFooterProps> = ({+ disabled,+ isLoading,+ onConfirm,+ confirmText,+ prependText = '',+ errorText = ''+}) => {+ return (+ <LoadDataFooterContainer>+ <div>+ {errorText && <ErrorContainer>{errorText}</ErrorContainer>}+ {prependText}+ <AddDataButton+ disabled={disabled}+ isLoading={isLoading}+ onClick={onConfirm}+ width="100px"+ cta+ >+ {confirmText}+ </AddDataButton>+ </div>+ </LoadDataFooterContainer>+ );+};++export default LoadDataFooter;diff --git a/src/components/src/modals/tilesets-modals/load-tileset.tsx b/src/components/src/modals/tilesets-modals/load-tileset.tsxnew file mode 100644index 000000000..bbef3a9a3--- /dev/null+++ b/src/components/src/modals/tilesets-modals/load-tileset.tsx@@ -0,0 +1,173 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import React, {useCallback, useMemo, useState} from 'react';+import {IntlShape} from 'react-intl';+import JSONPretty from 'react-json-pretty';+import {AutoSizer} from 'react-virtualized';+import styled from 'styled-components';++import {VectorTileIcon} from '@kepler.gl/layers';+import {getError} from '@kepler.gl/utils';++import {MetaResponse} from './common';+import LoadDataFooter from './load-data-footer';+// import TilesetIcon from './tileset-icon';+import TilesetVectorForm from './tileset-vector-form';++const WIDTH_ICON = '62px';++const LoadTilesetTabContainer = styled.div`+ color: ${props => props.theme.AZURE};+`;++const Container = styled.div`+ display: grid;+ grid-template-columns: repeat(2, minmax(0, 1fr));+ background-color: ${props => props.theme.WHITE};+`;++// @ts-expect-error+const TilesetTypeContainer = styled.div`+ display: grid;+ grid-template-columns: repeat(4, ${WIDTH_ICON});+`;++const MetaContainer = styled.div`+ display: flex;+ max-height: 400px;+ background-color: ${({theme}) => theme.editorBackground};+`;++export interface MetaInnerContainerProps {+ width: number;+ height: number;+}++const MetaInnerContainer = styled.div<MetaInnerContainerProps>`+ position: relative;+ border: 1px solid ${props => props.theme.selectBorderColorLT};+ background-color: white;+ border-radius: 2px;+ display: inline-block;+ font: inherit;+ line-height: 1.5em;+ padding: 0.5em 3.5em 0.5em 1em;+ margin-left: 10px;+ box-sizing: border-box;+ overflow-y: scroll;+ overflow-x: auto;+ white-space: pre-wrap;+ word-wrap: break-word;+ height: ${props => props.height}px;+ width: ${props => props.width}px;+ max-width: 600px;+`;++const StyledHeaderMessage = styled.div`+ color: ${props => props.theme.textColorLT};+ font-size: 14px;+ margin-bottom: 12px;+`;++type LoadTilesetTabProps = {+ meta: {[key: string]: any};+ isAddingDatasets: boolean;+ onTilesetAdded: (tilesetInfo: any, metadata?: any) => void;+ intl: IntlShape;+};++const TILE_TYPES = [+ {+ id: 'vectorTile',+ label: 'Vector Tile',+ Icon: VectorTileIcon,+ Component: TilesetVectorForm+ }+];++function isReady(response) {+ return response.dataset && !response.loading && !response.error;+}++function LoadTilesetTabFactory() {+ const LoadTilesetTab: React.FC<LoadTilesetTabProps> = ({+ onTilesetAdded,+ isAddingDatasets,+ intl+ }) => {+ // @ts-expect-error+ const [typeIndex, setTypeIndex] = useState<number>(0);+ const [response, setResponse] = useState<MetaResponse>({});++ const error = response.error;+ const loading = response.loading;+ const data = response.metadata;+ const jsonDataText = useMemo(() => JSON.stringify(data, null, 2), [data]);++ const createTileDataset = useCallback(() => {+ const {dataset, metadata} = response;+ if (dataset) {+ onTilesetAdded(dataset, metadata);+ }+ }, [onTilesetAdded, response]);++ const CurrentForm = TILE_TYPES[typeIndex].Component;++ const tilesetHeaderMessage = `${intl.formatMessage({+ id: 'tilesetSetup.header'+ })}`;++ return (+ <LoadTilesetTabContainer>+ <Container>+ <div>+ <StyledHeaderMessage>{tilesetHeaderMessage}</StyledHeaderMessage>+ {/**+ <div>+ <label htmlFor="tileset-type">Tileset Type</label>+ </div>+ * Enable once we support different vector layers+ <TilesetTypeContainer className="tileset-type">+ {TILE_TYPES.map((tileType, index) => (+ <TilesetIcon+ key={tileType.label}+ name={tileType.label}+ Icon={<tileType.Icon height={WIDTH_ICON} />}+ onClick={() => setTypeIndex(index)}+ selected={typeIndex === index}+ />+ ))}+ </TilesetTypeContainer>+ */}+ <div>+ <CurrentForm setResponse={setResponse} />+ {error && <div>{getError(error)}</div>}+ </div>+ </div>+ <MetaContainer>+ {data && (+ <AutoSizer>+ {({height, width}) => (+ <MetaInnerContainer height={height} width={width}>+ <JSONPretty id="json-pretty" json={jsonDataText} />+ </MetaInnerContainer>+ )}+ </AutoSizer>+ )}+ </MetaContainer>+ </Container>+ <LoadDataFooter+ disabled={Boolean(error) || !isReady(response)}+ isLoading={loading || isAddingDatasets}+ onConfirm={createTileDataset}+ confirmText="Add Tileset"+ />+ </LoadTilesetTabContainer>+ );+ };++ return LoadTilesetTab;+}++export default LoadTilesetTabFactory;diff --git a/src/components/src/modals/tilesets-modals/tileset-icon.tsx b/src/components/src/modals/tilesets-modals/tileset-icon.tsxnew file mode 100644index 000000000..1a83bfab7--- /dev/null+++ b/src/components/src/modals/tilesets-modals/tileset-icon.tsx@@ -0,0 +1,37 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import React from 'react';+import styled from 'styled-components';++const TileIconContainer = styled.div`+ display: flex;+ flex-direction: column;+ text-align: center;+ cursor: pointer;+`;++type TileIconProps = {+ selected?: boolean;+};++const TileIcon = styled.div<TileIconProps>`+ background-color: ${props => props.theme.GREY10};+ color: ${props => (props.selected ? props.theme.BLUE : props.theme.AZURE200)};+`;++type TilesetIconProps = {+ Icon: React.ReactNode;+ name: string;+ onClick: () => void;+ selected?: boolean;+};++const TilesetIcon: React.FC<TilesetIconProps> = ({Icon, name, onClick, selected = false}) => (+ <TileIconContainer onClick={onClick}>+ <TileIcon selected={selected}>{Icon}</TileIcon>+ <div>{name}</div>+ </TileIconContainer>+);++export default TilesetIcon;diff --git a/src/components/src/modals/tilesets-modals/tileset-vector-form.tsx b/src/components/src/modals/tilesets-modals/tileset-vector-form.tsxnew file mode 100644index 000000000..92c9a0430--- /dev/null+++ b/src/components/src/modals/tilesets-modals/tileset-vector-form.tsx@@ -0,0 +1,186 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import React, {useCallback, useEffect, useState, useMemo} from 'react';+import styled from 'styled-components';++import {+ getMetaUrl,+ parseVectorMetadata,+ VectorTileMetadata,+ DatasetType,+ VectorTileType+} from '@kepler.gl/layers';+import {TileJSON} from '@loaders.gl/mvt';+import {PMTilesMetadata} from '@loaders.gl/pmtiles';++import {default as useFetchVectorTileMetadata} from '../../hooks/use-fetch-vector-tile-metadata';+import {DatasetCreationAttributes, MetaResponse} from './common';+import {InputLight} from '../../common';++const TilesetInputContainer = styled.div`+ display: grid;+ grid-template-rows: repeat(3, 1fr);+ row-gap: 18px;+ font-size: 12px;+`;++const TilesetInputDescription = styled.div`+ text-align: center;+ color: ${props => props.theme.AZURE200};+ font-size: 11px;+`;++export type VectorTilesetFormData = {+ name: string;+ dataUrl: string;+ metadataUrl?: string;+};++const isPMTilesUrl = (url?: string | null)=> url?.includes('.pmtiles')++export function getDatasetAttributesFromVectorTile({+ name,+ dataUrl,+ metadataUrl+}: VectorTilesetFormData): DatasetCreationAttributes {+ return {+ name,+ type: DatasetType.VECTOR_TILE,+ metadata: {+ type: isPMTilesUrl(dataUrl) ? VectorTileType.PMTILES : VectorTileType.MVT,+ tilesetDataUrl: dataUrl,+ tilesetMetadataUrl: metadataUrl+ }+ };+}++type TilesetVectorFormProps = {+ setResponse: (response: MetaResponse) => void;+};++const TilesetVectorForm: React.FC<TilesetVectorFormProps> = ({setResponse}) => {+ const [tileName, setTileName] = useState<string>('');+ const [tileUrl, setTileUrl] = useState<string>('');+ const [metadataUrl, setMetadataUrl] = useState<string | null>('');++ const onTileNameChange = useCallback(+ (event: React.ChangeEvent<HTMLInputElement>) => {+ event.preventDefault();+ setTileName(event.target.value);+ },+ [setTileName]+ );++ const onTileMetaUrlChange = useCallback(+ (event: React.ChangeEvent<HTMLInputElement>) => {+ event.preventDefault();+ setMetadataUrl(event.target.value);+ },+ [setMetadataUrl]+ );++ const onTileUrlChange = useCallback(+ async (event: React.ChangeEvent<HTMLInputElement>) => {+ event.preventDefault();+ const newTileUrl = event.target.value;+ setTileUrl(newTileUrl);+ const potentialMetadataUrl = isPMTilesUrl(newTileUrl)+ ? newTileUrl+ : getMetaUrl(newTileUrl);+ if (!metadataUrl && potentialMetadataUrl) {+ // check if URL exists before setting it as the metadata URL+ const resp = await fetch(potentialMetadataUrl);+ if (resp.ok) setMetadataUrl(potentialMetadataUrl);+ }+ if (!tileName) {+ setTileName(newTileUrl.split('/').pop() || newTileUrl);+ }+ },+ [setTileUrl, tileName, setMetadataUrl, metadataUrl]+ );+ const process = useMemo(() => {+ return (value: PMTilesMetadata | TileJSON) =>+ parseVectorMetadata(value, {tileUrl: metadataUrl});+ }, [metadataUrl]);++ const {+ data: metadata,+ loading,+ error: metaError+ } = useFetchVectorTileMetadata({+ url: metadataUrl,+ type: isPMTilesUrl(metadataUrl) ? VectorTileType.PMTILES : VectorTileType.MVT,+ process+ });++ useEffect(() => {+ if (tileName && tileUrl) {+ const dataset = getDatasetAttributesFromVectorTile({+ name: tileName,+ dataUrl: tileUrl,+ metadataUrl: metadataUrl ?? undefined+ });+ setResponse({+ metadata,+ dataset,+ loading,+ error: metaError+ });+ } else {+ setResponse({+ metadata,+ dataset: null,+ loading,+ error: metaError+ });+ }+ }, [setResponse, metadata, loading, metaError, tileUrl, tileName, metadataUrl]);++ useEffect(() => {+ if (metadata) {+ setTileName((metadata as VectorTileMetadata).name || '');+ }+ }, [metadata]);++ return (+ <TilesetInputContainer>+ <div>+ <label htmlFor="tileset-name">Name</label>+ <InputLight+ id="tile-name"+ placeholder="Name your tileset"+ value={tileName}+ onChange={onTileNameChange}+ />+ </div>+ <div>+ <label htmlFor="tile-url">Tileset URL</label>+ <InputLight+ id="tile-url"+ placeholder="Tileset URL"+ value={tileUrl}+ onChange={onTileUrlChange}+ />+ <TilesetInputDescription>+ Requires {x}, {y}, {z} placeholders in URL or .pmtile+ extension.+ </TilesetInputDescription>+ </div>+ <div>+ <label htmlFor="tile-metadata">Tileset metadata URL</label>+ <InputLight+ id="tile-metadata"+ placeholder="Tileset metadata"+ value={metadataUrl ?? undefined}+ onChange={onTileMetaUrlChange}+ />+ <TilesetInputDescription>+ Optional, but recommended. Supports json, txt+ </TilesetInputDescription>+ </div>+ </TilesetInputContainer>+ );+};++export default TilesetVectorForm;diff --git a/src/components/src/side-panel/common/dataset-info.tsx b/src/components/src/side-panel/common/dataset-info.tsxindex ae7d6d5e1..bb7e689b6 100644--- a/src/components/src/side-panel/common/dataset-info.tsx+++ b/src/components/src/side-panel/common/dataset-info.tsx@@ -4,6 +4,8 @@import React from 'react';import styled from 'styled-components';import {format} from 'd3-format';++import {DatasetType} from '@kepler.gl/layers';import {FormattedMessage} from '@kepler.gl/localization';import {DataContainerInterface} from '@kepler.gl/utils';@@ -11,6 +13,7 @@ const numFormat = format(',');type MiniDataset = {dataContainer: DataContainerInterface;+ type?: string;};export type DatasetInfoProps = {@@ -27,7 +30,11 @@ export default function DatasetInfoFactory() {const DatasetInfo: React.FC<DatasetInfoProps> = ({dataset}: DatasetInfoProps) => (<StyledDataRowCount className="source-data-rows"><FormattedMessage- id={'datasetInfo.rowCount'}+ id={+ dataset.type === DatasetType.VECTOR_TILE+ ? 'datasetInfo.vectorTile'+ : 'datasetInfo.rowCount'+ }values={{rowCount: numFormat(dataset.dataContainer.numRows())}}/></StyledDataRowCount>diff --git a/src/components/src/side-panel/common/source-data-catalog.tsx b/src/components/src/side-panel/common/source-data-catalog.tsxindex 5a08c4786..2a278ad09 100644--- a/src/components/src/side-panel/common/source-data-catalog.tsx+++ b/src/components/src/side-panel/common/source-data-catalog.tsx@@ -21,6 +21,7 @@ type MiniDataset = {color: RGBColor;label?: string;dataContainer: DataContainerInterface;+ type?: string;};type MiniDatasets = {diff --git a/src/components/src/side-panel/layer-panel/layer-configurator.tsx b/src/components/src/side-panel/layer-panel/layer-configurator.tsxindex 72e9fbb17..984ba1080 100644--- a/src/components/src/side-panel/layer-panel/layer-configurator.tsx+++ b/src/components/src/side-panel/layer-panel/layer-configurator.tsx@@ -25,11 +25,18 @@ import LayerTypeSelectorFactory from './layer-type-selector';import TextLabelPanelFactory from './text-label-panel';import VisConfigSliderFactory from './vis-config-slider';import VisConfigSwitchFactory from './vis-config-switch';+import VectorTileLayerConfiguratorFactory from './vector-tile-layer-configurator';import {capitalizeFirstLetter} from '@kepler.gl/utils';import {AGGREGATION_TYPE_OPTIONS, LAYER_TYPES} from '@kepler.gl/constants';-import {AggregationLayer, Layer, LayerBaseConfig, VisualChannel} from '@kepler.gl/layers';+import {+ AggregationLayer,+ Layer,+ LayerBaseConfig,+ VisualChannel,+ matchDatasetType+} from '@kepler.gl/layers';import {ActionHandler, toggleModal} from '@kepler.gl/actions';import {Datasets} from '@kepler.gl/table';@@ -126,7 +133,8 @@ LayerConfiguratorFactory.deps = [LayerColorSelectorFactory,LayerColorRangeSelectorFactory,ArcLayerColorSelectorFactory,- AggrScaleSelectorFactory+ AggrScaleSelectorFactory,+ VectorTileLayerConfiguratorFactory];export default function LayerConfiguratorFactory(@@ -141,7 +149,8 @@ export default function LayerConfiguratorFactory(LayerColorSelector: ReturnType<typeof LayerColorSelectorFactory>,LayerColorRangeSelector: ReturnType<typeof LayerColorRangeSelectorFactory>,ArcLayerColorSelector: ReturnType<typeof ArcLayerColorSelectorFactory>,- AggrScaleSelector: ReturnType<typeof AggrScaleSelectorFactory>+ AggrScaleSelector: ReturnType<typeof AggrScaleSelectorFactory>,+ VectorTileLayerConfigurator: ReturnType<typeof VectorTileLayerConfiguratorFactory>): React.ComponentType<LayerConfiguratorProps> {class LayerConfigurator extends Component<LayerConfiguratorProps> {_renderPointLayerConfig(props) {@@ -152,6 +161,10 @@ export default function LayerConfiguratorFactory(return this._renderScatterplotLayerConfig(props);}+ _renderVectorTileLayerConfig(props) {+ return <VectorTileLayerConfigurator {...props} />;+ }+_renderScatterplotLayerConfig({layer,visConfiguratorProps,@@ -1047,6 +1060,12 @@ export default function LayerConfiguratorFactory(const dataset = getLayerDataset(datasets, layer);const renderTemplate = layer.type && `_render${capitalizeFirstLetter(layer.type)}LayerConfig`;+ // show only datasets that can be used by the layer+ const sourceDataSelectorOptions = Object.keys(datasets).reduce(+ (acc, id) => (matchDatasetType(datasets[id], layer) ? {...acc, [id]: datasets[id]} : acc),+ {}+ );+return (<StyledLayerConfigurator>{layer.layerInfoModal && !layer.supportedColumnModes ? (@@ -1063,7 +1082,7 @@ export default function LayerConfiguratorFactory(/><ConfigGroupCollapsibleContent><SourceDataSelector- datasets={datasets}+ datasets={sourceDataSelectorOptions}id={layer.id}dataId={config.dataId}// @ts-ignorediff --git a/src/components/src/side-panel/layer-panel/radius-by-zoom-input.tsx b/src/components/src/side-panel/layer-panel/radius-by-zoom-input.tsxnew file mode 100644index 000000000..f5058d9ad--- /dev/null+++ b/src/components/src/side-panel/layer-panel/radius-by-zoom-input.tsx@@ -0,0 +1,283 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import classnames from 'classnames';+import React, {useCallback, useState} from 'react';+// @ts-expect-error - react-sortable-hoc libdef does not match true exports+import {sortableContainer, sortableElement, sortableHandle} from 'react-sortable-hoc';+import styled, {css} from 'styled-components';++import {ZoomStops, ZoomStopsConfig} from '@kepler.gl/types';+import {arrayMove} from '@kepler.gl/utils';++import {Icons, Input, PanelLabel, Button} from '../../common';++const SliderInput = styled(Input)`+ width: 48px;+ margin: 0 4px 0 8px;+ font-size: ${props => props.theme.list1Size};+`;++type StyledInputRowProps = {+ isEditing?: boolean;+};++const StyledInputRow = styled.div<StyledInputRowProps>`+ display: flex;+ align-items: center;+ width: 100%;+ justify-content: space-between;+ .layer__drag-handle {+ visibility: ${props => (props.isEditing ? 'visible' : 'hidden')};+ }+ .side-panel-panel__label {+ margin-bottom: 0;+ text-transform: none;+ }+`;++type StyledTrashProps = {+ isEditing?: boolean;+};++const StyledTrash = styled.div<StyledTrashProps>`+ color: ${props => props.theme.subtextColor};+ display: flex;+ align-items: center;+ margin-left: 8px;+ visibility: ${props => (props.isEditing ? 'visible' : 'hidden')};++ &:hover {+ cursor: pointer;+ color: ${props => props.theme.subtextColorActive};+ }+`;++const StyledDragHandle = styled.div`+ display: flex;+ align-items: center;+ opacity: 0;+`;++const dragHandleActive = css`+ .layer__drag-handle {+ color: ${props => props.theme.textColorHl};+ opacity: 1;+ cursor: move;+ }+`;++const StyledSortableItem = styled.div`+ display: flex;+ align-items: center;+ padding: 4px 0;+ z-index: ${props => props.theme.dropdownWrapperZ + 1};+ margin-left: -6px;++ &:not(.sorting) {+ &:hover {+ ${dragHandleActive};+ }+ }++ &.sorting-colors {+ background-color: ${props => props.theme.panelBackgroundHover};+ ${dragHandleActive};+ }+`;++const SortableContainer = sortableContainer(({children}) => <div>{children}</div>);+const DragHandle = sortableHandle(({className, children}) => (+ <StyledDragHandle className={className}>{children}</StyledDragHandle>+));+const SortableItem = sortableElement(({children, isSorting}) => (+ <StyledSortableItem+ className={classnames('custom-palette__sortable-items', {sorting: isSorting})}+ >+ {children}+ </StyledSortableItem>+));++function stringToNumber(val) {+ return val === '' ? null : Number(val);+}++type InputRowProps = {+ idx: number;+ stop: number;+ value: number;+ isSorting: boolean;+ isEditing: boolean;+ onChange: (value: [number | null, number | null]) => void;+ onRemove: () => void;+};+const InputRow: React.FC<InputRowProps> = ({+ idx,+ stop,+ value,+ isSorting,+ isEditing,+ onChange,+ onRemove+}) => {+ return (+ <SortableItem key={idx} index={idx} isSorting={isSorting}>+ <StyledInputRow isEditing={isEditing}>+ <DragHandle className="layer__drag-handle">+ <Icons.VertDots height="20px" />+ </DragHandle>+ <PanelLabel>zoom</PanelLabel>+ <SliderInput+ className="vis-config-zoom__input__stop"+ type="number"+ id={`${idx}-stop`}+ key={`${idx}-stop`}+ value={stop}+ onChange={e => onChange([stringToNumber(e.target.value), value])}+ disabled={!isEditing}+ />+ <SliderInput+ className="vis-config-zoom__input__value"+ type="number"+ id={`${idx}-value`}+ key={`${idx}-value`}+ value={value}+ onChange={e => onChange([stop, stringToNumber(e.target.value)])}+ disabled={!isEditing}+ />+ <PanelLabel>px</PanelLabel>+ <StyledTrash isEditing={isEditing}>+ <Icons.Trash onClick={onRemove} height="16px" />+ </StyledTrash>+ </StyledInputRow>+ </SortableItem>+ );+};++function insertStop(stops: ZoomStops): ZoomStops {+ let newStops: ZoomStops | null = null;+ let i = 0;+ while (!newStops && i < stops.length) {+ if (stops[i][0] + 1 < stops[i + 1][0]) {+ const st = stops[i][0] + 1;+ const value = (stops[i][1] + stops[i + 1][1]) / 2;+ newStops = [...stops.slice(0, i + 1), [st, value], ...stops.slice(i + 1)];+ } else {+ i++;+ }+ }++ if (!newStops) {+ newStops = [+ ...stops.slice(0, i),+ [stops[i][0], (stops[i][1] + stops[i + 1][1]) / 2],+ ...stops.slice(i)+ ];+ }++ return newStops;+}++type VisConfigByZoomInputContainerProps = {+ isEditing?: boolean;+};++const VisConfigByZoomInputContainer = styled.div<VisConfigByZoomInputContainerProps>`+ background-color: ${props => (props.isEditing ? props.theme.AZURE950 : 'transparent')};+ margin: 8px 8px 12px 8px;++ .bottom-action {+ margin-top: 8px;+ display: flex;+ align-items: center;+ justify-content: flex-end;+ }++ .bottom-action.editing {+ justify-content: space-between;+ }+`;++type Props = {+ config: ZoomStopsConfig;+ property: string;+ label: string;+ unit: string;+ onChange: (update: Record<string, ZoomStopsConfig>) => void;+};++const VisConfigByZoomInput: React.FC<Props> = ({config = {}, property, onChange}) => {+ const [stopsState, setStops] = useState(config.stops || []);+ const [isSorting, toggleSorting] = useState(false);+ const [isEditing, toggleEditing] = useState(false);++ const onConfirm = useCallback(() => {+ onChange({+ [property]: {+ ...config,+ stops: stopsState+ }+ });+ toggleEditing(false);+ }, [property, config, stopsState, onChange, toggleEditing]);+ const addStop = useCallback(() => setStops(insertStop(stopsState)), [setStops, stopsState]);+ const removeStop = useCallback(+ i => setStops([...stopsState.slice(0, i), ...stopsState.slice(i + 1)]),+ [setStops, stopsState]+ );+ const onSortEnd = useCallback(+ ({oldIndex, newIndex}) => {+ const newStopsState = arrayMove(stopsState, oldIndex, newIndex);+ setStops(newStopsState);+ toggleSorting(false);+ },+ [stopsState, setStops, toggleSorting]+ );+ const onSortStart = useCallback(() => {+ toggleSorting(true);+ }, [toggleSorting]);++ return (+ <VisConfigByZoomInputContainer isEditing={isEditing}>+ <SortableContainer+ className="custom-palette-container"+ onSortEnd={onSortEnd}+ onSortStart={onSortStart}+ lockAxis="y"+ helperClass="sorting-colors"+ useDragHandle+ >+ {stopsState.map((stop, idx) => (+ <InputRow+ isEditing={isEditing}+ key={`input-${idx}`}+ idx={idx}+ stop={stop[0]}+ value={stop[1]}+ isSorting={isSorting}+ onChange={v => setStops(Object.assign([...(stopsState || [])], {[idx]: v}))}+ onRemove={() => removeStop(idx)}+ />+ ))}+ </SortableContainer>+ {isEditing ? (+ <div className="bottom-action editing">+ <Button secondary onClick={addStop} small>+ <Icons.Add height="16px" /> Add Stop+ </Button>+ <Button onClick={onConfirm} small>+ Confirm+ </Button>+ </div>+ ) : (+ <div className="bottom-action">+ <Button onClick={() => toggleEditing(true)} small>+ Edit+ </Button>+ </div>+ )}+ </VisConfigByZoomInputContainer>+ );+};++export default VisConfigByZoomInput;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.tsxnew file mode 100644index 000000000..08a4cefe8--- /dev/null+++ b/src/components/src/side-panel/layer-panel/vector-tile-layer-configurator.tsx@@ -0,0 +1,191 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import React from 'react';+import {connect} from 'react-redux';+import styled from 'styled-components';++import {VectorTileLayer} from '@kepler.gl/layers';+import {KeplerTable as KeplerDataset} from '@kepler.gl/table';++import SourceDataSelectorFactory from '../common/source-data-selector';+import ChannelByValueSelectorFactory from './channel-by-value-selector';+import LayerConfigGroupFactory, {ConfigGroupCollapsibleContent} from './layer-config-group';+import {LayerColorRangeSelectorFactory, LayerColorSelectorFactory} from './layer-color-selector';+import VisConfigByZoomInput from './radius-by-zoom-input';+import VisConfigSliderFactory from './vis-config-slider';+import VisConfigSwitchFactory from './vis-config-switch';++const StyledLayerConfigurator = styled.div`+ margin-top: 12px;+`;++type GetProps<T extends (...args: any[]) => React.FC<any>> = Parameters<ReturnType<T>>[0];++type Props = {+ layer: VectorTileLayer;+ dataset?: KeplerDataset;+ visConfiguratorProps: any;+ layerChannelConfigProps: GetProps<typeof ChannelByValueSelectorFactory>;+ layerConfiguratorProps: any;+};++VectorTileLayerConfiguratorFactory.deps = [+ ChannelByValueSelectorFactory,+ LayerColorRangeSelectorFactory,+ LayerColorSelectorFactory,+ LayerConfigGroupFactory,+ VisConfigSliderFactory,+ VisConfigSwitchFactory,+ SourceDataSelectorFactory+];++function VectorTileLayerConfiguratorFactory(+ ChannelByValueSelector: ReturnType<typeof ChannelByValueSelectorFactory>,+ LayerColorRangeSelector: ReturnType<typeof LayerColorRangeSelectorFactory>,+ LayerColorSelector: ReturnType<typeof LayerColorSelectorFactory>,+ LayerConfigGroup: ReturnType<typeof LayerConfigGroupFactory>,+ VisConfigSlider: ReturnType<typeof VisConfigSliderFactory>,+ VisConfigSwitch: ReturnType<typeof VisConfigSwitchFactory>+): React.FC<Props> {+ const VectorTileLayerConfigurator = ({+ layer,+ dataset,+ visConfiguratorProps,+ layerChannelConfigProps,+ layerConfiguratorProps+ }: Props) => {+ return (+ <StyledLayerConfigurator>+ {/* Fill Color */}+ <LayerConfigGroup {...visConfiguratorProps} label="layer.fillColor">+ {layerChannelConfigProps.fields ? (+ <ChannelByValueSelector+ {...layerChannelConfigProps}+ channel={layer.visualChannels.color}+ />+ ) : null}+ {layer.config.colorField ? (+ <>+ <VisConfigSwitch+ {...visConfiguratorProps}+ {...layer.visConfigSettings.dynamicColor}+ />+ <LayerColorRangeSelector+ {...visConfiguratorProps}+ channel={layer.visualChannels.color}+ />+ </>+ ) : (+ <LayerColorSelector {...layerConfiguratorProps} />+ )}+ <VisConfigSlider {...layer.visConfigSettings.opacity} {...visConfiguratorProps} />+ </LayerConfigGroup>++ {/* Stroke color */}+ <LayerConfigGroup+ {...layer.visConfigSettings.stroked}+ {...visConfiguratorProps}+ label="layer.strokeColor"+ collapsible+ >+ <ChannelByValueSelector+ {...layerChannelConfigProps}+ channel={layer.visualChannels.strokeColor}+ />+ {layer.config.strokeColorField ? (+ <LayerColorRangeSelector+ {...visConfiguratorProps}+ property="strokeColorRange"+ channel={layer.visualChannels.strokeColor}+ />+ ) : (+ <LayerColorSelector+ {...visConfiguratorProps}+ selectedColor={layer.config.visConfig.strokeColor}+ property="strokeColor"+ />+ )}+ <ConfigGroupCollapsibleContent>+ <VisConfigSlider {...layer.visConfigSettings.strokeOpacity} {...visConfiguratorProps} />+ </ConfigGroupCollapsibleContent>+ </LayerConfigGroup>++ {/* Stroke Width */}+ <LayerConfigGroup+ {...visConfiguratorProps}+ {...layer.visConfigSettings.stroked}+ label="layer.strokeWidth"+ collapsible+ >+ {layer.config.sizeField ? (+ <VisConfigSlider+ {...layer.visConfigSettings.sizeRange}+ {...visConfiguratorProps}+ label={false}+ />+ ) : (+ <VisConfigSlider+ {...layer.visConfigSettings.strokeWidth}+ {...visConfiguratorProps}+ label={false}+ />+ )}+ <ConfigGroupCollapsibleContent>+ <ChannelByValueSelector+ {...layerChannelConfigProps}+ channel={layer.visualChannels.size}+ />+ </ConfigGroupCollapsibleContent>+ </LayerConfigGroup>++ {/* Elevation */}+ <LayerConfigGroup+ {...visConfiguratorProps}+ {...layer.visConfigSettings.enable3d}+ collapsible+ >+ <VisConfigSlider+ {...layer.visConfigSettings.elevationScale}+ {...visConfiguratorProps}+ label={false}+ />+ {layerChannelConfigProps.fields ? (+ <ChannelByValueSelector+ {...layerChannelConfigProps}+ channel={layer.visualChannels.height}+ />+ ) : null}+ </LayerConfigGroup>++ {/* Radius */}+ <LayerConfigGroup+ {...visConfiguratorProps}+ label={'layer.radius'}+ description="Point radius in meters"+ >+ {layer.config.visConfig.radiusByZoom?.enabled && visConfiguratorProps.onChange ? (+ <VisConfigByZoomInput+ config={layer.config.visConfig.radiusByZoom}+ onChange={visConfiguratorProps.onChange}+ label="Radius"+ property="radiusByZoom"+ unit="px"+ />+ ) : (+ <VisConfigSlider+ {...layer.visConfigSettings.radius}+ {...visConfiguratorProps}+ label={false}+ />+ )}+ </LayerConfigGroup>+ </StyledLayerConfigurator>+ );+ };++ const ConnectedVectorTileLayerConfigurator = connect(state => state)(VectorTileLayerConfigurator);+ return ConnectedVectorTileLayerConfigurator;+}++export default VectorTileLayerConfiguratorFactory;diff --git a/src/constants/src/default-settings.ts b/src/constants/src/default-settings.tsindex bb0a43269..96fa61b2e 100644--- a/src/constants/src/default-settings.ts+++ b/src/constants/src/default-settings.ts@@ -1169,7 +1169,8 @@ export const MAP_INFO_CHARACTER = {// Load dataexport const LOADING_METHODS = keyMirror({upload: null,- storage: null+ storage: null,+ tileset: null});export const DEFAULT_FEATURE_FLAGS = {};diff --git a/src/constants/src/layers.ts b/src/constants/src/layers.tsindex b78c995c9..bf05cf60b 100644--- a/src/constants/src/layers.ts+++ b/src/constants/src/layers.ts@@ -573,7 +573,8 @@ export const LAYER_TYPES = keyMirror({hexagonId: null,'3D': null,trip: null,- s2: null+ s2: null,+ vectorTile: null});export const EDITOR_AVAILABLE_LAYERS: string[] = [diff --git a/src/layers/package.json b/src/layers/package.jsonindex afaf79949..da58fd8d2 100644--- a/src/layers/package.json+++ b/src/layers/package.json@@ -48,7 +48,9 @@"@loaders.gl/core": "^4.3.2","@loaders.gl/gis": "^4.3.2","@loaders.gl/gltf": "^4.3.2",+ "@loaders.gl/mvt": "^4.3.2","@loaders.gl/parquet": "^4.3.2",+ "@loaders.gl/pmtiles": "^4.3.2","@loaders.gl/schema": "^4.3.2","@loaders.gl/wkt": "^4.3.2","@luma.gl/constants": "^8.5.20",diff --git a/src/layers/src/aggregation-layer.ts b/src/layers/src/aggregation-layer.tsindex 5e2fd1f5b..5752379ec 100644--- a/src/layers/src/aggregation-layer.ts+++ b/src/layers/src/aggregation-layer.ts@@ -274,7 +274,8 @@ export default class AggregationLayer extends Layer {return this;}- updateLayerMeta(dataContainer, getPosition) {+ updateLayerMeta(dataset: KeplerTable, getPosition) {+ const {dataContainer} = dataset;// get bounds from pointsconst bounds = this.getPointsBounds(dataContainer, getPosition);diff --git a/src/layers/src/arc-layer/arc-layer.ts b/src/layers/src/arc-layer/arc-layer.tsindex 83ae57d3b..0b0744e15 100644--- a/src/layers/src/arc-layer/arc-layer.ts+++ b/src/layers/src/arc-layer/arc-layer.ts@@ -463,7 +463,9 @@ export default class ArcLayer extends Layer {}/* eslint-enable complexity */- updateLayerMeta(dataContainer) {+ updateLayerMeta(dataset: KeplerTable) {+ const {dataContainer} = dataset;+this.dataContainer = dataContainer;// get bounds from arcsdiff --git a/src/layers/src/base-layer.ts b/src/layers/src/base-layer.tsindex c1ce2772d..1ed9964e8 100644--- a/src/layers/src/base-layer.ts+++ b/src/layers/src/base-layer.ts@@ -658,7 +658,7 @@ class Layer {}// eslint-disable-next-line @typescript-eslint/no-unused-vars- formatLayerData(datasets: Datasets, oldLayerData?: any) {+ formatLayerData(datasets: Datasets, oldLayerData?: unknown, animationConfig?: AnimationConfig) {return {};}@@ -1077,7 +1077,7 @@ class Layer {}: {dataAccessor?: typeof defaultDataAccessor;dataContainer: DataContainerInterface;- indexKey?: number;+ indexKey?: number | null;}) {const attributeAccessors: {[key: string]: any} = {};@@ -1296,7 +1296,7 @@ class Layer {const triggerChanged = this.getChangedTriggers(dataUpdateTriggers);if (triggerChanged && (triggerChanged.getMeta || triggerChanged.getData)) {- this.updateLayerMeta(dataContainer, getPosition);+ this.updateLayerMeta(layerDataset, getPosition);// reset filteredItemCountthis.filteredItemCount = {};@@ -1627,7 +1627,7 @@ class Layer {}// eslint-disable-next-line @typescript-eslint/no-unused-vars- updateLayerMeta(dataContainer: DataContainerInterface, getPosition) {+ updateLayerMeta(dataset: KeplerTable, getPosition) {// implemented in subclasses}diff --git a/src/layers/src/geojson-layer/geojson-layer.ts b/src/layers/src/geojson-layer/geojson-layer.tsindex c7e6bbbb1..b6d150c6f 100644--- a/src/layers/src/geojson-layer/geojson-layer.ts+++ b/src/layers/src/geojson-layer/geojson-layer.ts@@ -547,7 +547,9 @@ export default class GeoJsonLayer extends Layer {return booleanWithin(turfPoint(point), polygon);}- updateLayerMeta(dataContainer) {+ updateLayerMeta(dataset: KeplerTable) {+ const {dataContainer} = dataset;+this.dataContainer = dataContainer;if (dataContainer instanceof ArrowDataContainer) {@@ -584,7 +586,7 @@ export default class GeoJsonLayer extends Layer {}}- setInitialLayerConfig(dataset) {+ setInitialLayerConfig(dataset: KeplerTable) {const {dataContainer} = dataset;if (!dataContainer.numRows()) {return this;@@ -603,7 +605,7 @@ export default class GeoJsonLayer extends Layer {}}- this.updateLayerMeta(dataContainer);+ this.updateLayerMeta(dataset);const {featureTypes} = this.meta;// default settings is stroke: true, filled: falsediff --git a/src/layers/src/h3-hexagon-layer/h3-hexagon-layer.ts b/src/layers/src/h3-hexagon-layer/h3-hexagon-layer.tsindex b56800e6e..f137c8676 100644--- a/src/layers/src/h3-hexagon-layer/h3-hexagon-layer.ts+++ b/src/layers/src/h3-hexagon-layer/h3-hexagon-layer.ts@@ -338,7 +338,9 @@ export default class HexagonIdLayer extends Layer {}/* eslint-enable complexity */- updateLayerMeta(dataContainer, getHexId) {+ updateLayerMeta(dataset: KeplerTable, getHexId) {+ const {dataContainer} = dataset;+const centroids = dataContainer.map((d, index) => {const id = getHexId({index});if (!h3IsValid(id)) {diff --git a/src/layers/src/heatmap-layer/heatmap-layer.ts b/src/layers/src/heatmap-layer/heatmap-layer.tsindex d2112a6be..c4a56e489 100644--- a/src/layers/src/heatmap-layer/heatmap-layer.ts+++ b/src/layers/src/heatmap-layer/heatmap-layer.ts@@ -8,6 +8,7 @@ import HeatmapLayerIcon from './heatmap-layer-icon';import {LayerBaseConfigPartial, LayerWeightConfig, VisualChannels} from '../base-layer';import {VisConfigColorRange, VisConfigNumber, HexColor, Merge, LayerColumn} from '@kepler.gl/types';import {hexToRgb, DataContainerInterface} from '@kepler.gl/utils';+import {KeplerTable} from '@kepler.gl/table';export type HeatmapLayerVisConfigSettings = {opacity: VisConfigNumber;@@ -207,7 +208,9 @@ class HeatmapLayer extends MapboxGLLayer {return layerConfig;}- updateLayerMeta(dataContainer) {+ updateLayerMeta(dataset: KeplerTable) {+ const {dataContainer} = dataset;+const getPosition = this.getPositionAccessor(dataContainer);const bounds = this.getPointsBounds(dataContainer, getPosition);this.updateMeta({bounds});diff --git a/src/layers/src/icon-layer/icon-layer.ts b/src/layers/src/icon-layer/icon-layer.tsindex 855b03bb0..ff733d6e7 100644--- a/src/layers/src/icon-layer/icon-layer.ts+++ b/src/layers/src/icon-layer/icon-layer.ts@@ -329,7 +329,8 @@ export default class IconLayer extends Layer {};}- updateLayerMeta(dataContainer, getPosition) {+ updateLayerMeta(dataset: KeplerTable, getPosition) {+ const {dataContainer} = dataset;const bounds = this.getPointsBounds(dataContainer, getPosition);this.updateMeta({bounds});}diff --git a/src/layers/src/index.ts b/src/layers/src/index.tsindex 20cbbbbc2..281a000ce 100644--- a/src/layers/src/index.ts+++ b/src/layers/src/index.ts@@ -32,7 +32,6 @@ import {default as TripLayer} from './trip-layer/trip-layer';export {defaultLineWidth as tripDefaultLineWidth} from './trip-layer/trip-layer';export {coordHasLength4,- containValidTime,isTripGeoJsonField,parseTripGeoJsonTimestamp,getAnimationDomainFromTimestamps@@ -41,6 +40,24 @@ import {default as S2GeometryLayer} from './s2-geometry-layer/s2-geometry-layer'export {defaultElevation as s2DefaultElevation} from './s2-geometry-layer/s2-geometry-layer';export {getS2Center} from './s2-geometry-layer/s2-utils';export {default as AggregationLayer} from './aggregation-layer';++import {default as VectorTileLayer} from './vector-tile/vector-tile-layer';++export {+ getMetaUrl,+ parseVectorMetadata,+ matchDatasetType,+ DatasetType,+ TileType,+ VectorTileType+} from './vector-tile/utils/vector-tile-utils';+export type {+ VectorTileDatasetMetadata,+ VectorTileMetadata+} from './vector-tile/utils/vector-tile-utils';+export {default as VectorTileIcon} from './vector-tile/vector-tile-icon';+export {default as VectorTileLayer} from './vector-tile/vector-tile-layer';+import {LAYER_TYPES} from '@kepler.gl/constants';export {parseGeoJsonRawFeature} from './geojson-layer/geojson-utils';// base layer@@ -68,7 +85,8 @@ export const KeplerGlLayers = {H3Layer,ScenegraphLayer,TripLayer,- S2GeometryLayer+ S2GeometryLayer,+ VectorTileLayer};export type LayerClassesType = typeof LayerClasses;@@ -85,7 +103,8 @@ export const LayerClasses = {[LAYER_TYPES.hexagonId]: H3Layer,[LAYER_TYPES['3D']]: ScenegraphLayer,[LAYER_TYPES.trip]: TripLayer,- [LAYER_TYPES.s2]: S2GeometryLayer+ [LAYER_TYPES.s2]: S2GeometryLayer,+ [LAYER_TYPES['vectorTile']]: VectorTileLayer};export * from './mapbox-utils';diff --git a/src/layers/src/point-layer/point-layer.ts b/src/layers/src/point-layer/point-layer.tsindex f593cdc14..5ac142bb7 100644--- a/src/layers/src/point-layer/point-layer.ts+++ b/src/layers/src/point-layer/point-layer.ts@@ -480,7 +480,8 @@ export default class PointLayer extends Layer {}/* eslint-enable complexity */- updateLayerMeta(dataContainer) {+ updateLayerMeta(dataset: KeplerTable) {+ const {dataContainer} = dataset;this.dataContainer = dataContainer;if (this.config.columnMode === COLUMN_MODE_GEOJSON) {@@ -489,7 +490,7 @@ export default class PointLayer extends Layer {} else if (this.config.columnMode === COLUMN_MODE_GEOARROW) {const boundsFromMetadata = getBoundsFromArrowMetadata(this.config.columns.geoarrow,- dataContainer+ dataContainer as ArrowDataContainer);if (boundsFromMetadata) {this.updateMeta({bounds: boundsFromMetadata});diff --git a/src/layers/src/s2-geometry-layer/s2-geometry-layer.ts b/src/layers/src/s2-geometry-layer/s2-geometry-layer.tsindex ac8ff1560..a6cffe98e 100644--- a/src/layers/src/s2-geometry-layer/s2-geometry-layer.ts+++ b/src/layers/src/s2-geometry-layer/s2-geometry-layer.ts@@ -277,7 +277,8 @@ export default class S2GeometryLayer extends Layer {return data;}- updateLayerMeta(dataContainer, getS2Token) {+ updateLayerMeta(dataset: KeplerTable, getS2Token) {+ const {dataContainer} = dataset;// add safe row flagconst centroids = dataContainer.reduce((acc, entry, index) => {diff --git a/src/layers/src/scenegraph-layer/scenegraph-layer.ts b/src/layers/src/scenegraph-layer/scenegraph-layer.tsindex a17958252..9bdebac8b 100644--- a/src/layers/src/scenegraph-layer/scenegraph-layer.ts+++ b/src/layers/src/scenegraph-layer/scenegraph-layer.ts@@ -185,7 +185,8 @@ export default class ScenegraphLayer extends Layer {};}- updateLayerMeta(dataContainer, getPosition) {+ updateLayerMeta(dataset: KeplerTable, getPosition) {+ const {dataContainer} = dataset;const bounds = this.getPointsBounds(dataContainer, getPosition);this.updateMeta({bounds});}diff --git a/src/layers/src/trip-layer/trip-layer.ts b/src/layers/src/trip-layer/trip-layer.tsindex d1eb4593b..e0c6d3588 100644--- a/src/layers/src/trip-layer/trip-layer.ts+++ b/src/layers/src/trip-layer/trip-layer.ts@@ -361,7 +361,8 @@ export default class TripLayer extends Layer {});}- updateLayerMeta(dataContainer: DataContainerInterface) {+ updateLayerMeta(dataset: KeplerTable) {+ const {dataContainer} = dataset;let getFeature;if (this.config.columnMode === COLUMN_MODE_GEOJSON) {getFeature = this.getPositionAccessor(dataContainer);@@ -408,7 +409,7 @@ export default class TripLayer extends Layer {}}- this.updateLayerMeta(dataContainer);+ this.updateLayerMeta(dataset);return this;}diff --git a/src/layers/src/trip-layer/trip-utils.ts b/src/layers/src/trip-layer/trip-utils.tsindex d76432baa..522905cb6 100644--- a/src/layers/src/trip-layer/trip-utils.ts+++ b/src/layers/src/trip-layer/trip-utils.ts@@ -1,13 +1,9 @@// SPDX-License-Identifier: MIT// Copyright contributors to the kepler.gl project-import {Analyzer, DATA_TYPES} from 'type-analyzer';--import {Field} from '@kepler.gl/types';-import {parseGeoJsonRawFeature, getGeojsonFeatureTypes} from '../geojson-layer/geojson-utils';import {DataContainerInterface, getSampleContainerData, timeToUnixMilli} from '@kepler.gl/utils';-import {notNullorUndefined} from '@kepler.gl/common-utils';+import {containValidTime, notNullorUndefined} from '@kepler.gl/common-utils';import {Feature} from '@turf/helpers';import {GeoJsonProperties, Geometry} from 'geojson';@@ -30,26 +26,6 @@ export function coordHasLength4(samples): boolean {return hasLength4;}-/**- * Check whether geojson linestring's 4th coordinate is 1) not timestamp 2) unix time stamp 3) real date time- * @param timestamps array to be tested if its elements are timestamp- * @returns the type of timestamp: unix/datetime/invalid(not timestamp)- */-export function containValidTime(timestamps: string[]): Field | null {- const formattedTimeStamps = timestamps.map(ts => ({ts}));- const ignoredDataTypes = Object.keys(DATA_TYPES).filter(- type => ![DATA_TYPES.TIME, DATA_TYPES.DATETIME].includes(type)- );-- // ignore all types but TIME to improve performance- const analyzedType = Analyzer.computeColMeta(formattedTimeStamps, [], {ignoredDataTypes})[0];-- if (!analyzedType || analyzedType.category !== 'TIME') {- return null;- }- return analyzedType;-}-/*** Check if geojson features are trip layer animatable by meeting 3 conditions* @param dataContainer geojson feature objects containerdiff --git a/src/layers/src/vector-tile/abstract-tile-layer.ts b/src/layers/src/vector-tile/abstract-tile-layer.tsnew file mode 100644index 000000000..dd105407b--- /dev/null+++ b/src/layers/src/vector-tile/abstract-tile-layer.ts@@ -0,0 +1,420 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import throttle from 'lodash.throttle';++import {notNullorUndefined} from '@kepler.gl/common-utils';+import {SCALE_TYPES, ALL_FIELD_TYPES, LAYER_VIS_CONFIGS} from '@kepler.gl/constants';+import {Layer, VisualChannel} from '@kepler.gl/layers';+import {+ KeplerTable as KeplerDataset,+ Datasets as KeplerDatasets,+ GpuFilter+} from '@kepler.gl/table';+import {+ AnimationConfig,+ Field,+ BindedLayerCallbacks,+ VisConfigBoolean,+ VisConfigRange,+ VisConfigColorRange,+ VisConfigNumber,+ VisConfigColorSelect,+ LayerColorConfig,+ LayerHeightConfig,+ Filter,+ Field as KeplerField,+ MapState,+ Merge,+ ZoomStopsConfig+} from '@kepler.gl/types';+import {findDefaultColorField, DataContainerInterface} from '@kepler.gl/utils';++import {+ LayerBaseConfig,+ LayerBaseConfigPartial,+ VisualChannelDomain,+ VisualChannelField+} from '../base-layer';+import {isTileDataset} from './utils/vector-tile-utils';+import TileDataset from './common-tile/tile-dataset';+import {isIndexedField, isDomainQuantiles} from './common-tile/tile-utils';++const DEFAULT_ELEVATION = 500;+export const DEFAULT_RADIUS = 1;++export type AbstractTileLayerVisConfigSettings = {+ strokeColor: VisConfigColorSelect;+ strokeOpacity: VisConfigNumber;+ radius: VisConfigRange;+ enable3d: VisConfigBoolean;+ stroked: VisConfigBoolean;+ transition: VisConfigBoolean;+ heightRange: VisConfigRange;+ elevationScale: VisConfigNumber;+ opacity: VisConfigNumber;+ colorRange: VisConfigColorRange;+ // TODO: figure out type for radiusByZoom vis config+ radiusByZoom: any;+ dynamicColor: VisConfigBoolean;+};++export type LayerData = {+ minZoom?: number;+ maxZoom?: number;+ getPointRadius?: () => number;+};++export type LayerOpts = {+ idx: number;+ mapState: MapState;+ data: LayerData;+ gpuFilter: GpuFilter;+ animationConfig: AnimationConfig;+ tilesetDataUrl?: string | null;+ layerCallbacks: BindedLayerCallbacks;+};++export const commonTileVisConfigs = {+ strokeColor: 'strokeColor' as const,+ strokeOpacity: {+ ...LAYER_VIS_CONFIGS.opacity,+ property: 'strokeOpacity'+ },+ radius: {+ ...LAYER_VIS_CONFIGS.radius,+ isRanged: true,+ range: [0, 1000],+ step: 0.1,+ defaultValue: [50, 50],+ allowCustomValue: false+ } as VisConfigRange,+ enable3d: 'enable3d' as const,+ stroked: {+ ...LAYER_VIS_CONFIGS.stroked,+ defaultValue: false+ },+ transition: {+ type: 'boolean',+ defaultValue: false,+ label: 'Transition',+ group: '',+ property: 'transition',+ description:+ 'Smoother transition during animation (enable transition will decrease performance)'+ } as VisConfigBoolean,+ heightRange: 'elevationRange' as const,+ elevationScale: {...LAYER_VIS_CONFIGS.elevationScale, allowCustomValue: false},+ opacity: 'opacity' as const,+ colorRange: 'colorRange' as const,+ // TODO: figure out type for radiusByZoom vis config+ radiusByZoom: {+ ...LAYER_VIS_CONFIGS.radius,+ defaultValue: {+ enabled: false,+ stops: null+ } as ZoomStopsConfig+ } as any,+ dynamicColor: {+ type: 'boolean',+ defaultValue: false,+ label: 'Dynamic Color',+ group: '',+ property: 'dynamicColor',+ description: 'Use a dynamic color scale based on data visible in the viewport'+ } as VisConfigBoolean+};++export type AbstractTileLayerConfig = Merge<+ LayerBaseConfig,+ {colorField?: VisualChannelField; colorScale?: string; colorDomain?: VisualChannelDomain}+>;++/**+ * Abstract tile layer, including common functionality for viewport-based datasets,+ * dynamic scales, and tile-based animation+ */+export default abstract class AbstractTileLayer<+ // Type of the tile itself+ T,+ // Type of the iterable data object (for HexTile, this is the same as the tile)+ I extends Iterable<any> = T extends Iterable<any> ? T : never+> extends Layer {+ declare config: AbstractTileLayerConfig;+ declare visConfigSettings: AbstractTileLayerVisConfigSettings;++ constructor(props: ConstructorParameters<typeof Layer>[0]) {+ super(props);+ this.registerVisConfig(commonTileVisConfigs);+ this.tileDataset = this.initTileDataset();+ }++ protected tileDataset: TileDataset<T, I>;+ protected setLayerDomain: BindedLayerCallbacks['onSetLayerDomain'] = undefined;++ protected abstract initTileDataset(): TileDataset<T, I>;++ static findDefaultLayerProps(dataset: KeplerDataset): {+ props: {dataId: string; label?: string; isVisible: boolean}[];+ } {+ if (!isTileDataset(dataset)) {+ return {props: []};+ }++ const newLayerProp = {+ dataId: dataset.id,+ label: dataset.label,+ isVisible: true+ };++ return {props: [newLayerProp]};+ }++ get requireData(): boolean {+ return true;+ }++ get requiredLayerColumns(): string[] {+ return [];+ }++ get visualChannels(): Record<string, VisualChannel> {+ return {+ color: {+ ...super.visualChannels.color,+ accessor: 'getFillColor',+ defaultValue: config => config.color+ },+ height: {+ property: 'height',+ field: 'heightField',+ scale: 'heightScale',+ domain: 'heightDomain',+ range: 'heightRange',+ key: 'height',+ channelScaleType: 'size',+ accessor: 'getElevation',+ condition: config => config.visConfig.enable3d,+ nullValue: 0,+ defaultValue: DEFAULT_ELEVATION+ }+ };+ }++ /**+ * Callback to invoke when the viewport changes+ */+ onViewportLoad = (tiles: T[]): void => {+ this.tileDataset.updateTiles(tiles);+ // Update dynamic color domain if required+ if (this.config.visConfig.dynamicColor) {+ this.setDynamicColorDomain();+ }+ };++ abstract accessRowValue(+ field?: KeplerField,+ indexKey?: number | null+ ): (field: KeplerField, datum: T extends Iterable<infer V> ? V : never) => string | number | null;++ accessVSFieldValue(inputField?: Field, indexKey?: number | null): any {+ return this.accessRowValue(inputField, indexKey);+ }++ getScaleOptions(channelKey: string): string[] {+ if (channelKey === 'color') {+ const channel = this.visualChannels.color;+ const field = this.config[channel.field];++ if (+ isDomainQuantiles(field?.filterProps?.domainQuantiles) ||+ this.config.visConfig.dynamicColor ||+ // If we've set the scale to quantile, we need to include it - there's a loading+ // period in which the visConfig isn't set yet, but if we don't return the right+ // scale type we lose it+ this.config.colorScale === SCALE_TYPES.quantile+ ) {+ return [SCALE_TYPES.quantize, SCALE_TYPES.quantile, SCALE_TYPES.custom];+ }+ return [SCALE_TYPES.quantize, SCALE_TYPES.custom];+ }++ return [SCALE_TYPES.linear];+ }++ setDynamicColorDomain = throttle((): void => {+ const {config, tileDataset, setLayerDomain} = this;+ const field = config.colorField;+ const {colorDomain, colorScale} = config;+ if (!tileDataset || !setLayerDomain || !field) return;++ if (colorScale === SCALE_TYPES.quantize) {+ const [min, max] = tileDataset.getExtent(field);+ if (!Array.isArray(colorDomain) || min !== colorDomain[0] || max !== colorDomain[1]) {+ setLayerDomain({domain: [min, max]});+ }+ } else if (colorScale === SCALE_TYPES.quantile) {+ const domain = tileDataset.getQuantileSample(field);+ setLayerDomain({domain});+ } else if (colorScale === SCALE_TYPES.ordinal) {+ const domain = tileDataset.getUniqueValues(field);+ setLayerDomain({domain});+ }+ }, 500);++ resetColorDomain(): void {+ const {datasetId, datasets} = this.meta;+ this.updateLayerVisualChannel(datasets?.[datasetId || ''], 'color');+ this.setLayerDomain?.({domain: this.config.colorDomain});+ }++ updateLayerConfig(+ newConfig: Partial<LayerBaseConfig> & Partial<LayerColorConfig>+ ): AbstractTileLayer<T, I> {+ // When the dynamic color setting changes, we need to recalculate the layer domain+ const old = this.config.visConfig.dynamicColor ?? false;+ const next = newConfig.visConfig?.dynamicColor ?? old;+ const scaleTypeChanged =+ newConfig.colorScale && this.config.colorScale !== newConfig.colorScale;++ super.updateLayerConfig(newConfig);+ const {colorField} = this.config;++ if (colorField) {+ // When we switch from dynamic to non-dynamic or vice versa, we need to update+ // the color domain. This is downstream from a dispatch call, so we use+ // setTimeout to avoid "reducers may not dispatch actions" errors+ if (next && (!old || scaleTypeChanged)) {+ setTimeout(() => this.setDynamicColorDomain(), 0);+ } else if (old && !next) {+ setTimeout(() => this.resetColorDomain(), 0);+ }+ }++ return this;+ }++ get animationDomain(): [number, number] | null | undefined {+ return this.config.animation.domain;+ }++ setInitialLayerConfig(dataset: KeplerDataset): AbstractTileLayer<T, I> {+ const defaultColorField = findDefaultColorField(dataset);++ if (defaultColorField) {+ this.updateLayerConfig({+ colorField: defaultColorField+ });+ this.updateLayerVisualChannel(dataset, 'color');+ }++ return this;+ }++ getDefaultLayerConfig(+ props: LayerBaseConfigPartial+ ): LayerBaseConfig & Partial<LayerColorConfig & LayerHeightConfig> {+ return {+ ...super.getDefaultLayerConfig(props),+ colorScale: SCALE_TYPES.quantize,++ // add height visual channel+ heightField: null,+ heightDomain: [0, 1],+ heightScale: 'linear'+ };+ }++ // We can render without columns, so we redefine this method+ shouldRenderLayer(): boolean {+ return Boolean(this.type && this.config.isVisible);+ }++ updateAnimationDomainByField(channel: string): void {+ const field = this.config[this.visualChannels[channel].field];++ if (isIndexedField(field) && field.indexBy.type === ALL_FIELD_TYPES.timestamp) {+ const {timeDomain} = field.indexBy;++ this.updateLayerConfig({+ animation: {+ ...timeDomain,+ enabled: true,+ startTime: timeDomain.domain[0]+ }+ });+ }+ }++ updateAnimationDomain(domain: [number, number] | null | undefined): void {+ this.updateLayerConfig({+ animation: {+ ...this.config.animation,+ domain+ }+ });+ }++ updateLayerDomain(datasets: KeplerDatasets, newFilter?: Filter): AbstractTileLayer<T, I> {+ super.updateLayerDomain(datasets, newFilter);+ if (newFilter) {+ // invalidate cachedVisibleDataset when e.g. changing filters+ this.tileDataset.invalidateCache();+ }+ Object.keys(this.visualChannels).forEach(channel => {+ this.updateAnimationDomainByField(channel);+ });+ return this;+ }++ updateLayerVisualChannel(dataset: KeplerDataset, channel: string): void {+ super.updateLayerVisualChannel(dataset, channel);++ // create animation if field is indexed by time+ this.updateAnimationDomainByField(channel);+ }++ formatLayerData(+ datasets: KeplerDatasets,+ oldLayerData: unknown,+ animationConfig: AnimationConfig+ ): LayerData {+ const {dataId} = this.config;+ if (!notNullorUndefined(dataId)) {+ return {};+ }+ const dataset = datasets[dataId];++ const dataUpdateTriggers = this.getDataUpdateTriggers(dataset);+ const triggerChanged = this.getChangedTriggers(dataUpdateTriggers);++ if (triggerChanged && triggerChanged.getMeta) {+ this.updateLayerMeta(dataset, datasets);+ }++ const indexKey = this.config.animation.enabled ? animationConfig.currentTime : null;+ const accessors = this.getAttributeAccessors({+ dataAccessor: () => d => d,+ dataContainer: dataset.dataContainer,+ indexKey+ });++ const getPointRadius = () => DEFAULT_RADIUS;++ const metadata = dataset?.metadata as LayerData | undefined;++ return {+ ...(metadata ? {minZoom: metadata.minZoom} : {}),+ ...(metadata ? {maxZoom: metadata.maxZoom} : {}),+ ...accessors,+ getPointRadius+ };+ }++ getGpuFilterValueAccessor({gpuFilter, animationConfig}: LayerOpts): any {+ const indexKey = this.config.animation.enabled ? animationConfig.currentTime : null;+ const valueAccessor = (dataContainer: DataContainerInterface, d) => field =>+ this.accessVSFieldValue(field, indexKey)(field, d);+ return gpuFilter.filterValueAccessor(null as any)(undefined, valueAccessor);+ }+}diff --git a/src/layers/src/vector-tile/common-tile/iterable-tile-set.ts b/src/layers/src/vector-tile/common-tile/iterable-tile-set.tsnew file mode 100644index 000000000..4e8c4d2e9--- /dev/null+++ b/src/layers/src/vector-tile/common-tile/iterable-tile-set.ts@@ -0,0 +1,68 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++/**+ * Custom iterator for data in a set of tiles+ */+class TileSetIterator<T extends Iterable<any>> {+ private tileIterator: Iterator<T extends Iterable<infer V> ? V : never> | null = null;+ private tileIndex = -1;++ constructor(private readonly tiles: readonly T[]) {}++ nextTile() {+ if (this.tileIndex < this.tiles.length - 1) {+ this.tileIndex++;+ this.tileIterator = this.tiles[this.tileIndex][Symbol.iterator]();+ } else {+ this.tileIterator = null;+ }+ }++ next(): IteratorResult<T extends Iterable<infer V> ? V : never> {+ // Startup: Get the iterator for the next tile+ if (this.tileIterator === null) this.nextTile();+ // Finish: We have no more tiles, we're done+ if (this.tileIterator === null) return {done: true, value: null};++ const next = this.tileIterator.next();+ // If we have a value for the current tile, return it+ if (!next.done) {+ return next;+ }++ // Otherwise, go on to the next tile+ this.tileIterator = null;+ return this.next();+ }++ [Symbol.iterator]() {+ return this;+ }+}++export type RowCountAccessor<T> = (tile: T) => number;++/**+ * An iterable object that can iterate over all rows+ * in a set of tiles, treat the set as a single iterable+ */+export default class IterableTileSet<T extends Iterable<any>> {+ rowCount = 0;+ private rowCounts: number[] = [];++ constructor(private readonly tiles: readonly T[], getRowCount: RowCountAccessor<T>) {+ for (const tile of tiles) {+ const count = getRowCount(tile);+ this.rowCount += count;+ this.rowCounts.push(count);+ }+ }++ /**+ * Iterate over all values in the set+ */+ [Symbol.iterator](): Iterator<T extends Iterable<infer V> ? V : never> {+ return new TileSetIterator<T>(this.tiles);+ }+}diff --git a/src/layers/src/vector-tile/common-tile/tile-dataset.ts b/src/layers/src/vector-tile/common-tile/tile-dataset.tsnew file mode 100644index 000000000..6d7050748--- /dev/null+++ b/src/layers/src/vector-tile/common-tile/tile-dataset.ts@@ -0,0 +1,216 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {Field as KeplerField} from '@kepler.gl/types';+import {quickInsertionSort} from '@kepler.gl/utils';++import IterableTileSet, {RowCountAccessor} from './iterable-tile-set';+import {pruneQuantiles} from '../utils/vector-tile-utils';++export type Datum = number | string | null;++type RowValueAccessor<T> = (+ field: KeplerField,+ row: T extends Iterable<infer V> ? V : never+) => Datum;+type RowValueAccessorFactory<T> = (+ field?: KeplerField,+ indexKey?: number | null+) => RowValueAccessor<T>;++type TileAccessors<T, I extends Iterable<any> = T extends Iterable<any> ? T : never> = {+ getTileId: (tile: T) => string;+ getIterable: (tile: T) => I;+ getRowCount: RowCountAccessor<I>;+ getRowValue: RowValueAccessorFactory<I>;+};++/**+ * Per-tile stats, for caching+ */+type TileFieldStats = {+ extent?: [number, number];+ sample?: Datum[];+ uniqueValues?: Set<Datum>;+};++/**+ * Stateful class offering dataset-style functions for the set of tiles.+ */+export default class TileDataset<T, I extends Iterable<any> = T extends Iterable<any> ? T : never> {+ private accessors: TileAccessors<T, I>;+ private tiles: readonly T[];+ private tileSet: IterableTileSet<I>;+ private tileIds: Set<string> = new Set();++ /** Cache for per-tile field stats: tileId -> fieldId -> stats */+ private tileStats: Map<string, Map<string, TileFieldStats>> = new Map();++ constructor(accessors: TileAccessors<T, I>, tiles?: readonly T[]) {+ this.accessors = accessors;+ this.tiles = [];+ this.tileSet = new IterableTileSet([], accessors.getRowCount);+ if (tiles) {+ this.updateTiles(tiles);+ }+ }++ /**+ * Invalidate the cached data+ */+ invalidateCache(): void {+ // TODO: implement later+ }++ /**+ * Update the set of tiles in the viewport+ */+ updateTiles(tiles: readonly T[]): void {+ const {getTileId, getIterable, getRowCount} = this.accessors;+ const tileIds = new Set<string>(tiles.map(getTileId));+ if (!areEqualSets(tileIds, this.tileIds)) {+ this.invalidateCache();+ }+ this.tiles = tiles;+ this.tileIds = tileIds;+ this.tileSet = new IterableTileSet(tiles.map(getIterable), getRowCount);+ }++ /**+ * Get the min/max domain of a field+ */+ getExtent(field: KeplerField): [number, number] {+ const {getRowValue, getIterable} = this.accessors;+ const accessor = getRowValue(field);+ let min = Infinity;+ let max = -Infinity;++ for (const tile of this.tiles) {+ // Check the cache+ let extent = this.getTileStat(tile, field, 'extent');+ if (!extent) {+ // Cache miss, calculate and cache+ extent = getTileExtent(getIterable(tile), field, accessor);+ this.setTileStat(tile, field, 'extent', extent);+ }+ if (extent) {+ if (extent[0] < min) min = extent[0];+ if (extent[1] > max) max = extent[1];+ }+ }+ return Number.isFinite(min) && Number.isFinite(max) ? [min, max] : [0, 0];+ }++ /**+ * Get a sample of field values to use in estimating quantiles+ */+ getQuantileSample(field: KeplerField, minRowCount = 1000): number[] {+ // TODO: There should be reasonable per-tile caching possible here+ const set = this.tileSet;+ const accessor = this.accessors.getRowValue(field);+ const sample: number[] = [];+ const sampleStep = Math.max(Math.floor(set.rowCount / minRowCount), 1);+ let i = 0;+ for (const row of set) {+ if (++i === sampleStep) {+ const val = accessor(field, row) as number | null;+ if (val !== null) sample.push(val);+ i = 0;+ }+ }+ quickInsertionSort(sample);+ pruneQuantiles(sample);+ return sample;+ }++ /**+ * Get a set of unique values for a field+ */+ getUniqueValues(field: KeplerField): Datum[] {+ const {getRowValue, getIterable} = this.accessors;+ const accessor = getRowValue(field);+ const uniques = new Set<Datum>();++ for (const tile of this.tiles) {+ // Check the cache+ let tileUniques = this.getTileStat(tile, field, 'uniqueValues');+ if (!tileUniques) {+ // Cache miss, calculate and cache+ tileUniques = getTileUniqueValues(getIterable(tile), field, accessor);+ this.setTileStat(tile, field, 'uniqueValues', tileUniques);+ }+ for (const val of tileUniques ?? []) {+ uniques.add(val);+ }+ }+ return [...uniques];+ }++ private getTileStat<K extends keyof TileFieldStats>(+ tile: T,+ field: KeplerField,+ stat: K+ ): TileFieldStats[K] {+ return this.tileStats.get(this.accessors.getTileId(tile))?.get(field.name)?.[stat];+ }++ private setTileStat<K extends keyof TileFieldStats>(+ tile: T,+ field: KeplerField,+ stat: K,+ value: TileFieldStats[K]+ ): void {+ const tileId = this.accessors.getTileId(tile);+ const tileStats = this.tileStats.get(tileId) ?? new Map();+ const tileFieldStats = tileStats.get(field.name) ?? {};+ tileFieldStats[stat] = value;+ tileStats.set(field.name, tileFieldStats);+ this.tileStats.set(tileId, tileStats);+ }+}++/**+ * Get the min/max domain of a field in a given tile+ */+function getTileExtent<I extends Iterable<any>>(+ iterable: I,+ field: KeplerField,+ accessor: RowValueAccessor<I>+): [number, number] | undefined {+ let min = Infinity;+ let max = -Infinity;+ for (const row of iterable) {+ const val = accessor(field, row) as number | null;+ if (val === null) continue;+ if (val < min) min = val;+ if (val > max) max = val;+ }+ return Number.isFinite(min) && Number.isFinite(max) ? [min, max] : undefined;+}++/**+ * Get unique values for a field in a given tile+ */+function getTileUniqueValues<I extends Iterable<any>>(+ iterable: I,+ field: KeplerField,+ accessor: RowValueAccessor<I>,+ maxUniques = 20+): Set<Datum> {+ const uniques = new Set<Datum>();+ for (const row of iterable) {+ if (uniques.size >= maxUniques) return uniques;+ uniques.add(accessor(field, row));+ }+ return uniques;+}++function areEqualSets(a: Set<string>, b: Set<string>): boolean {+ if (a.size !== b.size) return false;+ for (const val of a) {+ if (!b.has(val)) {+ return false;+ }+ }+ return true;+}diff --git a/src/layers/src/vector-tile/common-tile/tile-utils.ts b/src/layers/src/vector-tile/common-tile/tile-utils.tsnew file mode 100644index 000000000..41276377e--- /dev/null+++ b/src/layers/src/vector-tile/common-tile/tile-utils.ts@@ -0,0 +1,50 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {bisectLeft} from 'd3-array';++import {DomainStops, Field as KeplerField, ZoomStopsConfig} from '@kepler.gl/types';+import {DomainQuantiles} from '@kepler.gl/utils';++// helper functions+export function isDomainStops(domain: unknown): domain is DomainStops {+ return (+ typeof domain === 'object' &&+ domain !== null &&+ Array.isArray((domain as any).stops) &&+ Array.isArray((domain as any).z)+ );+}++export function isDomainQuantiles(domain: unknown): domain is DomainQuantiles {+ return (+ typeof domain === 'object' &&+ domain !== null &&+ Array.isArray((domain as any).quantiles) &&+ Array.isArray((domain as any).z)+ );+}++export function isIndexedField(field?: KeplerField | null): boolean {+ return Boolean(field && field.indexBy);+}++export function getPropertyByZoom(+ config: ZoomStopsConfig | undefined,+ defaultValue: number+): (zoom: number) => number {+ if (!config || !config.enabled || !Array.isArray(config.stops)) {+ return () => defaultValue;+ }+ const {stops} = config;+ const zSteps = stops.map(st => st[0]);++ return zoom => {+ const i = bisectLeft(zSteps, zoom);+ if (i === 0) {+ return stops[i][1];+ }++ return stops[i - 1][1];+ };+}diff --git a/src/layers/src/vector-tile/mvt-layer.ts b/src/layers/src/vector-tile/mvt-layer.tsnew file mode 100644index 000000000..4bfe933aa--- /dev/null+++ b/src/layers/src/vector-tile/mvt-layer.ts@@ -0,0 +1,105 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {Layer, LayersList} from '@deck.gl/core/typed';+import {ClipExtension} from '@deck.gl/extensions/typed';+import {+ MVTLayer as _MVTLayer,+ TileLayer,+ _getURLFromTemplate,+ _TileLoadProps,+ _Tile2DHeader+} from '@deck.gl/geo-layers/typed';++/*+ Custom MVT layer that works with MVTSource and PMTileSource.+ Changes:+ - getTileData: handles props.getTileData.+ - renderSubLayers: removed coordinates logic present in original MVTLayer:renderSubLayers.+ - renderSubLayers: set clipBounds.+ - loaders.gl & older deck.gl: geojson-table: data = data.features+*/++// @ts-expect-error need to patch private methods because of newer loaders.gl+export class MVTLayer<ExtraProps> extends _MVTLayer<ExtraProps> {+ getTileData(tile: _TileLoadProps): any {+ const {getTileData} = this.props;+ const {data} = this.state;++ tile.url =+ typeof data === 'string' || Array.isArray(data) ? _getURLFromTemplate(data, tile) : null;+ if (getTileData) {+ return getTileData(tile);+ }+ return null;+ }++ renderSubLayers(+ props: TileLayer['props'] & {+ id: string;+ data: any;+ _offset: number;+ tile: any;+ clipBounds?: number[];+ }+ ): Layer | null | LayersList {+ const {boundingBox} = props.tile;++ props.autoHighlight = true;++ if (boundingBox) {+ props.clipBounds = [...boundingBox[0], ...boundingBox[1]];+ props.extensions = [...(props.extensions || []), new ClipExtension()];+ }++ return this.props.renderSubLayers(props);+ }++ getHighlightedObjectIndex(tile: _Tile2DHeader): number {+ const {hoveredFeatureId, hoveredFeatureLayerName} = this.state;+ const {uniqueIdProperty, highlightedFeatureId} = this.props;+ let data = tile.content;+ data = data?.shape === 'geojson-table' ? data.features : data;++ const isHighlighted = isFeatureIdDefined(highlightedFeatureId);+ const isFeatureIdPresent = isFeatureIdDefined(hoveredFeatureId) || isHighlighted;++ if (!isFeatureIdPresent) {+ return -1;+ }++ const featureIdToHighlight = isHighlighted ? highlightedFeatureId : hoveredFeatureId;++ // Iterable data+ if (Array.isArray(data)) {+ return data.findIndex(feature => {+ const isMatchingId = getFeatureUniqueId(feature, uniqueIdProperty) === featureIdToHighlight;+ const isMatchingLayer =+ isHighlighted || getFeatureLayerName(feature) === hoveredFeatureLayerName;+ return isMatchingId && isMatchingLayer;+ });+ }++ return -1;+ }+}++function getFeatureUniqueId(feature, uniqueIdProperty: string | undefined) {+ if (feature.properties && uniqueIdProperty) {+ return feature.properties[uniqueIdProperty];+ }++ if ('id' in feature) {+ return feature.id;+ }++ return undefined;+}++function getFeatureLayerName(feature): string | null {+ return feature.properties?.layerName || null;+}++function isFeatureIdDefined(value: unknown): boolean {+ return value !== undefined && value !== null && value !== '';+}diff --git a/src/layers/src/vector-tile/utils/vector-tile-utils.spec.ts b/src/layers/src/vector-tile/utils/vector-tile-utils.spec.tsnew file mode 100644index 000000000..67ae2d266--- /dev/null+++ b/src/layers/src/vector-tile/utils/vector-tile-utils.spec.ts@@ -0,0 +1,255 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {DATA_TYPES} from 'type-analyzer';++import {ALL_FIELD_TYPES, FILTER_TYPES} from '@kepler.gl/constants';++import {getTileUrl, getMetaUrl, parseVectorMetadata as parseMetadata} from './vector-tile-utils';+import {PMTILES_METADATA, MVT_METADATA} from '../../../../../test/fixtures/tile-metadata';++describe('getTileUrl', () => {+ [+ {name: 'empty string', input: '', expected: null},+ {name: 'invalid URL', input: '/foo/bar/baz.mvt', expected: null},+ {+ name: 'Valid URL, no placeholders',+ input: 'http://www.example.com',+ expected: 'http://www.example.com/{z}/{x}/{y}.pbf'+ },+ {+ name: 'Valid URL, no placeholders, trailing slash',+ input: 'http://www.example.com/',+ expected: 'http://www.example.com/{z}/{x}/{y}.pbf'+ },+ {+ name: 'Valid URL, placeholders',+ input: 'http://www.example.com/{z}/{x}/{y}.pbf',+ expected: 'http://www.example.com/{z}/{x}/{y}.pbf'+ },+ {+ name: 'Valid URL, placeholders, mvt',+ input: 'http://www.example.com/{z}/{x}/{y}.mvt',+ expected: 'http://www.example.com/{z}/{x}/{y}.mvt'+ },+ {+ name: 'Valid URL, placeholders, mvt, access token',+ input: 'https://api.mapbox.com/v4/spam/{z}/{x}/{y}.mvt?access_token=sk.fobar.baz',+ expected: 'https://api.mapbox.com/v4/spam/{z}/{x}/{y}.mvt?access_token=sk.fobar.baz'+ }+ ].forEach(({name, input, expected}) => {+ test(name, () => {+ expect(getTileUrl(input)).toBe(expected);+ });+ });+});++describe('getMetaUrl', () => {+ [+ {name: 'empty string', input: '', expected: null},+ {name: 'invalid URL', input: '/foo/bar/baz.mvt', expected: null},+ {+ name: 'Valid URL, unknown domain',+ input: 'http://www.example.com/some_tiles',+ expected: 'http://www.example.com/some_tiles/metadata.json'+ },+ {+ name: 'Valid URL, unknown domain, trailing slash',+ input: 'http://www.example.com/some_tiles/',+ expected: 'http://www.example.com/some_tiles/metadata.json'+ },+ {+ name: 'Valid URL, Mapbox domain',+ input: 'https://api.mapbox.com/v4/spam/{z}/{x}/{y}.mvt',+ expected: 'https://api.mapbox.com/v4/spam.json'+ },+ {+ name: 'Valid URL, placeholders, mvt, access token',+ input: 'https://api.mapbox.com/v4/spam/{z}/{x}/{y}.mvt?access_token=sk.fobar.baz',+ expected: 'https://api.mapbox.com/v4/spam.json?access_token=sk.fobar.baz'+ },+ {+ name: 'Valid URL, placeholders, mvt, access token, published style version',+ input:+ 'https://api.mapbox.com/v4/spam/{z}/{x}/{y}.mvt?style=mapbox://styles/user/styleId@00&access_token=pk.xxx.zzz',+ expected:+ 'https://api.mapbox.com/v4/spam.json?style=mapbox://styles/user/styleId@00&access_token=pk.xxx.zzz'+ }+ ].forEach(({name, input, expected}) => {+ test(name, () => {+ expect(getMetaUrl(input)).toBe(expected);+ });+ });+});++test('parseMetadata, metadata from MVTSource and Mapbox URL', () => {+ expect(+ parseMetadata(MVT_METADATA, {tileUrl: 'https://api.mapbox.com/v4/spam/{z}/{x}/{y}.mvt'})+ ).toEqual({+ metaJson: null,+ bounds: [-180, -85, 180, 85],+ center: [0, 0, 0],+ maxZoom: 16,+ minZoom: 0,+ fields: [+ {+ name: 'class',+ id: 'class',+ format: '',+ filterProps: {+ domain: [],+ value: [],+ type: 'multiSelect',+ gpu: false+ },+ type: 'string',+ analyzerType: 'STRING'+ }+ ],+ name: 'Mapbox Streets v8',+ description: ''+ });+});++test('parseMetadata, PMTiles from PMTileSource', () => {+ expect(+ parseMetadata(PMTILES_METADATA, {+ tileUrl:+ 'https://4sq-studio-data-staging.s3.us-west-2.amazonaws.com/some_path/some_file.pmtiles'+ })+ ).toEqual({+ name: 'My Custom Tiles',+ description: 'My Custom Tiles Description',+ metaJson: null,+ bounds: [-150.1122219, -51.8952777, 179.3577783, 69.6043747],+ center: [14.0625, 50.7026397, 6],+ maxZoom: 6,+ minZoom: 0,+ fields: [+ {+ id: 'metric',+ name: 'metric',+ format: '',+ type: ALL_FIELD_TYPES.real,+ analyzerType: DATA_TYPES.FLOAT,+ filterProps: {+ domain: [-1, 10],+ value: [-1, 10],+ type: FILTER_TYPES.range,+ typeOptions: [FILTER_TYPES.range],+ gpu: true,+ step: 0.01+ }+ },+ {+ id: 'continent',+ name: 'continent',+ format: '',+ type: ALL_FIELD_TYPES.string,+ analyzerType: DATA_TYPES.STRING,+ filterProps: {+ domain: ['Africa', 'Asia', 'Europe', 'North America', 'Oceania', 'South America'],+ value: ['Africa', 'Asia', 'Europe', 'North America', 'Oceania', 'South America'],+ type: FILTER_TYPES.multiSelect,+ gpu: false+ }+ }+ ]+ });+});++test('parseMetadata, empty input', () => {+ expect(parseMetadata({})).toEqual({+ name: '',+ description: '',+ metaJson: null,+ bounds: null,+ center: null,+ maxZoom: null,+ minZoom: null,+ fields: []+ });++ expect(+ parseMetadata({}, {tileUrl: 'http://xyz.api.here.com/some_id/tile/web/{z}_{x}_{y}.pbf'})+ ).toEqual({+ name: '',+ description: '',+ metaJson: null,+ bounds: null,+ center: null,+ maxZoom: null,+ minZoom: null,+ fields: []+ });++ expect(parseMetadata({}, {tileUrl: 'https://api.mapbox.com/v4/spam/{z}/{x}/{y}.mvt'})).toEqual({+ name: '',+ description: '',+ metaJson: null,+ bounds: null,+ center: null,+ maxZoom: null,+ minZoom: null,+ fields: []+ });+});++describe('parseMetadata, bad cases', () => {+ [+ {name: 'empty string', input: '', expected: null},+ {name: 'null', input: null, expected: null},+ {name: 'undefined', input: undefined, expected: null},+ {name: 'string', input: 'spam', expected: null}+ ].forEach(({name, input, expected}) => {+ test(name, () => {+ expect(parseMetadata(input as any)).toBe(expected);+ });+ });+});++describe('getTileUrl', () => {+ test.each([+ ['', null],+ ['foo', null],+ ['http', null],+ ['http://', null],+ ['https://', null],+ ['http://mytiles.com', 'http://mytiles.com/{z}/{x}/{y}.pbf'],+ ['https://mytiles.com', 'https://mytiles.com/{z}/{x}/{y}.pbf'],+ ['http://mytiles.com/', 'http://mytiles.com/{z}/{x}/{y}.pbf'],+ ['http://mytiles.com/foo', 'http://mytiles.com/foo/{z}/{x}/{y}.pbf'],+ ['http://mytiles.com/foo/', 'http://mytiles.com/foo/{z}/{x}/{y}.pbf'],+ ['https://mytiles.com/foo/bar.baz', 'https://mytiles.com/foo/bar.baz/{z}/{x}/{y}.pbf'],+ ['http://localhost', 'http://localhost/{z}/{x}/{y}.pbf'],+ ['http://localhost/', 'http://localhost/{z}/{x}/{y}.pbf'],+ ['http://localhost:8080', 'http://localhost:8080/{z}/{x}/{y}.pbf'],+ ['http://localhost:8080/', 'http://localhost:8080/{z}/{x}/{y}.pbf'],+ ['http://mytiles.com/foo/{z}/{x}/{y}.mvt', 'http://mytiles.com/foo/{z}/{x}/{y}.mvt']+ ])('%s', (input, expected) => {+ expect(getTileUrl(input)).toEqual(expected);+ });+});++describe('getMetaUrl', () => {+ test.each([+ ['', null],+ ['foo', null],+ ['http', null],+ ['http://', null],+ ['https://', null],+ ['http://mytiles.com', 'http://mytiles.com/metadata.json'],+ ['https://mytiles.com', 'https://mytiles.com/metadata.json'],+ ['http://mytiles.com/', 'http://mytiles.com/metadata.json'],+ ['http://mytiles.com/foo', 'http://mytiles.com/foo/metadata.json'],+ ['http://mytiles.com/foo/', 'http://mytiles.com/foo/metadata.json'],+ ['https://mytiles.com/foo/bar.baz', 'https://mytiles.com/foo/bar.baz/metadata.json'],+ ['http://localhost', 'http://localhost/metadata.json'],+ ['http://localhost/', 'http://localhost/metadata.json'],+ ['http://localhost:8080', 'http://localhost:8080/metadata.json'],+ ['http://localhost:8080/', 'http://localhost:8080/metadata.json'],+ ['http://mytiles.com/foo/{z}/{x}/{y}.mvt', 'http://mytiles.com/foo/metadata.json']+ ])('%s', (input, expected) => {+ expect(getMetaUrl(input)).toEqual(expected);+ });+});diff --git a/src/layers/src/vector-tile/utils/vector-tile-utils.ts b/src/layers/src/vector-tile/utils/vector-tile-utils.tsnew file mode 100644index 000000000..5928480bf--- /dev/null+++ b/src/layers/src/vector-tile/utils/vector-tile-utils.ts@@ -0,0 +1,698 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {ascending} from 'd3-array';+import Console from 'global/console';+import uniq from 'lodash.uniq';+import {DATA_TYPES} from 'type-analyzer';++import {TileJSON} from '@loaders.gl/mvt';+import {PMTilesMetadata} from '@loaders.gl/pmtiles';++import {+ analyzerTypeToFieldType,+ containValidTime,+ notNullorUndefined as notNullOrUndefined,+ parseUri+} from '@kepler.gl/common-utils';+import {ALL_FIELD_TYPES, FILTER_TYPES} from '@kepler.gl/constants';+import {+ FilterProps,+ NumericFieldFilterProps,+ BooleanFieldFilterProps,+ StringFieldFilterProps,+ KeplerTable as KeplerDataset+} from '@kepler.gl/table';+import {Field as KeplerField} from '@kepler.gl/types';+import {clamp, formatNumberByStep, getNumericStepSize, timeToUnixMilli} from '@kepler.gl/utils';++import {default as BaseLayer} from '../../base-layer';++export enum DatasetType {+ LOCAL = 'local',+ VECTOR_TILE = 'vectorTile'+}++export enum TileType {+ VECTOR_TILE = 'vectorTile'+}++export enum VectorTileType {+ MVT = 'mvt',+ PMTILES = 'pmtiles'+}++export type VectorTileDatasetMetadata = {+ type: VectorTileType;+ tilesetDataUrl: string;+ tilesetMetadataUrl?: string;+};++export const getLoaderOptions = () => {+ return {+ mvt: {+ workerUrl: `https://unpkg.com/@loaders.gl/mvt/dist/mvt-worker.js`+ }+ };+};++export function isTileDataset(dataset: KeplerDataset | {type: string}): boolean {+ return Boolean(dataset?.type === DatasetType.VECTOR_TILE);+}++type VectorTileField = {+ analyzerType: string;+ name: string;+ id: string;+ format: string;+ type: string;+ filterProps?: FilterProps;+};++export type VectorTileMetadata = {+ metaJson: any | null;+ bounds: number[] | null;+ center: number[] | null;+ maxZoom: number | null;+ minZoom: number | null;+ name?: string;+ description?: string;+ fields: VectorTileField[];+};++type TilesetMetadata = VectorTileMetadata;++export type TippecanoeLayerAttribute = {+ attribute?: string;+ type?: string;+ min?: number;+ max?: number;+ values: string[] | number[];+};++type TippecanoeLayer = {+ attributes?: TippecanoeLayerAttribute[];+};+type TilesetFunction = {+ getMetaUrl: (s: string) => string | null;+ parseMetadata: (d: any, options?: any) => TilesetMetadata | null;+};+// https://github.com/mapbox/tilejson-spec/tree/master/2.2.0+function isLat(num: any): boolean {+ return Number.isFinite(num) && num <= 90 && num >= -90;+}+function isLng(num: any): boolean {+ return Number.isFinite(num) && num <= 180 && num >= -180;+}+function isZoom(num: any): boolean {+ return Number.isFinite(num) && num >= 0 && num <= 22;+}+function fromArrayOrString(data: string | number[]): number[] | null {+ if (typeof data === 'string') {+ return data.split(',').map(parseFloat);+ } else if (Array.isArray(data)) {+ return data;+ }+ return null;+}++function parseCenter(center: string | number[]): number[] | null {+ // supported formats+ // string: "-96.657715,40.126127,-90.140061,43.516689",+ // array: [-91.505127,41.615442,14]+ const result = fromArrayOrString(center);+ if (+ Array.isArray(result) &&+ result.length === 3 &&+ isLng(result[0]) &&+ isLat(result[1]) &&+ isZoom(result[2])+ ) {+ return result;+ }+ return null;+}++/**+ * bounds should be [minLng, minLat, maxLng, maxLat]+ * @param {*} bounds+ */+function parseBounds(bounds: string | number[]): number[] | null {+ // supported formats+ // string: "-96.657715,40.126127,-90.140061,43.516689",+ // array: [ -180, -85.05112877980659, 180, 85.0511287798066 ]+ const result = fromArrayOrString(bounds);+ // validate bounds+ if (+ Array.isArray(result) &&+ result.length === 4 &&+ [result[0], result[2]].every(isLng) &&+ [result[1], result[3]].every(isLat)+ ) {+ return result;+ }+ return null;+}++const X_PATT = /\{x\}/;+const Y_PATT = /\{y\}/;+const Z_PATT = /\{z\}/;++function isFullyQualifiedTileUrl(tileUrl: string): boolean {+ return X_PATT.test(tileUrl) && Y_PATT.test(tileUrl) && Z_PATT.test(tileUrl);+}++/**+ * Normalize tile URL+ * @param {string} tileUrl Initial tile URL, which may be either the root URL for the+ * tileset or a fully qualified template+ * @param {function} validateUrl function to validate tile URL+ * @return {string|null} Fully qualified tile URL template, or null if input does not+ * appear to be a valid URL+ */+export function getTileUrl(+ tileUrl: string,+ validateUrl: (s: string) => boolean = isFullyQualifiedTileUrl+): string | null {+ // Check for a valid URL. Ideally we'd have a simple method here.+ const uriParts = parseUri(tileUrl);+ if (!uriParts.protocol || !uriParts.host) {+ return null;+ }+ if (validateUrl(tileUrl)) {+ return tileUrl;+ }+ return `${tileUrl.replace(/\/$/, '')}/{z}/{x}/{y}.pbf`;+}++/**+ * Map of util functions for different tileset types, keyed by host+ */+const TILESET_FUNCTIONS: {[key: string]: TilesetFunction} = {+ 'api.mapbox.com': {+ getMetaUrl: getMetaUrlMapbox,+ parseMetadata: parseMetadataTileJSON+ },+ default: {+ getMetaUrl: getMetaUrlTippecanoe,+ parseMetadata: parseMetadataTileJSON+ }+};++function getTilesetFunctions(tileUrl: string | null): TilesetFunction {+ let host = '';+ try {+ host = new URL(tileUrl || '').hostname;+ } catch (error) {+ // do nothing+ }++ return TILESET_FUNCTIONS[host] || TILESET_FUNCTIONS.default;+}++/**+ * Get the metadata URL for a given tileset+ */+export function getMetaUrl(tileUrl: string): string | null {+ return getTilesetFunctions(tileUrl).getMetaUrl(tileUrl);+}+type ParseMetadataOption = {+ tileUrl?: string | null;+};+/**+ * Parse the metadata for a given tileset+ */+export function parseVectorMetadata(+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types+ metadata: PMTilesMetadata | TileJSON,+ option?: ParseMetadataOption+): TilesetMetadata | null {+ const {tileUrl = ''} = option || {};+ return getTilesetFunctions(tileUrl).parseMetadata(metadata);+}++const MAPBOX_URL_PATT = /\/\{z\}\/\{x\}\/\{y\}\.mvt/;++function getMetaUrlMapbox(tileUrl = ''): string {+ return tileUrl.replace(MAPBOX_URL_PATT, '.json');+}++function parseMetadataTileJSON(metadata: PMTilesMetadata | TileJSON): TilesetMetadata | null {+ const parsed = parseMetadataTippecanoeFromDataSource(metadata);+ if (!parsed) return null;+ // Fields already parsed from `json` property+ if (parsed.fields?.length) {+ return parsed;+ }++ return parsed;+}++function getMetaUrlTippecanoe(tileUrl) {+ const url = getTileUrl(tileUrl);+ if (!url) return null;+ // assumes the structure <url_base>/{z}...+ const baseUrl = url.split(Z_PATT)[0].replace(/\/$/, '');+ return `${baseUrl}/metadata.json`;+}++/**+ * Special parsing for metadata returned by MVTSource and PMTilesSource.+ * @param metadata Tileset metadata parsed by a DataSouce+ * @returns Metadata in Kepler-friendly format.+ */+function parseMetadataTippecanoeFromDataSource(+ metadata: PMTilesMetadata | TileJSON | null+): TilesetMetadata | null {+ if (!metadata || typeof metadata !== 'object') {+ return null;+ }++ let result: TilesetMetadata = {+ metaJson: null,+ bounds: null,+ center: null,+ maxZoom: null,+ minZoom: null,+ fields: []+ };++ const mvtMetadata = metadata as TileJSON;+ const pmTileMetadata = metadata as PMTilesMetadata;++ // try to parse metaJson+ if (typeof mvtMetadata.metaJson === 'string') {+ try {+ result.metaJson = JSON.parse(mvtMetadata.metaJson);+ } catch (err) {+ // do nothing+ }+ } else if (typeof mvtMetadata.metaJson === 'object') {+ result.metaJson = mvtMetadata.metaJson;+ }++ result.bounds = parseBounds(+ Array.isArray(metadata.boundingBox) ? metadata.boundingBox.flat() : ''+ );++ // PMTileSource has centerZoom and center [lon, lat], MVTSource - [lon, lat, zoom]+ const center =+ pmTileMetadata.centerZoom !== undefined && Array.isArray(metadata.center)+ ? [...metadata.center, pmTileMetadata.centerZoom]+ : metadata.center;+ result.center = parseCenter(center || '');++ result.maxZoom = safeParseFloat(metadata.maxZoom);+ result.minZoom = safeParseFloat(metadata.minZoom);+ result.name = metadata.name || '';+ result.description = mvtMetadata.description || pmTileMetadata.tilejson?.description || '';++ if (Array.isArray(pmTileMetadata.tilejson?.layers)) {+ const layers = pmTilesLayerToTippecanoeLayer(pmTileMetadata.tilejson?.layers);+ result.fields = collectAttributes(layers);+ } else if (Array.isArray(mvtMetadata.layers)) {+ const layers = pmTilesLayerToTippecanoeLayer(mvtMetadata.layers);+ result.fields = collectAttributes(layers);+ }++ result = {+ ...result,+ ...parseMetaJson(result.metaJson)+ };++ return result;+}++function safeParseFloat(input: unknown): number | null {+ const result =+ typeof input === 'string' ? parseFloat(input) : typeof input === 'number' ? input : null;+ return result === null || isNaN(result) ? null : result;+}++function parseMetaJson(metaJson: any): {fields: VectorTileField[]} | null {+ if (!metaJson || typeof metaJson !== 'object') {+ return null;+ }++ if (metaJson.tilestats && Array.isArray(metaJson.tilestats.layers)) {+ // we are in luck!+ return {fields: collectAttributes(metaJson.tilestats.layers)};+ }+ return null;+}++function getTimeAnimationDomain(mappedValue: number[]): {+ domain: number[];+ timeSteps: number[];+ mappedValue: number[];+ duration: number;+} {+ const timeSteps = uniq(mappedValue).sort(ascending).filter(notNullOrUndefined);++ const domain = [timeSteps[0], timeSteps[timeSteps.length - 1]];++ // if taks 10 * 1000 ms to finish the entire animation+ const duration = 10000 / timeSteps.length;+ const clamped = clamp([100, 2000], duration);++ return {domain, timeSteps, duration: clamped, mappedValue};+}++const pmTileTypeToAttrMap = {+ float32: 'number',+ string: 'string',+ utf8: 'string',+ int: 'int',+ boolean: 'boolean'+};++/**+ * Transform TileJSON['layers'] back to TippecanoeLayer+ */+function pmTilesLayerToTippecanoeLayer(layers: TileJSON['layers']): TippecanoeLayer[] {+ if (!layers) return [];++ const outLayers: TippecanoeLayer[] = [];+ for (const layer of layers) {+ const {fields = []} = layer || {};+ for (const pmField of fields) {+ const attribute = {+ attribute: pmField.name,+ type: pmTileTypeToAttrMap[pmField.type],+ count: pmField.uniqueValueCount,+ values: (pmField.values ?? []) as number[] | string[],+ min: pmField.min,+ max: pmField.max+ };++ outLayers.push({+ attributes: [attribute]+ });+ }+ }+ return outLayers;+}++function collectAttributes(layers: TippecanoeLayer[] = []): VectorTileField[] {+ const fields = {};+ const indexedAttributes: {[key: string]: TippecanoeLayerAttribute[]} = {};++ for (const layer of layers) {+ const {attributes = []} = layer || {};+ for (const attr of attributes) {+ const name = attr.attribute;+ if (typeof name === 'string') {+ // eslint-disable-next-line max-depth+ if (name.split('|').length > 1) {+ // indexed field+ const fname = name.split('|')[0];+ indexedAttributes[fname] = indexedAttributes[fname] || [];+ indexedAttributes[fname].push(attr);+ } else if (!fields[name]) {+ fields[name] = attributeToField(attr);+ } else {+ mergeAttributeDomain(fields[name], attr);+ }+ }+ }+ }++ // parse indexed attribute, and put index key unidentified back as normal field+ for (const [name, attrs] of Object.entries(indexedAttributes)) {+ const {indexedField, unidentified} = parseIndexedField(name, attrs);+ fields[indexedField.name] = indexedField;+ for (const unidentifiedAttr of unidentified) {+ if (unidentifiedAttr.attribute) {+ fields[unidentifiedAttr.attribute] =+ fields[unidentifiedAttr.attribute] || attributeToField(unidentifiedAttr);+ mergeAttributeDomain(fields[unidentifiedAttr.attribute], unidentifiedAttr);+ }+ }+ }++ return Object.values(fields);+}++function getIndexKeyFromFieldName(name: string): string | null {+ return name && name.split('|').length > 1 ? name.split('|')[1] : null;+}++export type FieldIndexBy = {+ format: string;+ type: string;+ mappedValue: {+ [key: string]: string;+ };+};++function parseIndexedField(+ name: string,+ attrs: TippecanoeLayerAttribute[]+): {+ unidentified: TippecanoeLayerAttribute[];+ indexedField: VectorTileField;+} {+ const unidentified: TippecanoeLayerAttribute[] = [];+ // analyze time format+ let field;++ for (const attr of attrs) {+ const fieldName = attr.attribute;+ const indexKey = getIndexKeyFromFieldName(fieldName || '');+ const analyzedType = indexKey && containValidTime([indexKey]);+ if (analyzedType) {+ field = field || {+ ...attributeToField(attr),+ // overide name and id to truncated name+ name,+ id: name,+ indexBy: {+ format: analyzedType.format,+ type: analyzerTypeToFieldType(analyzedType.type),+ mappedValue: {}+ }+ };+ mergeAttributeDomain(field, attr);+ // save epoch time in mappedValue+ let fieldTs: string | number | null = indexKey;+ if (analyzedType.format === 'x' || analyzedType.format === 'X') {+ fieldTs = Number(indexKey);+ }+ const epoch = timeToUnixMilli(fieldTs, analyzedType.format);+ if (epoch) {+ field.indexBy.mappedValue[epoch] = fieldName;+ }+ } else {+ // key is not valid timestamp+ unidentified.push(attr);+ }+ }++ if (field.indexBy && field.indexBy.type === ALL_FIELD_TYPES.timestamp) {+ field.indexBy.timeDomain = getTimeAnimationDomain(+ Object.keys(field.indexBy.mappedValue).map(Number)+ );+ }++ return {unidentified, indexedField: field};+}++function compare(num1: number | undefined, num2: number | undefined, operator: string): number {+ return Number.isFinite(num1) && Number.isFinite(num2)+ ? Math[operator](num1 as number, num2 as number)+ : Number.isFinite(num1)+ ? num1+ : Number.isFinite(num2)+ ? num2+ : NaN;+}++export function getFilterProps(+ fieldType: string,+ attribute: TippecanoeLayerAttribute+): FilterProps {+ switch (fieldType) {+ case ALL_FIELD_TYPES.real:+ case ALL_FIELD_TYPES.integer: {+ const [min, max] = getAttributeDomain(fieldType, attribute);+ const diff = max - min;+ const step = getNumericStepSize(diff) || 0.1;+ const domain = [+ formatNumberByStep(min, step, 'floor'),+ formatNumberByStep(max, step, 'ceil')+ ];+ const filterProps: NumericFieldFilterProps = {+ domain,+ value: domain as [number, number],+ type: FILTER_TYPES.range,+ typeOptions: [FILTER_TYPES.range],+ gpu: true,+ step+ };+ return filterProps;+ }++ case ALL_FIELD_TYPES.boolean: {+ const filterProps: BooleanFieldFilterProps = {+ domain: [true, false],+ value: true,+ type: FILTER_TYPES.select,+ gpu: true+ };+ return filterProps;+ }++ default: {+ // Assume string for all other fields+ const filterProps: StringFieldFilterProps = {+ domain: attribute.values as string[],+ value: attribute.values as string[],+ type: FILTER_TYPES.multiSelect,+ gpu: false+ };+ return filterProps;+ }+ }+}++function attributeToField(attribute: TippecanoeLayerAttribute = {values: []}): VectorTileField {+ // attribute: "_season_peaks_color"+ // count: 1000+ // max: 0.95+ // min: 0.24375+ // type: "number"+ const fieldTypes = attributeTypeToFieldType(attribute.type);+ return {+ name: attribute.attribute as string,+ id: attribute.attribute as string,+ format: '',+ filterProps: getFilterProps(fieldTypes.type, attribute),+ ...fieldTypes+ };+}++function getAttributeDomain(type: string | null, attribute: TippecanoeLayerAttribute): number[] {+ switch (type) {+ case ALL_FIELD_TYPES.real:+ case ALL_FIELD_TYPES.integer:+ return [+ Number.isFinite(attribute.min) ? (attribute.min as number) : NaN,+ Number.isFinite(attribute.max) ? (attribute.max as number) : NaN+ ];++ case ALL_FIELD_TYPES.boolean:+ return [0, 1];+ default:+ return [0, 1];+ }+}++/**+ * @param field This function mutates the field parameter+ */+function mergeAttributeDomain(field: KeplerField, attribute: TippecanoeLayerAttribute): void {+ switch (field.type) {+ case ALL_FIELD_TYPES.real:+ case ALL_FIELD_TYPES.integer: {+ const domain = field.metadata?.domain || field.filterProps?.domain;+ if (domain) {+ domain.min = compare(attribute.min, domain[0], 'min');+ domain.max = compare(attribute.max, domain[1], 'max');+ }+ return;+ }+ default:+ return;+ }+}++// possible types https://github.com/mapbox/tippecanoe#modifying-feature-attributes+const attrTypeMap = {+ number: {+ type: ALL_FIELD_TYPES.real,+ analyzerType: DATA_TYPES.FLOAT+ },+ numeric: {+ type: ALL_FIELD_TYPES.real,+ analyzerType: DATA_TYPES.FLOAT+ },+ string: {+ type: ALL_FIELD_TYPES.string,+ analyzerType: DATA_TYPES.STRING+ },+ vachar: {+ type: ALL_FIELD_TYPES.string,+ analyzerType: DATA_TYPES.STRING+ },+ float: {+ type: ALL_FIELD_TYPES.real,+ analyzerType: DATA_TYPES.FLOAT+ },+ int: {+ type: ALL_FIELD_TYPES.integer,+ analyzerType: DATA_TYPES.INT+ },+ int4: {+ type: ALL_FIELD_TYPES.integer,+ analyzerType: DATA_TYPES.INT+ },+ boolean: {+ type: ALL_FIELD_TYPES.boolean,+ analyzerType: DATA_TYPES.BOOLEAN+ },+ bool: {+ type: ALL_FIELD_TYPES.boolean,+ analyzerType: DATA_TYPES.BOOLEAN+ }+};++function attributeTypeToFieldType(aType?: string): {type: string; analyzerType: string} {+ const type = aType?.toLowerCase();+ if (!type || !attrTypeMap[type]) {+ Console.warn(+ `cannot convert attribute type ${type} to kepler.gl data type, use string by default`+ );+ return attrTypeMap.string;+ }++ return attrTypeMap[type];+}++/**+ * Returns true if a dataset can be used as source data for a layer.+ * @param dataset A dataset.+ * @param layer A layer.+ * @returns Returns true if a dataset can be used as source data for a layer.+ */+export function matchDatasetType(dataset: KeplerDataset, layer: BaseLayer): boolean {+ // allow selection if type is not assigned yet+ if (!layer.type) {+ return true;+ }+ // allow selection if is current selected dataset+ if (layer.config.dataId === dataset.id) {+ return true;+ }+ // allow selection if layer doesn't have supportedDatasetTypes+ if (!layer.supportedDatasetTypes) {+ return true;+ }++ return (+ Array.isArray(layer.supportedDatasetTypes) &&+ layer.supportedDatasetTypes.includes(dataset.type || '')+ );+}++/**+ * Remove null/0 values from the bottom of the quantiles. If the column has many nulls+ * or 0s at the bottom of the quantiles, it will wash out color scales and produce+ * meaningless "no value" legend entries. We want to keep the first 0 and no others.+ * Operates in place.+ */+export function pruneQuantiles(quantiles: number[]): void {+ const firstNonZeroIdx = quantiles.findIndex(d => d !== null && d !== 0);+ if (firstNonZeroIdx > 0) {+ quantiles.splice(0, firstNonZeroIdx - 1);+ }+}diff --git a/src/layers/src/vector-tile/vector-tile-icon.tsx b/src/layers/src/vector-tile/vector-tile-icon.tsxnew file mode 100644index 000000000..ce0344545--- /dev/null+++ b/src/layers/src/vector-tile/vector-tile-icon.tsx@@ -0,0 +1,78 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import PropTypes from 'prop-types';+import React, {Component} from 'react';++import {Base} from '../base';++class VectorTileIcon extends Component {+ static propTypes = {+ height: PropTypes.string,+ colors: PropTypes.arrayOf(PropTypes.string.isRequired)+ };++ static defaultProps = {+ height: '16px',+ predefinedClassName: 'vector-tile-layer-icon',+ totalColor: 2+ };++ render(): React.ReactNode {+ return (+ <Base {...this.props}>+ <g id="vector-tile-layer">+ <polygon+ id="Path"+ points="12.1949893 42.9771532 3 37.4596134 12.2699446 31.9771532 21 37.45407"+ style={{opacity: 0.6}}+ />+ <polygon+ id="Path-Copy-3"+ points="22.2609912 37 13.0660019 31.4824602 22.3359466 26 31.0660019 31.4769168"+ style={{opacity: 1}}+ />+ <polygon+ id="Path-Copy-6"+ points="32.1949893 21 23 15.4824602 32.2699446 10 41 15.4769168"+ style={{+ opacity: 1+ }}+ />+ <polygon+ id="Path-Copy"+ points="22.2609912 49 13.0660019 43.4824602 22.3359466 38 31.0660019 43.4769168"+ style={{opacity: 1}}+ />+ <polygon+ id="Path-Copy-4"+ points="32.3269931 43.0228468 23.1320038 37.505307 32.4019485 32.0228468 41.1320038 37.4997636"+ style={{opacity: 0.6}}+ />+ <polygon+ id="Path-Copy-7"+ points="42.3269931 37.0228468 33.1320038 31.505307 42.4019485 26.0228468 51.1320038 31.4997636"+ style={{opacity: 1}}+ />+ <polygon+ id="Path-Copy-2"+ points="32.2609912 55 23.0660019 49.4824602 32.3359466 44 41.0660019 49.4769168"+ style={{opacity: 0.6}}+ />+ <polygon+ id="Path-Copy-5"+ points="42.3269931 49.0228468 33.1320038 43.505307 42.4019485 38.0228468 51.1320038 43.4997636"+ style={{opacity: 1}}+ />+ <polygon+ id="Path-Copy-8"+ points="53.3269931 43.0228468 44.1320038 37.505307 53.4019485 32.0228468 62.1320038 37.4997636"+ style={{opacity: 0.4}}+ />+ </g>+ </Base>+ );+ }+}++export default VectorTileIcon;diff --git a/src/layers/src/vector-tile/vector-tile-layer.ts b/src/layers/src/vector-tile/vector-tile-layer.tsnew file mode 100644index 000000000..a4ed87543--- /dev/null+++ b/src/layers/src/vector-tile/vector-tile-layer.ts@@ -0,0 +1,584 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {FeatureCollection, Feature} from 'geojson';++import {Layer as DeckLayer} from '@deck.gl/core/typed';+import {_Tile2DHeader as Tile2DHeader} from '@deck.gl/geo-layers/typed';+import {GeoJsonLayer} from '@deck.gl/layers/typed';+import {MVTSource, MVTTileSource} from '@loaders.gl/mvt';+import {PMTilesSource, PMTilesTileSource} from '@loaders.gl/pmtiles';+import GL from '@luma.gl/constants';++import {notNullorUndefined} from '@kepler.gl/common-utils';+import {+ SCALE_TYPES,+ CHANNEL_SCALES,+ DEFAULT_COLOR_UI,+ LAYER_VIS_CONFIGS+} from '@kepler.gl/constants';+import {+ Layer as KeplerLayer,+ VisualChannel,+ DatasetType,+ TileType,+ VectorTileType,+ VectorTileDatasetMetadata+} from '@kepler.gl/layers';+import {+ KeplerTable as KeplerDataset,+ Datasets as KeplerDatasets,+ GpuFilter+} from '@kepler.gl/table';+import {+ AnimationConfig,+ Field as KeplerField,+ LayerColorConfig,+ LayerHeightConfig,+ Merge,+ MapState,+ BindedLayerCallbacks,+ VisConfigRange,+ VisConfigNumber,+ DomainStops+} from '@kepler.gl/types';+import {DataContainerInterface} from '@kepler.gl/utils';++import {getTileUrl} from './utils/vector-tile-utils';+import {MVTLayer as CustomMVTLayer} from './mvt-layer';+import VectorTileIcon from './vector-tile-icon';+import {+ LayerBaseConfig,+ LayerBaseConfigPartial,+ VisualChannelDomain,+ VisualChannelField+} from '../base-layer';+import {VectorTileMetadata, getLoaderOptions} from './utils/vector-tile-utils';++import AbstractTileLayer, {+ LayerData as CommonLayerData,+ commonTileVisConfigs,+ AbstractTileLayerConfig,+ AbstractTileLayerVisConfigSettings+} from './abstract-tile-layer';+import TileDataset from './common-tile/tile-dataset';+import {+ isDomainStops,+ isDomainQuantiles,+ isIndexedField,+ getPropertyByZoom+} from './common-tile/tile-utils';++export const DEFAULT_HIGHLIGHT_COLOR = [252, 242, 26, 150];+export const MAX_CACHE_SIZE_MOBILE = 1; // Minimize caching, visible tiles will always be loaded+export const DEFAULT_STROKE_WIDTH = 1;++/**+ * Type for transformRequest returned parameters.+ */+export type RequestParameters = {+ /** The URL to be requested. */+ url: string;+ /** Search parameters to be added onto the URL. */+ searchParams: URLSearchParams;+ /** Options passed to fetch. */+ options: RequestInit;+};++// This type *seems* to be what loaders.gl currently returns for tile content.+// Apparently this might be different depending on the loaders version, and for...+// reasons we use two different versions of loaders right now.+// TODO: The Features[] version should not be needed when we update to a newer+// version of Deck.gl and use only one version of loaders+type TileContent =+ | (FeatureCollection & {shape: 'geojson-table'})+ | (Feature[] & {shape: undefined});++type VectorTile = Tile2DHeader<TileContent>;++type LayerData = CommonLayerData & {+ tilesetDataUrl?: string | null;+ tileSource: MVTTileSource | PMTilesTileSource | null;+};++type VectorTileLayerRenderOptions = Merge<+ {+ idx: number;+ visible: boolean;+ mapState: MapState;+ data: any;+ animationConfig: AnimationConfig;+ gpuFilter: GpuFilter;+ layerCallbacks: BindedLayerCallbacks;+ objectHovered: {+ index: number;+ tile: VectorTile;+ sourceLayer: typeof GeoJsonLayer;+ };+ },+ LayerData+>;++export const vectorTileVisConfigs = {+ ...commonTileVisConfigs,++ stroked: {+ ...LAYER_VIS_CONFIGS.stroked,+ defaultValue: false+ },++ // TODO figure out why strokeColorScale can't be const+ strokeColorScale: 'strokeColorScale' as any,+ strokeColorRange: 'strokeColorRange' as const,++ sizeRange: 'strokeWidthRange' as const,+ strokeWidth: {+ ...LAYER_VIS_CONFIGS.thickness,+ property: 'strokeWidth',+ defaultValue: 0.5,+ allowCustomValue: false+ }+};++export type VectorTileLayerConfig = Merge<+ AbstractTileLayerConfig,+ {+ sizeField?: VisualChannelField;+ sizeScale?: string;+ sizeDomain?: VisualChannelDomain;+ strokeColorField: VisualChannelField;+ }+>;++export type VectorTileLayerVisConfigSettings = Merge<+ AbstractTileLayerVisConfigSettings,+ {+ sizeRange: VisConfigRange;+ strokeWidth: VisConfigNumber;+ }+>;++export default class VectorTileLayer extends AbstractTileLayer<VectorTile, Feature[]> {+ declare config: VectorTileLayerConfig;+ declare visConfigSettings: VectorTileLayerVisConfigSettings;++ constructor(props: ConstructorParameters<typeof AbstractTileLayer>[0]) {+ super(props);+ this.registerVisConfig(vectorTileVisConfigs);+ this.tileDataset = this.initTileDataset();+ }++ meta = {};++ static findDefaultLayerProps(dataset: KeplerDataset): {+ props: {dataId: string; label?: string; isVisible: boolean}[];+ } {+ if (dataset.type !== DatasetType.VECTOR_TILE) {+ return {props: []};+ }+ return super.findDefaultLayerProps(dataset);+ }++ initTileDataset(): TileDataset<VectorTile, Feature[]> {+ return new TileDataset({+ getTileId: (tile: VectorTile): string => tile.id,+ getIterable: (tile: VectorTile): Feature[] => {+ if (tile.content) {+ return tile.content.shape === 'geojson-table' ? tile.content.features : tile.content;+ }+ return [];+ },+ getRowCount: (features: Feature[]): number => features.length,+ getRowValue: this.accessRowValue+ });+ }++ get type() {+ return TileType.VECTOR_TILE;+ }++ get name(): string {+ return 'Vector Tile';+ }++ get layerIcon(): KeplerLayer['layerIcon'] {+ return VectorTileIcon;+ }++ get supportedDatasetTypes(): DatasetType[] {+ return [DatasetType.VECTOR_TILE];+ }++ get visualChannels(): Record<string, VisualChannel> {+ const visualChannels = super.visualChannels;+ return {+ ...visualChannels,+ strokeColor: {+ property: 'strokeColor',+ field: 'strokeColorField',+ scale: 'strokeColorScale',+ domain: 'strokeColorDomain',+ range: 'strokeColorRange',+ key: 'strokeColor',+ channelScaleType: CHANNEL_SCALES.color,+ accessor: 'getLineColor',+ condition: config => config.visConfig.stroked,+ nullValue: visualChannels.color.nullValue,+ getAttributeValue: config => config.visConfig.strokeColor || config.color+ },+ size: {+ property: 'stroke',+ field: 'sizeField',+ scale: 'sizeScale',+ domain: 'sizeDomain',+ range: 'sizeRange',+ key: 'size',+ channelScaleType: CHANNEL_SCALES.size,+ nullValue: 0,+ accessor: 'getLineWidth',+ condition: config => config.visConfig.stroked,+ getAttributeValue: config => config.visConfig.strokeWidth || DEFAULT_STROKE_WIDTH+ }+ };+ }++ getDefaultLayerConfig(+ props: LayerBaseConfigPartial+ ): LayerBaseConfig & Partial<LayerColorConfig & LayerHeightConfig> {+ const defaultLayerConfig = super.getDefaultLayerConfig(props);+ return {+ ...defaultLayerConfig,+ colorScale: SCALE_TYPES.quantize,++ strokeColorField: null,+ strokeColorDomain: [0, 1],+ strokeColorScale: SCALE_TYPES.quantile,+ colorUI: {+ ...defaultLayerConfig.colorUI,+ // @ts-expect-error LayerConfig+ strokeColorRange: DEFAULT_COLOR_UI+ }+ };+ }++ getHoverData(+ object: {properties?: Record<string, Record<string, unknown>>},+ dataContainer: DataContainerInterface,+ fields: KeplerField[]+ ): (Record<string, unknown> | null)[] {+ return fields.map(f => object.properties?.[f.name] ?? null);+ }++ calculateLayerDomain(+ dataset: KeplerDataset,+ visualChannel: VisualChannel+ ): DomainStops | number[] {+ const defaultDomain = [0, 1];++ const field = this.config[visualChannel.field];+ const scale = this.config[visualChannel.scale];+ if (!field) {+ // if colorField or sizeField were set back to null+ return defaultDomain;+ }+ if (scale === SCALE_TYPES.quantile && isDomainQuantiles(field?.filterProps?.domainQuantiles)) {+ return field.filterProps.domainQuantiles;+ }+ if (isDomainStops(field?.filterProps?.domainStops)) {+ return field.filterProps.domainStops;+ } else if (Array.isArray(field?.filterProps?.domain)) {+ return field.filterProps.domain;+ }++ return defaultDomain;+ }++ getScaleOptions(channelKey: string): string[] {+ let options = KeplerLayer.prototype.getScaleOptions.call(this, channelKey);++ const channel = this.visualChannels.strokeColor;+ const field = this.config[channel.field];+ if (+ !(+ isDomainQuantiles(field?.filterProps?.domainQuantiles) ||+ this.config.visConfig.dynamicColor ||+ // If we've set the scale to quantile, we need to include it - there's a loading+ // period in which the visConfig isn't set yet, but if we don't return the right+ // scale type we lose it+ this.config.colorScale === SCALE_TYPES.quantile+ )+ ) {+ options = options.filter(scale => scale !== SCALE_TYPES.quantile);+ }++ return options;+ }++ accessRowValue(+ field?: KeplerField,+ indexKey?: number | null+ ): (field: KeplerField, datum: Feature) => number | null {+ // if is indexed field+ if (isIndexedField(field) && indexKey !== null) {+ const fieldName = indexKey && field?.indexBy?.mappedValue[indexKey];+ if (fieldName) {+ return (f, datum) => {+ if (datum.properties) {+ return datum.properties[fieldName];+ }+ // TODO debug this with indexed tiled dataset+ return datum[fieldName];+ };+ }+ }++ // default+ return (f, datum) => {+ if (f && datum.properties) {+ return datum.properties[f.name];+ }+ // support picking & highlighting+ return f ? datum[f.fieldIdx] : null;+ };+ }++ updateLayerMeta(dataset: KeplerDataset, datasets: KeplerDatasets): void {+ if (dataset.type !== DatasetType.VECTOR_TILE) {+ return;+ }++ const datasetMeta = dataset.metadata as VectorTileMetadata & VectorTileDatasetMetadata;+ this.updateMeta({+ datasetId: dataset.id,+ datasets,+ bounds: datasetMeta.bounds+ });+ }++ formatLayerData(+ datasets: KeplerDatasets,+ oldLayerData: unknown,+ animationConfig: AnimationConfig+ ): LayerData {+ const {dataId} = this.config;+ if (!notNullorUndefined(dataId)) {+ return {tileSource: null};+ }+ const dataset = datasets[dataId];++ let tilesetDataUrl: string | undefined;+ let tileSource: LayerData['tileSource'] = null;++ if (dataset?.type === DatasetType.VECTOR_TILE) {+ const datasetMetadata = dataset.metadata as VectorTileMetadata & VectorTileDatasetMetadata;+ if (datasetMetadata?.type === VectorTileType.MVT) {+ const transformFetch = async (input: RequestInfo | URL, init?: RequestInit | undefined) => {+ const requestData: RequestParameters = {+ url: input as string,+ searchParams: new URLSearchParams(),+ options: init ?? {}+ };++ return fetch(requestData.url, requestData.options);+ };++ tilesetDataUrl = datasetMetadata?.tilesetDataUrl;+ tileSource = tilesetDataUrl+ ? MVTSource.createDataSource(decodeURIComponent(tilesetDataUrl), {+ mvt: {+ metadataUrl: datasetMetadata?.tilesetMetadataUrl ?? null,+ loadOptions: {+ fetch: transformFetch+ }+ }+ })+ : null;+ } else if (datasetMetadata?.type === VectorTileType.PMTILES) {+ // TODO: to render image pmtiles need to use TileLayer and BitmapLayer (https://github.com/visgl/loaders.gl/blob/master/examples/website/tiles/components/tile-source-layer.ts)+ tilesetDataUrl = datasetMetadata?.tilesetDataUrl;+ tileSource = tilesetDataUrl ? PMTilesSource.createDataSource(tilesetDataUrl, {}) : null;+ }+ }++ return {+ ...super.formatLayerData(datasets, oldLayerData, animationConfig),+ tilesetDataUrl: typeof tilesetDataUrl === 'string' ? getTileUrl(tilesetDataUrl) : null,+ tileSource+ };+ }++ hasHoveredObject(objectInfo) {+ if (super.hasHoveredObject(objectInfo)) {+ const features = objectInfo?.tile?.content?.features;+ return features[objectInfo.index];+ }+ return null;+ }++ renderSubLayers(props: Record<string, any>): DeckLayer | DeckLayer[] {+ let {data} = props;++ data = data?.shape === 'geojson-table' ? data.features : data;+ if (!data?.length) {+ return [];+ }++ const tile: Tile2DHeader = props.tile;+ const zoom = tile.index.z;++ return new GeoJsonLayer({+ ...props,+ data,+ getFillColor: props.getFillColorByZoom ? props.getFillColor(zoom) : props.getFillColor,+ getElevation: props.getElevationByZoom ? props.getElevation(zoom) : props.getElevation,+ pointRadiusScale: props.getPointRadiusScaleByZoom(zoom),+ // For some reason tile Layer reset autoHighlight to false+ pickable: true,+ autoHighlight: true,+ stroked: props.stroked,+ // wrapLongitude: true causes missing side polygon when extrude is enabled+ wrapLongitude: false+ });+ }++ // generate a deck layer+ renderLayer(opts: VectorTileLayerRenderOptions): DeckLayer[] {+ const {mapState, data, animationConfig, gpuFilter, objectHovered, layerCallbacks} = opts;+ const {animation, visConfig} = this.config;++ this.setLayerDomain = layerCallbacks.onSetLayerDomain;++ const defaultLayerProps = this.getDefaultDeckLayerProps(opts);+ const eleZoomFactor = this.getElevationZoomFactor(mapState);++ const transitions = this.config.visConfig.transition+ ? {+ getFillColor: {+ duration: animationConfig.duration+ },+ getElevation: {+ duration: animationConfig.duration+ }+ }+ : undefined;++ const colorField = this.config.colorField as KeplerField;+ const heightField = this.config.heightField as KeplerField;+ const strokeColorField = this.config.strokeColorField as KeplerField;+ const sizeField = this.config.sizeField as KeplerField;++ if (data.tileSource) {+ const hoveredObject = this.hasHoveredObject(objectHovered);++ const layers = [+ new CustomMVTLayer({+ ...defaultLayerProps,+ ...data,+ onViewportLoad: this.onViewportLoad,+ data: data.tilesetDataUrl,+ getTileData: data.tileSource?.getTileData,+ tileSource: data.tileSource,+ getFilterValue: this.getGpuFilterValueAccessor(opts),+ filterRange: gpuFilter.filterRange,+ lineWidthUnits: 'pixels',+ binary: false,+ elevationScale: visConfig.elevationScale * eleZoomFactor,+ extruded: visConfig.enable3d,+ stroked: visConfig.stroked,++ // TODO: this is hard coded, design a UI to allow user assigned unique property id+ // uniqueIdProperty: 'ufid',+ renderSubLayers: this.renderSubLayers,+ // when radiusUnits is meter+ getPointRadiusScaleByZoom: getPropertyByZoom(visConfig.radiusByZoom, visConfig.radius),+ pointRadiusScale: 1,++ pointRadiusMinPixels: 1,+ autoHighlight: true,+ highlightColor: DEFAULT_HIGHLIGHT_COLOR,+ pickable: true,+ transitions,+ updateTriggers: {+ getFilterValue: {+ ...gpuFilter.filterValueUpdateTriggers,+ currentTime: animation.enabled ? animationConfig.currentTime : null+ },+ getFillColor: {+ color: this.config.color,+ colorField: this.config.colorField,+ colorScale: this.config.colorScale,+ colorDomain: this.config.colorDomain,+ colorRange: visConfig.colorRange,+ currentTime: isIndexedField(colorField) ? animationConfig.currentTime : null+ },+ getElevation: {+ heightField: this.config.heightField,+ heightScaleType: this.config.heightScale,+ heightRange: visConfig.heightRange,+ currentTime: isIndexedField(heightField) ? animationConfig.currentTime : null+ },+ getLineColor: {+ strokeColor: visConfig.strokeColor,+ strokeColorField: this.config.strokeColorField,+ // @ts-expect-error prop not in LayerConfig+ strokeColorScale: this.config.strokeColorScale,+ // @ts-expect-error prop not in LayerConfig+ strokeColorDomain: this.config.strokeColorDomain,+ // FIXME: Strip out empty arrays from individual color map steps, and replace with `null`, otherwise the layer may show the incorrect color.+ // So far it seems that it uses the previous color chosen in the palette rather than the currently chosen color for the specific custom ordinal value when there are "sparse" color maps.+ // In other words, a color map with "holes" of colors with unassigned field values, which may have been assigned in the past.+ // For example "abc" was green, stored as `["abc"]`. Then "abc" was reassigned to the red color map step, stored as `["abc"]`. Now the green color map step's stored value is `[]`, and the layer will incorrectly still render "abc" in green.+ // Quick patch example:+ // strokeColorRange: visConfig?.strokeColorRange?.colorMap?.map(cm =>+ // cm[0]?.length === 0 ? [null, cm[1]] : cm+ // ),+ // Note: for regular scales the colorMap in the above patch is undefined and breaks strokeColorRange update trigger.+ strokeColorRange: visConfig.strokeColorRange,+ currentTime: isIndexedField(strokeColorField) ? animationConfig.currentTime : null+ },+ getLineWidth: {+ sizeRange: visConfig.sizeRange,+ strokeWidth: visConfig.strokeWidth,+ sizeField: this.config.sizeField,+ sizeScale: this.config.sizeScale,+ sizeDomain: this.config.sizeDomain,+ currentTime: isIndexedField(sizeField) ? animationConfig.currentTime : null+ }+ },+ _subLayerProps: {+ 'polygons-stroke': {opacity: visConfig.strokeOpacity},+ 'polygons-fill': {+ parameters: {+ cullFace: GL.BACK+ }+ }+ },+ loadOptions: {+ mvt: getLoaderOptions().mvt+ }+ }),+ // hover layer+ ...(hoveredObject+ ? [+ new GeoJsonLayer({+ // @ts-expect-error not typed+ ...objectHovered.sourceLayer?.props,+ ...(this.getDefaultHoverLayerProps() as any),+ visible: true,+ wrapLongitude: false,+ data: [hoveredObject],+ getLineColor: this.config.highlightColor,+ getFillColor: this.config.highlightColor,+ // always draw outline+ stroked: true,+ filled: false+ })+ ]+ : [])+ ];++ return layers;+ }+ return [];+ }+}diff --git a/src/localization/src/translations/en.ts b/src/localization/src/translations/en.tsindex f2940df43..e15d760b1 100644--- a/src/localization/src/translations/en.ts+++ b/src/localization/src/translations/en.ts@@ -111,7 +111,8 @@ export default {hexagonid: 'H3',trip: 'trip',s2: 'S2',- '3d': '3D'+ '3d': '3D',+ vectortile: 'vector tile'},layerUpdateError:'An error occurred during layer update: {errorMessage}. Make sure the format of the input data is valid.',@@ -207,7 +208,8 @@ export default {removeDataset: 'Remove dataset'},datasetInfo: {- rowCount: '{rowCount} rows'+ rowCount: '{rowCount} rows',+ vectorTile: 'Vector tile'},tooltip: {hideLayer: 'Hide layer',@@ -317,7 +319,8 @@ export default {filteredData: 'Filtered data',unfilteredData: 'Unfiltered Data',fileCount: '{fileCount} Files',- rowCount: '{rowCount} Rows'+ rowCount: '{rowCount} Rows',+ vectorTileWarning: "* Export Data for Vector Tile datasets isn't supported"},deleteData: {warning: 'you are going to delete this dataset. It will affect {length} layers'@@ -396,6 +399,7 @@ export default {},loadData: {upload: 'Load Files',+ tileset: 'Tileset',storage: 'Load from Storage'},tripInfo: {@@ -598,6 +602,9 @@ ${'```'}fileNotSupported: 'File {errorFiles} is not supported.',or: 'or'},+ tilesetSetup: {+ header: 'Setup Vector Tiles'+ },geocoder: {title: 'Enter an address or coordinates, ex 37.79,-122.40'},diff --git a/src/reducers/src/layer-utils.ts b/src/reducers/src/layer-utils.tsindex 443d74c38..bb92caf03 100644--- a/src/reducers/src/layer-utils.ts+++ b/src/reducers/src/layer-utils.ts@@ -14,6 +14,8 @@ import {Editor,Feature,FeatureSelectionContext,+ BindedLayerCallbacks,+ LayerCallbacks,Viewport} from '@kepler.gl/types';import {@@ -345,28 +347,16 @@ export type ComputeDeckLayersProps = {export function bindLayerCallbacks(layerCallbacks: LayerCallbacks = {},idx: number-): Record<string, (idx: number, val: any) => void> {+): BindedLayerCallbacks {return Object.keys(layerCallbacks).reduce((accu, key) => ({...accu,[key]: val => layerCallbacks[key](idx, val)}),- {} as Record<string, (_idx: number, val: any) => void>+ {} as Record<string, (val: unknown) => void>);}-export type LayerCallbacks = {- onLayerHover?: (idx: number, value: any) => void;- onSetLayerDomain?: (idx: number, value: any) => void;- onFilteredItemsChange?: (- idx: number,- event: {- id: string;- count: number;- }- ) => void;-};-// eslint-disable-next-line complexityexport function computeDeckLayers({visState, mapState, mapStyle}: any,diff --git a/src/schemas/src/dataset-schema.ts b/src/schemas/src/dataset-schema.tsindex ecd0446e6..9141a899a 100644--- a/src/schemas/src/dataset-schema.ts+++ b/src/schemas/src/dataset-schema.ts@@ -4,7 +4,7 @@import pick from 'lodash.pick';import {console as globalConsole} from 'global/window';-import {ProtoDataset, RGBColor} from '@kepler.gl/types';+import {ProtoDataset, RGBColor, JsonObject} from '@kepler.gl/types';import {KeplerTable} from '@kepler.gl/table';import {VERSIONS} from './versions';import Schema from './schema';@@ -32,6 +32,8 @@ export type SavedDatasetV1 = {color: RGBColor;allData: any[][];fields: SavedField[];+ type?: string;+ metadata?: JsonObject;};};@@ -90,7 +92,9 @@ export const propertiesV1 = {key: 'fields',version: VERSIONS.v1,properties: fieldPropertiesV1- })+ }),+ type: null,+ metadata: null};export class DatasetSchema extends Schema {@@ -144,7 +148,8 @@ export class DatasetSchema extends Schema {// get format of all fieldsreturn {data: {fields: updatedFields, rows: dataset.allData},- info: pick(dataset, ['id', 'label', 'color'])+ info: pick(dataset, ['id', 'label', 'color', 'type']),+ ...(dataset.metadata ? {metadata: dataset.metadata} : {})};}}diff --git a/src/table/src/index.ts b/src/table/src/index.tsindex 464188128..c34b1e599 100644--- a/src/table/src/index.ts+++ b/src/table/src/index.ts@@ -12,6 +12,13 @@ export {maybeToDate} from './kepler-table';/* eslint-disable prettier/prettier */-export type {FilterProps, GpuFilter, Datasets, BooleanFieldFilterProps} from './kepler-table';+export type {+ FilterProps,+ GpuFilter,+ Datasets,+ BooleanFieldFilterProps,+ NumericFieldFilterProps,+ StringFieldFilterProps+} from './kepler-table';export * from './gpu-filter-utils';export * from './dataset-utils';diff --git a/src/types/datasets.d.ts b/src/types/datasets.d.tsnew file mode 100644index 000000000..12b8f774d--- /dev/null+++ b/src/types/datasets.d.ts@@ -0,0 +1,15 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++export type ZoomStops = number[][];++export type DomainStops = {+ z: number[];+ stops: ZoomStops;+ interpolation: 'interpolate';+};++export type ZoomStopsConfig = {+ enabled?: boolean;+ stops: ZoomStops | null;+};diff --git a/src/types/index.d.ts b/src/types/index.d.tsindex 931131c21..25425171e 100644--- a/src/types/index.d.ts+++ b/src/types/index.d.ts@@ -8,3 +8,4 @@ export * from './schemas';export * from './types';export * from './components';export * from './effects';+export * from './datasets';diff --git a/src/types/layers.d.ts b/src/types/layers.d.tsindex 0669d9d4e..f588f5630 100644--- a/src/types/layers.d.ts+++ b/src/types/layers.d.ts@@ -99,6 +99,17 @@ export type LayerWeightConfig = {weightField: VisualChannelField;};+export type IndexBy = {+ format: string;+ type: string;+ mappedValue: Record<number, string>;+ timeDomain: {+ domain: [number, number];+ timeSteps: number[];+ duration?: number;+ };+};+export type Field = {name: string;displayName: string;@@ -111,6 +122,7 @@ export type Field = {filterProps?: FilterProps;metadata?: Record<string, any>;displayFormat?: string;+ indexBy?: IndexBy;};export type FieldPair = {@@ -340,3 +352,21 @@ export type LayerTextConfig = {textAnchor: TextConfigSelect;textAlignment: TextConfigSelect;};++export type LayerCallbacks = {+ onLayerHover?: (idx: number, value: any) => void;+ onSetLayerDomain?: (idx: number, value: any) => void;+ onFilteredItemsChange?: (+ idx: number,+ event: {+ id: string;+ count: number;+ }+ ) => void;+};++export type BindedLayerCallbacks = {+ onLayerHover?: (value: any) => void;+ onSetLayerDomain?: (value: any) => void;+ onFilteredItemsChange?: (event: {id: string; count: number}) => void;+};diff --git a/src/types/types.d.ts b/src/types/types.d.tsindex 2044f0d43..c2d3af446 100644--- a/src/types/types.d.ts+++ b/src/types/types.d.ts@@ -33,3 +33,11 @@ export type ProcessorResult = {cols?: any[];metadata?: any;} | null;++export type Json = JsonScalar | JsonArray | JsonObject;+export type JsonObjectOrArray = JsonArray | JsonObject;+export type JsonScalar = string | number | boolean | null;+export type JsonArray = Json[];+export type JsonObject = {+ [key: string]: Json | undefined;+};diff --git a/src/utils/src/filter-utils.ts b/src/utils/src/filter-utils.tsindex 37e7e42ee..1083ff774 100644--- a/src/utils/src/filter-utils.ts+++ b/src/utils/src/filter-utils.ts@@ -755,6 +755,10 @@ export function isInRange(val: any, domain: number[]): boolean {return false;}+ if (Array.isArray(val)) {+ return domain[0] <= val[0] && val[1] <= domain[1];+ }+return val >= domain[0] && val <= domain[1];}diff --git a/src/utils/src/index.ts b/src/utils/src/index.tsindex 856596f69..a487a88ae 100644--- a/src/utils/src/index.ts+++ b/src/utils/src/index.ts@@ -150,3 +150,5 @@ export {DataRow} from './data-row';// Application configexport {getApplicationConfig, initApplicationConfig} from './application-config';export type {KeplerApplicationConfig, MapLibInstance} from './application-config';++export {default as quickInsertionSort} from './quick-insertion-sort';diff --git a/src/utils/src/quick-insertion-sort.ts b/src/utils/src/quick-insertion-sort.tsnew file mode 100644index 000000000..7767da9ea--- /dev/null+++ b/src/utils/src/quick-insertion-sort.ts@@ -0,0 +1,128 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++/**+ * Inplace quick insertion sorting algorithm for numeric values+ * 1. using a stack to eliminate recursion+ * 2. sorting inplace to reduce memory usage+ * 3. using insertion sort for small partition sizes+ * The original source code is from:+ * https://www.measurethat.net/Benchmarks/Show/3549/0/javascript-sorting-algorithms+ * https://quick.work/?page=view-blog&id=24+ *+ * @param arr+ */+export default function quickInsertionSort(arr: number[]): void {+ if (!arr || arr.length < 1) {+ return;+ }+ const stack: number[][] = [];+ stack.push([0, arr.length, 2 * Math.floor(Math.log(arr.length) / Math.log(2))]);++ while (stack.length > 0) {+ const [start, end, depth] = stack[stack.length - 1];+ stack.pop();++ if (depth === 0) {+ // for worst case of quick sort: too many partitions+ shellSortBound(arr, start, end);+ } else {+ const pivot = Math.round((start + end) / 2);+ const pivotNewIndex = inplaceQuicksortPartition(arr, start, end, pivot);+ // more than 32 elements: faster to be sorted in QuickSort partition+ // less than 32 elements: faster to be sorted in InsertionSort+ if (end - pivotNewIndex > 16) {+ stack.push([pivotNewIndex, end, depth - 1]);+ }+ if (pivotNewIndex - start > 16) {+ stack.push([start, pivotNewIndex, depth - 1]);+ }+ }+ }+ insertionSort(arr);+}++/**+ * shellsort is a generalization of insertion sort+ * shellsort perform best on partially sorted array+ * Don't use shellsort on array (>10k)+ * @param arr+ * @param start+ * @param end+ *+ * Learn more about Shellsort at https://en.wikipedia.org/wiki/Shellsort+ */+function shellSortBound(arr: number[], start: number, end: number) {+ if (arr.length <= 1) return;+ let inc = Math.round((start + end) / 2);+ let i;+ let j;+ let t;++ while (inc > start) {+ for (i = inc; i < end; i++) {+ t = arr[i];+ j = i;+ while (j >= inc && arr[j - inc] > t) {+ arr[j] = arr[j - inc];+ j -= inc;+ }+ arr[j] = t;+ }+ inc = Math.round((inc - start) / 2.2 + start);+ }+}++/**+ * Insertion sort+ * @param arr+ */+function insertionSort(arr: number[]) {+ for (let i = 1, l = arr.length; i < l; i++) {+ const value = arr[i];+ let j;+ for (j = i - 1; j >= 0; j--) {+ if (arr[j] <= value) break;+ arr[j + 1] = arr[j];+ }+ arr[j + 1] = value;+ }+}++/**+ * In-place quick sort+ * @param arr+ * @param start+ * @param end+ * @param pivotIndex+ * @returns number+ */+function inplaceQuicksortPartition(+ arr: number[],+ start: number,+ end: number,+ pivotIndex: number+): number {+ let i = start;+ let j = end;+ const pivot = arr[pivotIndex];+ const partition = true;+ while (partition) {+ while (arr[i] < pivot) {+ i++;+ }+ j--;+ while (pivot < arr[j]) {+ j--;+ }+ if (!(i < j)) {+ return i;+ }+ // swap(arr, i, j);+ const t = arr[i];+ arr[i] = arr[j];+ arr[j] = t;+ i++;+ }+ return i;+}diff --git a/src/utils/src/utils.ts b/src/utils/src/utils.tsindex d4a9288f6..1e163de34 100644--- a/src/utils/src/utils.ts+++ b/src/utils/src/utils.ts@@ -133,6 +133,8 @@ type ErrorObject = {message?: any;};+export const DEFAULT_ERROR_MESSAGE = 'Something went wrong';+/*** Get error information of unknown type* Extracts as much human readable information as possible@@ -142,9 +144,12 @@ type ErrorObject = {* @param {*} err - Unknown error* @return {string} - human readable error msg*/-export function getError(err?: Error | ErrorObject | string): string {+export function getError(+ err?: Error | ErrorObject | string,+ defaultMessage: string = DEFAULT_ERROR_MESSAGE+): string {if (!err) {- return 'Something went wrong';+ return defaultMessage;}if (typeof err === 'string') {@@ -152,17 +157,16 @@ export function getError(err?: Error | ErrorObject | string): string {} else if (err instanceof Error) {return err.message;} else if (typeof err === 'object') {- return err.error+ return Object.prototype.hasOwnProperty.call(err, 'message')+ ? getError(err.message)+ : Object.prototype.hasOwnProperty.call(err, 'error')? getError(err.error)- : err.err+ : Object.prototype.hasOwnProperty.call(err, 'err')? getError(err.err)- : err.message- ? getError(err.message): JSON.stringify(err);}- // @ts-ignore- return null;+ return defaultMessage;}export function arrayInsert<T>(arr: T[], index: number, val: T): T[] {diff --git a/test/browser/components/side-panel/layer-list.spec.js b/test/browser/components/side-panel/layer-list.spec.jsindex aaf310c29..e8baca61b 100644--- a/test/browser/components/side-panel/layer-list.spec.js+++ b/test/browser/components/side-panel/layer-list.spec.js@@ -20,7 +20,7 @@ import {keplerGlReducerCore as keplerGlReducer} from '@kepler.gl/reducers';import {renderWithTheme} from '../../../helpers/component-jest-utils';import testLayerData from '../../../fixtures/test-layer-data';import {dataId as csvDataId} from '../../../fixtures/test-csv-data';-import {applyActions} from '../../../helpers/mock-state';+import {applyActions} from '../../../helpers/mock-state-utils';// TODO: need to be deleted and imported from raw-statesconst InitialState = keplerGlReducer(undefined, keplerGlInit({}));diff --git a/test/fixtures/tile-metadata.ts b/test/fixtures/tile-metadata.tsnew file mode 100644index 000000000..718a62418--- /dev/null+++ b/test/fixtures/tile-metadata.ts@@ -0,0 +1,86 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++export const MVT_METADATA = {+ name: 'Mapbox Streets v8',+ description: '',+ boundingBox: [+ [-180, -85],+ [180, 85]+ ] as [[number, number], [number, number]],+ center: [0, 0, 0],+ maxZoom: 16,+ minZoom: 0,+ layers: [+ {+ name: 'landuse',+ description: '',+ id: 'landuse',+ minzoom: 5,+ source: 'mapbox.mapbox-streets-v8',+ source_name: 'Mapbox Streets v8',+ fields: [+ {+ name: 'class',+ type: 'string'+ }+ ]+ }+ ]+};++export const PMTILES_METADATA = {+ name: 'My Custom Tiles',+ format: 'pmtiles' as const,+ formatVersion: 3,+ attributions: [],+ tileMIMEType: 'application/vnd.mapbox-vector-tile' as const,+ minZoom: 0,+ maxZoom: 6,+ boundingBox: [+ [-150.1122219, -51.8952777],+ [179.3577783, 69.6043747]+ ] as [[number, number], [number, number]],+ center: [14.0625, 50.7026397],+ centerZoom: 6,+ tilejson: {+ name: 'My Custom Tiles',+ description: 'My Custom Tiles Description',+ generator: 'tippecanoe v2.37.1',+ generatorOptions:+ 'tippecanoe -zg -o output.pmtiles --drop-densest-as-needed ne_10m_railroads.geojson',+ boundingBox: [+ [-150.112222, -51.895278],+ [179.357778, 69.604375]+ ],+ center: null,+ maxZoom: null,+ minZoom: null,+ layers: [+ {+ name: 'ne_10m_railroads',+ id: 'ne_10m_railroads',+ description: '',+ minzoom: 0,+ maxzoom: 6,+ dominantGeometry: 'LineString',+ fields: [+ {+ name: 'metric',+ type: 'float32',+ min: -1,+ max: 10,+ uniqueValueCount: 2,+ values: [-1, 10]+ },+ {+ name: 'continent',+ type: 'utf8',+ uniqueValueCount: 6,+ values: ['Africa', 'Asia', 'Europe', 'North America', 'Oceania', 'South America']+ }+ ]+ }+ ]+ }+};diff --git a/test/helpers/mock-state-utils.js b/test/helpers/mock-state-utils.jsnew file mode 100644index 000000000..1382b1e0d--- /dev/null+++ b/test/helpers/mock-state-utils.js@@ -0,0 +1,66 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {drainTasksForTesting, succeedTaskWithValues} from 'react-palm/tasks';++import {KeplerTable} from '@kepler.gl/table';++/**+ * Applies actions one by one using the reducer.+ * After each action drain Tasks and automarically resolve tasks+ * of type CREATE_TABLE_TASK in order to create datasets.+ * @param {Reducer} reducer A reducer to use.+ * @param {State} initialState Initial state.+ * @param {Action | Action[]}actions An array of actions.+ * @returns+ */+export function applyActions(reducer, initialState, actions) {+ const actionQ = Array.isArray(actions) ? actions : [actions];++ // remove any existing tasks before actions+ drainTasksForTesting();++ let updatedState = actionQ.reduce((updatedState, {action, payload}) => {+ let newState = reducer(updatedState, action(...payload));+ const tasks = drainTasksForTesting();+ newState = applyCreateTableTasks(tasks, reducer, newState);+ return newState;+ }, initialState);++ return updatedState;+}++/**+ * Execute existing tasts and mock CREATE_TABLE_TASK with success.+ * @param {VisStateReducer} reducer+ * @param {VisState} initialState+ * @returns+ */+export function applyExistingDatasetTasks(reducer, initialState) {+ const tasks = drainTasksForTesting();+ return applyCreateTableTasks(tasks, reducer, initialState);+}++/**+ * Execute tasks and mock CREATE_TABLE_TASK with success.+ * @param {Task[]} tasks+ * @param {Reducer} reducer+ * @param {VisState} initialState+ * @returns+ */+export const applyCreateTableTasks = (tasks, reducer, initialState) => {+ return tasks.reduce((updatedState, task) => {+ if (!task.label.includes('CREATE_TABLE_TASK')) return updatedState;+ const tables = task.payload.map(payload => mockCreateNewDataEntry(payload));+ return reducer(updatedState, succeedTaskWithValues(task, tables));+ }, initialState);+};++/**+ * Mock instant sync result of createNewDataEntry.+ */+const mockCreateNewDataEntry = ({info, color, opts, data}) => {+ const table = new KeplerTable({info, color, ...opts});+ table.importData({data});+ return table;+};diff --git a/test/helpers/mock-state.js b/test/helpers/mock-state.jsindex 85a109876..d6baf2005 100644--- a/test/helpers/mock-state.js+++ b/test/helpers/mock-state.js@@ -3,7 +3,6 @@import test from 'tape-catch';import cloneDeep from 'lodash.clonedeep';-import {drainTasksForTesting, succeedTaskWithValues} from 'react-palm/tasks';import {colorPaletteToColorRange} from '@kepler.gl/constants';import {getInitialInputStyle,@@ -28,7 +27,6 @@ import {UIStateActions,ProviderActions} from '@kepler.gl/actions';-import {KeplerTable} from '@kepler.gl/table';// fixturesimport {@@ -58,6 +56,14 @@ import testArcData, {arcDataInfo, config as arcDataConfig} from '../fixtures/tesimport tripGeojson, {tripDataInfo} from '../fixtures/trip-geojson';import {processCsvData, processGeojson, processRowObject} from '@kepler.gl/processors';import {MOCK_MAP_STYLE} from './mock-map-styles';+import {+ applyActions,+ applyCreateTableTasks,+ applyExistingDatasetTasks,+ mockCreateNewDataEntry+} from './mock-state-utils';++export {applyActions, applyCreateTableTasks, applyExistingDatasetTasks, mockCreateNewDataEntry};const geojsonFields = cloneDeep(fields);const geojsonRows = cloneDeep(rows);@@ -80,69 +86,9 @@ export const geojsonInfo = {params: {file: null}};-/**- * Applies actions one by one using the reducer.- * After each action drain Tasks and automarically resolve tasks- * of type CREATE_TABLE_TASK in order to create datasets.- * @param {Reducer} reducer A reducer to use.- * @param {State} initialState Initial state.- * @param {Action | Action[]}actions An array of actions.- * @returns- */-export function applyActions(reducer, initialState, actions) {- const actionQ = Array.isArray(actions) ? actions : [actions];-- // remove any existing tasks before actions- drainTasksForTesting();-- let updatedState = actionQ.reduce((updatedState, {action, payload}) => {- let newState = reducer(updatedState, action(...payload));- const tasks = drainTasksForTesting();- newState = applyCreateTableTasks(tasks, reducer, newState);- return newState;- }, initialState);-- return updatedState;-}-// TODO: need to be deleted and imported from raw-statesexport const InitialState = keplerGlReducer(undefined, {});-/**- * Mock instant sync result of createNewDataEntry.- */-const mockCreateNewDataEntry = ({info, color, opts, data}) => {- const table = new KeplerTable({info, color, ...opts});- table.importData({data});- return table;-};--/**- * Execute tasks and mock CREATE_TABLE_TASK with success.- * @param {Task[]} tasks- * @param {Reducer} reducer- * @param {VisState} initialState- * @returns- */-export const applyCreateTableTasks = (tasks, reducer, initialState) => {- return tasks.reduce((updatedState, task) => {- if (!task.label.includes('CREATE_TABLE_TASK')) return updatedState;- const tables = task.payload.map(payload => mockCreateNewDataEntry(payload));- return reducer(updatedState, succeedTaskWithValues(task, tables));- }, initialState);-};--/**- * Execute existing tasts and mock CREATE_TABLE_TASK with success.- * @param {VisStateReducer} reducer- * @param {VisState} initialState- * @returns- */-export function applyExistingDatasetTasks(reducer, initialState) {- const tasks = drainTasksForTesting();- return applyCreateTableTasks(tasks, reducer, initialState);-}-/*** Mock app state with uploaded geojson and csv file* @returns {Immutable} appStatediff --git a/yarn.lock b/yarn.lockindex 95be795c9..2f9366fd4 100644--- a/yarn.lock+++ b/yarn.lock@@ -3412,7 +3412,9 @@ __metadata:"@loaders.gl/core": "npm:^4.3.2""@loaders.gl/gis": "npm:^4.3.2""@loaders.gl/gltf": "npm:^4.3.2"+ "@loaders.gl/mvt": "npm:^4.3.2""@loaders.gl/parquet": "npm:^4.3.2"+ "@loaders.gl/pmtiles": "npm:^4.3.2""@loaders.gl/schema": "npm:^4.3.2""@loaders.gl/wkt": "npm:^4.3.2""@luma.gl/constants": "npm:^8.5.20"@@ -4309,6 +4311,17 @@ __metadata:languageName: nodelinkType: hard+"@loaders.gl/images@npm:4.3.3":+ version: 4.3.3+ resolution: "@loaders.gl/images@npm:4.3.3"+ dependencies:+ "@loaders.gl/loader-utils": "npm:4.3.3"+ peerDependencies:+ "@loaders.gl/core": ^4.3.0+ checksum: 10c0/9b6dbcce46b687532bd8d6b73bd24b3e315ead3ed5601e0b46e9a0232300405dc14a67e9e7e5aa7abcd25e104f475ec7ddf27a136b8eccc3e57043be9b07b4d8+ languageName: node+ linkType: hard+"@loaders.gl/json@npm:4.3.2":version: 4.3.2resolution: "@loaders.gl/json@npm:4.3.2"@@ -4347,6 +4360,23 @@ __metadata:languageName: nodelinkType: hard+"@loaders.gl/mvt@npm:4.3.3, @loaders.gl/mvt@npm:^4.3.2":+ version: 4.3.3+ resolution: "@loaders.gl/mvt@npm:4.3.3"+ dependencies:+ "@loaders.gl/gis": "npm:4.3.3"+ "@loaders.gl/images": "npm:4.3.3"+ "@loaders.gl/loader-utils": "npm:4.3.3"+ "@loaders.gl/schema": "npm:4.3.3"+ "@math.gl/polygon": "npm:^4.1.0"+ "@probe.gl/stats": "npm:^4.0.0"+ pbf: "npm:^3.2.1"+ peerDependencies:+ "@loaders.gl/core": ^4.3.0+ checksum: 10c0/438061e08d1ca414d798a9414ba71637bfd0a8c1f1175bb4fb19f67553820d275aef664a47d94de6e5b11210be3d6f92cb6c0e2d84caa6a674d7b6c35a94f475+ languageName: node+ linkType: hard+"@loaders.gl/mvt@npm:^3.4.13":version: 3.4.15resolution: "@loaders.gl/mvt@npm:3.4.15"@@ -4393,6 +4423,21 @@ __metadata:languageName: nodelinkType: hard+"@loaders.gl/pmtiles@npm:^4.3.2":+ version: 4.3.3+ resolution: "@loaders.gl/pmtiles@npm:4.3.3"+ dependencies:+ "@loaders.gl/images": "npm:4.3.3"+ "@loaders.gl/loader-utils": "npm:4.3.3"+ "@loaders.gl/mvt": "npm:4.3.3"+ "@loaders.gl/schema": "npm:4.3.3"+ pmtiles: "npm:^3.0.4"+ peerDependencies:+ "@loaders.gl/core": ^4.3.0+ checksum: 10c0/2b77243fcb3f3b61d795ce5344774feeb604b2d419384ca366a52797739d9614ced381df4d3cde30b0e2c4e5914fd9803b634d1fef785ee15a2d22950ac0148c+ languageName: node+ linkType: hard+"@loaders.gl/polyfills@npm:4.3.2":version: 4.3.2resolution: "@loaders.gl/polyfills@npm:4.3.2"@@ -6534,7 +6579,7 @@ __metadata:languageName: nodelinkType: hard-"@probe.gl/stats@npm:^4.0.2":+"@probe.gl/stats@npm:^4.0.0, @probe.gl/stats@npm:^4.0.2":version: 4.0.9resolution: "@probe.gl/stats@npm:4.0.9"checksum: 10c0/23c205232a45941b13d1e87efe060d81f3a09339b0294991fc9fdf6dd9d090a9e6c79cccf23ce8f09e5c14aa44ef128836639490df22729a13214b0ff3c7cd80@@ -9493,6 +9538,15 @@ __metadata:languageName: nodelinkType: hard+"@types/leaflet@npm:^1.9.8":+ version: 1.9.15+ resolution: "@types/leaflet@npm:1.9.15"+ dependencies:+ "@types/geojson": "npm:*"+ checksum: 10c0/6537da4107f833e38359ca9e98e2c161d686af58ff8873c65c102e146b881761ca6212b08fae4e755380ee001073f856ee46c5ea25592a53f39e2b9e5d934d42+ languageName: node+ linkType: hard+"@types/lodash.clonedeep@npm:^4.5.7":version: 4.5.9resolution: "@types/lodash.clonedeep@npm:4.5.9"@@ -16402,6 +16456,13 @@ __metadata:languageName: nodelinkType: hard+"fflate@npm:^0.8.0":+ version: 0.8.2+ resolution: "fflate@npm:0.8.2"+ checksum: 10c0/03448d630c0a583abea594835a9fdb2aaf7d67787055a761515bf4ed862913cfd693b4c4ffd5c3f3b355a70cf1e19033e9ae5aedcca103188aaff91b8bd6e293+ languageName: node+ linkType: hard+"figgy-pudding@npm:^3.5.1":version: 3.5.2resolution: "figgy-pudding@npm:3.5.2"@@ -23857,6 +23918,16 @@ __metadata:languageName: nodelinkType: hard+"pmtiles@npm:^3.0.4":+ version: 3.2.1+ resolution: "pmtiles@npm:3.2.1"+ dependencies:+ "@types/leaflet": "npm:^1.9.8"+ fflate: "npm:^0.8.0"+ checksum: 10c0/645612bb23837808345581dde9cee89c2987b0556c26ac0b661dffeef42c53ceb888bb5be8817ef3fd40623b96dc6036808773529ee5f6694ddbb38af4bc5bcb+ languageName: node+ linkType: hard+"pngjs-nozlib@npm:^1.0.0":version: 1.0.0resolution: "pngjs-nozlib@npm:1.0.0"