Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[feat] Raster Tile Layer - development in progress (#3048)
Igor Dykhta3570ac4292b0
diff --git a/docs/user-guides/c-types-of-layers/README.md b/docs/user-guides/c-types-of-layers/README.mdindex 1b040c82b..410ac1754 100644--- a/docs/user-guides/c-types-of-layers/README.md+++ b/docs/user-guides/c-types-of-layers/README.md@@ -102,3 +102,17 @@ Supported URL templates:- pmtiles (https://your-cdn/filename.pmtiles)[Back to table of contents](../README.md)++## Raster Tile Layer++Raster layers are used to show satellite and aerial imagery. They allow you to work interactively directly with massive, image collections stored in .pmtiles (in raster format) or Cloud Optimized GeoTIFF format.++Supported URL templates:++- Users can reference remote **.pmtiles files in raster format** for raster layers by supplying a direct link to the file.++- **Cloud-Optimized GeoTIFFs (COG)** can also be used in raster layers by providing standardized Spatio-Temporal Asset Catalog (STAC) metadata.+ - The metadata file must be a valid *STAC Item* or *STAC Collection*, version 1.0.0 or higher.+ - Raster data referenced in STAC assets should be Cloud-Optimized GeoTIFFs and need to be publicly accessible via HTTPS.+ - STAC item and collections *must have Electro-Optical and Raster extensions*, and at least one asset must have both eo:bands and raster:bands information. common_name must be provided in eo:bands and data_type must be provided in raster:bands.+ - Users must use their own instances of raster tile servers, such as Titiler. [TODO: add instructions]diff --git a/examples/demo-app/src/components/load-data-modal/load-remote-map.js b/examples/demo-app/src/components/load-data-modal/load-remote-map.jsindex b09df129c..084fb14fc 100644--- a/examples/demo-app/src/components/load-data-modal/load-remote-map.js+++ b/examples/demo-app/src/components/load-data-modal/load-remote-map.js@@ -6,11 +6,11 @@ import React, {Component} from 'react';import PropTypes from 'prop-types';import styled from 'styled-components';+import {validateUrl} from '@kepler.gl/common-utils';import {Button} from '@kepler.gl/components';import {FormattedMessage} from '@kepler.gl/localization';import {CORS_LINK} from '../../constants/default-settings';-import {validateUrl} from '../../utils/url';const propTypes = {onLoadRemoteMap: PropTypes.func.isRequireddiff --git a/examples/demo-app/src/utils/url.js b/examples/demo-app/src/utils/url.jsdeleted file mode 100644index e0e380e6c..000000000--- a/examples/demo-app/src/utils/url.js+++ /dev/null@@ -1,15 +0,0 @@-// SPDX-License-Identifier: MIT-// Copyright contributors to the kepler.gl project--/**- * Validates an url- * @param str- */-export function validateUrl(str) {- try {- new URL(str);- return true;- } catch {- return false;- }-}diff --git a/src/common-utils/src/url.ts b/src/common-utils/src/url.tsindex 02c25e373..968d9164e 100644--- a/src/common-utils/src/url.ts+++ b/src/common-utils/src/url.ts@@ -52,3 +52,23 @@ parseUri.options = {/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}};++/**+ * Validates an url+ * @param str+ */+export function validateUrl(str) {+ try {+ new URL(str);+ return true;+ } catch {+ return false;+ }+}++/**+ * Checks whether a given URL points to a PMTiles file.+ * @param url The URL to check.+ * @returns True if the URL includes '.pmtiles', otherwise false.+ */+export const isPMTilesUrl = (url?: string | null) => url?.includes('.pmtiles');diff --git a/src/components/src/hooks/use-fetch-raster-tile-metadata.ts b/src/components/src/hooks/use-fetch-raster-tile-metadata.tsnew file mode 100644index 000000000..7fa9539f8--- /dev/null+++ b/src/components/src/hooks/use-fetch-raster-tile-metadata.ts@@ -0,0 +1,83 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {fetch} from 'global';+import {useEffect, useState} from 'react';++import {PMTilesSource, PMTilesMetadata} from '@loaders.gl/pmtiles';++import {VectorTileMetadata} from '@kepler.gl/table';+import {JsonObjectOrArray, StacTypes} from '@kepler.gl/types';+import {RasterTileType} from '@kepler.gl/constants';++type RasterMetadataResponse = VectorTileMetadata | StacTypes.CompleteSTACObject | null;++export type FetchJsonProps = {+ url: string | null;+ rasterTileType: RasterTileType;+ options?: JsonObjectOrArray;+ process: (+ json: PMTilesMetadata | JsonObjectOrArray,+ options: {metadataUrl: string; rasterTileType: RasterTileType}+ ) => RasterMetadataResponse | Error;+};++export type UseFetchJsonReturn = {+ data: RasterMetadataResponse;+ loading: boolean;+ error: Error | null;+};++export default function useFetchJson({+ url,+ rasterTileType,+ options,+ process+}: FetchJsonProps): UseFetchJsonReturn {+ const [error, setError] = useState<Error | null>(null);+ const [data, setData] = useState<RasterMetadataResponse>(null);+ const [loading, setLoading] = useState<boolean>(false);++ useEffect(() => {+ const getAndProcessMetadata = async () => {+ setError(null);+ setData(null);+ if (url) {+ setLoading(true);++ try {+ let rawMetadata: PMTilesMetadata | JsonObjectOrArray | null = null;+ if (rasterTileType === RasterTileType.PMTILES) {+ const tileSource = PMTilesSource.createDataSource(url, {});+ rawMetadata = await tileSource.metadata;+ } else {+ const response = await fetch(url, options);+ if (!response.ok) {+ throw new Error(`Failed Fetch ${url}`);+ }+ rawMetadata = await response.json();+ }++ if (!rawMetadata) {+ throw new Error('Failed to fetch metadata');+ }++ const processedMetadata = process(rawMetadata, {rasterTileType, metadataUrl: url});+ if (processedMetadata instanceof Error) {+ setError(processedMetadata);+ } else {+ setError(null);+ setData(processedMetadata);+ }+ } catch (metadataError) {+ setError(metadataError as Error);+ }+ setLoading(false);+ }+ };++ getAndProcessMetadata();+ }, [url, rasterTileType, options, process]);++ return {data, loading, error};+}diff --git a/src/components/src/loading-indicator.tsx b/src/components/src/loading-indicator.tsxindex 58bef9bd7..9b0027274 100644--- a/src/components/src/loading-indicator.tsx+++ b/src/components/src/loading-indicator.tsx@@ -30,6 +30,9 @@ type LoadingIndicatorProps = {sidePanelWidth?: number;};+/** Extra adjustment for the loading indicator when side panel is visible */+const LEFT_POSITION_ADJUSTMENT = 3;+const LoadingIndicator: React.FC<PropsWithChildren<LoadingIndicatorProps> & {theme: any}> = ({isVisible,children,@@ -37,7 +40,9 @@ const LoadingIndicator: React.FC<PropsWithChildren<LoadingIndicatorProps> & {thesidePanelWidth,theme}) => {- const left = (activeSidePanel ? sidePanelWidth || 0 : 0) + theme.sidePanel.margin.left;+ const left =+ (activeSidePanel ? (sidePanelWidth || 0) + LEFT_POSITION_ADJUSTMENT : 0) ++ theme.sidePanel.margin.left;return (<StyledContainer $isVisible={isVisible} $left={left}>diff --git a/src/components/src/map-container.tsx b/src/components/src/map-container.tsxindex a3b2a16c6..ab0d25c7d 100644--- a/src/components/src/map-container.tsx+++ b/src/components/src/map-container.tsx@@ -31,7 +31,8 @@ import {LayerBaseConfig,VisualChannelDomain,EditorLayerUtils,- AggregatedBin+ AggregatedBin,+ isRasterTilesBeingLoaded} from '@kepler.gl/layers';import {DatasetAttribution,@@ -1157,7 +1158,7 @@ export default function MapContainerFactory({this._renderMapPopover()}{primary !== isSplit ? (<LoadingIndicator- isVisible={Boolean(isLoadingIndicatorVisible)}+ isVisible={isLoadingIndicatorVisible || isRasterTilesBeingLoaded()}activeSidePanel={Boolean(activeSidePanel)}sidePanelWidth={sidePanelWidth}>diff --git a/src/components/src/modals/export-data-modal.tsx b/src/components/src/modals/export-data-modal.tsxindex 7a36e3090..c871977b2 100644--- a/src/components/src/modals/export-data-modal.tsx+++ b/src/components/src/modals/export-data-modal.tsx@@ -91,9 +91,10 @@ const ExportDataModalFactory = () => {} = this.props;const exportAllDatasets = selectedDataset ? !datasets[selectedDataset] : true;- const showVectorTileWarning = Object.keys(datasets).some(datasetId => {+ const showTiledDatasetWarning = Object.keys(datasets).some(datasetId => {return (- datasets[datasetId].type === DatasetType.VECTOR_TILE &&+ (datasets[datasetId].type === DatasetType.VECTOR_TILE ||+ datasets[datasetId].type === DatasetType.RASTER_TILE) &&(selectedDataset === datasetId || exportAllDatasets));});@@ -182,10 +183,10 @@ const ExportDataModalFactory = () => {</StyledFilteredOption></div></StyledExportSection>- {showVectorTileWarning ? (+ {showTiledDatasetWarning ? (<div className="title"><StyledWarning>- <FormattedMessage id={'modal.exportData.vectorTileWarning'} />+ <FormattedMessage id={'modal.exportData.tiledDatasetWarning'} /></StyledWarning></div>) : null}diff --git a/src/components/src/modals/tilesets-modals/common.tsx b/src/components/src/modals/tilesets-modals/common.tsxindex c257a4239..1dfcf4947 100644--- a/src/components/src/modals/tilesets-modals/common.tsx+++ b/src/components/src/modals/tilesets-modals/common.tsx@@ -1,7 +1,8 @@// SPDX-License-Identifier: MIT// Copyright contributors to the kepler.gl project-import {VectorTileMetadata} from '@kepler.gl/table';+import type {VectorTileMetadata} from '@kepler.gl/table';+import type {StacTypes} from '@kepler.gl/types';export type DatasetCreationAttributes = {name: string;@@ -10,7 +11,7 @@ export type DatasetCreationAttributes = {};export type MetaResponse = {- metadata?: VectorTileMetadata | null;+ metadata?: VectorTileMetadata | StacTypes.CompleteSTACObject | null;dataset?: DatasetCreationAttributes | null;loading?: boolean;error?: Error | null;diff --git a/src/components/src/modals/tilesets-modals/load-tileset.tsx b/src/components/src/modals/tilesets-modals/load-tileset.tsxindex 234ff8abf..bd15ade76 100644--- a/src/components/src/modals/tilesets-modals/load-tileset.tsx+++ b/src/components/src/modals/tilesets-modals/load-tileset.tsx@@ -7,15 +7,16 @@ 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 {VectorTileIcon, RasterTileIcon} from '@kepler.gl/layers';+import {getError, getApplicationConfig} from '@kepler.gl/utils';import {MetaResponse} from './common';import LoadDataFooter from './load-data-footer';-// import TilesetIcon from './tileset-icon';+import TilesetIcon from './tileset-icon';import TilesetVectorForm from './tileset-vector-form';+import TilesetRasterForm from './tileset-raster-form';-// const WIDTH_ICON = '62px';+const WIDTH_ICON = '70px';const LoadTilesetTabContainer = styled.div`color: ${props => props.theme.AZURE};@@ -28,12 +29,12 @@ const Container = styled.div`background-color: ${props => props.theme.WHITE};`;-/*const TilesetTypeContainer = styled.div`display: grid;grid-template-columns: repeat(4, ${WIDTH_ICON});+ column-gap: 10px;+ margin-bottom: 20px;`;-*/const MetaContainer = styled.div`display: flex;@@ -71,7 +72,6 @@ const MetaInnerContainer = styled.div<MetaInnerContainerProps>`const StyledHeaderMessage = styled.div`color: ${props => props.theme.textColorLT};font-size: 14px;- margin-bottom: 12px;`;type LoadTilesetTabProps = {@@ -87,6 +87,12 @@ const TILE_TYPES = [label: 'Vector Tile',Icon: VectorTileIcon,Component: TilesetVectorForm+ },+ {+ id: 'rasterTile',+ label: 'Raster Tile',+ Icon: RasterTileIcon,+ Component: TilesetRasterForm}];@@ -95,12 +101,8 @@ function isReady(response) {}function LoadTilesetTabFactory() {- const LoadTilesetTab: React.FC<LoadTilesetTabProps> = ({- onTilesetAdded,- isAddingDatasets,- intl- }) => {- const [typeIndex /* , setTypeIndex*/] = useState<number>(0);+ const LoadTilesetTab: React.FC<LoadTilesetTabProps> = ({onTilesetAdded, isAddingDatasets}) => {+ const [typeIndex, setTypeIndex] = useState<number>(0);const [response, setResponse] = useState<MetaResponse>({});const error = response.error;@@ -115,24 +117,22 @@ function LoadTilesetTabFactory() {}}, [onTilesetAdded, response]);- const CurrentForm = TILE_TYPES[typeIndex].Component;+ // temp patch to hide raster tile layer while in development+ const enableRasterTileLayer = getApplicationConfig().enableRasterTileLayer;+ const tileTypes = useMemo(() => {+ return enableRasterTileLayer ? TILE_TYPES : [TILE_TYPES[0]];+ }, [enableRasterTileLayer]);- const tilesetHeaderMessage = `${intl.formatMessage({- id: 'tilesetSetup.header'- })}`;+ const CurrentForm = tileTypes[typeIndex].Component;return (<LoadTilesetTabContainer><Container><div>- <StyledHeaderMessage>{tilesetHeaderMessage}</StyledHeaderMessage>- {/**- <div>- <label htmlFor="tileset-type">Tileset Type</label>- </div>- * Enable once we support different vector layers+ <StyledHeaderMessage>Tileset Type</StyledHeaderMessage>+<TilesetTypeContainer className="tileset-type">- {TILE_TYPES.map((tileType, index) => (+ {tileTypes.map((tileType, index) => (<TilesetIconkey={tileType.label}name={tileType.label}@@ -142,7 +142,6 @@ function LoadTilesetTabFactory() {/>))}</TilesetTypeContainer>- */}<div><CurrentForm setResponse={setResponse} />{/**diff --git a/src/components/src/modals/tilesets-modals/tileset-icon.tsx b/src/components/src/modals/tilesets-modals/tileset-icon.tsxindex 1a83bfab7..37a89ece4 100644--- a/src/components/src/modals/tilesets-modals/tileset-icon.tsx+++ b/src/components/src/modals/tilesets-modals/tileset-icon.tsx@@ -17,7 +17,13 @@ type TileIconProps = {const TileIcon = styled.div<TileIconProps>`background-color: ${props => props.theme.GREY10};- color: ${props => (props.selected ? props.theme.BLUE : props.theme.AZURE200)};+ color: ${props =>+ props.selected ? props.theme.primaryBtnBgdHover : props.theme.secondaryBtnBgd};+ opacity: ${props => (props.selected ? 1 : 0.5)};+`;++const TileLabel = styled.div<TileIconProps>`+ font-weight: ${props => (props.selected ? 'bold' : 'normal')};`;type TilesetIconProps = {@@ -30,7 +36,7 @@ type TilesetIconProps = {const TilesetIcon: React.FC<TilesetIconProps> = ({Icon, name, onClick, selected = false}) => (<TileIconContainer onClick={onClick}><TileIcon selected={selected}>{Icon}</TileIcon>- <div>{name}</div>+ <TileLabel selected={selected}>{name}</TileLabel></TileIconContainer>);diff --git a/src/components/src/modals/tilesets-modals/tileset-raster-form.tsx b/src/components/src/modals/tilesets-modals/tileset-raster-form.tsxnew file mode 100644index 000000000..1cde9a4b2--- /dev/null+++ b/src/components/src/modals/tilesets-modals/tileset-raster-form.tsx@@ -0,0 +1,221 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import React, {useCallback, useEffect, useState} from 'react';+import styled from 'styled-components';++import {PMTilesMetadata} from '@loaders.gl/pmtiles';++import {isPMTilesUrl, validateUrl} from '@kepler.gl/common-utils';+import {DatasetType, RasterTileType, PMTilesType} from '@kepler.gl/constants';+import {JsonObjectOrArray} from '@kepler.gl/types';+import {parseRasterMetadata, parseVectorMetadata} from '@kepler.gl/table';+import {getApplicationConfig} from '@kepler.gl/utils';++import {default as useFetchJson} from '../../hooks/use-fetch-raster-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 RasterTilesetMeta = {+ name: string;+ metadataUrl: string;+ rasterTileServerUrls?: string[];+};++export function getDatasetAttributesFromRasterTile({+ name,+ metadataUrl,+ rasterTileServerUrls+}: RasterTilesetMeta): DatasetCreationAttributes {+ return {+ name,+ type: DatasetType.RASTER_TILE,+ metadata: {+ metadataUrl,+ ...(rasterTileServerUrls ? {rasterTileServerUrls} : {})+ }+ };+}++type RasterTileFormProps = {+ setResponse: (response: MetaResponse) => void;+};++const parseMetadataAllowCollections = (+ metadata: JsonObjectOrArray | PMTilesMetadata,+ {metadataUrl, rasterTileType}: {metadataUrl: string; rasterTileType: RasterTileType}+) => {+ return rasterTileType === RasterTileType.PMTILES+ ? parseVectorMetadata(metadata as PMTilesMetadata, {+ tileUrl: metadataUrl+ })+ : parseRasterMetadata(metadata as JsonObjectOrArray, {allowCollections: true});+};++const RasterTileForm: React.FC<RasterTileFormProps> = ({setResponse}) => {+ const [tileName, setTileName] = useState<string>('');+ const [currentUrl, setCurrentUrl] = useState<string>('');+ const [metadataUrl, setMetadataUrl] = useState<string>('');+ const [rasterTileServerUrls, setRasterTileServerUrls] = useState<string>(+ (getApplicationConfig().rasterServerUrls || []).join(',')+ );++ const onTileNameChange = useCallback(+ (event: React.ChangeEvent<HTMLInputElement>) => {+ event.preventDefault();+ setTileName(event.target.value);+ },+ [setTileName]+ );++ const onMetadataUrlChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {+ event.preventDefault();+ const {value} = event.target;+ setMetadataUrl(value);+ setTileName(value.split('/').pop() || '');+ setCurrentUrl(value);+ }, []);++ const onRasterTileServerUrlsChange = useCallback(+ (event: React.ChangeEvent<HTMLInputElement>) => {+ event.preventDefault();+ setRasterTileServerUrls(event.target.value);+ },+ [setRasterTileServerUrls]+ );++ const {+ data: metadata,+ loading,+ error: metaError+ } = useFetchJson({+ url: currentUrl,+ rasterTileType: isPMTilesUrl(currentUrl) ? RasterTileType.PMTILES : RasterTileType.STAC,+ process: parseMetadataAllowCollections+ });++ useEffect(() => {+ if (tileName && metadataUrl) {+ const pmtilesType = metadata?.pmtilesType;++ if (pmtilesType === PMTilesType.MVT) {+ return setResponse({+ metadata,+ dataset: null,+ loading,+ error: new Error('For .pmtiles in mvt format, please use the Vector Tile form.')+ });+ }++ let error = metaError;++ // check for raster tile servers for STAC items and collections+ let rasterTileServers;+ if (+ !error+ // We still need raster tile servers for PMTiles when we plan to use elevation+ ) {+ rasterTileServers = rasterTileServerUrls+ .split(',')+ .map(server => server.trim())+ .filter(server => server);+ if (+ rasterTileServers.length < 1 ||+ !rasterTileServers.every(server => validateUrl(server))+ ) {+ if (pmtilesType) {+ // For raster tiles elevation support is optional+ // TODO display a warning, but not a blocking error+ rasterTileServers = [];+ } else {+ error = new Error(+ 'Provide valid raster tile server urls to support STAC and elevations.'+ );+ }+ }+ }++ const dataset = getDatasetAttributesFromRasterTile({+ name: tileName,+ metadataUrl,+ rasterTileServerUrls: rasterTileServers+ });++ setResponse({+ metadata,+ dataset,+ loading,+ error+ });+ } else {+ setResponse({+ metadata,+ dataset: null,+ loading,+ error: metaError+ });+ }+ }, [+ metadata,+ loading,+ metaError,+ currentUrl,+ tileName,+ metadataUrl,+ rasterTileServerUrls,+ setResponse+ ]);++ return (+ <TilesetInputContainer>+ <div>+ <label htmlFor="tileset-name">Name</label>+ <InputLight+ id="tileset-name"+ placeholder="Name your tileset"+ value={tileName}+ onChange={onTileNameChange}+ />+ </div>+ <div>+ <label htmlFor="tile-metadata">Tileset metadata URL</label>+ <InputLight+ id="tile-metadata"+ placeholder="Tileset metadata"+ value={metadataUrl ?? undefined}+ onChange={onMetadataUrlChange}+ />+ <TilesetInputDescription>+ Supports raster .pmtiles. Limited support for STAC Items and Collections.+ </TilesetInputDescription>+ </div>+ <div>+ <label htmlFor="tileset-raster-servers">Raster tile servers</label>+ <InputLight+ id="tileset-raster-servers"+ placeholder="Raster tile servers (separated by commas)"+ value={rasterTileServerUrls}+ onChange={onRasterTileServerUrlsChange}+ />+ <TilesetInputDescription>+ Raster tile server URLs for Cloud Optimized GeoTIFF tilesets and elevation.+ </TilesetInputDescription>+ </div>+ </TilesetInputContainer>+ );+};++export default RasterTileForm;diff --git a/src/components/src/modals/tilesets-modals/tileset-vector-form.tsx b/src/components/src/modals/tilesets-modals/tileset-vector-form.tsxindex 53690e813..1d9040f86 100644--- a/src/components/src/modals/tilesets-modals/tileset-vector-form.tsx+++ b/src/components/src/modals/tilesets-modals/tileset-vector-form.tsx@@ -4,8 +4,10 @@import React, {useCallback, useEffect, useState, useMemo} from 'react';import styled from 'styled-components';+import {isPMTilesUrl} from '@kepler.gl/common-utils';import {DatasetType,+ PMTilesType,RemoteTileFormat,VectorTileDatasetMetadata,REMOTE_TILE@@ -38,8 +40,6 @@ export type VectorTilesetFormData = {metadataUrl?: string;};-const isPMTilesUrl = (url?: string | null) => url?.includes('.pmtiles');-export type VectorTileDatasetCreationAttributes = Merge<DatasetCreationAttributes,{@@ -143,6 +143,15 @@ const TilesetVectorForm: React.FC<TilesetVectorFormProps> = ({setResponse}) => {useEffect(() => {if (tileName && tileUrl) {+ if (metadata?.pmtilesType === PMTilesType.RASTER) {+ return setResponse({+ metadata,+ dataset: null,+ loading,+ error: new Error('For .pmtiles in raster format, please use the Raster Tile form.')+ });+ }+const dataset = getDatasetAttributesFromVectorTile({name: tileName,dataUrl: tileUrl,@@ -175,7 +184,10 @@ const TilesetVectorForm: React.FC<TilesetVectorFormProps> = ({setResponse}) => {useEffect(() => {if (metadata) {- setTileName((metadata as VectorTileMetadata).name || '');+ const name = (metadata as VectorTileMetadata).name;+ if (name) {+ setTileName(name);+ }}}, [metadata]);@@ -184,7 +196,7 @@ const TilesetVectorForm: React.FC<TilesetVectorFormProps> = ({setResponse}) => {<div><label htmlFor="tileset-name">Name</label><InputLight- id="tile-name"+ id="tileset-name"placeholder="Name your tileset"value={tileName}onChange={onTileNameChange}diff --git a/src/components/src/side-panel/common/dataset-info.tsx b/src/components/src/side-panel/common/dataset-info.tsxindex 743a77c65..4bb618e7d 100644--- a/src/components/src/side-panel/common/dataset-info.tsx+++ b/src/components/src/side-panel/common/dataset-info.tsx@@ -33,6 +33,8 @@ export default function DatasetInfoFactory() {id={dataset.type === DatasetType.VECTOR_TILE? 'datasetInfo.vectorTile'+ : dataset.type === DatasetType.RASTER_TILE+ ? 'datasetInfo.rasterTile': 'datasetInfo.rowCount'}values={{rowCount: numFormat(dataset.dataContainer.numRows())}}diff --git a/src/components/src/side-panel/layer-manager.tsx b/src/components/src/side-panel/layer-manager.tsxindex ffbc340c7..3bac4bf4e 100644--- a/src/components/src/side-panel/layer-manager.tsx+++ b/src/components/src/side-panel/layer-manager.tsx@@ -1,7 +1,7 @@// SPDX-License-Identifier: MIT// Copyright contributors to the kepler.gl project-import React, {useCallback} from 'react';+import React, {useCallback, useMemo} from 'react';import {injectIntl, WrappedComponentProps} from 'react-intl';import styled from 'styled-components';@@ -24,6 +24,7 @@ import {UIStateActions, VisStateActions, MapStateActions, ActionHandler} from '@import {SidePanelItem} from '../types';import {PanelListView} from '@kepler.gl/types';import {Datasets} from '@kepler.gl/table';+import {getApplicationConfig} from '@kepler.gl/utils';type LayerBlendingSelectorProps = {layerBlending: string;@@ -189,6 +190,14 @@ function LayerManagerFactory(const isSortByDatasetMode = panelListView === PANEL_VIEW_TOGGLES.byDataset;+ const enableRasterTileLayer = getApplicationConfig().enableRasterTileLayer;+ const filteredLayerClasses = useMemo(() => {+ if (enableRasterTileLayer) return layerClasses;+ /* eslint-disable @typescript-eslint/no-unused-vars */+ const {rasterTile, ...restClasses} = layerClasses;+ return restClasses as LayerClassesType;+ }, [enableRasterTileLayer, layerClasses]);+return (<div className="layer-manager"><SidePanelSection>@@ -221,7 +230,7 @@ function LayerManagerFactory(updateTableColor={updateTableColor}removeDataset={removeDataset}layerOrder={layerOrder}- layerClasses={layerClasses}+ layerClasses={filteredLayerClasses}uiStateActions={uiStateActions}visStateActions={visStateActions}mapStateActions={mapStateActions}@@ -235,7 +244,7 @@ function LayerManagerFactory(uiStateActions={uiStateActions}visStateActions={visStateActions}mapStateActions={mapStateActions}- layerClasses={layerClasses}+ layerClasses={filteredLayerClasses}/>)}</SidePanelSection>diff --git a/src/components/src/side-panel/layer-panel/dataset-layer-section.tsx b/src/components/src/side-panel/layer-panel/dataset-layer-section.tsxindex 7100837c4..45980656b 100644--- a/src/components/src/side-panel/layer-panel/dataset-layer-section.tsx+++ b/src/components/src/side-panel/layer-panel/dataset-layer-section.tsx@@ -9,6 +9,7 @@ import LayerListFactory from './layer-list';import {Layer, LayerClassesType} from '@kepler.gl/layers';import {UIStateActions, ActionHandler, VisStateActions, MapStateActions} from '@kepler.gl/actions';import {KeplerTable, Datasets} from '@kepler.gl/table';+import {getApplicationConfig} from '@kepler.gl/utils';type DatasetLayerSectionProps = {datasets: Datasets;@@ -57,6 +58,14 @@ function DatasetLayerSectionFactory(return {[dataset.id]: dataset};}, [dataset]);+ const enableRasterTileLayer = getApplicationConfig().enableRasterTileLayer;+ const filteredLayerClasses = useMemo(() => {+ if (enableRasterTileLayer) return layerClasses;+ /* eslint-disable @typescript-eslint/no-unused-vars */+ const {rasterTile, ...restClasses} = layerClasses;+ return restClasses as LayerClassesType;+ }, [enableRasterTileLayer, layerClasses]);+return (<DatasetLayerSectionWrapper><SourceDataCatalog@@ -70,7 +79,7 @@ function DatasetLayerSectionFactory(datasets={datasets}layerOrder={layerOrder}layers={layers}- layerClasses={layerClasses}+ layerClasses={filteredLayerClasses}uiStateActions={uiStateActions}visStateActions={visStateActions}mapStateActions={mapStateActions}diff --git a/src/components/src/side-panel/layer-panel/layer-configurator.tsx b/src/components/src/side-panel/layer-panel/layer-configurator.tsxindex 86e176159..83d078578 100644--- a/src/components/src/side-panel/layer-panel/layer-configurator.tsx+++ b/src/components/src/side-panel/layer-panel/layer-configurator.tsx@@ -25,6 +25,8 @@ 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 RasterTileLayerConfiguratorFactory from './raster-tile-layer-configurator';import VectorTileLayerConfiguratorFactory from './vector-tile-layer-configurator';import {ActionHandler, toggleModal} from '@kepler.gl/actions';@@ -128,7 +130,8 @@ LayerConfiguratorFactory.deps = [LayerColorRangeSelectorFactory,ArcLayerColorSelectorFactory,AggrScaleSelectorFactory,- VectorTileLayerConfiguratorFactory+ VectorTileLayerConfiguratorFactory,+ RasterTileLayerConfiguratorFactory];export default function LayerConfiguratorFactory(@@ -144,7 +147,8 @@ export default function LayerConfiguratorFactory(LayerColorRangeSelector: ReturnType<typeof LayerColorRangeSelectorFactory>,ArcLayerColorSelector: ReturnType<typeof ArcLayerColorSelectorFactory>,AggrScaleSelector: ReturnType<typeof AggrScaleSelectorFactory>,- VectorTileLayerConfigurator: ReturnType<typeof VectorTileLayerConfiguratorFactory>+ VectorTileLayerConfigurator: ReturnType<typeof VectorTileLayerConfiguratorFactory>,+ RasterTileLayerConfigurator: ReturnType<typeof RasterTileLayerConfiguratorFactory>): React.ComponentType<LayerConfiguratorProps> {class LayerConfigurator extends Component<LayerConfiguratorProps> {_renderPointLayerConfig(props) {@@ -159,6 +163,10 @@ export default function LayerConfiguratorFactory(return <VectorTileLayerConfigurator {...props} />;}+ _renderRasterTileLayerConfig(props) {+ return <RasterTileLayerConfigurator {...props} />;+ }+_renderScatterplotLayerConfig({layer, visConfiguratorProps, layerChannelConfigProps, layerConfiguratorProps},showInteractionControlsdiff --git a/src/components/src/side-panel/layer-panel/layer-type-list-item.tsx b/src/components/src/side-panel/layer-panel/layer-type-list-item.tsxindex c2dfa23cb..b7424098a 100644--- a/src/components/src/side-panel/layer-panel/layer-type-list-item.tsx+++ b/src/components/src/side-panel/layer-panel/layer-type-list-item.tsx@@ -58,6 +58,7 @@ const StyledListItem = styled.div`font-size: 12px;text-align: center;color: ${props => props.theme.selectColor};+ max-width: ${props => props.theme.layerTypeIconSizeL}px;}`;diff --git a/src/components/src/side-panel/layer-panel/raster-tile-colormap-list-item.tsx b/src/components/src/side-panel/layer-panel/raster-tile-colormap-list-item.tsxnew file mode 100644index 000000000..cecb5fba2--- /dev/null+++ b/src/components/src/side-panel/layer-panel/raster-tile-colormap-list-item.tsx@@ -0,0 +1,68 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import React from 'react';+import styled from 'styled-components';++import {RasterLayerResources as cdnUrls} from '@kepler.gl/constants';+import {getCategoricalColormapDataUrl} from '@kepler.gl/layers';+import type {CategoricalColormapOptions} from '@kepler.gl/layers';++const StyledColorMapListItem = styled.div`+ display: flex;+ align-items: center;+ justify-content: space-between;++ .color-name {+ width: calc(40% - 8px);+ margin-right: 8px;+ color: ${props => props.theme.subtextColor};+ }+ .color-image {+ width: 60%;+ height: 10px;+ border-radius: 2px;+ background: ${props => props.theme.panelContentBackground};+ overflow: hidden;+ display: flex;+ }+`;++const ColorMapImg: React.FC<{id: string; categoricalOptions: CategoricalColormapOptions}> = ({+ id,+ categoricalOptions+}) => {+ let url = '';+ if (id === '_categorical') {+ const dataUrl = getCategoricalColormapDataUrl(categoricalOptions);+ if (!dataUrl) {+ return null;+ }+ url = dataUrl;+ } else {+ url = cdnUrls.rasterColorMap(id);+ }+ return <img src={url} style={{width: '100%', height: '100%'}} />;+};++export const ColorMapListItem: React.FC<{+ value: {id: string; label: string};+ categoricalOptions: CategoricalColormapOptions;+}> = ({value, categoricalOptions}) => (+ <StyledColorMapListItem>+ <div className="color-name">{value?.label}</div>+ <div className="color-image">+ <ColorMapImg id={value?.id} categoricalOptions={categoricalOptions} />+ </div>+ </StyledColorMapListItem>+);++export const getColorMapListItemComponent = (+ categoricalOptions: CategoricalColormapOptions+): React.FC<{+ value: {id: string; label: string};+}> => {+ return (props: {value: {id: string; label: string}}) => (+ <ColorMapListItem {...props} categoricalOptions={categoricalOptions} />+ );+};diff --git a/src/components/src/side-panel/layer-panel/raster-tile-layer-configurator.tsx b/src/components/src/side-panel/layer-panel/raster-tile-layer-configurator.tsxnew file mode 100644index 000000000..b1f363cf6--- /dev/null+++ b/src/components/src/side-panel/layer-panel/raster-tile-layer-configurator.tsx@@ -0,0 +1,565 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import React, {useEffect, useMemo} from 'react';+import styled from 'styled-components';++import {PMTilesType} from '@kepler.gl/constants';+import {+ filterAvailablePresets,+ getEOBands,+ getRasterStatisticsMinMax,+ getSingleBandPresetOptions,+ isColormapAllowed,+ isFilterAllowed,+ isRescalingAllowed,+ isSearchableStac,+ CompleteSTACObject,+ GetTileDataCustomProps,+ DataSourceParams,+ PresetOption,+ RasterTileLayer,+ CATEGORICAL_COLORMAP_ID,+ PRESET_OPTIONS,+ RASTER_COLOR_RESET_PARAMS,+ DATA_SOURCE_COLOR_DEFAULTS+} from '@kepler.gl/layers';+import {FormattedMessage} from '@kepler.gl/localization';+import {capitalizeFirstLetter, getApplicationConfig} from '@kepler.gl/utils';+import {KeplerTable as KeplerDataset} from '@kepler.gl/table';+import type {StacTypes} from '@kepler.gl/types';++import {getColorMapListItemComponent} from './raster-tile-colormap-list-item';++import {+ Input,+ PanelLabel,+ PanelLabelWrapper,+ SidePanelSection+} from '../../common/styled-components';+import Switch from '../../common/switch';+import InfoHelperFactory from '../../common/info-helper';+import ItemSelector from '../../common/item-selector/item-selector';+import VisConfigSliderFactory from '../../side-panel/layer-panel/vis-config-slider';+import LayerConfigGroupFactory from '../../side-panel/layer-panel/layer-config-group';+import VisConfigSwitchFactory from '../../side-panel/layer-panel/vis-config-switch';++type EOBand = StacTypes.Band;++const STAC_SEARCH_UI_ENABLED = true;++const StyledVisConfigSwitch = styled.div`+ display: flex;+ justify-content: space-between;++ .vis-config-switch__title {+ display: flex;+ }+`;++const CustomVisConfigSwitch = ({+ layer: {id, config},+ property,+ onChange,+ label,+ disabled+}: {+ layer: RasterTileLayer;+ property: string;+ onChange: (value: any) => void;+ label: string;+ disabled: boolean;+}) => (+ <SidePanelSection disabled={Boolean(disabled)}>+ <StyledVisConfigSwitch className="vis-config-switch">+ <div className="vis-config-switch__title">+ <PanelLabelWrapper>+ {label ? (+ <PanelLabel>+ {(label && <FormattedMessage id={label} />) || capitalizeFirstLetter(property)}+ </PanelLabel>+ ) : null}+ </PanelLabelWrapper>+ </div>+ <div className="vis-config-switch__switch">+ <Switch+ checked={config.visConfig[property]}+ id={`${id}-${property}-switch`}+ onChange={() => onChange({[property]: !config.visConfig[property]})}+ />+ </div>+ </StyledVisConfigSwitch>+ </SidePanelSection>+);++const StyledLayerConfigurator = styled.div`+ margin-top: 12px;+`;++const DescriptionText = styled(PanelLabel)`+ text-transform: none;+ display: inline;+ color: ${props => props.theme.subtextColor};++ a {+ text-decoration: underline;+ margin-left: 4px;+ }+`;++// TODO: combine these two helpers into one+function findVisConfigItemById(layer, prop) {+ return layer.visConfigSettings[prop].options.find(op => op.id === layer.config.visConfig[prop]);+}++function findItemById(layer, options, prop) {+ // For now it's possible that non-raster metadata will be passed to the raster configurator, so+ // options may be null+ return options?.find(op => op.id === layer.config.visConfig[prop]);+}++/**+ * Generate drop down labels for single band selector+ * @param stac - STAC item metadata object+ * @returns array of {id, label} elements+ */+function getBandSelectorOptions(stac: CompleteSTACObject): {id?: string; label?: string}[] {+ const eoBands = getEOBands(stac) || [];+ return eoBands.map((eoBand: EOBand) => ({+ id: eoBand.name || eoBand.common_name,+ label: eoBand.common_name+ ? `${eoBand.name || eoBand.common_name} (${eoBand.common_name})`+ : eoBand.name+ }));+}++function getCategoricalColormapListItem(categoricalColorMap) {+ let categoricalItem: {label: string; id: string} | undefined;+ if (categoricalColorMap) {+ categoricalItem = {label: 'Categorical', id: CATEGORICAL_COLORMAP_ID};+ }+ return categoricalItem;+}++/**+ * Check if data source global pixel color range is calculated correctly+ * @param layer - raster layer+ * @param stac - STAC metadata object+ * @param preset - preset name+ * @param singleBandPresetOptions - options for single band preset+ * @returns boolean value+ */+function isDataSourceColorRangeAvailable(+ layer: RasterTileLayer,+ stac: CompleteSTACObject,+ preset: string,+ singleBandPresetOptions: PresetOption['singleBand']+): boolean {+ let minPixelValue: number | null = null;+ let maxPixelValue: number | null = null;+ const dataSourceParams: DataSourceParams | null = layer.getDataSourceParams(stac, preset, {+ singleBand: singleBandPresetOptions+ });+ if (dataSourceParams) {+ minPixelValue = dataSourceParams.minPixelValue;+ maxPixelValue = dataSourceParams.maxPixelValue;+ }+ return minPixelValue !== null && maxPixelValue !== null;+}++/**+ * Updates color parameters based on a change in visualization preset.+ *+ * This function adjusts the color processing parameters when the visualization+ * preset changes, based on whether the preset is switching to or from a+ * single-band. Determines whether to apply color+ * enhancements or reset parameters based on the preset transition.+ *+ * @param stac A STAC (SpatioTemporal Asset Catalog) object representing the dataset.+ * @param newPreset The old visualization preset being applied.+ * @param newPreset The new visualization preset being applied.+ * @returns The updated color parameters to apply.+ */+function updateColorParamsOnPresetChange(+ stac: CompleteSTACObject,+ oldPreset: string,+ newPreset: string+) {+ let colorParams = {};+ const colorOverrides = DATA_SOURCE_COLOR_DEFAULTS[stac.id];+ if (colorOverrides) return colorParams;++ const bandOfInterest = 'singleBand';+ if (oldPreset === bandOfInterest && newPreset !== bandOfInterest) {+ // enchance colors for multiband combinations+ colorParams = colorOverrides;+ } else if (oldPreset !== bandOfInterest && newPreset === bandOfInterest) {+ // reset image processng params+ colorParams = RASTER_COLOR_RESET_PARAMS;+ }++ return colorParams;+}++type RasterTileLayerConfiguratorProps = {+ layer: RasterTileLayer;+ visConfiguratorProps: any;+ dataset: KeplerDataset;+};++RasterTileLayerConfiguratorFactory.deps = [+ LayerConfigGroupFactory,+ VisConfigSliderFactory,+ InfoHelperFactory,+ VisConfigSwitchFactory+];++function RasterTileLayerConfiguratorFactory(+ LayerConfigGroup: ReturnType<typeof LayerConfigGroupFactory>,+ VisConfigSlider: ReturnType<typeof VisConfigSliderFactory>,+ InfoHelper: ReturnType<typeof InfoHelperFactory>,+ VisConfigSwitch: ReturnType<typeof VisConfigSwitchFactory>+): React.FC<RasterTileLayerConfiguratorProps> {+ /**+ * Wrapper around configurator to check for dataset.metadata being null/undefined+ */+ const STACCheckConfiguratorWrapper = ({+ layer,+ visConfiguratorProps,+ dataset+ }: RasterTileLayerConfiguratorProps) => {+ const stac = dataset?.metadata;++ // If no dataset is loaded into Kepler, stac can be undefined+ if (!stac) {+ return null;+ }++ return (+ <RasterTileLayerConfigurator+ layer={layer}+ visConfiguratorProps={visConfiguratorProps}+ dataset={dataset}+ />+ );+ };++ // eslint-disable-next-line complexity+ const RasterTileLayerConfigurator: React.FC<RasterTileLayerConfiguratorProps> = ({+ layer,+ visConfiguratorProps,+ dataset+ }) => {+ const {+ preset,+ nonLinearRescaling,+ useSTACSearching,+ colorRange: {colorMap: categoricalColorMap},+ dynamicColor,+ singleBandName+ } = layer.config.visConfig;++ const stac = dataset?.metadata as GetTileDataCustomProps['stac'];++ const availablePresets = useMemo(() => filterAvailablePresets(stac, PRESET_OPTIONS), [stac]);++ const presetOptions = useMemo(+ () =>+ (+ layer.visConfigSettings.preset.options as unknown as {+ id: string;+ label: string;+ }[]+ ).filter(({id}) => availablePresets?.includes(id)),+ [layer.visConfigSettings.preset.options, availablePresets]+ );+ const singleBandOptions = useMemo(() => getBandSelectorOptions(stac), [stac]);+ const colormapOptions = useMemo(() => {+ const options = [+ ...(layer.visConfigSettings.colormapId.options as unknown as {+ id: string;+ label: string;+ }[])+ ];+ const categoricalListItem = getCategoricalColormapListItem(categoricalColorMap);+ if (categoricalListItem) {+ options.push(categoricalListItem);+ }+ return options;+ }, [layer.visConfigSettings.colormapId.options, categoricalColorMap]);++ const {bandCombination} = PRESET_OPTIONS[preset] || {};+ const colormapAllowed = isColormapAllowed(bandCombination);+ const rescalingAllowed = !categoricalColorMap && isRescalingAllowed(bandCombination);+ const filterAllowed = isFilterAllowed(bandCombination);++ // Here we show the UI when useSTACSearching is explicitly set to true so that the UI shows up+ const stacSearchAllowed = isSearchableStac(stac) || useSTACSearching;++ const selectedColormap =+ findVisConfigItemById(layer, 'colormapId') ||+ getCategoricalColormapListItem(categoricalColorMap);+ const selectedPreset = findVisConfigItemById(layer, 'preset');+ const selectedSingleBandName = findItemById(layer, singleBandOptions, 'singleBandName');++ const singleBandPresetOptions = getSingleBandPresetOptions(stac, singleBandName);+ const [minCategoricalBandValue, maxCategoricalBandValue] = getRasterStatisticsMinMax(+ stac,+ preset,+ singleBandPresetOptions+ );+ const ColorMapListItem = useMemo(+ () =>+ getColorMapListItemComponent({+ colorMap: categoricalColorMap,+ minValue: minCategoricalBandValue,+ maxValue: maxCategoricalBandValue+ }),+ [categoricalColorMap, minCategoricalBandValue, maxCategoricalBandValue]+ );+ const isDynamicColorsOnly = !isDataSourceColorRangeAvailable(+ layer,+ stac,+ preset,+ singleBandPresetOptions+ );++ // Default of `dynamicColor` is false. Set it true if it is not possible to get data source+ // wide color range+ useEffect(() => {+ if (isDynamicColorsOnly && !dynamicColor) {+ visConfiguratorProps.onChange({dynamicColor: true});+ }+ }, [visConfiguratorProps, dynamicColor, isDynamicColorsOnly]);++ // For PMTiles in raster format, only show opacity and terrain options for now+ if (stac.pmtilesType === PMTilesType.RASTER) {+ return (+ <StyledLayerConfigurator>+ <LayerConfigGroup {...visConfiguratorProps} label="Visual Settings" collapsible={false}>+ <VisConfigSlider {...layer.visConfigSettings.opacity} {...visConfiguratorProps} />+ </LayerConfigGroup>+ {getApplicationConfig().rasterServerSupportsElevation &&+ stac.rasterTileServerUrls?.length && (+ <LayerConfigGroup {...visConfiguratorProps} label="Terrain">+ <VisConfigSwitch+ {...visConfiguratorProps}+ {...layer.visConfigSettings.enableTerrain}+ />+ </LayerConfigGroup>+ )}+ </StyledLayerConfigurator>+ );+ }++ return (+ <StyledLayerConfigurator>+ {availablePresets && (+ <LayerConfigGroup {...visConfiguratorProps} label="Image Selection" collapsible={false}>+ <SidePanelSection>+ <PanelLabelWrapper>+ <PanelLabel>Preset</PanelLabel>+ <InfoHelper+ id="preset"+ description="Select a preset to describe how to combine spectral bands."+ />+ </PanelLabelWrapper>+ <ItemSelector+ selectedItems={selectedPreset}+ options={presetOptions}+ multiSelect={false}+ searchable={false}+ displayOption="label"+ getOptionValue="id"+ onChange={newPreset => {+ const overrides = updateColorParamsOnPresetChange(+ stac,+ visConfiguratorProps.layer.config.visConfig.preset,+ newPreset as string+ );+ visConfiguratorProps.onChange({...overrides, preset: newPreset});+ }}+ />+ {selectedPreset?.description ? (+ <DescriptionText>+ {selectedPreset?.description}+ {selectedPreset?.infoUrl ? (+ <a target="_blank" rel="noopener noreferrer" href={selectedPreset?.infoUrl}>+ More Info+ </a>+ ) : null}+ </DescriptionText>+ ) : null}+ </SidePanelSection>++ {selectedPreset.id === 'singleBand' && (+ <SidePanelSection>+ <PanelLabelWrapper>+ <PanelLabel>Single Band Name</PanelLabel>+ <InfoHelper+ id={`${layer.id}-single-band-name`}+ description="Select a single band."+ />+ </PanelLabelWrapper>+ <ItemSelector+ selectedItems={selectedSingleBandName}+ options={singleBandOptions}+ multiSelect={false}+ searchable={false}+ displayOption="label"+ getOptionValue="id"+ onChange={val => {+ visConfiguratorProps.onChange({singleBandName: val});+ }}+ />+ </SidePanelSection>+ )}++ {STAC_SEARCH_UI_ENABLED && stacSearchAllowed && (+ <CustomVisConfigSwitch+ {...layer.visConfigSettings.useSTACSearching}+ {...visConfiguratorProps}+ />+ )}++ {STAC_SEARCH_UI_ENABLED && (+ <>+ {stacSearchAllowed && useSTACSearching ? (+ <SidePanelSection>+ <PanelLabelWrapper>+ <PanelLabel>STAC Search Provider</PanelLabel>+ </PanelLabelWrapper>+ <ItemSelector+ {...layer.visConfigSettings.stacSearchProvider}+ selectedItems={findVisConfigItemById(layer, 'stacSearchProvider')}+ placeholder="Choose search provider"+ multiSelect={false}+ searchable={false}+ displayOption="label"+ getOptionValue="id"+ onChange={val => {+ // TODO: check when switching layers so that you don't mismatch allowed mosaics with layers+ visConfiguratorProps.onChange({stacSearchProvider: val});+ }}+ />++ <PanelLabelWrapper>+ <PanelLabel>Date Range</PanelLabel>+ </PanelLabelWrapper>+ <Input+ type="text"+ id={`${layer.id}-startDate`}+ onChange={({target: {value}}) => {+ visConfiguratorProps.onChange({startDate: value});+ }}+ value={layer.config.visConfig.startDate}+ />+ <Input+ type="text"+ id={`${layer.id}-endDate`}+ onChange={({target: {value}}) => {+ visConfiguratorProps.onChange({endDate: value});+ }}+ value={layer.config.visConfig.endDate}+ />+ <DescriptionText>Date format must be "YYYY-MM-DD"</DescriptionText>+ </SidePanelSection>+ ) : null}+ </>+ )}+ </LayerConfigGroup>+ )}++ <LayerConfigGroup {...visConfiguratorProps} label="Visual Settings" collapsible={false}>+ <VisConfigSlider {...layer.visConfigSettings.opacity} {...visConfiguratorProps} />+ {colormapAllowed && (+ <SidePanelSection>+ <PanelLabel>Colormap</PanelLabel>+ <ItemSelector+ selectedItems={selectedColormap}+ options={colormapOptions}+ multiSelect={false}+ displayOption="label"+ getOptionValue="id"+ onChange={val => {+ visConfiguratorProps.onChange({colormapId: val});+ }}+ DropDownLineItemRenderComponent={ColorMapListItem}+ filterOption="label"+ searchable+ />+ </SidePanelSection>+ )}+ </LayerConfigGroup>++ {/* Rescaling */}+ {rescalingAllowed && (+ <LayerConfigGroup {...visConfiguratorProps} label="Rescaling">+ <VisConfigSwitch+ {...visConfiguratorProps}+ {...layer.visConfigSettings.dynamicColor}+ disabled={isDynamicColorsOnly}+ />+ <VisConfigSwitch+ {...layer.visConfigSettings.nonLinearRescaling}+ {...visConfiguratorProps}+ label={nonLinearRescaling ? 'Non-Linear Rescaling' : 'Linear Rescaling'}+ />++ {/* TODO: add sliders for red, green, blue, not a single slider */}+ {nonLinearRescaling ? (+ <div>+ <VisConfigSlider+ {...layer.visConfigSettings.gammaContrastFactor}+ {...visConfiguratorProps}+ />+ <VisConfigSlider+ {...layer.visConfigSettings.sigmoidalContrastFactor}+ {...visConfiguratorProps}+ />+ <VisConfigSlider+ {...layer.visConfigSettings.sigmoidalBiasFactor}+ {...visConfiguratorProps}+ />+ </div>+ ) : (+ <VisConfigSlider+ {...layer.visConfigSettings.linearRescalingFactor}+ {...visConfiguratorProps}+ />+ )}++ <VisConfigSlider+ {...layer.visConfigSettings.saturationValue}+ {...visConfiguratorProps}+ />+ </LayerConfigGroup>+ )}++ {filterAllowed && (+ <LayerConfigGroup+ {...layer.visConfigSettings.filterEnabled}+ {...visConfiguratorProps}+ collapsible+ >+ <VisConfigSlider {...layer.visConfigSettings.filterRange} {...visConfiguratorProps} />+ </LayerConfigGroup>+ )}++ {getApplicationConfig().rasterServerSupportsElevation &&+ stac.rasterTileServerUrls?.length && (+ <LayerConfigGroup {...visConfiguratorProps} label="Terrain">+ <VisConfigSwitch+ {...visConfiguratorProps}+ {...layer.visConfigSettings.enableTerrain}+ />+ </LayerConfigGroup>+ )}+ </StyledLayerConfigurator>+ );+ };++ return STACCheckConfiguratorWrapper;+}++export default RasterTileLayerConfiguratorFactory;diff --git a/src/constants/src/dataset.ts b/src/constants/src/dataset.tsindex 3d11bf8c1..544c35fc5 100644--- a/src/constants/src/dataset.ts+++ b/src/constants/src/dataset.ts@@ -3,7 +3,8 @@export enum DatasetType {LOCAL = 'local',- VECTOR_TILE = 'vector-tile'+ VECTOR_TILE = 'vector-tile',+ RASTER_TILE = 'raster-tile'}export enum RemoteTileFormat {@@ -11,6 +12,11 @@ export enum RemoteTileFormat {PMTILES = 'pmtiles'}+export enum PMTilesType {+ RASTER = 'raster',+ MVT = 'mvt'+}+export const REMOTE_TILE = 'remote';export type VectorTileDatasetMetadata = {@@ -19,3 +25,42 @@ export type VectorTileDatasetMetadata = {tilesetDataUrl: string;tilesetMetadataUrl?: string;};++/**+ * Raster tileset metadata in STAC Item format. STAC version must be >= 1.0.0,+ * and the EO and Raster STAC extensions are required. This metadata shape can+ * be passed to the map to synchronously add a raster tileset.+ * @see https://github.com/radiantearth/stac-spec/blob/master/item-spec/item-spec.md+ */+export type RasterTileLocalMetadata = {+ type: 'Feature';++ /** URL for tileset metadata. */+ metadataUrl?: string;+ stac_version: string;+ stac_extensions: string[];+ assets: Record<string, any>;+};++/**+ * Raster tileset metadata with a remote metadata URL. This metadata can+ * be passed to the map to asynchronously load a raster tileset.+ */+export type RasterTileRemoteMetadata = {+ metadataUrl: string;+};++export enum RasterTileType {+ STAC = 'stac',+ PMTILES = 'pmtiles'+}++export type RasterTileMetadataSourceType = {+ pmtilesType?: PMTilesType;+};++/**+ * Raster tileset metadata.+ */+export type RasterTileDatasetMetadata = (RasterTileLocalMetadata | RasterTileRemoteMetadata) &+ RasterTileMetadataSourceType;diff --git a/src/constants/src/default-settings.ts b/src/constants/src/default-settings.tsindex 6fa98fa8d..fd311a216 100644--- a/src/constants/src/default-settings.ts+++ b/src/constants/src/default-settings.ts@@ -1593,3 +1593,25 @@ export enum GEOARROW_EXTENSIONS {MULTIPOLYGON = 'geoarrow.multipolygon',WKB = 'geoarrow.wkb'}++export const RasterLayerResources = {+ rasterColorMap: (colormapId: string) => {+ return `${KEPLER_UNFOLDED_BUCKET}/raster/colormaps/${colormapId}.png`;+ }+};++export const LOADERS_CDN_URL = 'https://unpkg.com/@loaders.gl';++export const getLoaderOptions = () => {+ return {+ mvt: {+ workerUrl: `${LOADERS_CDN_URL}/mvt/dist/mvt-worker.js`+ },+ 'quantized-mesh': {+ // workerUrl: `${LOADERS_CDN_URL}/terrain/dist/quantized-mesh-worker.js`+ },+ npy: {+ // workerUrl: `${LOADERS_CDN_URL}/textures/dist/npy-worker.js`+ }+ };+};diff --git a/src/constants/src/layers.ts b/src/constants/src/layers.tsindex ec43607a7..93eca9cf0 100644--- a/src/constants/src/layers.ts+++ b/src/constants/src/layers.ts@@ -553,7 +553,8 @@ export const LAYER_TYPES = keyMirror({'3D': null,trip: null,s2: null,- vectorTile: null+ vectorTile: null,+ rasterTile: null});export const EDITOR_AVAILABLE_LAYERS: string[] = [diff --git a/src/deckgl-layers/src/index.ts b/src/deckgl-layers/src/index.tsindex fc321fb45..6ef6d37af 100644--- a/src/deckgl-layers/src/index.ts+++ b/src/deckgl-layers/src/index.ts@@ -14,6 +14,10 @@ export {default as EnhancedLineLayer} from './line-layer/line-layer';export {default as SvgIconLayer} from './svg-icon-layer/svg-icon-layer';export {default as FilterArrowExtension} from './deckgl-extensions/filter-arrow-layer';+export {default as RasterLayer} from './raster/raster-layer/raster-layer';+export {default as RasterMeshLayer} from './raster/raster-mesh-layer/raster-mesh-layer';+export * as RasterWebGL from './raster/webgl';+export * from './layer-utils/shader-utils';export * from './3d-building-layer/types';diff --git a/src/deckgl-layers/src/raster/images.ts b/src/deckgl-layers/src/raster/images.tsnew file mode 100644index 000000000..c9165d4c7--- /dev/null+++ b/src/deckgl-layers/src/raster/images.ts@@ -0,0 +1,182 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import GL from '@luma.gl/constants';+import {isWebGL2, Texture2D} from '@luma.gl/core';+import type {Texture2DProps} from '@luma.gl/webgl';+import isEqual from 'lodash.isequal';++import type {ImageInput, ImageState} from './types';++/**+ * Texture parameters that should work for every texture on both WebGL1 and WebGL2+ */+const DEFAULT_UNIVERSAL_TEXTURE_PARAMETERS = {+ [GL.TEXTURE_MIN_FILTER]: GL.NEAREST,+ [GL.TEXTURE_MAG_FILTER]: GL.NEAREST,+ [GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,+ [GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE+};++type LoadImagesOptions = {+ gl: WebGLRenderingContext | WebGL2RenderingContext;+ images: ImageState;+ imagesData: ImageInput;+ oldImagesData: ImageInput;+};++/**+ * Load image items to webgl context+ * @param gl webgl rendering context+ * @param imageItem image item, might be single texture or array of textures+ * @returns loaded single webgl texture or array of webgl texture or null+ */+function loadImageItem(+ gl: WebGLRenderingContext | WebGL2RenderingContext,+ imageItem: Texture2DProps | Texture2D | (Texture2DProps | Texture2D)[]+): null | Texture2D | Texture2D[] {+ let result: null | Texture2D | Texture2D[];+ if (Array.isArray(imageItem)) {+ const dirtyResult = imageItem.map(x => loadTexture(gl, x));+ result = [];+ for (const texture of dirtyResult) {+ if (texture) {+ result.push(texture);+ }+ }+ if (!result.length) {+ result = null;+ }+ } else {+ result = loadTexture(gl, imageItem);+ }+ return result;+}++// eslint-disable-next-line complexity+export function loadImages({+ gl,+ images,+ imagesData,+ oldImagesData+}: LoadImagesOptions): ImageState | null {+ // Change to `true` if we need to setState with a new `images` object+ let imagesDirty = false;++ // If there are any removed keys, which previously existed in oldProps and+ // this.state.images but no longer exist in props, remove from the images+ // object+ if (oldImagesData) {+ for (const key in oldImagesData) {+ if (imagesData && !(key in imagesData) && key in images) {+ delete images[key];+ imagesDirty = true;+ }+ }+ }++ // Check if any keys of props.images have changed+ const changedKeys: string[] = [];+ for (const key in imagesData) {+ // If oldProps.images didn't exist or it existed and this key didn't exist+ if (!oldImagesData || (oldImagesData && !(key in oldImagesData))) {+ changedKeys.push(key);+ continue;+ }++ // Deep compare when the key previously existed to see if it changed+ if (!isEqual(imagesData[key], oldImagesData[key])) {+ changedKeys.push(key);+ }+ }++ for (const key of changedKeys) {+ const imageData = imagesData[key];+ if (!imageData) {+ continue;+ }++ const loadedItem = loadImageItem(gl, imageData);+ if (loadedItem) {+ images[key] = loadedItem;+ }+ imagesDirty = true;+ }++ if (imagesDirty) {+ return images;+ }++ return null;+}++/**+ * Create Texture2D object from image data+ */+function loadTexture(+ gl: WebGLRenderingContext | WebGL2RenderingContext,+ imageData: Texture2D | Texture2DProps+): Texture2D | null {+ if (!imageData) {+ return null;+ }++ if (imageData instanceof Texture2D) {+ return imageData;+ }++ let textureParams: Texture2DProps = {+ parameters: DEFAULT_UNIVERSAL_TEXTURE_PARAMETERS,+ ...imageData+ };++ if (!isWebGL2(gl)) {+ textureParams = webgl1TextureFallbacks(textureParams);+ }++ return new Texture2D(gl, textureParams);+}++/**+ * Texture fallbacks for WebGL1+ * Fallback ideas derived from viv+ * https://github.com/hms-dbmi/viv/blob/5bcec429eeba55914ef3d7155a610d82048520a0/src/layers/XRLayer/XRLayer.js#L280-L302+ */+function webgl1TextureFallbacks(textureParams: Texture2DProps): Texture2DProps {+ // Set mipmaps to false+ // Not sure if this is necessary?+ // Might actually only be necessary for uint textures+ textureParams.mipmaps = false;++ // Change format to Luminance+ if (textureParams.format && [GL.R8UI, GL.R16UI, GL.R32UI].includes(textureParams.format)) {+ textureParams.format = GL.LUMINANCE;+ }++ // Change dataFormat to Luminance+ if (textureParams.dataFormat === GL.RED_INTEGER) {+ textureParams.dataFormat = GL.LUMINANCE;+ }++ // Set data type to float+ if (+ textureParams.type &&+ [GL.UNSIGNED_BYTE, GL.UNSIGNED_SHORT, GL.UNSIGNED_INT].includes(textureParams.type)+ ) {+ textureParams.type = GL.FLOAT;+ }++ // Cast data to float 32 if one of the uint types+ if (+ textureParams.data instanceof Uint8Array ||+ textureParams.data instanceof Uint16Array ||+ textureParams.data instanceof Uint32Array+ ) {+ textureParams.data = new Float32Array(textureParams.data);+ }++ // Override texture parameters to make sure they're valid on WebGL1+ textureParams.parameters = {...textureParams.parameters, ...DEFAULT_UNIVERSAL_TEXTURE_PARAMETERS};++ return textureParams;+}diff --git a/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl1.fs.ts b/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl1.fs.tsnew file mode 100644index 000000000..6febf0f52--- /dev/null+++ b/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl1.fs.ts@@ -0,0 +1,96 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++export default `\+#define SHADER_NAME raster-layer-fragment-shader++#ifdef GL_ES+precision highp float;+#endif++varying vec2 vTexCoord;+varying vec2 vTexPos;++uniform float desaturate;+uniform vec4 transparentColor;+uniform vec3 tintColor;+uniform float opacity;++uniform float coordinateConversion;+uniform vec4 bounds;++/* projection utils */+const float TILE_SIZE = 512.0;+const float PI = 3.1415926536;+const float WORLD_SCALE = TILE_SIZE / PI / 2.0;++// from degrees to Web Mercator+vec2 lnglat_to_mercator(vec2 lnglat) {+ float x = lnglat.x;+ float y = clamp(lnglat.y, -89.9, 89.9);+ return vec2(+ radians(x) + PI,+ PI + log(tan(PI * 0.25 + radians(y) * 0.5))+ ) * WORLD_SCALE;+}++// from Web Mercator to degrees+vec2 mercator_to_lnglat(vec2 xy) {+ xy /= WORLD_SCALE;+ return degrees(vec2(+ xy.x - PI,+ atan(exp(xy.y - PI)) * 2.0 - PI * 0.5+ ));+}+/* End projection utils */++// apply desaturation+vec3 color_desaturate(vec3 color) {+ float luminance = (color.r + color.g + color.b) * 0.333333333;+ return mix(color, vec3(luminance), desaturate);+}++// apply tint+vec3 color_tint(vec3 color) {+ return color * tintColor;+}++// blend with background color+vec4 apply_opacity(vec3 color, float alpha) {+ if (transparentColor.a == 0.0) {+ return vec4(color, alpha);+ }+ float blendedAlpha = alpha + transparentColor.a * (1.0 - alpha);+ float highLightRatio = alpha / blendedAlpha;+ vec3 blendedRGB = mix(transparentColor.rgb, color, highLightRatio);+ return vec4(blendedRGB, blendedAlpha);+}++vec2 getUV(vec2 pos) {+ return vec2(+ (pos.x - bounds[0]) / (bounds[2] - bounds[0]),+ (pos.y - bounds[3]) / (bounds[1] - bounds[3])+ );+}++void main(void) {+ vec2 uv = vTexCoord;+ if (coordinateConversion < -0.5) {+ vec2 lnglat = mercator_to_lnglat(vTexPos);+ uv = getUV(lnglat);+ } else if (coordinateConversion > 0.5) {+ vec2 commonPos = lnglat_to_mercator(vTexPos);+ uv = getUV(commonPos);+ }++ vec4 image;+ DECKGL_CREATE_COLOR(image, vTexCoord);++ DECKGL_MUTATE_COLOR(image, vTexCoord);++ gl_FragColor = apply_opacity(color_tint(color_desaturate(image.rgb)), opacity);++ geometry.uv = uv;+ DECKGL_FILTER_COLOR(gl_FragColor, geometry);+}+`;diff --git a/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl1.vs.ts b/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl1.vs.tsnew file mode 100644index 000000000..179266d40--- /dev/null+++ b/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl1.vs.ts@@ -0,0 +1,37 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++export default `\+#define SHADER_NAME raster-layer-vertex-shader++attribute vec2 texCoords;+attribute vec3 positions;+attribute vec3 positions64Low;++varying vec2 vTexCoord;+varying vec2 vTexPos;++uniform float coordinateConversion;++const vec3 pickingColor = vec3(1.0, 0.0, 0.0);++void main(void) {+ geometry.worldPosition = positions;+ geometry.uv = texCoords;+ geometry.pickingColor = pickingColor;++ gl_Position = project_position_to_clipspace(positions, positions64Low, vec3(0.0), geometry.position);+ DECKGL_FILTER_GL_POSITION(gl_Position, geometry);++ vTexCoord = texCoords;++ if (coordinateConversion < -0.5) {+ vTexPos = geometry.position.xy;+ } else if (coordinateConversion > 0.5) {+ vTexPos = geometry.worldPosition.xy;+ }++ vec4 color = vec4(0.0);+ DECKGL_FILTER_COLOR(color, geometry);+}+`;diff --git a/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl2.fs.ts b/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl2.fs.tsnew file mode 100644index 000000000..69ca332c2--- /dev/null+++ b/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl2.fs.ts@@ -0,0 +1,100 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++export default `\+#version 300 es+#define SHADER_NAME raster-layer-fragment-shader++// Ref https://www.khronos.org/registry/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf#page=60+precision mediump float;+precision mediump int;+precision mediump usampler2D;++in vec2 vTexCoord;+in vec2 vTexPos;++out vec4 color;++uniform float desaturate;+uniform vec4 transparentColor;+uniform vec3 tintColor;+uniform float opacity;++uniform float coordinateConversion;+uniform vec4 bounds;++/* projection utils */+const float TILE_SIZE = 512.0;+const float PI = 3.1415926536;+const float WORLD_SCALE = TILE_SIZE / PI / 2.0;++// from degrees to Web Mercator+vec2 lnglat_to_mercator(vec2 lnglat) {+ float x = lnglat.x;+ float y = clamp(lnglat.y, -89.9, 89.9);+ return vec2(+ radians(x) + PI,+ PI + log(tan(PI * 0.25 + radians(y) * 0.5))+ ) * WORLD_SCALE;+}++// from Web Mercator to degrees+vec2 mercator_to_lnglat(vec2 xy) {+ xy /= WORLD_SCALE;+ return degrees(vec2(+ xy.x - PI,+ atan(exp(xy.y - PI)) * 2.0 - PI * 0.5+ ));+}+/* End projection utils */++// apply desaturation+vec3 color_desaturate(vec3 color) {+ float luminance = (color.r + color.g + color.b) * 0.333333333;+ return mix(color, vec3(luminance), desaturate);+}++// apply tint+vec3 color_tint(vec3 color) {+ return color * tintColor;+}++// blend with background color+vec4 apply_opacity(vec3 color, float alpha) {+ if (transparentColor.a == 0.0) {+ return vec4(color, alpha);+ }+ float blendedAlpha = alpha + transparentColor.a * (1.0 - alpha);+ float highLightRatio = alpha / blendedAlpha;+ vec3 blendedRGB = mix(transparentColor.rgb, color, highLightRatio);+ return vec4(blendedRGB, blendedAlpha);+}++vec2 getUV(vec2 pos) {+ return vec2(+ (pos.x - bounds[0]) / (bounds[2] - bounds[0]),+ (pos.y - bounds[3]) / (bounds[1] - bounds[3])+ );+}++void main(void) {+ vec2 uv = vTexCoord;+ if (coordinateConversion < -0.5) {+ vec2 lnglat = mercator_to_lnglat(vTexPos);+ uv = getUV(lnglat);+ } else if (coordinateConversion > 0.5) {+ vec2 commonPos = lnglat_to_mercator(vTexPos);+ uv = getUV(commonPos);+ }++ vec4 image;+ DECKGL_CREATE_COLOR(image, vTexCoord);++ DECKGL_MUTATE_COLOR(image, vTexCoord);++ color = apply_opacity(color_tint(color_desaturate(image.rgb)), opacity);++ geometry.uv = uv;+ DECKGL_FILTER_COLOR(color, geometry);+}+`;diff --git a/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl2.vs.ts b/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl2.vs.tsnew file mode 100644index 000000000..6bb698a84--- /dev/null+++ b/src/deckgl-layers/src/raster/raster-layer/raster-layer-webgl2.vs.ts@@ -0,0 +1,40 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++export default `\+#version 300 es+#define SHADER_NAME raster-layer-vertex-shader++precision mediump float;++in vec2 texCoords;+in vec3 positions;+in vec3 positions64Low;++out vec2 vTexCoord;+out vec2 vTexPos;++uniform float coordinateConversion;++const vec3 pickingColor = vec3(1.0, 0.0, 0.0);++void main(void) {+ geometry.worldPosition = positions;+ geometry.uv = texCoords;+ geometry.pickingColor = pickingColor;++ gl_Position = project_position_to_clipspace(positions, positions64Low, vec3(0.0), geometry.position);+ DECKGL_FILTER_GL_POSITION(gl_Position, geometry);++ vTexCoord = texCoords;++ if (coordinateConversion < -0.5) {+ vTexPos = geometry.position.xy;+ } else if (coordinateConversion > 0.5) {+ vTexPos = geometry.worldPosition.xy;+ }++ vec4 color = vec4(0.0);+ DECKGL_FILTER_COLOR(color, geometry);+}+`;diff --git a/src/deckgl-layers/src/raster/raster-layer/raster-layer.ts b/src/deckgl-layers/src/raster/raster-layer/raster-layer.tsnew file mode 100644index 000000000..0706aa75c--- /dev/null+++ b/src/deckgl-layers/src/raster/raster-layer/raster-layer.ts@@ -0,0 +1,182 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {project32, UpdateParameters} from '@deck.gl/core/typed';+import {BitmapLayer} from '@deck.gl/layers/typed';+import {isWebGL2} from '@luma.gl/core';+import {ProgramManager} from '@luma.gl/engine';++import fsWebGL1 from './raster-layer-webgl1.fs';+import vsWebGL1 from './raster-layer-webgl1.vs';+import fsWebGL2 from './raster-layer-webgl2.fs';+import vsWebGL2 from './raster-layer-webgl2.vs';+import {loadImages} from '../images';+import type {RasterLayerAddedProps, ImageState} from '../types';+import {modulesEqual} from '../util';++const defaultProps = {+ ...BitmapLayer.defaultProps,+ modules: {type: 'array', value: [], compare: true},+ images: {type: 'object', value: {}, compare: true},+ moduleProps: {type: 'object', value: {}, compare: true}+};++export default class RasterLayer extends BitmapLayer<RasterLayerAddedProps> {+ declare state: BitmapLayer<RasterLayerAddedProps>['state'] & {+ images: ImageState;+ };++ initializeState(): void {+ const {gl} = this.context;+ const programManager = ProgramManager.getDefaultProgramManager(gl);++ const fsStr1 = 'fs:DECKGL_MUTATE_COLOR(inout vec4 image, in vec2 coord)';+ const fsStr2 = 'fs:DECKGL_CREATE_COLOR(inout vec4 image, in vec2 coord)';++ // Only initialize shader hook functions _once globally_+ // Since the program manager is shared across all layers, but many layers+ // might be created, this solves the performance issue of always adding new+ // hook functions.+ if (!programManager._hookFunctions.includes(fsStr1)) {+ programManager.addShaderHook(fsStr1);+ }+ if (!programManager._hookFunctions.includes(fsStr2)) {+ programManager.addShaderHook(fsStr2);+ }++ // images is a mapping from keys to Texture2D objects. The keys should match+ // names of uniforms in shader modules+ this.setState({images: {}});++ super.initializeState();+ }++ draw({uniforms}: {uniforms: {[key: string]: any}}): void {+ const {model, images, coordinateConversion, bounds} = this.state;+ const {desaturate, transparentColor, tintColor, moduleProps} = this.props;++ // Render the image+ if (+ !model ||+ !images ||+ Object.keys(images).length === 0 ||+ !Object.values(images).every(item => item)+ ) {+ return;+ }++ model+ .setUniforms({+ ...uniforms,+ desaturate,+ transparentColor: transparentColor?.map(x => (x ? x / 255 : 0)),+ tintColor: tintColor?.slice(0, 3).map(x => x / 255),+ coordinateConversion,+ bounds+ })+ .updateModuleSettings({+ ...moduleProps,+ ...images+ })+ .draw();+ }++ getShaders(): any {+ const {gl} = this.context;+ const {modules = []} = this.props;+ const webgl2 = isWebGL2(gl);++ // Choose webgl version for module+ // If fs2 or fs1 keys exist, prefer them, but fall back to fs, so that+ // version-independent modules don't need to care+ for (const module of modules) {+ module.fs = webgl2 ? module.fs2 || module.fs : module.fs1 || module.fs;++ // Sampler type is always float for WebGL1+ if (!webgl2 && module.defines) {+ module.defines.SAMPLER_TYPE = 'sampler2D';+ }+ }++ return {+ ...super.getShaders(),+ vs: webgl2 ? vsWebGL2 : vsWebGL1,+ fs: webgl2 ? fsWebGL2 : fsWebGL1,+ modules: [project32, ...modules]+ };+ }++ // eslint-disable-next-line complexity+ updateState(params: UpdateParameters<BitmapLayer<RasterLayerAddedProps>>): void {+ const {props, oldProps, changeFlags} = params;+ const modules = props && props.modules;+ const oldModules = oldProps && oldProps.modules;++ // setup model first+ // If the list of modules changed, need to recompile the shaders+ if (changeFlags.extensionsChanged || !modulesEqual(modules, oldModules)) {+ const {gl} = this.context;+ this.state.model?.delete();+ this.state.model = this._getModel(gl);+ this.getAttributeManager()?.invalidateAll();+ }++ if (props && props.images) {+ this.updateImages({props, oldProps});+ }++ const attributeManager = this.getAttributeManager();++ if (props.bounds !== oldProps.bounds) {+ const oldMesh = this.state.mesh;+ const mesh = this._createMesh();+ this.state.model?.setVertexCount(mesh.vertexCount);+ for (const key in mesh) {+ if (oldMesh && oldMesh[key] !== mesh[key]) {+ attributeManager?.invalidate(key);+ }+ }+ this.setState({mesh, ...this._getCoordinateUniforms()});+ } else if (props._imageCoordinateSystem !== oldProps._imageCoordinateSystem) {+ this.setState(this._getCoordinateUniforms());+ }+ }++ updateImages({+ props,+ oldProps+ }: {+ props: RasterLayerAddedProps;+ oldProps: RasterLayerAddedProps;+ }): void {+ const {images} = this.state;+ const {gl} = this.context;++ const newImages = loadImages({+ gl,+ images,+ imagesData: props.images,+ oldImagesData: oldProps.images+ });+ if (newImages) {+ this.setState({images: newImages});+ }+ }++ finalizeState(): void {+ super.finalizeState(this.context);++ if (this.state.images) {+ for (const image of Object.values(this.state.images)) {+ if (Array.isArray(image)) {+ image.map(x => x && x.delete());+ } else if (image) {+ image.delete();+ }+ }+ }+ }+}++RasterLayer.defaultProps = defaultProps;+RasterLayer.layerName = 'RasterLayer';diff --git a/src/deckgl-layers/src/raster/raster-mesh-layer/matrix.ts b/src/deckgl-layers/src/raster/raster-mesh-layer/matrix.tsnew file mode 100644index 000000000..a517b24fd--- /dev/null+++ b/src/deckgl-layers/src/raster/raster-mesh-layer/matrix.ts@@ -0,0 +1,19 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {COORDINATE_SYSTEM, Viewport, CoordinateSystem} from '@deck.gl/core/typed';++// only apply composeModelMatrix when in cartesian or meter_offsets coordinate system+// with `composeModelMatrix` enabled, the rotation part of the layer's modelMatrix will be composed to instance's transformations+// since rotating latitude and longitude can not provide meaningful results, hence `composeModelMatrix` is disabled+// when in LNGLAT and LNGLAT_OFFSET coordinates.+export function shouldComposeModelMatrix(+ viewport: Viewport,+ coordinateSystem: CoordinateSystem+): boolean {+ return (+ coordinateSystem === COORDINATE_SYSTEM.CARTESIAN ||+ coordinateSystem === COORDINATE_SYSTEM.METER_OFFSETS ||+ (coordinateSystem === COORDINATE_SYSTEM.DEFAULT && !viewport.isGeospatial)+ );+}diff --git a/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl1.fs.ts b/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl1.fs.tsnew file mode 100644index 000000000..20a1cd172--- /dev/null+++ b/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl1.fs.ts@@ -0,0 +1,48 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++export default `\+#define SHADER_NAME raster-mesh-layer-fs++precision highp float;++uniform bool hasTexture;++uniform bool flatShading;+uniform float opacity;++varying vec2 vTexCoord;+varying vec3 cameraPosition;+varying vec3 normals_commonspace;+varying vec4 position_commonspace;+varying vec4 vColor;++void main(void) {+ geometry.uv = vTexCoord;+ vec4 image;+ DECKGL_CREATE_COLOR(image, vTexCoord);++ DECKGL_MUTATE_COLOR(image, vTexCoord);++ vec3 normal;+ if (flatShading) {++// This is necessary because+// headless.gl reports the extension as+// available but does not support it in+// the shader.+#ifdef DERIVATIVES_AVAILABLE+ normal = normalize(cross(dFdx(position_commonspace.xyz), dFdy(position_commonspace.xyz)));+#else+ normal = vec3(0.0, 0.0, 1.0);+#endif+ } else {+ normal = normals_commonspace;+ }++ vec3 lightColor = lighting_getLightColor(image.rgb, cameraPosition, position_commonspace.xyz, normal);+ gl_FragColor = vec4(lightColor, opacity);++ DECKGL_FILTER_COLOR(gl_FragColor, geometry);+}+`;diff --git a/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl1.vs.ts b/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl1.vs.tsnew file mode 100644index 000000000..b5e13423e--- /dev/null+++ b/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl1.vs.ts@@ -0,0 +1,60 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++export default `\+#define SHADER_NAME raster-mesh-layer-vs++// Scale the model+uniform float sizeScale;+uniform bool composeModelMatrix;++// Primitive attributes+attribute vec3 positions;+attribute vec3 normals;+attribute vec3 colors;+attribute vec2 texCoords;++// Instance attributes+attribute vec3 instancePositions;+attribute vec3 instancePositions64Low;+attribute vec4 instanceColors;+attribute vec3 instancePickingColors;+attribute mat3 instanceModelMatrix;+attribute vec3 instanceTranslation;++// Outputs to fragment shader+varying vec2 vTexCoord;+varying vec3 cameraPosition;+varying vec3 normals_commonspace;+varying vec4 position_commonspace;+varying vec4 vColor;++void main(void) {+ geometry.worldPosition = instancePositions;+ geometry.uv = texCoords;+ geometry.pickingColor = instancePickingColors;++ vTexCoord = texCoords;+ cameraPosition = project_uCameraPosition;+ normals_commonspace = project_normal(instanceModelMatrix * normals);+ vColor = vec4(colors * instanceColors.rgb, instanceColors.a);+ geometry.normal = normals_commonspace;++ vec3 pos = (instanceModelMatrix * positions) * sizeScale + instanceTranslation;++ if (composeModelMatrix) {+ DECKGL_FILTER_SIZE(pos, geometry);+ gl_Position = project_position_to_clipspace(pos + instancePositions, instancePositions64Low, vec3(0.0), position_commonspace);+ }+ else {+ pos = project_size(pos);+ DECKGL_FILTER_SIZE(pos, geometry);+ gl_Position = project_position_to_clipspace(instancePositions, instancePositions64Low, pos, position_commonspace);+ }++ geometry.position = position_commonspace;+ DECKGL_FILTER_GL_POSITION(gl_Position, geometry);++ DECKGL_FILTER_COLOR(vColor, geometry);+}+`;diff --git a/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl2.fs.ts b/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl2.fs.tsnew file mode 100644index 000000000..841de2f08--- /dev/null+++ b/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl2.fs.ts@@ -0,0 +1,51 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++export default `\+#version 300 es+#define SHADER_NAME raster-mesh-layer-fs++precision highp float;++uniform bool hasTexture;++uniform bool flatShading;+uniform float opacity;++in vec2 vTexCoord;+in vec3 cameraPosition;+in vec3 normals_commonspace;+in vec4 position_commonspace;+in vec4 vColor;++out vec4 fragColor;++void main(void) {+ geometry.uv = vTexCoord;+ vec4 image;+ DECKGL_CREATE_COLOR(image, vTexCoord);++ DECKGL_MUTATE_COLOR(image, vTexCoord);++ vec3 normal;+ if (flatShading) {++// This is necessary because+// headless.gl reports the extension as+// available but does not support it in+// the shader.+#ifdef DERIVATIVES_AVAILABLE+ normal = normalize(cross(dFdx(position_commonspace.xyz), dFdy(position_commonspace.xyz)));+#else+ normal = vec3(0.0, 0.0, 1.0);+#endif+ } else {+ normal = normals_commonspace;+ }++ vec3 lightColor = lighting_getLightColor(image.rgb, cameraPosition, position_commonspace.xyz, normal);+ fragColor = vec4(lightColor, opacity);++ DECKGL_FILTER_COLOR(fragColor, geometry);+}+`;diff --git a/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl2.vs.ts b/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl2.vs.tsnew file mode 100644index 000000000..b3a05acf1--- /dev/null+++ b/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer-webgl2.vs.ts@@ -0,0 +1,61 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++export default `\+#version 300 es+#define SHADER_NAME raster-mesh-layer-vs++// Scale the model+uniform float sizeScale;+uniform bool composeModelMatrix;++// Primitive attributes+in vec3 positions;+in vec3 normals;+in vec3 colors;+in vec2 texCoords;++// Instance attributes+in vec3 instancePositions;+in vec3 instancePositions64Low;+in vec4 instanceColors;+in vec3 instancePickingColors;+in mat3 instanceModelMatrix;+in vec3 instanceTranslation;++// Outputs to fragment shader+out vec2 vTexCoord;+out vec3 cameraPosition;+out vec3 normals_commonspace;+out vec4 position_commonspace;+out vec4 vColor;++void main(void) {+ geometry.worldPosition = instancePositions;+ geometry.uv = texCoords;+ geometry.pickingColor = instancePickingColors;++ vTexCoord = texCoords;+ cameraPosition = project_uCameraPosition;+ normals_commonspace = project_normal(instanceModelMatrix * normals);+ vColor = vec4(colors * instanceColors.rgb, instanceColors.a);+ geometry.normal = normals_commonspace;++ vec3 pos = (instanceModelMatrix * positions) * sizeScale + instanceTranslation;++ if (composeModelMatrix) {+ DECKGL_FILTER_SIZE(pos, geometry);+ gl_Position = project_position_to_clipspace(pos + instancePositions, instancePositions64Low, vec3(0.0), position_commonspace);+ }+ else {+ pos = project_size(pos);+ DECKGL_FILTER_SIZE(pos, geometry);+ gl_Position = project_position_to_clipspace(instancePositions, instancePositions64Low, pos, position_commonspace);+ }++ geometry.position = position_commonspace;+ DECKGL_FILTER_GL_POSITION(gl_Position, geometry);++ DECKGL_FILTER_COLOR(vColor, geometry);+}+`;diff --git a/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer.ts b/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer.tsnew file mode 100644index 000000000..f65d0fe01--- /dev/null+++ b/src/deckgl-layers/src/raster/raster-mesh-layer/raster-mesh-layer.ts@@ -0,0 +1,233 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {project32, phongLighting, log, UpdateParameters} from '@deck.gl/core/typed';+import {SimpleMeshLayer, SimpleMeshLayerProps} from '@deck.gl/mesh-layers/typed';+import GL from '@luma.gl/constants';+import {Model, Geometry, isWebGL2} from '@luma.gl/core';+import {ProgramManager} from '@luma.gl/engine';+import {UniformsOptions} from '@luma.gl/webgl/src/classes/uniforms';++import {shouldComposeModelMatrix} from './matrix';+import fsWebGL1 from './raster-mesh-layer-webgl1.fs';+import vsWebGL1 from './raster-mesh-layer-webgl1.vs';+import fsWebGL2 from './raster-mesh-layer-webgl2.fs';+import vsWebGL2 from './raster-mesh-layer-webgl2.vs';+import {loadImages} from '../images';+import type {RasterLayerAddedProps, ImageState} from '../types';+import {modulesEqual} from '../util';++type Mesh = SimpleMeshLayerProps['mesh'];++function validateGeometryAttributes(attributes) {+ log.assert(+ attributes.positions || attributes.POSITION,+ 'RasterMeshLayer requires "postions" or "POSITION" attribute in mesh property.'+ );+}++/*+ * Convert mesh data into geometry+ * @returns geometry+ */+function getGeometry(data): Geometry {+ if (data.attributes) {+ validateGeometryAttributes(data.attributes);+ if (data instanceof Geometry) {+ return data;+ }+ return new Geometry(data);+ } else if (data.positions || data.POSITION) {+ validateGeometryAttributes(data);+ return new Geometry({+ attributes: data+ });+ }+ throw Error('Invalid mesh');+}++const defaultProps = {+ ...SimpleMeshLayer.defaultProps,+ modules: {type: 'array', value: [], compare: true},+ images: {type: 'object', value: {}, compare: true},+ moduleProps: {type: 'object', value: {}, compare: true}+};++export default class RasterMeshLayer extends SimpleMeshLayer<any, RasterLayerAddedProps> {+ declare state: SimpleMeshLayer<RasterLayerAddedProps>['state'] & {+ images: ImageState;+ };++ initializeState(): void {+ const {gl} = this.context;+ const programManager = ProgramManager.getDefaultProgramManager(gl);++ const fsStr1 = 'fs:DECKGL_MUTATE_COLOR(inout vec4 image, in vec2 coord)';+ const fsStr2 = 'fs:DECKGL_CREATE_COLOR(inout vec4 image, in vec2 coord)';++ // Only initialize shader hook functions _once globally_+ // Since the program manager is shared across all layers, but many layers+ // might be created, this solves the performance issue of always adding new+ // hook functions.+ if (!programManager._hookFunctions.includes(fsStr1)) {+ programManager.addShaderHook(fsStr1);+ }+ if (!programManager._hookFunctions.includes(fsStr2)) {+ programManager.addShaderHook(fsStr2);+ }++ // images is a mapping from keys to Texture2D objects. The keys should match+ // names of uniforms in shader modules+ this.setState({images: {}});++ super.initializeState();+ }++ getShaders(): any {+ const {gl} = this.context;+ const {modules = []} = this.props;+ const webgl2 = isWebGL2(gl);++ // Choose webgl version for module+ // If fs2 or fs1 keys exist, prefer them, but fall back to fs, so that+ // version-independent modules don't need to care+ for (const module of modules) {+ module.fs = webgl2 ? module.fs2 || module.fs : module.fs1 || module.fs;++ // Sampler type is always float for WebGL1+ if (!webgl2 && module.defines) {+ module.defines.SAMPLER_TYPE = 'sampler2D';+ }+ }++ return {+ ...super.getShaders(),+ vs: webgl2 ? vsWebGL2 : vsWebGL1,+ fs: webgl2 ? fsWebGL2 : fsWebGL1,+ modules: [project32, phongLighting, ...modules]+ };+ }++ // eslint-disable-next-line complexity+ updateState(params: UpdateParameters<SimpleMeshLayer<any, RasterLayerAddedProps>>): void {+ const {props, oldProps, changeFlags, context} = params;+ super.updateState({props, oldProps, changeFlags, context});++ const modules = props && props.modules;+ const oldModules = oldProps && oldProps.modules;++ // If the list of modules changed, need to recompile the shaders+ if (+ props.mesh !== oldProps.mesh ||+ changeFlags.extensionsChanged ||+ !modulesEqual(modules, oldModules)+ ) {+ if (this.state.model) {+ this.state.model.delete();+ }+ if (props.mesh) {+ this.state.model = this.getModel(props.mesh as Mesh);++ const attributes = (props.mesh as any).attributes || props.mesh;+ this.setState({+ hasNormals: Boolean(attributes.NORMAL || attributes.normals)+ });+ }+ this.getAttributeManager()?.invalidateAll();+ }++ if (props && props.images) {+ this.updateImages({props, oldProps});+ }++ if (this.state.model) {+ this.state.model.setDrawMode(this.props.wireframe ? GL.LINE_STRIP : GL.TRIANGLES);+ }+ }++ updateImages({+ props,+ oldProps+ }: {+ props: RasterLayerAddedProps;+ oldProps: RasterLayerAddedProps;+ }): void {+ const {images} = this.state;+ const {gl} = this.context;++ const newImages = loadImages({+ gl,+ images,+ imagesData: props.images,+ oldImagesData: oldProps.images+ });++ if (newImages) {+ this.setState({images: newImages});+ }+ }++ draw({uniforms}: UniformsOptions): void {+ const {model, images} = this.state;+ const {moduleProps} = this.props;++ // Render the image+ if (+ !model ||+ !images ||+ Object.keys(images).length === 0 ||+ !Object.values(images).every(item => item)+ ) {+ return;+ }++ const {viewport} = this.context;+ const {sizeScale, coordinateSystem, _instanced} = this.props;++ model+ .setUniforms(+ Object.assign({}, uniforms, {+ sizeScale,+ composeModelMatrix: !_instanced || shouldComposeModelMatrix(viewport, coordinateSystem),+ flatShading: !this.state.hasNormals+ })+ )+ .updateModuleSettings({+ ...moduleProps,+ ...images+ })+ .draw();+ }++ finalizeState(): void {+ super.finalizeState(this.context);++ if (this.state.images) {+ for (const image of Object.values(this.state.images)) {+ if (Array.isArray(image)) {+ image.map(x => x && x.delete());+ } else if (image) {+ image.delete();+ }+ }+ }+ }++ protected getModel(mesh: Mesh): Model {+ const {gl} = this.context;++ const model = new Model(+ gl,+ Object.assign({}, this.getShaders(), {+ id: this.props.id,+ geometry: getGeometry(mesh),+ isInstanced: true+ })+ );++ return model;+ }+}++RasterMeshLayer.layerName = 'RasterMeshLayer';+RasterMeshLayer.defaultProps = defaultProps;diff --git a/src/deckgl-layers/src/raster/types.ts b/src/deckgl-layers/src/raster/types.tsnew file mode 100644index 000000000..06b94cd78--- /dev/null+++ b/src/deckgl-layers/src/raster/types.ts@@ -0,0 +1,26 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import type {Texture2DProps, Texture2D} from '@luma.gl/webgl';++import type {ShaderModule} from './webgl';++/** Allowed input for images prop+ * Texture2D is already on the GPU, while Texture2DProps can be data on the CPU that is not yet copied to the GPU.+ */+export type ImageInput = Record<+ string,+ Texture2DProps | Texture2D | (Texture2DProps | Texture2D)[]+>;++/** Internal storage of images+ * The Texture2D object references data on the GPU+ */+export type ImageState = Record<string, Texture2D | Texture2D[]>;++/** Properties added by RasterLayer. */+export type RasterLayerAddedProps = {+ modules: ShaderModule[];+ images: ImageInput;+ moduleProps: Record<string, number>;+};diff --git a/src/deckgl-layers/src/raster/util.ts b/src/deckgl-layers/src/raster/util.tsnew file mode 100644index 000000000..9eac44aaa--- /dev/null+++ b/src/deckgl-layers/src/raster/util.ts@@ -0,0 +1,26 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {ShaderModule} from './webgl/types';++/**+ * Test if two lists of modules are equal+ *+ * @param modules Modules list+ * @param oldModules Modules list+ *+ * @return true if both lists are equal+ */+export function modulesEqual(modules: ShaderModule[], oldModules: ShaderModule[]): boolean {+ if (modules.length !== oldModules.length) {+ return false;+ }++ for (let i = 0; i < modules.length; i++) {+ if (modules[i].name !== oldModules[i].name) {+ return false;+ }+ }++ return true;+}diff --git a/src/deckgl-layers/src/raster/webgl/color/colormap.ts b/src/deckgl-layers/src/raster/webgl/color/colormap.tsnew file mode 100644index 000000000..5903f9e76--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/color/colormap.ts@@ -0,0 +1,78 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {Texture2D} from '@luma.gl/webgl';++import {GetUniformsOutput, ShaderModule} from '../types';++const fs = `\+uniform sampler2D uColormapTexture;+uniform int uHasCategoricalColors;+uniform int uCategoricalMinValue;+uniform int uCategoricalMaxValue;+uniform int uMaxPixelValue;++// Apply colormap texture given value+// Since the texture only varies in the x direction, setting v to 0.5 as a+// constant is fine+// Assumes the input range of value is -1 to 1+vec4 colormap(sampler2D cmap, vec4 image) {+ vec2 uv;+ if (uHasCategoricalColors == 1) {+ float step = float(uMaxPixelValue) / float(uCategoricalMaxValue - uCategoricalMinValue);+ uv = vec2(image.r * step, 0.5);+ } else {+ uv = vec2(0.5 * image.r + 0.5, 0.5);+ }+ vec4 color = texture2D(cmap, uv);+ if(color.a <= 0.0) discard;+ return color;+}+`;++function getUniforms(+ opts: {+ imageColormap?: Texture2D;+ minCategoricalBandValue?: number;+ maxCategoricalBandValue?: number;+ dataTypeMaxValue?: number;+ maxPixelValue?: number;+ } = {}+): GetUniformsOutput {+ const {+ imageColormap,+ minCategoricalBandValue,+ maxCategoricalBandValue,+ dataTypeMaxValue,+ maxPixelValue+ } = opts;++ if (!imageColormap) {+ return null;+ }++ const isSupportedDataType = Number.isFinite(dataTypeMaxValue);+ const isCategorical =+ isSupportedDataType &&+ Number.isFinite(maxPixelValue) &&+ Number.isFinite(minCategoricalBandValue) &&+ Number.isFinite(maxCategoricalBandValue);+ return {+ uColormapTexture: imageColormap,+ uHasCategoricalColors: isCategorical ? 1 : 0,+ uCategoricalMinValue: Number.isFinite(minCategoricalBandValue) ? minCategoricalBandValue : 0,+ uCategoricalMaxValue: Number.isFinite(maxCategoricalBandValue) ? maxCategoricalBandValue : 0,+ uMaxPixelValue: Number.isFinite(maxPixelValue) ? maxPixelValue : 0+ };+}++export const colormap: ShaderModule = {+ name: 'colormap',+ fs,+ getUniforms,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ image = colormap(uColormapTexture, image);+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/color/filter.ts b/src/deckgl-layers/src/raster/webgl/color/filter.tsnew file mode 100644index 000000000..210ca5760--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/color/filter.ts@@ -0,0 +1,85 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {GetUniformsOutput, ShaderModule} from '../types';++const fs = `\+uniform float filterMin1;+uniform float filterMax1;+uniform float filterMin2;+uniform float filterMax2;+uniform float filterMin3;+uniform float filterMax3;+uniform float filterMin4;+uniform float filterMax4;+`;++// You can't pass JS' -Infinity or Infinity to a shader as a uniform+const inf = Math.pow(2, 62);++// eslint-disable-next-line complexity+function getUniforms(+ opts: {+ filterMin1?: number;+ filterMin2?: number;+ filterMin3?: number;+ filterMin4?: number;+ filterMax1?: number;+ filterMax2?: number;+ filterMax3?: number;+ filterMax4?: number;+ } = {}+): GetUniformsOutput {+ const {+ filterMin1,+ filterMin2,+ filterMin3,+ filterMin4,+ filterMax1,+ filterMax2,+ filterMax3,+ filterMax4+ } = opts;++ if (+ Number.isFinite(filterMin1) ||+ Number.isFinite(filterMin2) ||+ Number.isFinite(filterMin3) ||+ Number.isFinite(filterMin4) ||+ Number.isFinite(filterMax1) ||+ Number.isFinite(filterMax2) ||+ Number.isFinite(filterMax3) ||+ Number.isFinite(filterMax4)+ ) {+ return {+ filterMin1: Number.isFinite(filterMin1) ? filterMin1 : -inf,+ filterMin2: Number.isFinite(filterMin2) ? filterMin2 : -inf,+ filterMin3: Number.isFinite(filterMin3) ? filterMin3 : -inf,+ filterMin4: Number.isFinite(filterMin4) ? filterMin4 : -inf,+ filterMax1: Number.isFinite(filterMax1) ? filterMax1 : inf,+ filterMax2: Number.isFinite(filterMax2) ? filterMax2 : inf,+ filterMax3: Number.isFinite(filterMax3) ? filterMax3 : inf,+ filterMax4: Number.isFinite(filterMax4) ? filterMax4 : inf+ };+ }++ return null;+}++export const filter: ShaderModule = {+ name: 'filter',+ fs,+ getUniforms,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ if (image.r < filterMin1) discard;+ if (image.g < filterMin2) discard;+ if (image.b < filterMin3) discard;+ if (image.a < filterMin4) discard;+ if (image.r > filterMax1) discard;+ if (image.g > filterMax2) discard;+ if (image.b > filterMax3) discard;+ if (image.a > filterMax4) discard;+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/color/gamma-contrast.ts b/src/deckgl-layers/src/raster/webgl/color/gamma-contrast.tsnew file mode 100644index 000000000..6ef5cb8c6--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/color/gamma-contrast.ts@@ -0,0 +1,99 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {GetUniformsOutput, ShaderModule} from '../types';++// Gamma correction is a nonlinear operation that+// adjusts the image's channel values pixel-by-pixel according+// to a power-law:+//+// .. math:: pixel_{out} = pixel_{in} ^ {gamma}+//+// Setting gamma (:math:gamma) to be less than 1.0 darkens the image and+// setting gamma to be greater than 1.0 lightens it.++// Parameters+// ----------+// gamma (:math:gamma): float+// Reasonable values range from 0.8 to 2.4.++// NOTE: Input array must have float values between 0 and 1!+// NOTE: gamma must be >= 0+const fs = `\+#define epsilon 0.00000001++uniform float gamma1;+uniform float gamma2;+uniform float gamma3;+uniform float gamma4;++float gammaContrast(float arr, float g) {+ // Gamma must be > 0+ g = clamp(g, epsilon, g);++ return pow(arr, 1.0 / g);+}++vec4 gammaContrast(vec4 arr, float g1, float g2, float g3, float g4) {+ arr.r = gammaContrast(arr.r, g1);+ arr.g = gammaContrast(arr.g, g2);+ arr.b = gammaContrast(arr.b, g3);+ arr.a = gammaContrast(arr.a, g4);++ return arr;+}+`;++function getUniforms(+ opts: {+ gammaContrastValue?: number;+ gammaContrastValue1?: number;+ gammaContrastValue2?: number;+ gammaContrastValue3?: number;+ gammaContrastValue4?: number;+ } = {}+): GetUniformsOutput {+ const {+ gammaContrastValue,+ gammaContrastValue1,+ gammaContrastValue2,+ gammaContrastValue3,+ gammaContrastValue4+ } = opts;++ // Gamma must be > 0, so not using Number.isFinite is fine++ if (gammaContrastValue) {+ return {+ gamma1: gammaContrastValue,+ gamma2: gammaContrastValue,+ gamma3: gammaContrastValue,+ gamma4: gammaContrastValue+ };+ } else if (+ gammaContrastValue1 ||+ gammaContrastValue2 ||+ gammaContrastValue3 ||+ gammaContrastValue4+ ) {+ return {+ gamma1: gammaContrastValue1 || 1,+ gamma2: gammaContrastValue2 || 1,+ gamma3: gammaContrastValue3 || 1,+ gamma4: gammaContrastValue4 || 1+ };+ }++ return null;+}++export const gammaContrast: ShaderModule = {+ name: 'gamma_contrast',+ fs,+ getUniforms,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ image = gammaContrast(image, gamma1, gamma2, gamma3, gamma4);+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/color/linear-rescale.ts b/src/deckgl-layers/src/raster/webgl/color/linear-rescale.tsnew file mode 100644index 000000000..7e8d31358--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/color/linear-rescale.ts@@ -0,0 +1,40 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {GetUniformsOutput, ShaderModule} from '../types';++const fs = `\+uniform float linearRescaleScaler;+uniform float linearRescaleOffset;++// Perform a linear rescaling of image+vec4 linear_rescale(vec4 arr, float scaler, float offset) {+ return arr * scaler + offset;+}+`;++function getUniforms(+ opts: {linearRescaleScaler?: number; linearRescaleOffset?: number} = {}+): GetUniformsOutput {+ const {linearRescaleScaler, linearRescaleOffset} = opts;++ if (!Number.isFinite(linearRescaleScaler) && !Number.isFinite(linearRescaleOffset)) {+ return null;+ }++ return {+ linearRescaleScaler: Number.isFinite(linearRescaleScaler) ? linearRescaleScaler : 1,+ linearRescaleOffset: Number.isFinite(linearRescaleOffset) ? linearRescaleOffset : 0+ };+}++export const linearRescale: ShaderModule = {+ name: 'linear_rescale',+ fs,+ getUniforms,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ image = linear_rescale(image, linearRescaleScaler, linearRescaleOffset);+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/color/saturation.ts b/src/deckgl-layers/src/raster/webgl/color/saturation.tsnew file mode 100644index 000000000..916659851--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/color/saturation.ts@@ -0,0 +1,47 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {GetUniformsOutput, ShaderModule} from '../types';++/**+ * Adjusts the saturation of a color.+ * From cesium:+ * https://github.com/CesiumGS/cesium/blob/master/Source/Shaders/Builtin/Functions/saturation.glsl+ *+ * @param {vec3} rgb The color.+ * @param {float} adjustment The amount to adjust the saturation of the color. Usually between 0 and 2.+ *+ * @returns {vec3} The color with the saturation adjusted.+ */+const fs = `\+uniform float uSaturationValue;+vec3 saturate(vec3 rgb, float adjustment) {+ // Algorithm from Chapter 16 of OpenGL Shading Language+ const vec3 W = vec3(0.2125, 0.7154, 0.0721);+ vec3 intensity = vec3(dot(rgb, W));+ return mix(intensity, rgb, adjustment);+}+`;++function getUniforms(opts: {saturationValue?: number} = {}): GetUniformsOutput {+ const {saturationValue} = opts;++ if (!saturationValue) {+ return null;+ }++ return {+ uSaturationValue: Number.isFinite(saturationValue) ? saturationValue : 1+ };+}++export const saturation: ShaderModule = {+ name: 'saturation',+ fs,+ getUniforms,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ image = vec4(saturate(image.rgb, uSaturationValue), image.a);+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/color/sigmoidal-contrast.ts b/src/deckgl-layers/src/raster/webgl/color/sigmoidal-contrast.tsnew file mode 100644index 000000000..354152c2f--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/color/sigmoidal-contrast.ts@@ -0,0 +1,104 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {GetUniformsOutput, ShaderModule} from '../types';++// From mapbox/rio-color under the MIT License+//+// Sigmoidal contrast is a type of contrast control that+// adjusts the contrast without saturating highlights or shadows.+// It allows control over two factors:+// the contrast range from light to dark, and where the middle value+// of the mid-tones falls. The result is a non-linear and smooth+// contrast change.+// Parameters+// ----------+// arr : ndarray, float, 0 .. 1+// Array of color values to adjust+// contrast : integer+// Enhances the intensity differences between the lighter and darker+// elements of the image. For example, 0 is none, 3 is typical and+// 20 is a lot.+// bias : float, between 0 and 1+// Threshold level for the contrast function to center on+// (typically centered at 0.5)+// Notes+// ----------+// Sigmoidal contrast is based on the sigmoidal transfer function:+// .. math:: g(u) = ( 1/(1 + e^{- \alpha * u + \beta)})+// This sigmoid function is scaled so that the output is bound by+// the interval [0, 1].+// .. math:: ( 1/(1 + e^(\beta * (\alpha - u))) - 1/(1 + e^(\beta * \alpha)))/+// ( 1/(1 + e^(\beta*(\alpha - 1))) - 1/(1 + e^(\beta * \alpha)) )+// Where :math: `\alpha` is the threshold level, and :math: `\beta` the+// contrast factor to be applied.+// References+// ----------+// .. [CT] Hany Farid "Fundamentals of Image Processing"+// http://www.cs.dartmouth.edu/farid/downloads/tutorials/fip.pdf+const fs = `\+#define epsilon 0.00000001++uniform float sigmoidalContrast;+uniform float sigmoidalBias;++// NOTE: Input array must have float values between 0 and 1!+// NOTE: bias must be a scalar float between 0 and 1!+vec4 calculateSigmoidalContrast(vec4 arr, float contrast, float bias) {+ // We use the names alpha and beta to match documentation.+ float alpha = bias;+ float beta = contrast;++ // alpha must be >= 0+ alpha = clamp(alpha, epsilon, alpha);++ if (beta > 0.) {+ vec4 numerator = 1. / (1. + exp(beta * (alpha - arr))) - 1. / (+ 1. + exp(beta * alpha)+ );+ float denominator = 1. / (1. + exp(beta * (alpha - 1.))) - 1. / (+ 1. + exp(beta * alpha)+ );+ arr = numerator / denominator;+ } else if (beta < 0.) {+ arr = (+ (beta * alpha) - log(+ (+ 1.0 / (+ (arr / (1.0 + exp((beta * alpha) - beta))) -+ (arr / (1.0 + exp(beta * alpha))) ++ (1.0 / (1.0 + exp(beta * alpha)))+ )+ ) - 1.0)+ ) / beta;+ }++ return arr;+}+`;++function getUniforms(+ opts: {sigmoidalContrast?: number; sigmoidalBias?: number} = {}+): GetUniformsOutput {+ const {sigmoidalContrast, sigmoidalBias} = opts;++ if (!(Number.isFinite(sigmoidalContrast) || Number.isFinite(sigmoidalBias))) {+ return null;+ }++ return {+ sigmoidalContrast: Number.isFinite(sigmoidalContrast) ? sigmoidalContrast : 0,+ sigmoidalBias: Number.isFinite(sigmoidalBias) ? sigmoidalBias : 0.5+ };+}++export const sigmoidalContrast: ShaderModule = {+ name: 'sigmoidalContrast',+ fs,+ getUniforms,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ image = calculateSigmoidalContrast(image, sigmoidalContrast, sigmoidalBias);+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/index.ts b/src/deckgl-layers/src/raster/webgl/index.tsnew file mode 100644index 000000000..ceedf1fbe--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/index.ts@@ -0,0 +1,27 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++// Create texture+export {combineBandsFloat, combineBandsUint, combineBandsInt} from './texture/combine-bands';+export {rgbaImage} from './texture/rgba-image';+export {maskFloat, maskUint, maskInt} from './texture/mask';+export {reorderBands} from './texture/reorder-bands';++// Color operations+export {colormap} from './color/colormap';+export {linearRescale} from './color/linear-rescale';+export {sigmoidalContrast} from './color/sigmoidal-contrast';+export {gammaContrast} from './color/gamma-contrast';+export {saturation} from './color/saturation';+export {filter} from './color/filter';++// Pansharpening+export {pansharpenBrovey} from './pansharpen/pansharpen-brovey';++// Spectral indices+export {enhancedVegetationIndex} from './spectral-indices/evi';+export {modifiedSoilAdjustedVegetationIndex} from './spectral-indices/msavi';+export {normalizedDifference} from './spectral-indices/normalized-difference';+export {soilAdjustedVegetationIndex} from './spectral-indices/savi';++export type {ShaderModule} from './types';diff --git a/src/deckgl-layers/src/raster/webgl/pansharpen/pansharpen-brovey.ts b/src/deckgl-layers/src/raster/webgl/pansharpen/pansharpen-brovey.tsnew file mode 100644index 000000000..cdf916a36--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/pansharpen/pansharpen-brovey.ts@@ -0,0 +1,76 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {Texture2D} from '@luma.gl/webgl';++import {GetUniformsOutput, ShaderModule} from '../types';++// Brovey Method: Each resampled, multispectral pixel is+// multiplied by the ratio of the corresponding+// panchromatic pixel intensity to the sum of all the+// multispectral intensities.+// Original code from https://github.com/mapbox/rio-pansharpen+//+const fs1 = `\+uniform sampler2D bitmapTexturePan;+uniform float panWeight;++float pansharpen_brovey_ratio(vec4 rgb, float pan, float weight) {+ return pan / ((rgb.r + rgb.g + rgb.b * weight) / (2. + weight));+}++vec4 pansharpen_brovey_calc(vec4 rgb, float pan, float weight) {+ float ratio = pansharpen_brovey_ratio(rgb, pan, weight);+ return ratio * rgb;+}+`;++const fs2 = `\+precision mediump usampler2D;++#ifdef SAMPLER_TYPE+ uniform SAMPLER_TYPE bitmapTexturePan;+#else+ uniform sampler2D bitmapTexturePan;+#endif++uniform float panWeight;++float pansharpen_brovey_ratio(vec4 rgb, float pan, float weight) {+ return pan / ((rgb.r + rgb.g + rgb.b * weight) / (2. + weight));+}++vec4 pansharpen_brovey_calc(vec4 rgb, float pan, float weight) {+ float ratio = pansharpen_brovey_ratio(rgb, pan, weight);+ return ratio * rgb;+}+`;++function getUniforms(opts: {imagePan?: Texture2D; panWeight?: number} = {}): GetUniformsOutput {+ const {imagePan, panWeight = 0.2} = opts;++ if (!imagePan) {+ return null;+ }++ return {+ bitmapTexturePan: imagePan,+ panWeight+ };+}++export const pansharpenBrovey: ShaderModule = {+ name: 'pansharpen_brovey',+ fs1,+ fs2,+ defines: {+ SAMPLER_TYPE: 'sampler2D'+ },+ getUniforms,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ float pan_band = float(texture2D(bitmapTexturePan, coord).r);+ image = pansharpen_brovey_calc(image, pan_band, panWeight);+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/spectral-indices/evi.ts b/src/deckgl-layers/src/raster/webgl/spectral-indices/evi.tsnew file mode 100644index 000000000..85804de2a--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/spectral-indices/evi.ts@@ -0,0 +1,34 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {ShaderModule} from '../types';++// Calculate enhanced vegetation index+// Expected to be ordered:+// red: Landsat 8 band 5+// green: Landsat 8 band 4+// blue: Landsat 8 band 2+//+// EVI = 2.5 * ((Band 5 – Band 4) / (Band 5 + 6 * Band 4 – 7.5 * Band 2 + 1))+// https://www.usgs.gov/land-resources/nli/landsat/landsat-enhanced-vegetation-index+const fs = `\+float enhanced_vegetation_index_calc(vec4 image) {+ float band5 = image.r;+ float band4 = image.g;+ float band2 = image.b;++ float numerator = band5 - band4;+ float denominator = band5 + (6. * band4) - (7.5 * band2) + 1.;+ return 2.5 * (numerator / denominator);+}+`;++export const enhancedVegetationIndex: ShaderModule = {+ name: 'enhanced_vegetation_index',+ fs,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ image = vec4(enhanced_vegetation_index_calc(image), 0., 0., 0.);+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/spectral-indices/msavi.ts b/src/deckgl-layers/src/raster/webgl/spectral-indices/msavi.tsnew file mode 100644index 000000000..b0a77bf93--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/spectral-indices/msavi.ts@@ -0,0 +1,31 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {ShaderModule} from '../types';++// Calculate modified soil-adjusted vegetation index+// Expected to be ordered:+// red: Landsat 8 band 5+// green: Landsat 8 band 4+//+// MSAVI = (2 * Band 5 + 1 – sqrt ((2 * Band 5 + 1)^2 – 8 * (Band 5 – Band 4))) / 2+// https://www.usgs.gov/land-resources/nli/landsat/landsat-modified-soil-adjusted-vegetation-index+const fs = `\+float modified_soil_adjusted_vegetation_index_calc(vec4 image) {+ float band5 = image.r;+ float band4 = image.g;++ float to_sqrt = ((2. * band5 + 1.) * (2. * band5 + 1.)) - (8. * (band5 - band4));+ return ((2. * band5) + 1. - sqrt(to_sqrt)) / 2.;+}+`;++export const modifiedSoilAdjustedVegetationIndex: ShaderModule = {+ name: 'modified_soil_adjusted_vegetation_index',+ fs,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ image = vec4(modified_soil_adjusted_vegetation_index_calc(image), 0., 0., 0.);+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/spectral-indices/normalized-difference.ts b/src/deckgl-layers/src/raster/webgl/spectral-indices/normalized-difference.tsnew file mode 100644index 000000000..891fa51c9--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/spectral-indices/normalized-difference.ts@@ -0,0 +1,21 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {ShaderModule} from '../types';++// Calculate standard normalized difference+const fs = `\+float normalized_difference_calc(vec4 image) {+ return ((image.r - image.g) / (image.r + image.g));+}+`;++export const normalizedDifference: ShaderModule = {+ name: 'normalized_difference',+ fs,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ image = vec4(normalized_difference_calc(image), 0., 0., 0.);+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/spectral-indices/savi.ts b/src/deckgl-layers/src/raster/webgl/spectral-indices/savi.tsnew file mode 100644index 000000000..e3342ac0f--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/spectral-indices/savi.ts@@ -0,0 +1,28 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {ShaderModule} from '../types';++// Calculate soil-adjusted vegetation index+// SAVI = ((Band 5 – Band 4) / (Band 5 + Band 4 + 0.5)) * (1.5).+// https://www.usgs.gov/land-resources/nli/landsat/landsat-soil-adjusted-vegetation-index+const fs = `\+float soil_adjusted_vegetation_index_calc(vec4 image) {+ float band5 = image.r;+ float band4 = image.g;++ float numerator = band5 - band4;+ float denominator = (band5 + band4 + 0.5) * 1.5;+ return numerator / denominator;+}+`;++export const soilAdjustedVegetationIndex: ShaderModule = {+ name: 'soil_adjusted_vegetation_index',+ fs,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ image = vec4(soil_adjusted_vegetation_index_calc(image), 0., 0., 0.);+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/texture/combine-bands.ts b/src/deckgl-layers/src/raster/webgl/texture/combine-bands.tsnew file mode 100644index 000000000..ff58e12fa--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/texture/combine-bands.ts@@ -0,0 +1,89 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {Texture2D} from '@luma.gl/webgl';++import {GetUniformsOutput, ShaderModule} from '../types';++function getUniforms(opts: {imageBands?: Texture2D[]} = {}): GetUniformsOutput {+ const {imageBands} = opts;++ if (!imageBands || imageBands.length === 0) {+ return null;+ }++ const [bitmapTextureR, bitmapTextureG, bitmapTextureB, bitmapTextureA] = imageBands;++ // return default values to prevent deck.gl validation warnings.+ // Note: all bands must be set for the shadow effect to work as expected.+ return {+ bitmapTextureR: bitmapTextureR || bitmapTextureR,+ bitmapTextureG: bitmapTextureG || bitmapTextureR,+ bitmapTextureB: bitmapTextureB || bitmapTextureR,+ bitmapTextureA: bitmapTextureA || bitmapTextureR+ };+}++const fs1 = `\+uniform sampler2D bitmapTextureR;+uniform sampler2D bitmapTextureG;+uniform sampler2D bitmapTextureB;+uniform sampler2D bitmapTextureA;+`;++const fs2 = `\+precision mediump float;+precision mediump int;+precision mediump usampler2D;++#ifdef SAMPLER_TYPE+ uniform SAMPLER_TYPE bitmapTextureR;+ uniform SAMPLER_TYPE bitmapTextureG;+ uniform SAMPLER_TYPE bitmapTextureB;+ uniform SAMPLER_TYPE bitmapTextureA;+#else+ uniform sampler2D bitmapTextureR;+ uniform sampler2D bitmapTextureG;+ uniform sampler2D bitmapTextureB;+ uniform sampler2D bitmapTextureA;+#endif+`;++const combineBands: ShaderModule = {+ name: 'combine-bands',+ fs1,+ fs2,+ getUniforms,+ defines: {+ SAMPLER_TYPE: 'sampler2D'+ },+ inject: {+ 'fs:DECKGL_CREATE_COLOR': `+ float channel1 = float(texture2D(bitmapTextureR, coord).r);+ float channel2 = float(texture2D(bitmapTextureG, coord).r);+ float channel3 = float(texture2D(bitmapTextureB, coord).r);+ float channel4 = float(texture2D(bitmapTextureA, coord).r);++ image = vec4(channel1, channel2, channel3, channel4);+ `+ }+};++export const combineBandsFloat: ShaderModule = {+ ...combineBands,+ name: 'combine-bands-float'+};+export const combineBandsUint: ShaderModule = {+ ...combineBands,+ name: 'combine-bands-uint',+ defines: {+ SAMPLER_TYPE: 'usampler2D'+ }+};+export const combineBandsInt: ShaderModule = {+ ...combineBands,+ name: 'combine-bands-int',+ defines: {+ SAMPLER_TYPE: 'isampler2D'+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/texture/mask.ts b/src/deckgl-layers/src/raster/webgl/texture/mask.tsnew file mode 100644index 000000000..4ab37e185--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/texture/mask.ts@@ -0,0 +1,80 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {Texture2D} from '@luma.gl/webgl';++import {GetUniformsOutput, ShaderModule} from '../types';++const inf = Math.pow(2, 62);++function getUniforms(+ opts: {imageMask?: Texture2D; maskKeepMin?: number; maskKeepMax?: number} = {}+): GetUniformsOutput {+ const {imageMask, maskKeepMin, maskKeepMax} = opts;+ if (!imageMask) {+ return null;+ }++ return {+ bitmapTextureMask: imageMask,+ uMaskKeepMin: Number.isFinite(maskKeepMin) ? maskKeepMin : -inf,+ uMaskKeepMax: Number.isFinite(maskKeepMax) ? maskKeepMax : inf+ };+}++const fs1 = `\+uniform sampler2D bitmapTextureMask;+uniform float uMaskKeepMin;+uniform float uMaskKeepMax;+`;++const fs2 = `\+precision mediump float;+precision mediump int;+precision mediump usampler2D;++#ifdef SAMPLER_TYPE+ uniform SAMPLER_TYPE bitmapTextureMask;+#else+ uniform sampler2D bitmapTextureMask;+#endif++uniform float uMaskKeepMin;+uniform float uMaskKeepMax;+`;++const mask: ShaderModule = {+ name: 'mask-image',+ fs1,+ fs2,+ getUniforms,+ defines: {+ SAMPLER_TYPE: 'sampler2D'+ },+ inject: {+ 'fs:DECKGL_CREATE_COLOR': `+ float mask_value = float(texture2D(bitmapTextureMask, coord).r);+ if (mask_value < uMaskKeepMin) discard;+ if (mask_value > uMaskKeepMax) discard;+ `+ }+};++export const maskFloat: ShaderModule = {+ ...mask,+ name: 'mask-image-float'+};+export const maskUint: ShaderModule = {+ ...mask,+ name: 'mask-image-uint',+ defines: {+ SAMPLER_TYPE: 'usampler2D'+ }+};+export const maskInt: ShaderModule = {+ ...mask,+ name: 'mask-image-int',+ defines: {+ SAMPLER_TYPE: 'isampler2D'+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/texture/reorder-bands.ts b/src/deckgl-layers/src/raster/webgl/texture/reorder-bands.tsnew file mode 100644index 000000000..87e21a009--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/texture/reorder-bands.ts@@ -0,0 +1,63 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {ShaderModule, GetUniformsOutput} from '../types';++/**+ * Reorder image bands on GPU+ * Uses a permutation matrix to reorder a vec4+ */++const fs = `\+uniform mat4 uReorder;++vec4 reorder_image(vec4 image, mat4 ordering) {+ return image.rgba * ordering;+}+`;++function getUniforms(opts: {ordering?: number[]} = {}): GetUniformsOutput {+ const {ordering} = opts;++ if (!ordering) {+ return null;+ }++ return {+ uReorder: constructPermutationMatrix(ordering)+ };+}++/**+ * Construct permutation matrix from vector+ *+ * @param vector Vector describing how to reorder bands+ *+ * @return a mat4 permutation matrix representing how to reorder bands+ */+export function constructPermutationMatrix(vector: number[]): number[] {+ const mat4 = Array(16).fill(0);+ let row = 0;+ for (const index of vector) {+ mat4[row * 4 + index] = 1;+ row += 1;+ }++ // If input vector wasn't of length 4, add identity in final places+ for (let r = row; r < 4; r++) {+ mat4[r * 4 + r] = 1;+ }++ return mat4;+}++export const reorderBands: ShaderModule = {+ name: 'reorder-bands',+ fs,+ getUniforms,+ inject: {+ 'fs:DECKGL_MUTATE_COLOR': `+ image = reorder_image(image, uReorder);+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/texture/rgba-image.ts b/src/deckgl-layers/src/raster/webgl/texture/rgba-image.tsnew file mode 100644index 000000000..27221776a--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/texture/rgba-image.ts@@ -0,0 +1,49 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {Texture2D} from '@luma.gl/webgl';++import {GetUniformsOutput, ShaderModule} from '../types';++function getUniforms(opts: {imageRgba?: Texture2D} = {}): GetUniformsOutput {+ const {imageRgba} = opts;++ if (!imageRgba) {+ return null;+ }++ return {+ bitmapTextureRgba: imageRgba+ };+}++const fs1 = `\+uniform sampler2D bitmapTextureRgba;+`;++const fs2 = `\+precision mediump float;+precision mediump int;+precision mediump usampler2D;++#ifdef SAMPLER_TYPE+ uniform SAMPLER_TYPE bitmapTextureRgba;+#else+ uniform sampler2D bitmapTextureRgba;+#endif+`;++export const rgbaImage: ShaderModule = {+ name: 'rgba-image',+ fs1,+ fs2,+ getUniforms,+ defines: {+ SAMPLER_TYPE: 'sampler2D'+ },+ inject: {+ 'fs:DECKGL_CREATE_COLOR': `+ image = vec4(texture2D(bitmapTextureRgba, coord));+ `+ }+};diff --git a/src/deckgl-layers/src/raster/webgl/types.ts b/src/deckgl-layers/src/raster/webgl/types.tsnew file mode 100644index 000000000..38fe7493c--- /dev/null+++ b/src/deckgl-layers/src/raster/webgl/types.ts@@ -0,0 +1,33 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {Texture2D} from '@luma.gl/webgl';++export interface ShaderModule {+ /** A unique name for this shader module */+ name: string;++ /** A fragment shader to be used in both WebGL1 and WebGL2 environments */+ fs?: string;++ /** A fragment shader to be used only in WebGL1 environments */+ fs1?: string;++ /** A fragment shader to be used only in WebGL2 environments */+ fs2?: string;++ /** A vertex shader to inject */+ vs?: string;+ uniforms?: Record<string, any>;+ getUniforms?: (opts: object) => GetUniformsOutput;++ /** Optional constants to define when injecting */+ defines?: Record<string, string>;+ inject?: Record<string, string>;+ dependencies?: ShaderModule[];+ deprecations?: any[];+}++export type UniformType = number | number[] | Texture2D | undefined;++export type GetUniformsOutput = Record<string, UniformType> | null;diff --git a/src/layers/src/index.ts b/src/layers/src/index.tsindex 202946a09..9f8d4635a 100644--- a/src/layers/src/index.ts+++ b/src/layers/src/index.ts@@ -45,6 +45,22 @@ import {default as VectorTileLayer} from './vector-tile/vector-tile-layer';export {default as VectorTileIcon} from './vector-tile/vector-tile-icon';export {default as VectorTileLayer} from './vector-tile/vector-tile-layer';+import {default as RasterTileLayer} from './raster-tile/raster-tile-layer';+export {default as RasterTileIcon} from './raster-tile/raster-tile-icon';+export {+ default as RasterTileLayer,+ isTilesBeingLoaded as isRasterTilesBeingLoaded+} from './raster-tile/raster-tile-layer';+export {+ CATEGORICAL_COLORMAP_ID,+ DATA_SOURCE_COLOR_DEFAULTS,+ RASTER_COLOR_RESET_PARAMS,+ PRESET_OPTIONS+} from './raster-tile/config';+export {getCategoricalColormapDataUrl} from './raster-tile/image';+export * from './raster-tile/types';+export * from './raster-tile/raster-tile-utils';+import {LAYER_TYPES} from '@kepler.gl/constants';export {parseGeoJsonRawFeature} from './geojson-layer/geojson-utils';// base layer@@ -72,7 +88,8 @@ export const KeplerGlLayers = {ScenegraphLayer,TripLayer,S2GeometryLayer,- VectorTileLayer+ VectorTileLayer,+ RasterTileLayer};export type LayerClassesType = typeof LayerClasses;@@ -90,7 +107,8 @@ export const LayerClasses = {[LAYER_TYPES['3D']]: ScenegraphLayer,[LAYER_TYPES.trip]: TripLayer,[LAYER_TYPES.s2]: S2GeometryLayer,- [LAYER_TYPES['vectorTile']]: VectorTileLayer+ [LAYER_TYPES['vectorTile']]: VectorTileLayer,+ [LAYER_TYPES['rasterTile']]: RasterTileLayer};export * from './mapbox-utils';diff --git a/src/layers/src/raster-tile/config.ts b/src/layers/src/raster-tile/config.tsnew file mode 100644index 000000000..2cb7113f4--- /dev/null+++ b/src/layers/src/raster-tile/config.ts@@ -0,0 +1,751 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {RasterTileLayerColorMaps, RasterTileLayerPresets} from './raster-tile-layer-schema';+import {+ VisConfigBoolean,+ VisConfigNumber,+ VisConfigRange,+ VisConfigObjectSelection,+ VisConfigInput+} from '@kepler.gl/types';++import {ColorRescaling, ConfigOption, PresetData, BandCombination} from './types';++/**+ * Known Data Source IDs that work with current STAC Layer limitations+ */+export enum DATA_SOURCE_IDS {+ SENTINEL_2_L1C = 'sentinel-2-l1c',+ SENTINEL_2_L1A = 'sentinel-2-l2a',+ SENTINEL_2_C1_L2A = 'sentinel-2-c1-l2a',+ SENTINEL_2_PRE_C1_L2A = 'sentinel-2-pre-c1-l2a',+ LANDSAT_C2_L1 = 'landsat-c2-l1',+ LANDSAT_C2_L2 = 'landsat-c2-l2',+ MODIS_09A1_061 = 'modis-09A1-061',+ MODIS_43A4_061 = 'modis-43A4-061',+ MOIDS_09Q1_061 = 'modis-09Q1-061'+}++export const RASTER_COLOR_RESET_PARAMS = {+ gammaContrastFactor: 1,+ sigmoidalContrastFactor: 0,+ sigmoidalBiasFactor: 0.5,+ saturationValue: 1.0,+ dynamicColor: false+};++const DEFAULT_SENTINEL_COLOR_DEFAULTS = {+ gammaContrastFactor: 2.2,+ sigmoidalContrastFactor: 23,+ sigmoidalBiasFactor: 0.12,+ saturationValue: 2.0+};++const DEFAULT_MODIS_COLOR_DEFAULTS = {+ gammaContrastFactor: 2.0,+ sigmoidalContrastFactor: 8,+ sigmoidalBiasFactor: 0.13,+ saturationValue: 1.1+};++const DEFAULT_SENTINEL_BAND_MAPPING = {+ coastal: 'B01',+ blue: 'B02',+ green: 'B03',+ red: 'B04',+ rededge1: 'B05',+ rededge2: 'B06',+ rededge3: 'B07',+ nir: 'B08',+ nir08: 'B8A',+ nir09: 'B09',+ cirrus: 'B10',+ swir11: 'B11',+ swir16: 'B11',+ swir12: 'B12',+ swir22: 'B12'+};++const DEFAULT_MODIS_BAND_MAPPING = {+ sur_refl_b01: 'B01',+ sur_refl_b02: 'B02',+ sur_refl_b03: 'B03',+ sur_refl_b04: 'B04',+ sur_refl_b05: 'B05',+ sur_refl_b06: 'B06',+ sur_refl_b07: 'B07',+ Nadir_Reflectance_Band1: 'B01',+ Nadir_Reflectance_Band2: 'B02',+ Nadir_Reflectance_Band3: 'B03',+ Nadir_Reflectance_Band4: 'B04',+ Nadir_Reflectance_Band5: 'B05',+ Nadir_Reflectance_Band6: 'B06',+ Nadir_Reflectance_Band7: 'B07'+};++const DEFAULT_LANDSAT_C2_L1_BAND_MAPPING = {+ green: 'B01',+ red: 'B02',+ nir08: 'B03',+ nir09: 'B04'+};++const DEFAULT_LANDSAT_C2_L2_BAND_MAPPING = {+ blue: 'B01',+ green: 'B02',+ red: 'B03',+ nir08: 'B04',+ swir16: 'B05',+ lwir: 'B06',+ swir22: 'B07',+ coastal: 'B01',+ lwir11: 'B06'+};++export const DEFAULT_BAND_MAPPINGS = {+ [DATA_SOURCE_IDS.SENTINEL_2_L1C]: DEFAULT_SENTINEL_BAND_MAPPING,+ [DATA_SOURCE_IDS.SENTINEL_2_L1A]: DEFAULT_SENTINEL_BAND_MAPPING,+ [DATA_SOURCE_IDS.SENTINEL_2_C1_L2A]: DEFAULT_SENTINEL_BAND_MAPPING,+ [DATA_SOURCE_IDS.SENTINEL_2_PRE_C1_L2A]: DEFAULT_SENTINEL_BAND_MAPPING,+ [DATA_SOURCE_IDS.LANDSAT_C2_L1]: DEFAULT_LANDSAT_C2_L1_BAND_MAPPING,+ [DATA_SOURCE_IDS.LANDSAT_C2_L2]: DEFAULT_LANDSAT_C2_L2_BAND_MAPPING,+ [DATA_SOURCE_IDS.MODIS_09A1_061]: DEFAULT_MODIS_BAND_MAPPING,+ [DATA_SOURCE_IDS.MODIS_43A4_061]: DEFAULT_MODIS_BAND_MAPPING,+ [DATA_SOURCE_IDS.MOIDS_09Q1_061]: DEFAULT_MODIS_BAND_MAPPING+};++/**+ * Per-data source color rescaling defaults for known collections+ */+export const DATA_SOURCE_COLOR_DEFAULTS: Record<DATA_SOURCE_IDS, ColorRescaling> = {+ // Note: good for True Color, too saturated for other channels+ [DATA_SOURCE_IDS.SENTINEL_2_L1C]: DEFAULT_SENTINEL_COLOR_DEFAULTS,+ [DATA_SOURCE_IDS.SENTINEL_2_L1A]: DEFAULT_SENTINEL_COLOR_DEFAULTS,+ [DATA_SOURCE_IDS.SENTINEL_2_C1_L2A]: DEFAULT_SENTINEL_COLOR_DEFAULTS,+ [DATA_SOURCE_IDS.SENTINEL_2_PRE_C1_L2A]: DEFAULT_SENTINEL_COLOR_DEFAULTS,+ [DATA_SOURCE_IDS.LANDSAT_C2_L1]: DEFAULT_SENTINEL_COLOR_DEFAULTS,+ [DATA_SOURCE_IDS.LANDSAT_C2_L2]: DEFAULT_SENTINEL_COLOR_DEFAULTS,+ [DATA_SOURCE_IDS.MODIS_09A1_061]: DEFAULT_MODIS_COLOR_DEFAULTS,+ [DATA_SOURCE_IDS.MODIS_43A4_061]: DEFAULT_MODIS_COLOR_DEFAULTS,+ [DATA_SOURCE_IDS.MOIDS_09Q1_061]: DEFAULT_MODIS_COLOR_DEFAULTS+};++/**+ * Available "presets"+ *+ * I define a "preset" as one specific manner of loading bands and combining them on the frontend.+ * In the future I expect we'll want a UI that gives the user full flexibility.+ */+export const PRESET_OPTIONS: Record<string, PresetData> = {+ trueColor: {+ label: 'True Color',+ id: RasterTileLayerPresets.trueColor,+ bandCombination: BandCombination.Rgb,+ commonNames: ['red', 'green', 'blue']+ },+ infrared: {+ label: 'Infrared',+ id: RasterTileLayerPresets.infrared,+ bandCombination: BandCombination.Rgb,+ commonNames: ['nir', 'red', 'green'],+ description: 'False-color infrared composite. Near-infrared, red, green mapped to RGB'+ },+ agriculture: {+ label: 'Agriculture',+ id: RasterTileLayerPresets.agriculture,+ bandCombination: BandCombination.Rgb,+ commonNames: ['swir16', 'nir', 'blue'],+ description:+ 'False-color agriculture composite. Short-wave infrared 1, near-infrared, blue mapped to RGB.'+ },+ forestBurn: {+ label: 'Forest Burn',+ id: RasterTileLayerPresets.forestBurn,+ bandCombination: BandCombination.Rgb,+ commonNames: ['swir22', 'nir', 'blue'],+ description:+ 'False-color forest burn composite. Short-wave infrared 2, near-infrared, blue mapped to RGB'+ },+ ndvi: {+ label: 'NDVI',+ id: RasterTileLayerPresets.ndvi,+ bandCombination: BandCombination.NormalizedDifference,+ commonNames: ['nir', 'red'],+ description: 'Normalized Difference Vegetation Index',+ infoUrl:+ 'https://www.usgs.gov/core-science-systems/nli/landsat/landsat-normalized-difference-vegetation-index'+ },+ // Comment this out for now, because in testing it looks like there's an overflow of some sort;+ // index values should be between -1 and 1, but values look way out of bounds+ // evi: {+ // label: 'EVI',+ // id: RasterTileLayerPresets.evi,+ // bandCombination: 'enhancedVegetationIndex',+ // commonNames: ['nir', 'red', 'blue']+ // },+ savi: {+ label: 'SAVI',+ id: RasterTileLayerPresets.savi,+ bandCombination: BandCombination.SoilAdjustedVegetationIndex,+ commonNames: ['nir', 'red'],+ description: 'Soil Adjusted Vegetation Index',+ infoUrl:+ 'https://www.usgs.gov/core-science-systems/nli/landsat/landsat-soil-adjusted-vegetation-index'+ },+ msavi: {+ label: 'MSAVI',+ id: RasterTileLayerPresets.msavi,+ bandCombination: BandCombination.ModifiedSoilAdjustedVegetationIndex,+ commonNames: ['nir', 'red'],+ description: 'Modified Soil Adjusted Vegetation Index',+ infoUrl:+ 'https://www.usgs.gov/core-science-systems/nli/landsat/landsat-modified-soil-adjusted-vegetation-index'+ },+ ndmi: {+ label: 'NDMI',+ id: RasterTileLayerPresets.ndmi,+ bandCombination: BandCombination.NormalizedDifference,+ commonNames: ['nir', 'swir16'],+ description: 'Normalized Difference Moisture Index',+ infoUrl:+ 'https://www.usgs.gov/core-science-systems/nli/landsat/normalized-difference-moisture-index'+ },+ nbr: {+ label: 'NBR',+ id: RasterTileLayerPresets.nbr,+ bandCombination: BandCombination.NormalizedDifference,+ commonNames: ['nir', 'swir22'],+ description: 'Normalized Burn Ratio',+ infoUrl: 'https://www.usgs.gov/core-science-systems/nli/landsat/landsat-normalized-burn-ratio'+ },+ nbr2: {+ label: 'NBR 2',+ id: RasterTileLayerPresets.nbr2,+ bandCombination: BandCombination.NormalizedDifference,+ commonNames: ['swir16', 'swir22'],+ description: 'Normalized Burn Ratio 2',+ infoUrl: 'https://www.usgs.gov/core-science-systems/nli/landsat/landsat-normalized-burn-ratio-2'+ },+ singleBand: {+ label: 'Single Band',+ id: RasterTileLayerPresets.singleBand,+ bandCombination: BandCombination.Single,+ description: 'Render a single band data'+ }+};++/**+ * Valid zoom ranges for each data source+ *+ * The maximum zoom is derived from the resolution of each data source.+ * The minimum zoom relates to the number of overviews in the COGs at the source. The more+ * overviews, the easier it is to create downsampled tiles. The minimum zooms can be reduced but it+ * takes longer to serve low-zoom tiles since the server must read from many image sources.+ */+// TODO: use render:max_overview_gsd from STAC+export const ZOOM_RANGES: Record<DATA_SOURCE_IDS, [number, number]> = {+ [DATA_SOURCE_IDS.SENTINEL_2_C1_L2A]: [8, 13],+ [DATA_SOURCE_IDS.SENTINEL_2_L1A]: [8, 13],+ [DATA_SOURCE_IDS.SENTINEL_2_L1C]: [8, 13],+ [DATA_SOURCE_IDS.SENTINEL_2_PRE_C1_L2A]: [8, 13],+ [DATA_SOURCE_IDS.LANDSAT_C2_L1]: [7, 12],+ [DATA_SOURCE_IDS.LANDSAT_C2_L2]: [7, 12],+ [DATA_SOURCE_IDS.MODIS_09A1_061]: [7, 13],+ [DATA_SOURCE_IDS.MODIS_43A4_061]: [7, 13],+ [DATA_SOURCE_IDS.MOIDS_09Q1_061]: [7, 13]+};++/**+ * Bit depth for each data source+ * Sentinel-2 is 12-bit; Landsat-8 is 16-bit; Planet is 12-bit; NAIP is 8-bit+ */+// TODO: use range value and dtype in STAC collection instead of this+export const MAX_PIXEL_VALUES: Record<DATA_SOURCE_IDS, number> = {+ [DATA_SOURCE_IDS.SENTINEL_2_C1_L2A]: Math.pow(2, 12) - 1,+ [DATA_SOURCE_IDS.SENTINEL_2_L1A]: Math.pow(2, 12) - 1,+ [DATA_SOURCE_IDS.SENTINEL_2_L1C]: Math.pow(2, 12) - 1,+ [DATA_SOURCE_IDS.SENTINEL_2_PRE_C1_L2A]: Math.pow(2, 12) - 1,+ [DATA_SOURCE_IDS.LANDSAT_C2_L1]: Math.pow(2, 16) - 1,+ [DATA_SOURCE_IDS.LANDSAT_C2_L2]: Math.pow(2, 16) - 1,+ [DATA_SOURCE_IDS.MODIS_09A1_061]: Math.pow(2, 12) - 1,+ [DATA_SOURCE_IDS.MODIS_43A4_061]: Math.pow(2, 12) - 1,+ [DATA_SOURCE_IDS.MOIDS_09Q1_061]: Math.pow(2, 12) - 1+};++/**+ * Id for categorical colormap. Unlike most colormaps. Categorical colormap image is created+ * from colormap set in visConfig+ */+export const CATEGORICAL_COLORMAP_ID = '_categorical';++/**+ * A list of available colormaps. Colormaps are originally derived from+ * matplotlib, then via rio-tiler. Colormaps are 10x256 PNG images that are+ * loaded as textures+ */++export const COLORMAP_OPTIONS: readonly ConfigOption[] = [+ {+ label: 'Cfastie',+ id: RasterTileLayerColorMaps.cfastie+ },+ {+ label: 'Rplumbo',+ id: RasterTileLayerColorMaps.rplumbo+ },+ {+ label: 'Schwarzwald',+ id: RasterTileLayerColorMaps.schwarzwald+ },+ {+ label: 'Viridis',+ id: RasterTileLayerColorMaps.viridis+ },+ {+ label: 'Plasma',+ id: RasterTileLayerColorMaps.plasma+ },+ {+ label: 'Inferno',+ id: RasterTileLayerColorMaps.inferno+ },+ {+ label: 'Magma',+ id: RasterTileLayerColorMaps.magma+ },+ {+ label: 'Cividis',+ id: RasterTileLayerColorMaps.cividis+ },+ {+ label: 'Greys',+ id: RasterTileLayerColorMaps.greys+ },+ {+ label: 'Purples',+ id: RasterTileLayerColorMaps.purples+ },+ {+ label: 'Blues',+ id: RasterTileLayerColorMaps.blues+ },+ {+ label: 'Greens',+ id: RasterTileLayerColorMaps.greens+ },+ {+ label: 'Oranges',+ id: RasterTileLayerColorMaps.oranges+ },+ {+ label: 'Reds',+ id: RasterTileLayerColorMaps.reds+ },+ {+ label: 'Yl-Or-Br',+ id: RasterTileLayerColorMaps.ylorbr+ },+ {+ label: 'Yl-Or-Rd',+ id: RasterTileLayerColorMaps.ylorrd+ },+ {+ label: 'Or-Rd',+ id: RasterTileLayerColorMaps.orrd+ },+ {+ label: 'Pu-Rd',+ id: RasterTileLayerColorMaps.purd+ },+ {+ label: 'Rd-Pu',+ id: RasterTileLayerColorMaps.rdpu+ },+ {+ label: 'Bu-Pu',+ id: RasterTileLayerColorMaps.bupu+ },+ {+ label: 'Gn-Bu',+ id: RasterTileLayerColorMaps.gnbu+ },+ {+ label: 'Pu-Bu',+ id: RasterTileLayerColorMaps.pubu+ },+ {+ label: 'Yl-Gn-Bu',+ id: RasterTileLayerColorMaps.ylgnbu+ },+ {+ label: 'Pu-Bu-Gn',+ id: RasterTileLayerColorMaps.pubugn+ },+ {+ label: 'Blue-Gn',+ id: RasterTileLayerColorMaps.bugn+ },+ {+ label: 'Yl-Gn',+ id: RasterTileLayerColorMaps.ylgn+ },+ {+ label: 'W-n-B',+ id: RasterTileLayerColorMaps.binary+ },+ {+ label: 'B-n-W',+ id: RasterTileLayerColorMaps.gray+ },+ {+ label: 'Bone',+ id: RasterTileLayerColorMaps.bone+ },+ {+ label: 'Pink',+ id: RasterTileLayerColorMaps.pink+ },+ {+ label: 'Spring',+ id: RasterTileLayerColorMaps.spring+ },+ {+ label: 'Summer',+ id: RasterTileLayerColorMaps.summer+ },+ {+ label: 'Autumn',+ id: RasterTileLayerColorMaps.autumn+ },+ {+ label: 'Winter',+ id: RasterTileLayerColorMaps.winter+ },+ {+ label: 'Cool',+ id: RasterTileLayerColorMaps.cool+ },+ {+ label: 'Wistia',+ id: RasterTileLayerColorMaps.wistia+ },+ {+ label: 'Hot',+ id: RasterTileLayerColorMaps.hot+ },+ {+ label: 'Afmhot',+ id: RasterTileLayerColorMaps.afmhot+ },+ {+ label: 'Heat',+ id: RasterTileLayerColorMaps.gist_heat+ },+ {+ label: 'Copper',+ id: RasterTileLayerColorMaps.copper+ },+ {+ label: 'Pi-Green',+ id: RasterTileLayerColorMaps.piyg+ },+ {+ label: 'Pr-Gn',+ id: RasterTileLayerColorMaps.prgn+ },+ {+ label: 'Br-Bg',+ id: RasterTileLayerColorMaps.brbg+ },+ {+ label: 'Pu-Or',+ id: RasterTileLayerColorMaps.puor+ },+ {+ label: 'Rd-Gy',+ id: RasterTileLayerColorMaps.rdgy+ },+ {+ label: 'Rd-Bu',+ id: RasterTileLayerColorMaps.rdbu+ },+ {+ label: 'Rd-Yl-Bu',+ id: RasterTileLayerColorMaps.rdylbu+ },+ {+ label: 'Rd-Yl-Gn',+ id: RasterTileLayerColorMaps.rdylgn+ },+ {+ label: 'Spectral',+ id: RasterTileLayerColorMaps.spectral+ },+ {+ label: 'Cool-Warm',+ id: RasterTileLayerColorMaps.coolwarm+ },+ {+ label: 'B-W-R',+ id: RasterTileLayerColorMaps.bwr+ },+ {+ label: 'Seismic',+ id: RasterTileLayerColorMaps.seismic+ },+ {+ label: 'Twilight',+ id: RasterTileLayerColorMaps.twilight+ },+ {+ label: 'Twilight Shifted',+ id: RasterTileLayerColorMaps.twilight_shifted+ },+ {+ label: 'HSV',+ id: RasterTileLayerColorMaps.hsv+ },+ {+ label: 'Flag',+ id: RasterTileLayerColorMaps.flag+ },+ {+ label: 'Prism',+ id: RasterTileLayerColorMaps.prism+ },+ {+ label: 'Ocean',+ id: RasterTileLayerColorMaps.ocean+ },+ {+ label: 'Gist Earth',+ id: RasterTileLayerColorMaps.gist_earth+ },+ {+ label: 'Terrain',+ id: RasterTileLayerColorMaps.terrain+ },+ {+ label: 'Gist Stern',+ id: RasterTileLayerColorMaps.gist_stern+ },+ {+ label: 'Gnuplot',+ id: RasterTileLayerColorMaps.gnuplot+ },+ {+ label: 'Gnuplot2',+ id: RasterTileLayerColorMaps.gnuplot2+ },+ {+ label: 'Cmrmap',+ id: RasterTileLayerColorMaps.cmrmap+ },+ {+ label: 'Cubehelix',+ id: RasterTileLayerColorMaps.cubehelix+ },+ {+ label: 'Brg',+ id: RasterTileLayerColorMaps.brg+ },+ {+ label: 'Gist Rainbow',+ id: RasterTileLayerColorMaps.gist_rainbow+ },+ {+ label: 'Rainbow',+ id: RasterTileLayerColorMaps.rainbow+ },+ {+ label: 'Jet',+ id: RasterTileLayerColorMaps.jet+ },+ {+ label: 'Nipy Spectral',+ id: RasterTileLayerColorMaps.nipy_spectral+ },+ {+ label: 'Gist NCAR',+ id: RasterTileLayerColorMaps.gist_ncar+ }+];++const STAC_SEARCH_PROVIDERS: ConfigOption[] = [+ // {+ // label: 'Microsoft Planetary Computer',+ // id: 'microsoft'+ // },+ {+ label: 'Element 84 Earth Search (AWS)',+ id: 'earth-search'+ }+];++/**+ * Configuration settings to be exposed through the UI+ */+export const rasterVisConfigs = {+ preset: {+ type: 'object-select',+ defaultValue: 'trueColor',+ label: 'Preset',+ options: Object.values(PRESET_OPTIONS),+ property: 'preset',+ group: ''+ } as VisConfigObjectSelection,+ useSTACSearching: {+ type: 'boolean',+ property: 'useSTACSearching',+ defaultValue: false,+ label: 'Use STAC Searching'+ } as VisConfigBoolean,+ stacSearchProvider: {+ type: 'object-select',+ defaultValue: 'earth-search',+ label: 'STAC Search Provider',+ options: STAC_SEARCH_PROVIDERS,+ property: 'stacSearchProvider'+ } as VisConfigObjectSelection,+ startDate: {+ type: 'input',+ defaultValue: '2020-02-02',+ label: 'Start Date',+ property: 'startDate'+ } as VisConfigInput,+ endDate: {+ type: 'input',+ defaultValue: '2020-03-02',+ label: 'End Date',+ property: 'endDate'+ } as VisConfigInput,+ 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,+ colormapId: {+ type: 'object-select',+ defaultValue: 'cfastie',+ label: 'Colormap',+ options: COLORMAP_OPTIONS,+ property: 'colormapId',+ group: ''+ } as VisConfigObjectSelection,+ // kepler's visConfig from kepler.gl https://github.com/foursquare/kepler.gl/blob/490a8938ffa1fac025e8d1997481acc1bffe4abd/src/layers/layer-factory.js#L228+ // key `colorRange` is required becuase it is hardcoded in kepler.gl+ // to make shallow copy of the visConfig item+ // https://github.com/foursquare/kepler.gl/blob/490a8938ffa1fac025e8d1997481acc1bffe4abd/src/reducers/vis-state-merger.js#L736+ colorRange: 'colorRange' as const,+ linearRescalingFactor: {+ defaultValue: [0, 1],+ // group: "color"+ isRanged: true,+ label: 'Linear Rescaling Factor',+ property: 'linearRescalingFactor',+ range: [0, 1],+ step: 0.01,+ type: 'number'+ } as VisConfigRange,+ // Non-linear rescaling for true-color images+ // If false, implies linear rescaling+ nonLinearRescaling: {+ type: 'boolean',+ property: 'nonLinearRescaling',+ defaultValue: true,+ // group: undefined+ label: 'Non-Linear Rescaling'+ } as VisConfigBoolean,+ gammaContrastFactor: {+ defaultValue: 1,+ // group: "color"+ isRanged: false,+ label: 'Gamma Contrast',+ property: 'gammaContrastFactor',+ range: [0, 3],+ step: 0.05,+ type: 'number'+ } as VisConfigNumber,+ sigmoidalContrastFactor: {+ defaultValue: 0,+ // group: "color"+ isRanged: false,+ label: 'Sigmoidal Contrast',+ property: 'sigmoidalContrastFactor',+ range: [0, 50],+ step: 1,+ type: 'number'+ } as VisConfigNumber,+ sigmoidalBiasFactor: {+ defaultValue: 0.5,+ // group: "color"+ isRanged: false,+ label: 'Sigmoidal Bias',+ property: 'sigmoidalBiasFactor',+ range: [0, 1],+ step: 0.01,+ type: 'number'+ } as VisConfigNumber,+ saturationValue: {+ defaultValue: 1,+ // group: "color"+ isRanged: false,+ label: 'Saturation',+ property: 'saturationValue',+ range: [0, 2],+ step: 0.05,+ type: 'number'+ } as VisConfigNumber,+ filterEnabled: {+ type: 'boolean',+ property: 'filterEnabled',+ defaultValue: false,+ // group: undefined+ label: 'Filter'+ } as VisConfigBoolean,+ filterRange: {+ defaultValue: [-1, 1],+ // group: "color"+ isRanged: true,+ label: 'Filter',+ property: 'filterRange',+ range: [-1, 1],+ step: 0.01,+ type: 'number'+ } as VisConfigRange,+ opacity: {+ defaultValue: 1,+ // group: "color"+ isRanged: false,+ label: 'Opacity',+ property: 'opacity',+ range: [0, 1],+ step: 0.05,+ type: 'number'+ } as VisConfigNumber,+ _stacQuery: {+ defaultValue: null,+ type: 'input'+ } as VisConfigInput,+ singleBandName: {+ type: 'object-select',+ defaultValue: null,+ label: 'Name of single band to render',+ // defined dynamically from STAC item/collection+ options: [],+ property: 'singleBandName',+ group: ''+ } as VisConfigObjectSelection,+ enableTerrain: {+ type: 'boolean',+ defaultValue: true,+ label: 'Enable 3D terrain',+ group: '',+ property: 'enableTerrain',+ description: 'Use 3D terrain when available. Only affects Map View in 3D mode.'+ } as VisConfigBoolean+};diff --git a/src/layers/src/raster-tile/gpu-utils.ts b/src/layers/src/raster-tile/gpu-utils.tsnew file mode 100644index 000000000..80660b0d1--- /dev/null+++ b/src/layers/src/raster-tile/gpu-utils.ts@@ -0,0 +1,498 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++/**+ * Functions and constants for handling webgl/luma.gl/deck.gl entities+ */++import {parse, fetchFile, load} from '@loaders.gl/core';+import {ImageLoader} from '@loaders.gl/images';+import {NPYLoader} from '@loaders.gl/textures';+import GL from '@luma.gl/constants';+import {Texture2DProps} from '@luma.gl/webgl';++import {getLoaderOptions} from '@kepler.gl/constants';+import {RasterWebGL} from '@kepler.gl/deckgl-layers';++type ShaderModule = RasterWebGL.ShaderModule;+const {+ combineBandsFloat,+ combineBandsInt,+ combineBandsUint,+ maskFloat,+ maskInt,+ maskUint,+ linearRescale,+ gammaContrast,+ sigmoidalContrast,+ normalizedDifference,+ enhancedVegetationIndex,+ soilAdjustedVegetationIndex,+ modifiedSoilAdjustedVegetationIndex,+ colormap: colormapModule,+ filter,+ saturation,+ reorderBands,+ rgbaImage+} = RasterWebGL;++import {+ CATEGORICAL_TEXTURE_WIDTH,+ dtypeMaxValue,+ generateCategoricalBitmapArray,+ isColormapAllowed,+ isFilterAllowed,+ isRescalingAllowed+} from './raster-tile-utils';+import {+ CategoricalColormapOptions,+ ImageData,+ NPYLoaderDataTypes,+ NPYLoaderResponse,+ RenderSubLayersProps+} from './types';++/**+ * Describe WebGL2 Texture parameters to use for given input data type+ */+interface WebGLTextureFormat {+ format: number;+ dataFormat: number;+ type: number;+}++/**+ * Convert TypedArray to WebGL2 Texture Parameters+ */+function getWebGL2TextureParameters(data: NPYLoaderDataTypes): WebGLTextureFormat | never {+ if (data instanceof Uint8Array || data instanceof Uint8ClampedArray) {+ return {+ // Note: texture data has no auto-rescaling; pixel values stay as 0-255+ format: GL.R8UI,+ dataFormat: GL.RED_INTEGER,+ type: GL.UNSIGNED_BYTE+ };+ }++ if (data instanceof Uint16Array) {+ return {+ format: GL.R16UI,+ dataFormat: GL.RED_INTEGER,+ type: GL.UNSIGNED_SHORT+ };+ }++ if (data instanceof Uint32Array) {+ return {+ format: GL.R32UI,+ dataFormat: GL.RED_INTEGER,+ type: GL.UNSIGNED_INT+ };+ }++ if (data instanceof Int8Array) {+ return {+ format: GL.R8I,+ dataFormat: GL.RED_INTEGER,+ type: GL.BYTE+ };+ }++ if (data instanceof Int16Array) {+ return {+ format: GL.R16I,+ dataFormat: GL.RED_INTEGER,+ type: GL.SHORT+ };+ }+ if (data instanceof Int32Array) {+ return {+ format: GL.R32I,+ dataFormat: GL.RED_INTEGER,+ type: GL.INT+ };+ }+ if (data instanceof Float32Array) {+ return {+ format: GL.R32F,+ dataFormat: GL.RED,+ type: GL.FLOAT+ };+ }++ if (data instanceof Float64Array) {+ return {+ format: GL.R32F,+ dataFormat: GL.RED,+ type: GL.FLOAT+ };+ }++ // For exhaustive check above; following should never occur+ // https://stackoverflow.com/a/58009992+ const unexpectedInput: never = data;+ throw new Error(unexpectedInput);+}++/**+ * Discrete-valued colormaps (e.g. from the output of+ * classification algorithms) in the raster layer. Previously, the values passed to+ * `TEXTURE_MIN_FILTER` and `TEXTURE_MAG_FILTER` were `GL.LINEAR`, which meant that the GPU would+ * linearly interpolate values between two neighboring colormap pixel values. Setting these values+ * to NEAREST means that the GPU will choose the nearest value on the texture2D lookup operation,+ * which fixes precision issues for discrete-valued colormaps. This should be ok for continuous+ * colormaps as long as the color difference between each pixel on the colormap is small.+ */+export const COLORMAP_TEXTURE_PARAMETERS = {+ [GL.TEXTURE_MIN_FILTER]: GL.NEAREST,+ [GL.TEXTURE_MAG_FILTER]: GL.NEAREST,+ [GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,+ [GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE+};++const DEFAULT_8BIT_TEXTURE_PARAMETERS = {+ [GL.TEXTURE_MIN_FILTER]: GL.LINEAR_MIPMAP_LINEAR,+ [GL.TEXTURE_MAG_FILTER]: GL.LINEAR,+ [GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,+ [GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE+};++const DEFAULT_HIGH_BIT_TEXTURE_PARAMETERS = {+ [GL.TEXTURE_MIN_FILTER]: GL.NEAREST,+ [GL.TEXTURE_MAG_FILTER]: GL.NEAREST,+ [GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,+ [GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE+};++/**+ * Select correct module type for "combineBands"+ *+ * combineBands joins up to four 2D arrays (contained in imageBands) into a single "rgba" image+ * texture on the GPU. That shader code needs to have the same data type as the actual image data.+ * E.g. for float data the texture needs to be `sampler2D`, for uint data the texture needs to be+ * `usampler2D` and for int data the texture needs to be `isampler2D`.+ */+export function getCombineBandsModule(imageBands: Texture2DProps[]): ShaderModule {+ // Each image array is expected/required to be of the same data type+ switch (imageBands[0].format) {+ case GL.R8UI:+ return combineBandsUint;+ case GL.R16UI:+ return combineBandsUint;+ case GL.R32UI:+ return combineBandsUint;+ case GL.R8I:+ return combineBandsInt;+ case GL.R16I:+ return combineBandsInt;+ case GL.R32I:+ return combineBandsInt;+ case GL.R32F:+ return combineBandsFloat;+ default:+ throw new Error('bad format');+ }+}++/** Select correct image masking shader module for mask data type+ * The imageMask could (at least in the future, theoretically) be of a different data format than+ * the imageBands data itself.+ */+export function getImageMaskModule(imageMask: Texture2DProps): ShaderModule {+ switch (imageMask.format) {+ case GL.R8UI:+ return maskUint;+ case GL.R16UI:+ return maskUint;+ case GL.R32UI:+ return maskUint;+ case GL.R8I:+ return maskInt;+ case GL.R16I:+ return maskInt;+ case GL.R32I:+ return maskInt;+ case GL.R32F:+ return maskFloat;+ default:+ throw new Error('bad format');+ }+}++/**+ * Load image and wrap with default WebGL texture parameters+ *+ * @param url URL to load image+ * @param textureParams parameters to pass to Texture2D+ *+ * @return image object to pass to Texture2D constructor+ */+export async function loadImage(+ url: string,+ textureParams: Texture2DProps = {},+ requestOptions: RequestInit = {}+): Promise<Texture2DProps> {+ const response = await fetchFile(url, requestOptions);+ const image = await parse(response, ImageLoader);++ return {+ data: image,+ parameters: DEFAULT_8BIT_TEXTURE_PARAMETERS,+ format: GL.RGB,+ ...textureParams+ };+}++type FetchLike = (url: string, options?: RequestInit) => Promise<Response>;+type LoadingOptions = {+ fetch?: typeof fetch | FetchLike;+};++/**+ * Load NPY Array+ *+ * The NPY format is described here: https://numpy.org/doc/stable/reference/generated/numpy.lib.format.html.+ * It's designed to be a very simple file format to hold an N-dimensional block of data. The header describes the data type, shape, and order (either C or Fortran) of the array.+ *+ * @param url URL to load NPY Array+ * @param split Whether to split single typed array representing an N-dimensional array into an Array with each dimension as its own typed array+ *+ * @return image object to pass to Texture2D constructor+ */+export async function loadNpyArray(+ request: {url: string; options: RequestInit},+ split: true,+ options?: LoadingOptions+): Promise<Texture2DProps[] | null>;+export async function loadNpyArray(+ request: {url: string; options: RequestInit},+ split: false,+ options?: LoadingOptions+): Promise<Texture2DProps | null>;+export async function loadNpyArray(+ request: {url: string; options: RequestInit},+ split: boolean,+ options?: LoadingOptions+): Promise<Texture2DProps | Texture2DProps[] | null> {+ try {+ const {npy: npyOptions} = getLoaderOptions();+ const response: NPYLoaderResponse = await load(request.url, NPYLoader, {+ npy: npyOptions,+ fetch: options?.fetch+ });++ if (!response || !response.data || request.options.signal?.aborted) {+ return null;+ }++ // Float64 data needs to be coerced to Float32 for the GPU+ if (response.data instanceof Float64Array) {+ response.data = Float32Array.from(response.data);+ }++ const {data, header} = response;+ const {shape} = header;+ const {format, dataFormat, type} = getWebGL2TextureParameters(data);++ // TODO: check height-width or width-height+ // Regardless, images usually square+ // TODO: handle cases of 256x256x1 instead of 1x256x256+ const [z, height, width] = shape;++ // Since we now use WebGL2 data types for 8-bit textures, we set the following for all textures+ const mipmaps = false;+ const parameters = DEFAULT_HIGH_BIT_TEXTURE_PARAMETERS;++ if (!split) {+ return {+ data,+ width,+ height,+ format,+ dataFormat,+ type,+ parameters,+ mipmaps+ };+ }++ // Split into individual arrays+ const channels: Texture2DProps[] = [];+ const channelSize = height * width;+ for (let i = 0; i < z; i++) {+ channels.push({+ data: data.subarray(i * channelSize, (i + 1) * channelSize),+ width,+ height,+ format,+ dataFormat,+ type,+ parameters,+ mipmaps+ });+ }+ return channels;+ } catch {+ return null;+ }+}++/**+ * Create texture data for categorical colormap scale+ * @param categoricalOptions - color map configuration and min-max values of categorical band+ * @returns texture data+ */+export function generateCategoricalColormapTexture(+ categoricalOptions: CategoricalColormapOptions+): Texture2DProps {+ const data = generateCategoricalBitmapArray(categoricalOptions);+ return {+ data,+ width: CATEGORICAL_TEXTURE_WIDTH,+ height: 1,+ format: GL.RGBA,+ dataFormat: GL.RGBA,+ type: GL.UNSIGNED_BYTE,+ parameters: COLORMAP_TEXTURE_PARAMETERS,+ mipmaps: false+ };+}++// TODO: would probably be simpler to only pass in the props actually used by this function. That+// would mean a smaller object than RenderSubLayersProps+// eslint-disable-next-line max-statements, complexity+export function getModules({+ images,+ props+}: {+ images: Partial<ImageData>;+ props?: RenderSubLayersProps;+}): {+ modules: ShaderModule[];+ moduleProps: Record<string, any>;+} {+ const moduleProps: Record<string, any> = {};+ // Array of luma.gl WebGL modules to pass to the RasterLayer+ const modules: ShaderModule[] = [];++ // use rgba image directly. Used for raster .pmtiles rendering+ if (images.imageRgba) {+ modules.push(rgbaImage);++ // no support for other modules atm for direct rgba mode+ return {modules, moduleProps};+ }++ if (!props) {+ return {modules, moduleProps};+ }++ const {+ renderBandIndexes,+ nonLinearRescaling,+ linearRescalingFactor,+ minPixelValue,+ maxPixelValue,+ gammaContrastFactor,+ sigmoidalContrastFactor,+ sigmoidalBiasFactor,+ saturationValue,+ bandCombination,+ filterEnabled,+ filterRange,+ dataType,+ minCategoricalBandValue,+ maxCategoricalBandValue,+ hasCategoricalColorMap+ } = props;++ if (Array.isArray(images.imageBands) && images.imageBands.length > 0) {+ modules.push(getCombineBandsModule(images.imageBands));+ }++ if (images.imageMask) {+ modules.push(getImageMaskModule(images.imageMask));+ // In general, data masks are 0 for nodata and the maximum value for valid data, e.g. 255 or+ // 65535 for uint8 or uint16 data, respectively+ moduleProps.maskKeepMin = 1;+ }++ if (Array.isArray(renderBandIndexes)) {+ modules.push(reorderBands);+ moduleProps.ordering = renderBandIndexes;+ }++ const globalRange = maxPixelValue - minPixelValue;+ // Fix rescaling if we are sure that dataset is categorical+ if (hasCategoricalColorMap) {+ modules.push(linearRescale);+ moduleProps.linearRescaleScaler = 1 / maxPixelValue;+ moduleProps.linearRescaleOffset = 0;+ } else if (isRescalingAllowed(bandCombination)) {+ if (!nonLinearRescaling) {+ const [min, max] = linearRescalingFactor;+ const localRange = max - min;++ // Add linear rescaling module+ modules.push(linearRescale);++ // Divide by local range * global range+ moduleProps.linearRescaleScaler = 1 / (localRange * globalRange);++ // Subtract off the local min+ moduleProps.linearRescaleOffset = -min;++ // Clamp to [0, 1] done automatically?+ } else {+ modules.push(linearRescale);+ moduleProps.linearRescaleScaler = 1 / maxPixelValue;+ moduleProps.linearRescaleOffset = 0;++ modules.push(gammaContrast);+ moduleProps.gammaContrastValue = gammaContrastFactor;++ modules.push(sigmoidalContrast);+ moduleProps.sigmoidalContrast = sigmoidalContrastFactor;+ moduleProps.sigmoidalBias = sigmoidalBiasFactor;+ }++ if (Number.isFinite(saturationValue) && saturationValue !== 1) {+ modules.push(saturation);+ moduleProps.saturationValue = saturationValue;+ }+ }++ switch (bandCombination) {+ case 'normalizedDifference':+ modules.push(normalizedDifference);+ break;+ case 'enhancedVegetationIndex':+ modules.push(enhancedVegetationIndex);+ break;+ case 'soilAdjustedVegetationIndex':+ modules.push(soilAdjustedVegetationIndex);+ break;+ case 'modifiedSoilAdjustedVegetationIndex':+ modules.push(modifiedSoilAdjustedVegetationIndex);+ break;+ default:+ break;+ }++ if (isFilterAllowed(bandCombination) && filterEnabled) {+ modules.push(filter);+ moduleProps.filterMin1 = filterRange[0];+ moduleProps.filterMax1 = filterRange[1];+ }++ // Apply colormap+ if (isColormapAllowed(bandCombination) && images.imageColormap) {+ modules.push(colormapModule);+ moduleProps.minCategoricalBandValue = minCategoricalBandValue;+ moduleProps.maxCategoricalBandValue = maxCategoricalBandValue;+ moduleProps.dataTypeMaxValue = dtypeMaxValue[dataType];+ moduleProps.maxPixelValue = maxPixelValue;+ }++ return {modules, moduleProps};+}diff --git a/src/layers/src/raster-tile/image.ts b/src/layers/src/raster-tile/image.tsnew file mode 100644index 000000000..96f461532--- /dev/null+++ b/src/layers/src/raster-tile/image.ts@@ -0,0 +1,410 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++/**+ * Utility functions to create objects to pass to deck.gl-raster+ */++import {load} from '@loaders.gl/core';+import {QuantizedMeshLoader} from '@loaders.gl/terrain';+import memoize from 'lodash.memoize';++import {getLoaderOptions, RasterLayerResources as cdnUrls} from '@kepler.gl/constants';++import {CATEGORICAL_COLORMAP_ID} from './config';+import {+ loadNpyArray,+ generateCategoricalColormapTexture,+ loadImage,+ COLORMAP_TEXTURE_PARAMETERS+} from './gpu-utils';+import {CATEGORICAL_TEXTURE_WIDTH, generateCategoricalBitmapArray} from './raster-tile-utils';+import {+ GetTileDataProps,+ ImageData,+ TerrainData,+ AssetRequestData,+ CategoricalColormapOptions,+ ColormapImageData+} from './types';+import {+ getTitilerUrl,+ getTerrainUrl,+ getSingleCOGUrlParams,+ getStacApiUrlParams,+ getMeshMaxError+} from './url';++/**+ * Create dataUrl image from bitmap array+ * @param bitmap - RGBA byte bitmap array+ * @param width - width of the image+ * @param height - height of the image+ * @returns base64 data url of the image+ */+export function getImageDataURL(bitmap: Uint8Array, width: number, height: number): string | null {+ const canvas = document.createElement('canvas');+ if (!canvas.getContext) {+ return null;+ }+ canvas.setAttribute('width', `${width}`);+ canvas.setAttribute('height', `${height}`);+ const ctx = canvas.getContext('2d');+ if (!ctx) {+ return null;+ }+ for (let i = 0; i < width; i++) {+ const [r, g, b, a] = bitmap.subarray(i * 4, i * 4 + 4);+ if (a !== 0) {+ ctx.fillStyle = `rgb(${r}, ${g}, ${b})`;+ ctx.fillRect(i, 0, 1, 1);+ }+ }+ return canvas.toDataURL('image/png');+}++// Global multiplier to be applied to terrain mesh resolution+// A smaller number towards 0 will cause generation of mesh data with a smaller max error and thus a+// higher resolution mesh+const MESH_MULTIPLIER = 0.8;++/**+ * Load images required for raster tile+ * @param assetRequests - STAC asset requests+ * @param colormapId - colormap id+ * @param categoricalOptions - categorical options requred for making of categorical colormap+ * @returns images map+ */+export async function loadImages(+ assetRequests: AssetRequestData[],+ colormapId: string,+ categoricalOptions: CategoricalColormapOptions+): Promise<ImageData> {+ // We load image data (single or multi asset based on number of requests) and colormap in parallel+ const [stacImages, colormap] = await Promise.all([+ loadSTACImageData(assetRequests),+ (colormapId !== CATEGORICAL_COLORMAP_ID && memoizedLoadColormap(colormapId)) || null+ ]);+ let imageColormap = colormap;+ if (colormapId === CATEGORICAL_COLORMAP_ID) {+ imageColormap = generateCategoricalColormapTexture(categoricalOptions);+ }+ return {+ ...stacImages,+ imageColormap+ };+}++/**+ * Cache loading of colormap. Each colormap file is very small, on the order of a few hundred bytes,+ * so a global cache like this is not expected to eat up too much memory.+ * Most colormaps are loaded from CDN. In case colormapId is categorical, the image is created from+ * categorical colormap options.+ * NOTE: this implementation will cache promise rejections as well, but since these are small files+ * on the CDN, that's an acceptable risk for the time being.+ */+const memoizedLoadColormap: (colormapId: string) => Promise<ColormapImageData | null> | null =+ memoize((colormapId: string) => {+ if (colormapId === CATEGORICAL_COLORMAP_ID) {+ return null;+ } else {+ return loadColormap(colormapId);+ }+ });++/**+ * Load PNG colormap.+ * Since the colormap files are so small (couple hundred bytes), we load them always, even for an+ * RGB composite where the colormap won't be used. After the first load, the colormap should be+ * loaded from the disk cache. By loading always, we can prevent unnecessarily triggering+ * getTileData, which reduces flashing from reloading the images on the GPU.+ *+ * @param colormapId ID of colormap PNG+ *+ * @returns image object to pass to Texture2D constructor+ */+async function loadColormap(colormapId: string): Promise<ColormapImageData | null> {+ if (!colormapId) {+ return null;+ }++ const request = await getAssetRequest({url: cdnUrls.rasterColorMap(colormapId), options: {}});+ return loadImage(request.url, COLORMAP_TEXTURE_PARAMETERS, request.options);+}++/**+ * Get the request data for loading a single or multi asset STAC metadata object.+ * @param options STAC metadata object and user-defined parameters.+ * @returns Image data, or null if the STAC metadata object is not supported.+ */+export async function getSTACImageRequests(+ options: GetTileDataProps+): Promise<AssetRequestData[] | null> {+ const {loadAssetIds} = options;++ if (loadAssetIds.length === 1) {+ const request = await getSingleAssetSTACRequest(options);+ return request ? [request] : null;+ }++ return await getMultiAssetSTACRequest(options);+}++// TODO: image loading should really be _driven by the modules_+// E.g. the user chooses what kind of pipeline they want on the GPU, then from those modules we can+// see whether the user wants the pansharpened band, colormap, etc+/**+ * Load images defined by a STAC metadata object plus user-defined parameters+ */+export async function loadSTACImageData(requests: AssetRequestData[]): Promise<ImageData> {+ return requests.length === 1 ? loadSingleAssetSTAC(requests[0]) : loadMultiAssetSTAC(requests);+}++/**+ * Utility function that forms an asset request based on given parameters.+ */+export async function getAssetRequest({+ url,+ params,+ options,+ useMask = true,+ responseRequiredBandIndices+}: {+ url: string;+ params?: URLSearchParams | null;+ options: RequestInit;+ useMask?: boolean;+ responseRequiredBandIndices?: number[] | null;+}): Promise<AssetRequestData> {+ const requestUrl = url;+ const requestParams = params ?? new URLSearchParams();+ const requestOptions = options;++ const assetUrl = requestParams ? `${requestUrl}?${requestParams.toString()}` : requestUrl;+ return {url: assetUrl, options: requestOptions, useMask, responseRequiredBandIndices};+}++/**+ * Utility function for forming a request for a single asset STAC metadata object.+ */+// eslint-disable-next-line complexity, max-statements+async function getSingleAssetSTACRequest(+ options: GetTileDataProps+): Promise<AssetRequestData | null> {+ const {+ stac,+ loadAssetIds,+ loadBandIndexes,+ signal,+ useSTACSearching,+ index: {x, y, z},+ stacSearchProvider,+ startDate,+ endDate,+ _stacQuery+ } = options;++ const useMask = true;++ // Only a single URL because only a single asset+ let url: string | null = null;+ let urlParams: URLSearchParams | null = new URLSearchParams();+ let responseRequiredBandIndices: number[] | null = loadBandIndexes;+ if (useSTACSearching && stac.type !== 'Feature') {+ urlParams = getStacApiUrlParams({+ stac,+ stacSearchProvider,+ startDate,+ endDate,+ mask: useMask,+ loadAssetIds,+ _stacQuery+ });+ url = getTitilerUrl({stac, useSTACSearching, x, y, z});+ } else if (stac.type === 'Feature') {+ // stac is an Item+ urlParams = getSingleCOGUrlParams({+ stac,+ loadAssetId: loadAssetIds[0],+ loadBandIndexes,+ mask: useMask+ });+ url = getTitilerUrl({stac, useSTACSearching, x, y, z});+ responseRequiredBandIndices = null;+ } else {+ return null;+ }++ if (!url) {+ return null;+ }++ return await getAssetRequest({+ url,+ params: urlParams,+ options: {signal},+ useMask,+ responseRequiredBandIndices+ });+}++/**+ * Utility function for forming requests for a multi asset STAC metadata object.+ */+async function getMultiAssetSTACRequest(+ options: GetTileDataProps+): Promise<AssetRequestData[] | null> {+ const {+ stac,+ loadAssetIds,+ loadBandIndexes,+ signal,+ useSTACSearching,+ stacSearchProvider,+ startDate,+ endDate,+ index: {x, y, z},+ _stacQuery+ } = options;++ // Multiple urls, one for each asset+ let requestData: {url: string; params: URLSearchParams | null; options?: RequestInit}[] = [];++ // We assume that there's only one validity mask for the entire asset, and therefore any of the+ // requests would return the same validity bitmap. Therefore we only request a mask for the first+ // asset+ const requestMask: boolean[] = new Array(loadAssetIds.length).fill(false);+ requestMask[0] = true;++ if (useSTACSearching && stac.type !== 'Feature') {+ requestData = zip(loadAssetIds, requestMask).map(([assetId, mask]) => {+ const params = getStacApiUrlParams({+ stac,+ stacSearchProvider,+ startDate,+ endDate,+ mask,+ loadAssetIds: [assetId],+ _stacQuery+ });+ const url = getTitilerUrl({stac, useSTACSearching, x, y, z});+ return {url, params};+ });+ } else if (stac.type === 'Feature') {+ requestData = zip3(loadAssetIds, loadBandIndexes, requestMask).map(+ ([assetId, bandIndex, mask]) => {+ const params = getSingleCOGUrlParams({+ stac,+ loadAssetId: assetId,+ loadBandIndexes: [bandIndex],+ mask+ });+ const url = getTitilerUrl({stac, useSTACSearching, x, y, z});+ return {url, params};+ }+ );+ }++ if (isValidRequestData(requestData)) {+ return await Promise.all(+ requestData.map(request =>+ getAssetRequest({...request, options: request.options ?? {signal}})+ )+ );+ }++ return null;+}++/**+ * Load image data when consolidated in bands within a single STAC Asset+ */+async function loadSingleAssetSTAC(request: AssetRequestData): Promise<ImageData> {+ const imageBands = await loadNpyArray(request, true);++ if (!imageBands) {+ return {+ imageBands+ };+ }++ const imageMask = (request.useMask && imageBands.pop()) || null;+ let mappedImageBands = imageBands;+ if (request.responseRequiredBandIndices) {+ mappedImageBands = request.responseRequiredBandIndices.map(i => imageBands[i]);+ }+ return {imageBands: mappedImageBands, imageMask};+}++/**+ * Load image data when split among multiple STAC Assets+ */+async function loadMultiAssetSTAC(requests: AssetRequestData[]): Promise<ImageData> {+ const results = await Promise.all(requests.map(request => loadNpyArray(request, true)));+ // The first request includes a mask+ const imageMask = results[0]?.pop() || null;+ const imageBands = results.flat().filter(band => band !== null) as ImageData['imageBands'];+ return {imageBands, imageMask};+}++/**+ * Iterate over two arrays simultaneously, similar to Python's zip builtin+ */+function zip<T1, T2>(a: T1[], b: T2[]): [T1, T2][] {+ return a.map((k, i) => [k, b[i]]);+}++/**+ * Iterate over three arrays simultaneously, similar to Python's zip builtin+ */+function zip3<T1, T2, T3>(a: T1[], b: T2[], c: T3[]): [T1, T2, T3][] {+ return a.map((k, i) => [k, b[i], c[i]]);+}++/**+ * Type guard to check if all array elements are strings+ */+function isValidRequestData(+ arr: {url: unknown; params: unknown; options?: unknown}[]+): arr is {url: string; params: URLSearchParams; options: unknown}[] {+ return arr.every(x => typeof x?.url === 'string' && x?.params !== null);+}++/**+ * Create base64 image data url for categorical colormap+ * @param categoricalOptions - color map configuration and min-max values of categorical band+ * @returns base64 image data url+ */+export function getCategoricalColormapDataUrl(+ categoricalOptions: CategoricalColormapOptions+): string | null {+ const bitmap = generateCategoricalBitmapArray(categoricalOptions);+ if (!bitmap) {+ return null;+ }+ return getImageDataURL(bitmap, CATEGORICAL_TEXTURE_WIDTH, 1);+}++export function loadTerrain(props: {+ index: {x: number; y: number; z: number};+ signal: AbortSignal;+ rasterTileServerUrls: string[];+}): Promise<TerrainData> {+ const {+ index: {x, y, z},+ signal,+ rasterTileServerUrls+ } = props;++ const meshMaxError = getMeshMaxError(z, MESH_MULTIPLIER);+ const terrainUrl = getTerrainUrl(rasterTileServerUrls, x, y, z, meshMaxError);+ const loaderOptions = getLoaderOptions();++ return load(terrainUrl, QuantizedMeshLoader, {+ fetch: {signal},+ 'quantized-mesh': {+ ...loaderOptions['quantized-mesh'],+ skirtHeight: meshMaxError * 2+ }+ }) as Promise<TerrainData>;+}diff --git a/src/layers/src/raster-tile/raster-tile-icon.tsx b/src/layers/src/raster-tile/raster-tile-icon.tsxnew file mode 100644index 000000000..709779f18--- /dev/null+++ b/src/layers/src/raster-tile/raster-tile-icon.tsx@@ -0,0 +1,48 @@+// 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 RasterTileIcon extends Component {+ static propTypes = {+ height: PropTypes.string,+ colors: PropTypes.arrayOf(PropTypes.string.isRequired)+ };++ static defaultProps = {+ height: '16px',+ predefinedClassName: 'raster-tile-layer-icon',+ totalColor: 2,+ viewBox: '6 2 50 50'+ };++ render(): React.ReactNode {+ return (+ <Base {...this.props}>+ <g>+ <path+ d="M38.244 36.0125C39.0513 35.2052 39.0513 33.8928 38.244 33.0855C37.6319 32.4735 36.6666 32.3479 35.9007 32.6936L33.8528 30.6463L37.2683 27.2302C37.398 27.1004 37.4705 26.9252 37.4705 26.7423C37.4705 26.5595 37.398 26.3835 37.2683 26.2545L31.9001 20.887L32.8764 19.9113L34.3399 21.3755C34.4745 21.5101 34.6511 21.5777 34.8278 21.5777C35.0044 21.5777 35.181 21.5101 35.3156 21.3755L42.1459 14.5452C42.4157 14.2754 42.4157 13.8393 42.1459 13.5695L38.2433 9.66621C37.9735 9.39642 37.5374 9.39642 37.2676 9.66621L30.4366 16.4965C30.3069 16.6262 30.2344 16.8015 30.2344 16.9843C30.2344 17.1672 30.3069 17.3431 30.4366 17.4722L31.9001 18.9364L30.9244 19.912L28.4853 17.4729C28.0513 17.0389 27.4392 16.8091 26.7161 16.8091C25.2078 16.8091 23.2682 17.811 21.655 19.4242C20.5379 20.5413 19.7016 21.8199 19.3 23.0253C18.8439 24.3922 18.9874 25.539 19.703 26.2545L22.1428 28.6943L21.1671 29.6707L19.703 28.2065C19.5732 28.0768 19.398 28.0043 19.2151 28.0043C19.0323 28.0043 18.8563 28.0768 18.7273 28.2065L11.897 35.0368C11.7673 35.1658 11.6948 35.3411 11.6948 35.5246C11.6948 35.7082 11.7673 35.8834 11.897 36.0125L15.8003 39.9151C15.9349 40.0497 16.1115 40.1173 16.2882 40.1173C16.4648 40.1173 16.6414 40.0497 16.776 39.9151L23.6063 33.0855C23.736 32.9558 23.8085 32.7812 23.8085 32.5977C23.8085 32.4141 23.736 32.2389 23.6063 32.1098L22.1421 30.6463L23.1185 29.67L28.4853 35.0368C28.615 35.1665 28.7903 35.239 28.9731 35.239C29.156 35.239 29.3319 35.1665 29.4609 35.0368L32.8764 31.622L34.9182 33.663C34.5538 34.4317 34.6808 35.377 35.3156 36.0125C35.7061 36.403 36.2264 36.619 36.7798 36.619C37.3325 36.619 37.8527 36.403 38.244 36.0125Z"+ fill="currentColor"+ />+ <path+ d="M36.6146 39.3087C37.9049 39.3087 39.1173 38.8063 40.0281 37.8955C40.9402 36.984 41.4426 35.771 41.4419 34.48C41.4419 34.0985 41.1328 33.79 40.7519 33.79C40.3703 33.79 40.0619 34.0992 40.0619 34.48C40.0619 35.4026 39.7031 36.2692 39.0524 36.9199C38.4024 37.5699 37.5365 37.9287 36.6146 37.9287C36.6133 37.9287 36.6119 37.9287 36.6119 37.9287C36.231 37.9287 35.9219 38.2371 35.9219 38.618C35.9219 38.9995 36.2303 39.3087 36.6112 39.3087C36.6126 39.3087 36.6133 39.3087 36.6146 39.3087Z"+ fill="currentColor"+ />+ <path+ d="M36.6119 40.69C36.2303 40.69 35.9219 40.9985 35.9219 41.38C35.9219 41.7616 36.2303 42.07 36.6119 42.07C40.7974 42.07 44.2026 38.6649 44.2026 34.48C44.2026 34.0985 43.8941 33.79 43.5126 33.79C43.131 33.79 42.8226 34.0985 42.8226 34.48C42.8226 37.9045 40.0363 40.69 36.6119 40.69Z"+ fill="currentColor"+ />+ <path+ d="M46.2719 33.79C45.8903 33.79 45.5819 34.0985 45.5819 34.48C45.5819 39.426 41.5578 43.45 36.6119 43.45C36.2303 43.45 35.9219 43.7585 35.9219 44.14C35.9219 44.5216 36.2303 44.83 36.6119 44.83C42.3189 44.83 46.9619 40.187 46.9619 34.48C46.9619 34.0985 46.6534 33.79 46.2719 33.79Z"+ fill="currentColor"+ />+ </g>+ </Base>+ );+ }+}++export default RasterTileIcon;diff --git a/src/layers/src/raster-tile/raster-tile-layer-schema.ts b/src/layers/src/raster-tile/raster-tile-layer-schema.tsnew file mode 100644index 000000000..bef832901--- /dev/null+++ b/src/layers/src/raster-tile/raster-tile-layer-schema.ts@@ -0,0 +1,92 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import keyMirror from 'keymirror';++export const RasterTileLayerColorMaps = keyMirror({+ cfastie: null,+ rplumbo: null,+ schwarzwald: null,+ viridis: null,+ plasma: null,+ inferno: null,+ magma: null,+ cividis: null,+ greys: null,+ purples: null,+ blues: null,+ greens: null,+ oranges: null,+ reds: null,+ ylorbr: null,+ ylorrd: null,+ orrd: null,+ purd: null,+ rdpu: null,+ bupu: null,+ gnbu: null,+ pubu: null,+ ylgnbu: null,+ pubugn: null,+ bugn: null,+ ylgn: null,+ binary: null,+ gray: null,+ bone: null,+ pink: null,+ spring: null,+ summer: null,+ autumn: null,+ winter: null,+ cool: null,+ wistia: null,+ hot: null,+ afmhot: null,+ gist_heat: null,+ copper: null,+ piyg: null,+ prgn: null,+ brbg: null,+ puor: null,+ rdgy: null,+ rdbu: null,+ rdylbu: null,+ rdylgn: null,+ spectral: null,+ coolwarm: null,+ bwr: null,+ seismic: null,+ twilight: null,+ twilight_shifted: null,+ hsv: null,+ flag: null,+ prism: null,+ ocean: null,+ gist_earth: null,+ terrain: null,+ gist_stern: null,+ gnuplot: null,+ gnuplot2: null,+ cmrmap: null,+ cubehelix: null,+ brg: null,+ gist_rainbow: null,+ rainbow: null,+ jet: null,+ nipy_spectral: null,+ gist_ncar: null+});++export const RasterTileLayerPresets = keyMirror({+ trueColor: null,+ infrared: null,+ agriculture: null,+ forestBurn: null,+ ndvi: null,+ savi: null,+ msavi: null,+ ndmi: null,+ nbr: null,+ nbr2: null,+ singleBand: null+});diff --git a/src/layers/src/raster-tile/raster-tile-layer.ts b/src/layers/src/raster-tile/raster-tile-layer.tsnew file mode 100644index 000000000..c893fdf25--- /dev/null+++ b/src/layers/src/raster-tile/raster-tile-layer.ts@@ -0,0 +1,920 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {COORDINATE_SYSTEM, Layer as DeckLayer} from '@deck.gl/core/typed';+import {TileLayer, GeoBoundingBox} from '@deck.gl/geo-layers/typed';+import {PMTilesSource, PMTilesTileSource} from '@loaders.gl/pmtiles';+import {Texture2DProps} from '@luma.gl/webgl';+import memoize from 'lodash.memoize';+import {Matrix4} from 'math.gl';++import {PathLayer} from '@deck.gl/layers/typed';+import {DatasetType, PMTilesType, LAYER_TYPES} from '@kepler.gl/constants';+import {RasterLayer, RasterMeshLayer} from '@kepler.gl/deckgl-layers';+import {Layer, VisualChannel} from '@kepler.gl/layers';+import {+ LayerBaseConfig,+ LayerColorConfig,+ LayerHeightConfig,+ VisConfigColorRange,+ VisConfigSelection,+ VisConfigBoolean,+ VisConfigInput,+ VisConfigRange,+ VisConfigNumber+} from '@kepler.gl/types';++import {notNullorUndefined} from '@kepler.gl/common-utils';+import {Datasets, KeplerTable as KeplerDataset, VectorTileMetadata} from '@kepler.gl/table';+import {getApplicationConfig} from '@kepler.gl/utils';++import {rasterVisConfigs, PRESET_OPTIONS, DATA_SOURCE_COLOR_DEFAULTS} from './config';+import {getModules} from './gpu-utils';+import {getSTACImageRequests, loadImages, loadTerrain} from './image';+import RasterIcon from './raster-tile-icon';+import {+ getDataSourceParams,+ getMaxRequests,+ bboxIntersects,+ computeZRange,+ getSTACBounds,+ getEOBands,+ filterAvailablePresets,+ getSingleBandPresetOptions,+ getImageMinMax,+ getMinMaxFromTile2DHeaders+} from './raster-tile-utils';+import {+ GetTileDataCustomProps,+ GetTileDataDefaultProps,+ GetTileDataProps,+ RenderSubLayersProps,+ GetTileDataOutput,+ DataSourceParams,+ KeplerRasterDataset,+ Tile2DHeader,+ PresetOption,+ CompleteSTACObject+} from './types';++import {FindDefaultLayerPropsReturnValue} from '../layer-utils';+import {default as KeplerLayer, LayerBaseConfigPartial} from '../base-layer';++// Adjust tileSize to devicePixelRatio, but not higher than 2 to reduce requests to server+const devicePixelRatio: number = Math.min(+ 2,+ (typeof window !== 'undefined' && window.devicePixelRatio) || 1+);++// Global counter that represents the number of tiles currently being loaded across all the raster tile layers+let tilesBeingLoaded = 0;++// This is a temp solution, as we don't have proper logic to track loading of asycn layers atm in Kepler.gl+export const isTilesBeingLoaded = () => {+ return tilesBeingLoaded > 0;+};++export const LOAD_ELEVATION_AFTER_ZOOM = 8.9;++const getShouldLoadTerrain = (stac, mapState, visConfig) => {+ return Boolean(+ // we need a raster tile server for elevations even when we user PMTiles+ stac.rasterTileServerUrls?.length > 0 &&+ // check the switch from the layer configurator+ visConfig.enableTerrain &&+ // for now only in 3D mode, not top mode+ mapState.dragRotate &&+ getApplicationConfig().rasterServerSupportsElevation+ );+};++export type RasterTileLayerVisConfigCommonSettings = {+ opacity: VisConfigNumber;+ enableTerrain: VisConfigBoolean;+};++export type RasterTileLayerVisConfigSettings = RasterTileLayerVisConfigCommonSettings & {+ preset: VisConfigSelection;+ useSTACSearching: VisConfigBoolean;+ stacSearchProvider: VisConfigSelection;+ startDate: VisConfigInput;+ endDate: VisConfigInput;+ dynamicColor: VisConfigBoolean;+ colormapId: VisConfigSelection;+ colorRange: VisConfigColorRange;+ linearRescalingFactor: VisConfigRange;+ nonLinearRescaling: VisConfigBoolean;+ gammaContrastFactor: VisConfigNumber;+ sigmoidalContrastFactor: VisConfigNumber;+ sigmoidalBiasFactor: VisConfigNumber;+ saturationValue: VisConfigNumber;+ filterEnabled: VisConfigBoolean;+ filterRange: VisConfigRange;+ _stacQuery: VisConfigInput;+ singleBandName: VisConfigSelection;+};++export default class RasterTileLayer extends Layer {+ declare visConfigSettings: RasterTileLayerVisConfigSettings;++ /** Min bands image data value, based on the current viewport */+ minViewportPixelValue = Infinity;+ /** Max bands image data value, based on the current viewport */+ maxViewportPixelValue = -Infinity;+ /** Memoized method that calculates data source params */++ getDataSourceParams: (+ stac: CompleteSTACObject,+ preset: string,+ presetOptions: PresetOption+ ) => DataSourceParams | null;++ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types+ constructor(props) {+ super(props);+ this.registerVisConfig(rasterVisConfigs);++ // No two STAC ids may be the same within a single collection, though it's conceivably possible+ // across multiple STAC collections there could be an id collision. Note: STAC Collection+ // objects don't have a `collection` key; STAC Item objects have a `collection` key (which+ // matches the collection's `id`).+ const resolver = (stac: CompleteSTACObject, preset: string, presetOptions: PresetOption) =>+ `${stac.id}-${stac.collection}-${preset}-${presetOptions.singleBand?.assetId}-${presetOptions.singleBand?.bandIndex}`;+ this.getDataSourceParams = memoize(+ (stac, preset, presetOptions) => getDataSourceParams(stac, preset, presetOptions),+ resolver+ );++ this.meta = {zRange: null};+ }++ static findDefaultLayerProps(dataset: KeplerDataset): FindDefaultLayerPropsReturnValue {+ if (dataset.type !== DatasetType.RASTER_TILE) {+ return {props: []};+ }++ const newLayerProp = {+ dataId: dataset.id,+ label: dataset.label,+ isVisible: true+ };++ return {props: [newLayerProp]};+ }++ get type(): string {+ return LAYER_TYPES.rasterTile;+ }++ get name(): string {+ return 'Raster Tile';+ }++ get layerIcon(): KeplerLayer['layerIcon'] {+ return RasterIcon;+ }++ get supportedDatasetTypes(): DatasetType[] {+ return [DatasetType.RASTER_TILE];+ }++ get visualChannels(): Record<string, VisualChannel> {+ const {+ visConfig: {+ colorRange: {colorLegends: categoricalColorLegends}+ }+ } = this.config;+ if (categoricalColorLegends) {+ return {+ color: super.visualChannels.color+ };+ } else {+ return {};+ }+ }++ get requireData(): boolean {+ return true;+ }++ getDefaultLayerConfig(+ props: LayerBaseConfigPartial+ ): LayerBaseConfig & Partial<LayerColorConfig & LayerHeightConfig> {+ return {+ ...super.getDefaultLayerConfig(props),+ colorField: {+ analyzerType: '',+ name: 'Category',+ format: '',+ type: 'string',+ fieldIdx: -1,+ valueAccessor: () => '',+ displayName: 'Category'+ }+ };+ }++ getHoverData() {+ return null;+ }++ // We can render without columns, so we redefine this method+ shouldRenderLayer(): boolean {+ return Boolean(this.type && this.config.isVisible);+ }++ formatLayerData(datasets: Datasets, oldLayerData?: any) {+ const {dataId} = this.config;+ if (!notNullorUndefined(dataId)) {+ return {};+ }+ const dataset = datasets[dataId];++ // call updateData to updateLayerMeta+ const dataUpdateTriggers = this.getDataUpdateTriggers(dataset);+ const triggerChanged = this.getChangedTriggers(dataUpdateTriggers);++ if (triggerChanged && triggerChanged.getMeta) {+ this.updateLayerMeta(dataset as KeplerRasterDataset);+ }++ let tileSource: PMTilesTileSource | null = null;+ if (dataset.metadata?.pmtilesType === PMTilesType.RASTER) {+ const metadataUrl = dataset.metadata.metadataUrl;+ // use the old tile source if it exists and matches the new metadataUrl+ const {dataset: oldDataset, tileSource: oldTileSource} = oldLayerData || {};+ if (oldDataset === dataset && oldTileSource?.data === metadataUrl) {+ tileSource = oldLayerData.tileSource;+ } else {+ tileSource = metadataUrl ? PMTilesSource.createDataSource(metadataUrl, {}) : null;+ }+ }++ return {dataset, tileSource};+ }++ updateLayerMeta(dataset: KeplerRasterDataset): void {+ if (dataset.type !== DatasetType.RASTER_TILE) {+ return;+ }++ const stac = dataset.metadata;+ if (stac.pmtilesType === PMTilesType.RASTER) {+ return this.updateMeta({+ bounds: stac.bounds+ });+ }++ const bounds = getSTACBounds(stac);+ if (bounds) {+ // If a STAC Item (i.e. stac.type === 'Feature'), then set map bounds.+ // But we don't want to set bounds for Landsat or Sentinel because it would zoom out to zoom+ // 0, and we don't have low-zoom tiles.+ if (stac.type === 'Feature') {+ this.updateMeta({bounds});+ }+ }+ }++ /**+ * Run when adding a new dataset+ */+ setInitialLayerConfig(dataset: KeplerDataset): RasterTileLayer {+ const stac = dataset?.metadata as GetTileDataCustomProps['stac'];++ if (!stac) {+ return this;+ }++ if (stac.pmtilesType === PMTilesType.RASTER) {+ return this;+ }++ // When a STAC Collection is added, useStacSearching should be true to prevent a black screen+ this.updateLayerVisConfig({+ useSTACSearching: stac.type === 'Collection'+ });++ // Set singleBandName if there is only one band+ const availableBands = getEOBands(stac) || [];+ if (availableBands?.length === 1) {+ this.updateLayerVisConfig({singleBandName: availableBands[0].name});+ }++ // Change the preset if the default one is not allowed+ const availablePresets = filterAvailablePresets(stac, PRESET_OPTIONS);+ if (+ availablePresets?.length &&+ !availablePresets?.includes(this.visConfigSettings.preset.defaultValue as string)+ ) {+ this.updateLayerVisConfig({preset: availablePresets[0]});+ }++ // Apply improved image processing props only for non single band modes+ const preset = this.config.visConfig.preset;+ const colorDefaults = DATA_SOURCE_COLOR_DEFAULTS[stac.id];+ if (colorDefaults && preset !== 'singleBand') {+ this.updateLayerVisConfig(colorDefaults);+ }++ /*+ LandSat specifies temporal bounds but the valid data is after end date+ const updatedDates = timeRangeToStacTemporalInterval(+ stac as StacTypes.STACCollection,+ this.config.visConfig.startDate,+ this.config.visConfig.endDate+ );+ this.updateLayerVisConfig(updatedDates);+ */++ return this;+ }++ /**+ * Update zRange of viewport. This is necessary so that tiles in extruded mode are shown properly+ * at high elevations: https://github.com/foursquare/studio-monorepo/pull/1892/files+ * Derived from https://github.com/visgl/deck.gl/blob/8d824a4b836fee3bfebe6fc962e0f03d8c1dbd0d/modules/geo-layers/src/terrain-layer/terrain-layer.js#L173-L196+ *+ * @param tiles Array of tiles in current viewport+ */+ onViewportLoad(tiles: (Tile2DHeader | null)[]): void {+ this.updateMinMaxPixelValue(tiles);+ const newZRange = computeZRange(tiles);+ if (!newZRange) {+ return;+ }++ const {zRange} = this.meta;+ if (!zRange || newZRange[0] < zRange[0] || newZRange[1] > zRange[1]) {+ this.meta.zRange = newZRange;+ }+ }++ /**+ * Update viewport-related minPxelValue and maxPixelValue+ * @param tiles - deck.gl tiles+ */+ updateMinMaxPixelValue(tiles: (Tile2DHeader | null)[]): void {+ const [min, max] = getMinMaxFromTile2DHeaders(tiles);+ this.minViewportPixelValue = min;+ this.maxViewportPixelValue = max;+ }++ /**+ * Calculate min and max values of input raster band image properties+ * and update `minBandsValue` and `maxBandsValue`+ * @param images - raster band image properties+ * @return [min, max] pixel values for the tile+ */+ getMinMaxPixelValues(images: Texture2DProps[] | null): [number, number] {+ let minPixelValue = Infinity;+ let maxPixelValue = -Infinity;+ if (images) {+ for (const image of images) {+ const [min, max] = getImageMinMax(image.data);+ if (typeof min === 'number') {+ minPixelValue = Math.min(min, minPixelValue);+ }+ if (typeof max === 'number') {+ maxPixelValue = Math.max(max, maxPixelValue);+ }+ }+ }+ return [minPixelValue, maxPixelValue];+ }++ // generate a deck layer+ renderLayer(opts): TileLayer<any>[] {+ const {data} = opts;++ if (data?.dataset?.metadata?.pmtilesType === PMTilesType.RASTER) {+ return this.renderPMTilesLayer(opts);+ }+ return this.renderStacLayer(opts);+ }++ private renderStacLayer(opts): TileLayer<any>[] {+ const {data, mapState} = opts;+ const stac = data?.dataset?.metadata as GetTileDataCustomProps['stac'];++ // If a tabular dataset is loaded, and then the layer type is switched from Point to Raster Tile+ // layer, this `stac` object will exist but will not be raster metadata+ if (!stac || !stac.stac_version) {+ return [];+ }++ const {visConfig} = this.config;+ const {id, opacity, visible} = this.getDefaultDeckLayerProps(opts);++ const {+ preset,+ colormapId,+ linearRescaling,+ linearRescalingFactor,+ nonLinearRescaling,+ gammaContrastFactor,+ sigmoidalContrastFactor,+ sigmoidalBiasFactor,+ saturationValue,+ filterEnabled,+ filterRange,+ useSTACSearching,+ stacSearchProvider,+ startDate,+ endDate,+ _stacQuery,+ singleBandName,+ colorRange: {colorMap: categoricalColorMap},+ dynamicColor+ } = visConfig;++ const shouldLoadTerrain = getShouldLoadTerrain(stac, mapState, visConfig);++ if (new Date(endDate) < new Date(startDate)) {+ return [];+ }++ const {bandCombination} = PRESET_OPTIONS[preset];++ const singleBand = getSingleBandPresetOptions(stac, singleBandName);+ const dataSourceParams: DataSourceParams | null = this.getDataSourceParams(stac, preset, {+ singleBand+ });++ if (!dataSourceParams) {+ return [];+ }++ const {+ minZoom,+ maxZoom,+ globalBounds,+ loadAssetIds,+ loadBandIndexes,+ renderBandIndexes,+ dataType,+ minPixelValue: dsMinPixelValue,+ maxPixelValue: dsMaxPixelValue,+ minCategoricalBandValue,+ maxCategoricalBandValue+ } = dataSourceParams;++ const minPixelValue =+ !dynamicColor && Number.isFinite(dsMinPixelValue)+ ? dsMinPixelValue+ : this.minViewportPixelValue;+ const maxPixelValue =+ !dynamicColor && Number.isFinite(dsMaxPixelValue)+ ? dsMaxPixelValue+ : this.maxViewportPixelValue;++ const getTileDataCustomProps: GetTileDataCustomProps = {+ stac,+ loadAssetIds,+ loadBandIndexes,+ colormapId,+ shouldLoadTerrain,+ globalBounds,+ useSTACSearching,+ stacSearchProvider,+ startDate,+ endDate,+ _stacQuery,+ categoricalColorMap,+ minCategoricalBandValue,+ maxCategoricalBandValue+ };++ const tileLayer = new TileLayer<any, RenderSubLayersProps>({+ id,+ minZoom,+ maxZoom,+ tileSize: 512 / devicePixelRatio,+ getTileData: (args: any) => this.getTileData({...args, ...getTileDataCustomProps}),+ onViewportLoad: this.onViewportLoad.bind(this),+ // @ts-expect-error - TS doesn't know we'll pass appropriate props here+ renderSubLayers: renderSubLayersStac,+ maxRequests: getMaxRequests(stac.rasterTileServerUrls || []),+ // Passing visible on to TileLayer is necessary for split view to work+ visible,+ updateTriggers: {+ getTileData: [+ shouldLoadTerrain,+ // Colormap has changed+ colormapId,+ // assets to be loaded has changed+ ...loadAssetIds,+ // band indexes to be loaded has changed+ ...loadBandIndexes,+ useSTACSearching,+ stacSearchProvider,+ startDate,+ endDate,+ _stacQuery,+ categoricalColorMap,+ minCategoricalBandValue,+ maxCategoricalBandValue+ ]+ },+ // Props for 3D mode+ ...(shouldLoadTerrain+ ? {+ zRange: this.meta.zRange || null,+ refinementStrategy: 'no-overlap'+ }+ : {}),+ renderBandIndexes,+ opacity,+ linearRescaling,+ linearRescalingFactor,+ nonLinearRescaling,+ minPixelValue,+ maxPixelValue,+ gammaContrastFactor,+ sigmoidalContrastFactor,+ sigmoidalBiasFactor,+ saturationValue,+ bandCombination,+ filterEnabled,+ filterRange,+ dataType,+ minCategoricalBandValue,+ maxCategoricalBandValue,+ hasCategoricalColorMap: Boolean(categoricalColorMap)+ });++ return [tileLayer];+ }++ private renderPMTilesLayer(opts): TileLayer<any>[] {+ const {id, opacity, visible} = this.getDefaultDeckLayerProps(opts);++ const {data, mapState} = opts;+ const metadata = data?.dataset?.metadata as VectorTileMetadata;+ const {visConfig} = this.config;++ const tileSource = data.tileSource;+ const showTileBorders = false;+ const minZoom = metadata.minZoom || 0;+ const maxZoom = metadata.maxZoom || 30;++ const shouldLoadTerrain = getShouldLoadTerrain(metadata, mapState, visConfig);++ return [+ new TileLayer<any, RasterTileLayerVisConfigCommonSettings>({+ id,+ getTileData: (args: any) =>+ this.getTileDataPMTiles({...args, shouldLoadTerrain, metadata}, tileSource),++ // Assume the pmtiles file support HTTP/2, so we aren't limited by the browser to a certain number per domain.+ maxRequests: 20,++ pickable: true,+ autoHighlight: showTileBorders,++ onViewportLoad: this.onViewportLoad.bind(this),++ minZoom,+ maxZoom,+ tileSize: 512 / devicePixelRatio,+ zoomOffset: devicePixelRatio === 1 ? -1 : 0,+ // @ts-expect-error - TS doesn't know we'll pass appropriate props here+ renderSubLayers: renderSubLayersPMTiles,++ tileSource,+ showTileBorders,++ visible,+ opacity,++ updateTriggers: {+ getTileData: [shouldLoadTerrain]+ },+ // Props for 3D mode+ ...(shouldLoadTerrain+ ? {+ zRange: this.meta.zRange || null,+ refinementStrategy: 'no-overlap'+ }+ : {})+ })+ ];+ }++ async getTileData(props: GetTileDataProps): Promise<GetTileDataOutput> {+ const {+ shouldLoadTerrain,+ globalBounds,+ bbox: {west, south, east, north},+ index: {z: zoom}+ } = props;++ if (globalBounds && !bboxIntersects(globalBounds, [west, south, east, north])) {+ // tile is outside of STAC object's bounding box; don't fetch tile+ return null;+ }++ const assetRequests = await getSTACImageRequests(props);+ // No assets to load, most likely due to props being invalid/unsupported+ if (!assetRequests) {+ // We still issue the loadTerrain request if applicable+ const terrain =+ shouldLoadTerrain &&+ LOAD_ELEVATION_AFTER_ZOOM < zoom &&+ (await loadTerrain({+ index: props.index,+ signal: props.signal,+ rasterTileServerUrls: props.stac.rasterTileServerUrls || []+ }));+ return {images: null, ...(terrain ? {terrain} : {})};+ }++ tilesBeingLoaded++;++ // Wrap loading images into a try/catch because tiles that are only briefly in view will be+ // aborted. The `AbortController.abort()` creates a DOMException error.+ // Note that since loading is async, try/catch will only work when awaited inside the try block+ try {+ const [images, terrain] = await Promise.all([+ loadImages(assetRequests, props.colormapId, {+ colorMap: props.categoricalColorMap,+ minValue: props.minCategoricalBandValue,+ maxValue: props.maxCategoricalBandValue+ }),+ shouldLoadTerrain && LOAD_ELEVATION_AFTER_ZOOM < zoom+ ? loadTerrain({+ index: props.index,+ signal: props.signal,+ rasterTileServerUrls: props.stac.rasterTileServerUrls || []+ })+ : null+ ]);++ const [min, max] = this.getMinMaxPixelValues(images.imageBands);+ tilesBeingLoaded--;++ // Add terrain data only if we requested it above+ return {+ images,+ ...(terrain ? {terrain} : {}),+ minPixelValue: min,+ maxPixelValue: max+ };+ } catch (error) {+ tilesBeingLoaded--;+ if ((error as any).name === 'AbortError') {+ // tile was aborted+ return null;+ }++ // Some other unhandled error+ throw error;+ }+ }++ async getTileDataPMTiles(+ props: GetTileDataDefaultProps & {+ shouldLoadTerrain: boolean;+ metadata: {rasterTileServerUrls: string[]};+ globalBounds: DataSourceParams['globalBounds'];+ },+ tileSource+ ): Promise<any> {+ const {+ shouldLoadTerrain,+ globalBounds,+ bbox: {west, south, east, north},+ index: {z: zoom},+ metadata+ } = props;++ if (globalBounds && !bboxIntersects(globalBounds, [west, south, east, north])) {+ // tile is outside of STAC object's bounding box; don't fetch tile+ return null;+ }++ tilesBeingLoaded++;++ try {+ const [image] = await Promise.all([tileSource.getTileData(props)]);+ // For nwo check if the base tile exists, as this can cause loading to get stuck with "sparse" PMTiles.+ const terrain =+ image && shouldLoadTerrain && LOAD_ELEVATION_AFTER_ZOOM < zoom+ ? await loadTerrain({+ index: props.index,+ signal: props.signal,+ rasterTileServerUrls: metadata.rasterTileServerUrls+ })+ : null;++ let minPixelValue: number | null = null;+ let maxPixelValue: number | null = null;+ if (image) {+ const [min, max] = this.getMinMaxPixelValues([{data: image}]);+ minPixelValue = min;+ maxPixelValue = max;+ }+ tilesBeingLoaded--;++ // Add terrain data only if we requested it above+ return {image, minPixelValue, maxPixelValue, ...(terrain ? {terrain} : {})};+ } catch (error) {+ tilesBeingLoaded--;++ if ((error as Error).name === 'AbortError') {+ // tile was aborted+ return null;+ }++ // Some other unhandled error+ throw error;+ }+ }+}++function renderSubLayersStac(props: RenderSubLayersProps): DeckLayer<any> | DeckLayer<any>[] {+ const {+ tile,+ data,+ minPixelValue: globalMinPixelValue,+ maxPixelValue: globalMaxPixelValue+ } = props;+ const bbox = tile.bbox as GeoBoundingBox;+ const {west, south, east, north} = bbox;++ if (!data) {+ return [];+ }++ const {+ images,+ terrain,+ minPixelValue: tileMinPixelValue,+ maxPixelValue: tileMaxPixelValue+ } = data;+ if (!images || (Array.isArray(images) && images.length === 0)) {+ return [];+ }++ const minPixelValue = Number.isFinite(globalMinPixelValue)+ ? globalMinPixelValue+ : tileMinPixelValue;+ const maxPixelValue = Number.isFinite(globalMaxPixelValue)+ ? globalMaxPixelValue+ : tileMaxPixelValue;+ if (typeof minPixelValue !== 'number' || typeof maxPixelValue !== 'number') {+ return [];+ }++ const {modules, moduleProps} = getModules({+ images,+ props: {...props, minPixelValue, maxPixelValue}+ });++ return terrain+ ? new RasterMeshLayer(props, {+ id: `raster-3d-layer-${props.id}`,+ // Dummy data+ data: [1],+ mesh: terrain,+ images,+ modules,+ moduleProps,++ getPolygonOffset: null,+ coordinateSystem: COORDINATE_SYSTEM.CARTESIAN,+ modelMatrix: getMercatorModelMatrix(tile.index),+ getPosition: () => [0, 0, 0],+ // Color to use if surfaceImage is unavailable+ getColor: [255, 255, 255]+ // material: false+ })+ : new RasterLayer(props, {+ id: `raster-2d-layer-${props.id}`,+ images,+ modules,+ moduleProps,+ bounds: [west, south, east, north],+ _imageCoordinateSystem: COORDINATE_SYSTEM.CARTESIAN+ });+}++// From https://github.com/uber/deck.gl/blob/b1901b11cbdcb82b317e1579ff236d1ca1d03ea7/modules/geo-layers/src/mvt-tile-layer/mvt-tile-layer.js#L41-L52+export function getMercatorModelMatrix(tile: {x: number; y: number; z: number}): Matrix4 {+ const WORLD_SIZE = 512;+ const worldScale = Math.pow(2, tile.z);++ const xScale = WORLD_SIZE / worldScale;+ const yScale = -xScale;++ const xOffset = (WORLD_SIZE * tile.x) / worldScale;+ const yOffset = WORLD_SIZE * (1 - tile.y / worldScale);++ return new Matrix4().translate([xOffset, yOffset, 0]).scale([xScale, yScale, 1]);+}++function renderSubLayersPMTiles(props: {+ id: string;+ data: any;+ tileSource: any;+ showTileBorders: any;+ minZoom: any;+ maxZoom: any;+ tile: any;+}) {+ const {+ tileSource,+ showTileBorders,+ minZoom,+ maxZoom,+ tile: {+ index: {z: zoom},+ bbox: {west, south, east, north}+ }+ } = props;++ const layers: any[] = [];++ if (props.data?.image) {+ switch (tileSource.mimeType) {+ case 'image/png':+ case 'image/jpeg':+ case 'image/webp':+ case 'image/avif':+ layers.push(getRasterLayerPMTiles(props));+ break;++ default:+ break;+ }+ }++ // Debug tile borders+ const borderColor = zoom <= minZoom || zoom >= maxZoom ? [255, 0, 0, 255] : [0, 0, 255, 255];+ if (showTileBorders) {+ layers.push(+ new PathLayer({+ id: `${props.id}-border`,+ data: [+ [+ [west, north],+ [west, south],+ [east, south],+ [east, north],+ [west, north]+ ]+ ],+ getPath: d => d,+ getColor: borderColor as any,+ widthMinPixels: 4+ })+ );+ }++ return layers;+}++function getRasterLayerPMTiles(props: {id: string; data: any; tile: any}) {+ const {tile, data} = props;+ const bbox = tile.bbox as GeoBoundingBox;+ const {west, south, east, north} = bbox;++ if (!data?.image) {+ return [];+ }++ const images = {imageRgba: {data: data.image}};+ const {terrain} = data;++ const {modules, moduleProps} = getModules({+ images+ });++ return terrain+ ? new RasterMeshLayer(props, {+ id: `raster-3d-layer-${props.id}`,+ // Dummy data+ data: [1],+ mesh: terrain,+ images,+ modules,+ moduleProps,++ getPolygonOffset: null,+ coordinateSystem: COORDINATE_SYSTEM.CARTESIAN,+ modelMatrix: getMercatorModelMatrix(tile.index),+ getPosition: () => [0, 0, 0],+ // Color to use if surfaceImage is unavailable+ getColor: [255, 255, 255]+ // material: false+ })+ : new RasterLayer(props, {+ id: `raster-2d-layer-${props.id}`,+ data: null as never,+ images,+ modules,+ moduleProps,+ bounds: [west, south, east, north],+ _imageCoordinateSystem: COORDINATE_SYSTEM.CARTESIAN+ });+}diff --git a/src/layers/src/raster-tile/raster-tile-utils.ts b/src/layers/src/raster-tile/raster-tile-utils.tsnew file mode 100644index 000000000..31827f81a--- /dev/null+++ b/src/layers/src/raster-tile/raster-tile-utils.ts@@ -0,0 +1,693 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++/**+ * Utility functions and constants for processing STAC metadata and other raster tile data+ */++import {TypedArray} from '@loaders.gl/loader-utils/src/types';+import {isArray} from '@math.gl/core';++import {StacTypes} from '@kepler.gl/types';+import {hexToRgb} from '@kepler.gl/utils';++import {PRESET_OPTIONS, ZOOM_RANGES} from './config';+import {+ DataSourceParams,+ PresetData,+ CompleteSTACObject,+ CompleteSTACAssetLinks,+ AssetIds,+ BandIndexes,+ RenderBandIndexes,+ Tile2DHeader,+ AssetRequestInfo,+ PresetOption,+ BandCombination,+ CategoricalColormapOptions+} from './types';++type Item = StacTypes.STACItem;+type Collection = StacTypes.STACCollection;+type EOBand = StacTypes.Band;+type DataTypeOfTheBand = StacTypes.DataTypeOfTheBand;++export const CATEGORICAL_TEXTURE_WIDTH = 256;++export function isColormapAllowed(bandCombination: BandCombination): boolean {+ return bandCombination !== BandCombination.Rgb;+}++export function isRescalingAllowed(bandCombination: BandCombination): boolean {+ return bandCombination === BandCombination.Rgb || bandCombination === BandCombination.Single;+}++export function isFilterAllowed(bandCombination: BandCombination): boolean {+ return bandCombination !== BandCombination.Rgb;+}++/**+ * Max value for data type+ *+ * Values of null might be calculated in runtime+ */+export const dtypeMaxValue: Record<DataTypeOfTheBand, number | null> = {+ uint8: Math.pow(2, 8) - 1,+ uint16: Math.pow(2, 16) - 1,+ uint32: Math.pow(2, 32) - 1,+ uint64: null,+ int8: Math.pow(2, 7) - 1,+ int16: Math.pow(2, 15) - 1,+ int32: Math.pow(2, 31) - 1,+ int64: null,+ float16: null,+ float32: null,+ float64: null,+ cint16: null,+ cint32: null,+ cfloat32: null,+ cfloat64: null,+ other: null+};++/**+ * Is this a STAC Collection that supports custom searching+ * TODO: currently this is a custom hack to support Sentinel. It will need to be generalized before being accessible+ * @param stac STAC object+ * @return If True, supports searching+ */+export function isSearchableStac(stac: CompleteSTACObject): boolean {+ return stac.type === 'Collection';++ // return stac.type !== 'Feature' && stac?.providers.some(+ // provider =>+ // provider.name.toLowerCase() === 'microsoft' &&+ // provider.url === 'https://planetarycomputer.microsoft.com'+ // );+}++function getZoomRange(stac: CompleteSTACObject): [number, number] {+ if (ZOOM_RANGES[stac.id]) {+ return ZOOM_RANGES[stac.id];+ }++ // For a single COG, having a full zoom range isn't really a problem.+ // the /cog/info endpoint doesn't describe zoom levels because it doesn't know the projection to serve the image in.+ // Default minzoom, maxzoom: [0, 20]+ return [0, 20];+}++/**+ * Infer data type from STAC item+ * This uses the `raster` extension, which is not yet very common+ * @param stac stac object+ * @return Data type of the band+ */+function getDataType(+ usableAssets: CompleteSTACAssetLinks,+ typesToCheck?: string[]+): DataTypeOfTheBand | null {+ const dataTypes = new Set<DataTypeOfTheBand>();+ for (const assetName in usableAssets) {+ const asset = usableAssets[assetName];+ if (!asset) {+ continue;+ }+ if (typesToCheck && !typesToCheck.includes(assetName)) {+ continue;+ }++ asset['raster:bands']?.forEach(band => band.data_type && dataTypes.add(band.data_type));+ }++ // TODO: support multiple data types across assets+ if (dataTypes.size !== 1) {+ return null;+ }++ return Array.from(dataTypes)[0];+}++/**+ * Get min and max values of the band from the band's data type values range+ * @param stac - stac metadata object+ * @param dtype - data type of the raster band+ * @returns min and max values for the band+ */+function getPixelRange(+ stac: CompleteSTACObject,+ dtype: DataTypeOfTheBand | null,+ [minRasterStatsValue, maxRasterStatsValue]: [number | undefined, number | undefined]+): [number, number] | null {+ if (!dtype) {+ return null;+ }++ // TODO: might not always be desired to leave min pixel value at 0+ const minPixelValue = 0;+ const maxPixelValue = dtypeMaxValue[dtype];++ // TODO check if this early return is expected+ if (!maxPixelValue) {+ return null;+ }++ if (+ !Number.isFinite(maxPixelValue) &&+ typeof minRasterStatsValue === 'number' &&+ typeof maxRasterStatsValue === 'number'+ ) {+ return [minRasterStatsValue, maxRasterStatsValue];+ }++ return [minPixelValue, maxPixelValue];+}++/**+ * Find min and max values throughout a raster band image+ * @param imageData raster band image data+ * @returns min and max values throughout the band+ */+export function getImageMinMax(imageData: TypedArray): [number | null, number | null] {+ // We cannot calculate min/max for Image/ImageBitmap+ if (!isArray(imageData)) {+ return [null, null];+ }+ let min = Infinity;+ let max = -Infinity;+ for (const value of imageData) {+ if (!isNaN(value)) {+ min = Math.min(min, value);+ max = Math.max(max, value);+ }+ }+ return [min, max];+}++/**+ * Find asset names and band indexes for given commonNames+ * Right now in Kepler, our available methods of rendering raster data are quite simple. We only allow a user to choose among `presets`, and under the hood we select which data files to load.+ * @param usableAssets [stac description]+ * @param commonNames an array of eo:common_name to search within bands+ * @return Band information or null if not all bands exist+ */+export function getBandIdsForCommonNames(+ stac: CompleteSTACObject,+ usableAssets: CompleteSTACAssetLinks,+ commonNames: string[]+): AssetRequestInfo | null {+ // An array of strings describing asset identifiers, e.g. the "key" in the assets object+ const assetIds: AssetIds = [];+ // An array of integers giving the index of the band within that asset's data+ // In general, data sources either have a single band per asset or all assets in a single band+ const bandIndexes: BandIndexes = [];++ for (const commonName of commonNames) {+ // Find asset that includes a band with this common name+ const result = findAssetWithName(usableAssets, commonName, 'common_name');+ if (result) {+ const [assetName, bandIndex] = result;+ assetIds.push(assetName);+ bandIndexes.push(bandIndex);+ }+ }++ if (assetIds.length === 0) {+ return null;+ }++ const {loadAssetIds, loadBandIndexes, renderBandIndexes} = consolidateBandIndexes(+ stac,+ assetIds,+ bandIndexes+ );++ return {loadAssetIds, loadBandIndexes, renderBandIndexes};+}++/**+ * Consolidate asset/band info into load info and render info+ * @param stac STAC object+ * @param assetIds+ * @param bandIndexes+ * @return Asset information+ */+function consolidateBandIndexes(+ stac: CompleteSTACObject,+ assetIds: AssetIds,+ bandIndexes: BandIndexes+): AssetRequestInfo {+ let loadAssetIds: AssetIds;+ let loadBandIndexes: BandIndexes;+ let renderBandIndexes: RenderBandIndexes;++ // All bands we want to load are in a single asset+ if (assetIds.length === 1 || Array.from(new Set(assetIds)).length === 1) {+ loadAssetIds = [assetIds[0]];++ const asset = getAssets(stac)[loadAssetIds[0]];++ // Request all bands in order, then reorder with renderBandIndexes+ // NOTE: usually (e.g. with NAIP) if all bands are in a single asset, then there are only ~4+ // bands. This approach of requesting all bands may be less efficient with more than 4 bands+ // (because of larger download sizes). In the future may want separate loading paths if we+ // encounter single asset objects with > 4 bands.++ // TODO: eo:bands can be _either_ on each asset _or_ on the STAC's properties, which is not currently handled+ loadBandIndexes = (asset['eo:bands'] as EOBand[])?.map((_, idx) => idx);+ renderBandIndexes = bandIndexes;+ } else {+ loadAssetIds = assetIds;+ loadBandIndexes = bandIndexes;+ renderBandIndexes = null;+ }++ return {loadAssetIds, loadBandIndexes, renderBandIndexes};+}++/**+ * Find the Asset in the STAC object that containing an eo:band with the desired common_name or name+ * @param assets assets object. Keys should be asset name and values should be asset data+ * @param name name or common_name in eo:bands+ * @param property 'name' or 'common_name'+ * @return name of asset containing desired band, index of band within asset+ */+export function findAssetWithName(+ assets: CompleteSTACAssetLinks,+ name: string,+ property: 'name' | 'common_name'+): [string, number] | null {+ for (const [assetName, assetData] of Object.entries(assets)) {+ const eoBands = assetData['eo:bands'] || [];++ // Search bands array of this asset+ for (let i = 0; i < eoBands.length; i++) {+ const eoBand = eoBands[i];+ if (name === eoBand[property]) {+ return [assetName, i];+ }+ }+ }++ return null;+}++function getSingleBandInfo(+ usableAssets: CompleteSTACAssetLinks,+ singleBandInfo: PresetOption['singleBand']+): AssetRequestInfo | null {+ if (!singleBandInfo?.assetId && Object.keys(usableAssets).length === 1) {+ singleBandInfo = {+ assetId: Object.keys(usableAssets)[0]+ };+ }++ if (!singleBandInfo) {+ return null;+ }++ const {assetId, bandIndex} = singleBandInfo;+ if (!Object.keys(usableAssets).includes(assetId)) {+ return null;+ }++ return {+ loadAssetIds: [assetId],+ loadBandIndexes: bandIndex ? [bandIndex] : [0],+ // No reordering of bands on the GPU+ renderBandIndexes: null+ };+}++export function getRasterStatisticsMinMax(+ stac: CompleteSTACObject,+ presetId: string,+ singleBandInfo: PresetOption['singleBand']+): [number | undefined, number | undefined] {+ if (presetId !== 'singleBand') {+ return [undefined, undefined];+ }+ let minCategoricalBandValue: number | undefined;+ let maxCategoricalBandValue: number | undefined;+ const usableAssets = getUsableAssets(stac);+ let bandMetadata = usableAssets[singleBandInfo?.assetId || ''];+ if (!bandMetadata) {+ const assetId = Object.keys(usableAssets)[0];+ bandMetadata = usableAssets[assetId];+ }++ if (bandMetadata) {+ minCategoricalBandValue = bandMetadata['raster:bands']?.[0].statistics?.minimum;+ maxCategoricalBandValue = bandMetadata['raster:bands']?.[0].statistics?.maximum;+ }++ return [minCategoricalBandValue, maxCategoricalBandValue];+}++export function getSingleBandPresetOptions(+ stac: CompleteSTACObject,+ singleBandName: string+): PresetOption['singleBand'] {+ const usableAssets = getUsableAssets(stac);+ let assetData = findAssetWithName(usableAssets, singleBandName, 'name');+ if (!assetData) {+ // some collections have common_name instead of name+ assetData = findAssetWithName(usableAssets, singleBandName, 'common_name');+ }+ let singleBand: PresetOption['singleBand'];+ if (assetData) {+ const [assetId, bandIndex] = assetData;+ singleBand = {assetId, bandIndex};+ }+ return singleBand;+}++/**+ * Calculate viewport-related min and max raster values from loaded tiles+ * @param tiles - deck.gl tiles+ * @returns [minPixelValue, maxPixelValue]+ */+export function getMinMaxFromTile2DHeaders(tiles: (Tile2DHeader | null)[]): [number, number] {+ const minValues: (number | null)[] = tiles+ .map((tile: Tile2DHeader | null) => {+ if (!tile || !tile.data) {+ return null;+ }+ const {minPixelValue} = tile.data;+ return minPixelValue;+ })+ .filter(value => value);+ const maxValues: (number | null)[] = tiles+ .map((tile: Tile2DHeader | null) => {+ if (!tile || !tile.data) {+ return null;+ }+ const {maxPixelValue} = tile.data;+ return maxPixelValue;+ })+ .filter(value => value);+ // We can cast to number[] because we have filtered null values+ return [Math.min(...(minValues as number[])), Math.max(...(maxValues as number[]))];+}++/**+ * Get loading params+ * @param stac STAC Object+ * @param preset Preset for display+ * @return Parameters for loading data+ */+export function getDataSourceParams(+ stac: CompleteSTACObject,+ presetId: string,+ presetOptions?: PresetOption+): DataSourceParams | null {+ const usableAssets = getUsableAssets(stac);+ const {commonNames, bandCombination} = PRESET_OPTIONS[presetId];+ const [minZoom, maxZoom] = getZoomRange(stac);++ let bandInfo: AssetRequestInfo | null = null;+ if (bandCombination === 'single') {+ bandInfo = getSingleBandInfo(usableAssets, presetOptions?.singleBand);+ } else if (commonNames) {+ bandInfo = getBandIdsForCommonNames(stac, usableAssets, commonNames);+ } else {+ return null;+ }++ if (!bandInfo) return null;++ const dtype = getDataType(usableAssets, bandInfo.loadAssetIds);+ const [minRasterStatsValue, maxRasterStatsValue] = getRasterStatisticsMinMax(+ stac,+ presetId,+ presetOptions?.singleBand+ );+ const pixelRange = getPixelRange(stac, dtype, [minRasterStatsValue, maxRasterStatsValue]);++ if (!pixelRange || !dtype) {+ return null;+ }++ const [minPixelValue, maxPixelValue] = pixelRange;+ const {loadAssetIds, loadBandIndexes, renderBandIndexes} = bandInfo;+ const globalBounds = getSTACBounds(stac);++ return {+ loadAssetIds,+ loadBandIndexes,+ renderBandIndexes,+ minZoom,+ maxZoom,+ minPixelValue,+ maxPixelValue,+ // use min and max statistics values for categorical re-scale+ minCategoricalBandValue: minRasterStatsValue,+ maxCategoricalBandValue: maxRasterStatsValue,+ globalBounds,+ dataType: dtype+ };+}++/**+ * Get Bounding Box of STAC object+ * @param stac+ * @return bounding box+ */+export function getSTACBounds(stac: Item | Collection): number[] | null {+ // Check if Item+ if (stac.type === 'Feature') {+ // bbox may be missing+ return stac.bbox || null;+ }++ // Then must be collection+ return stac.extent.spatial.bbox[0];+}++/**+ * Get all eo:band objects from STAC object+ * @param stac STAC object+ * @return array of eo:band objects or null+ */+export function getEOBands(stac: CompleteSTACObject): EOBand[] | null {+ const assets = getAssets(stac);+ if (!assets) {+ return null;+ }++ const eoBands: EOBand[] = [];+ for (const data of Object.values(assets)) {+ const assetBands = data['eo:bands'];+ if (Array.isArray(assetBands) && assetBands.length > 0) {+ eoBands.push(...assetBands);+ }+ }++ return eoBands;+}++/**+ * Filter presets by those that can be used with the given stac item+ * Each preset has a commonNames key, which is a list of eo `common_name` values. Some STAC items+ * may not have assets that span all of these combinations of `common_name`, so this filters the+ * input array.+ * @param stac STAC object+ * @param presetData object with requirements for each preset+ * @return An array of preset option ids that can be used with the given STAC item+ */+export function filterAvailablePresets(+ stac: CompleteSTACObject,+ presetData: Record<string, PresetData>+): string[] | null {+ if (!stac) {+ return null;+ }++ const eoBands = getEOBands(stac);++ if (!eoBands) {+ return null;+ }++ const availablePresetIds: string[] = [];+ for (const preset of Object.values(presetData)) {+ const {commonNames, bandCombination} = preset;++ // True if all required common names of preset exist in STAC object+ const allBandsExist = commonNames?.every(commonName =>+ eoBands.some(eoBand => eoBand.common_name === commonName)+ );++ if (allBandsExist || bandCombination === 'single') {+ availablePresetIds.push(preset.id);+ }+ }++ return availablePresetIds;+}++// TODO: would be better to have a generic here to relax the requirement that all input STACs have all extensions we list.+export function getAssets(stac: CompleteSTACObject): CompleteSTACAssetLinks {+ // stac is an Item+ if (stac.type === 'Feature') {+ return stac.assets;+ }++ // stac is a Collection+ // A STAC Collection optionally contains (if it includes the Item Assets Extension) an item_assets+ // key that describes the assets included in every Item in the Collection.+ return stac.item_assets;+}++/**+ * Find usable assets in main assets object+ * The `assets` object can point to many different objects, including original XML or JSON metadata,+ * thumbnails, etc. These aren't usable as image data in Studio.+ * @param stac STAC object+ * @return asset mapping including only assets usable in Studio+ */+export function getUsableAssets(stac: CompleteSTACObject): CompleteSTACAssetLinks {+ const allAssets = getAssets(stac);+ const usableAssets = {};+ for (const assetName in allAssets) {+ const assetData = allAssets[assetName];++ // We don't require data assets to have the "data" role, but if the asset has a non-data role,+ // we exclude it+ if (+ Array.isArray(assetData.roles) &&+ assetData.roles.some(role =>+ ['thumbnail', 'overview', 'metadata', 'visual'].includes(role)+ ) &&+ !assetData.roles.includes('data')+ ) {+ continue;+ }++ // raster:bands array exists+ if (assetData['raster:bands']) {+ usableAssets[assetName] = assetData;+ }++ // TODO: Could also mark the asset as unusable if the asset has a non-GeoTIFF or COG Media Type+ }++ return usableAssets;+}++/**+ * Get the max number of requests the TileLayer should be able to send at once+ * With HTTP 1 under Chrome, you can make a max of 6 concurrent requests per domain, so this number+ * should be 6 * number of domains tiles are loaded from.+ * @param stac STAC object+ * @return Number of permissible concurrent requests+ */+export function getMaxRequests(rasterTileServerUrls: []): number {+ return (rasterTileServerUrls.length || 1) * 6;+}++/**+ * Determine if two axis-aligned boxes intersect+ * @param bbox1 axis-aligned box+ * @param bbox2 axis-aligned box+ * @return true if boxes intersect+ */+export function bboxIntersects(bbox1: number[], bbox2: number[]): boolean {+ if (!Array.isArray(bbox1) || bbox1.length !== 4 || !Array.isArray(bbox2) || bbox2.length !== 4) {+ // Invalid input; can't make determination+ return true;+ }++ return !(+ bbox2[0] > bbox1[2] ||+ bbox2[2] < bbox1[0] ||+ bbox2[3] < bbox1[1] ||+ bbox2[1] > bbox1[3]+ );+}++/**+ * Compute zRange of tiles in viewport.+ * Derived from https://github.com/visgl/deck.gl/blob/8d824a4b836fee3bfebe6fc962e0f03d8c1dbd0d/modules/geo-layers/src/terrain-layer/terrain-layer.js#L173-L196+ * @param tiles Array of tiles in current viewport+ */+export function computeZRange(tiles: (Tile2DHeader | null)[] | null): [number, number] | null {+ // Abort if no tiles visible+ if (!tiles) {+ return null;+ }++ // Since getTileData returns an object with either {terrain, images} in 3d mode or {images} in+ // 2D mode, we can grab that object using tile.content.terrain, then grab the bounding box from+ // the header.+ const ranges: [number, number][] = tiles+ .map(tile => tile?.content?.terrain?.header?.boundingBox)+ .flatMap(bounds => (bounds ? [[bounds[0][2], bounds[1][2]]] : []));++ if (ranges.length === 0) {+ return null;+ }++ const minZ = Math.min(...ranges.map(x => x[0]));+ const maxZ = Math.max(...ranges.map(x => x[1]));++ return [minZ, maxZ];+}++/**+ * Create RGBA bitmap array for categorical color scale from categorical color map+ * @param categoricalOptions - color map configuration and min-max values of categorical band+ * @returns typed array with bitmap data+ */+export function generateCategoricalBitmapArray(+ categoricalOptions: CategoricalColormapOptions+): Uint8Array | null {+ const {colorMap, minValue = 0, maxValue = CATEGORICAL_TEXTURE_WIDTH - 1} = categoricalOptions;+ if (!colorMap) {+ return null;+ }+ const colorScaleMaxValue = maxValue - minValue;+ const step = CATEGORICAL_TEXTURE_WIDTH / (colorScaleMaxValue + 1);+ const data = new Uint8Array(CATEGORICAL_TEXTURE_WIDTH * 4);+ for (const [value, color] of colorMap) {+ if (typeof value !== 'number') {+ continue;+ }+ const rgb = hexToRgb(color);+ const minPixel = Math.floor(value * step);+ const maxPixel = Math.floor((value + 1) * step - 1);+ for (let i = minPixel; i <= maxPixel; i++) {+ data.set([...rgb, CATEGORICAL_TEXTURE_WIDTH - 1], i * 4);+ }+ }+ return data;+}++export function timeRangeToStacTemporalInterval(+ stac: StacTypes.STACCollection,+ startDate: string,+ endDate: string+): {startDate: string; endDate: string} {+ // TODO support multiple temporal intervals+ const interval = stac.extent?.temporal?.interval?.[0];+ if (!interval || !interval[0]) return {startDate, endDate};++ const collectionStart = interval[0] ? new Date(interval[0]) : new Date();+ const collectionEnd = interval[1] ? new Date(interval[1]) : new Date();++ const layerStart = new Date(startDate);+ const layerEnd = new Date(endDate);++ const clippedStart =+ layerStart < collectionStart || layerStart > collectionEnd ? collectionStart : layerStart;+ let clippedEnd = layerEnd > collectionEnd ? collectionEnd : layerEnd;+ if (clippedStart > clippedEnd) {+ clippedEnd = new Date(clippedStart);+ clippedEnd.setDate(clippedEnd.getDate() + 1);+ }++ return {+ startDate: clippedStart.toISOString().split('T')[0],+ endDate: clippedEnd.toISOString().split('T')[0]+ };+}diff --git a/src/layers/src/raster-tile/types.ts b/src/layers/src/raster-tile/types.tsnew file mode 100644index 000000000..6f99a0797--- /dev/null+++ b/src/layers/src/raster-tile/types.ts@@ -0,0 +1,261 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {_Tile2DHeader} from '@deck.gl/geo-layers/typed';+import {TypedArray} from '@loaders.gl/loader-utils/src/types';+import {Texture2DProps} from '@luma.gl/webgl';++import {KeplerTable as KeplerDataset} from '@kepler.gl/table';+import type {ColorMap, StacTypes} from '@kepler.gl/types';++export type STACObject = StacTypes.STACObject;+export type CompleteSTACItem = StacTypes.CompleteSTACItem;+export type CompleteSTACCollection = StacTypes.CompleteSTACCollection;+export type CompleteSTACObject = StacTypes.CompleteSTACObject;++type DataTypeOfTheBand = StacTypes.DataTypeOfTheBand;++export type CompleteSTACAssetLinks =+ | CompleteSTACItem['assets']+ | CompleteSTACCollection['item_assets'];++export enum BandCombination {+ /** Render a single raster band. */+ Single = 'single',++ /** Render three bands together on the GPU.+ * Note that this could be "true color" if the first band is red, the second is green, and the+ * third is blue. But it could also be "false color" if the three bands chosen are not RGB.+ */+ Rgb = 'rgb',++ /** Calculate the Normalized Difference Vegetation Index (NDVI) on two bands. */+ NormalizedDifference = 'normalizedDifference',++ /** Calculate the EVI on two bands. */+ EnhancedVegetationIndex = 'enhancedVegetationIndex',++ /** Calculate SAVI on two bands. */+ SoilAdjustedVegetationIndex = 'soilAdjustedVegetationIndex',++ /** Calculate MSAVI on two bands. */+ ModifiedSoilAdjustedVegetationIndex = 'modifiedSoilAdjustedVegetationIndex'+}++/**+ * Identifiers of STAC assets to load+ *+ * These identifiers should match the keys of the STAC assets object. Refer to+ * https://github.com/radiantearth/stac-spec/blob/master/item-spec/item-spec.md#assets+ */+export type AssetIds = string[];++/**+ * Indexes of bands within each asset that should be loaded.+ *+ * Each asset refers to a single Cloud-Optimized GeoTIFF file on disk. Each COG has a width, height,+ * and one or more bands.+ *+ * This array of indexes must be the same length as the list of AssetIds. The nth band index+ * describes the which index from the nth asset id to load.+ */+export type BandIndexes = number[];++/**+ * An array of integers representing how bands should be reordered on the GPU. This allows for+ * faster changing of bands for single-asset raster data, such as NAIP. Will be null for multi-asset+ * STAC items.+ */+export type RenderBandIndexes = BandIndexes | null;++export type AssetRequestInfo = {+ loadAssetIds: AssetIds;+ loadBandIndexes: BandIndexes;+ renderBandIndexes: RenderBandIndexes;+};++/** User-provided information for how to render a preset */+export type PresetOption = {+ singleBand?: {+ assetId: string;+ bandIndex?: number;+ };+};++export type DataSourceParams = AssetRequestInfo & {+ minZoom: number;+ maxZoom: number;+ minPixelValue: number;+ maxPixelValue: number;+ minCategoricalBandValue?: number;+ maxCategoricalBandValue?: number;+ globalBounds: number[] | null;+ dataType: DataTypeOfTheBand;+};++export type CategoricalColormapOptions = {+ colorMap?: ColorMap;+ minValue?: number;+ maxValue?: number;+};++export type ExtendedKeplerSTAC = {rasterTileServerUrls?: []};++/**+ * Custom fields we pass on to the getTileData callback+ * Anything required to know _what data to load_ should be passed in here.+ */+export type GetTileDataCustomProps = Pick<AssetRequestInfo, 'loadAssetIds' | 'loadBandIndexes'> & {+ stac: CompleteSTACObject & ExtendedKeplerSTAC;+ colormapId: string;+ categoricalColorMap: ColorMap;+ minCategoricalBandValue?: number;+ maxCategoricalBandValue?: number;++ /** If true, fetch terrain data as well as texture data. */+ shouldLoadTerrain: boolean;+ globalBounds: number[] | null;+ useSTACSearching: boolean;+ stacSearchProvider: string;+ startDate: string;+ endDate: string;++ /* Stringified JSON representing a STAC API Search Query the backend should request from a server. */+ _stacQuery?: string;+};++/** Properties provided to getTileData by the deck.gl TileLayer */+export interface GetTileDataDefaultProps {+ index: {x: number; y: number; z: number};+ url?: string;++ /** Bounding box of the current web mercator tile. */+ bbox: {west: number; north: number; east: number; south: number};++ /** AbortSignal used for cancelling requests */+ signal: AbortSignal;+}++/** Properties passed into the deck.gl TileLayer getTileData callback */+export type GetTileDataProps = GetTileDataCustomProps & GetTileDataDefaultProps;++export type GetTileDataOutput = any | null;++export type RenderSubLayersCustomProps = ColorRescaling &+ Pick<AssetRequestInfo, 'renderBandIndexes'> & {+ opacity: number;+ linearRescaling: boolean;+ linearRescalingFactor: [number, number];+ nonLinearRescaling: boolean;+ minPixelValue: number;+ maxPixelValue: number;+ bandCombination: BandCombination;+ filterEnabled: boolean;+ filterRange: [number, number];+ currentTime: number;+ dataType: DataTypeOfTheBand;+ minCategoricalBandValue?: number;+ maxCategoricalBandValue?: number;+ hasCategoricalColorMap: boolean;+ };++export interface RenderSubLayersDefaultProps {+ id: string;+ tile: _Tile2DHeader<GetTileDataOutput>;+ data: GetTileDataOutput;+}++export type RenderSubLayersProps = RenderSubLayersCustomProps & RenderSubLayersDefaultProps;++/** Required information on how to render each preset */+export interface PresetData {+ label: string;+ id: string;+ bandCombination: BandCombination;+ commonNames?: string[];+ description?: string;+ infoUrl?: string;+}++export interface ColorRescaling {+ gammaContrastFactor: number;+ sigmoidalContrastFactor: number;+ sigmoidalBiasFactor: number;+ saturationValue: number;+}++export interface ConfigOption {+ label: string;+ id: string;+}++export type ColormapImageData = Texture2DProps;++export interface ImageData {+ imageBands: Texture2DProps[] | null;+ imageColormap?: Texture2DProps | null;+ imageMask?: Texture2DProps | null;+ imagePan?: Texture2DProps | null;+ imageRgba?: Texture2DProps | null;+}++export type AssetRequestData = {+ url: string;+ options: RequestInit;+ useMask: boolean;+ /** Pass this property through the request to pick specific bands from the response */+ responseRequiredBandIndices?: number[] | null;+};++export type NPYLoaderDataTypes =+ | Uint8Array+ | Uint8ClampedArray+ | Int8Array+ | Uint16Array+ | Int16Array+ | Uint32Array+ | Int32Array+ | Float32Array+ | Float64Array;++export interface NPYLoaderResponse {+ data: NPYLoaderDataTypes;+ header: {+ descr: string;+ shape: number[];+ };+}++export interface TerrainData {+ header: {+ vertexCount: number;+ boundingBox: [[number, number, number], [number, number, number]];+ };+ mode: 4;+ indices: {+ value: TypedArray;+ size: 1;+ };+ attributes: {+ POSITION: {+ value: TypedArray;+ size: 3;+ };+ TEXCOORD_0: {+ value: TypedArray;+ size: 2;+ };+ };+}++export type KeplerRasterDataset = KeplerDataset & {+ metadata: CompleteSTACObject;+};++export interface Tile2DHeader {+ content?: {+ terrain?: TerrainData;+ images: GetTileDataOutput;+ } | null;+ get data(): GetTileDataOutput;+}diff --git a/src/layers/src/raster-tile/url.ts b/src/layers/src/raster-tile/url.tsnew file mode 100644index 000000000..fa5b92755--- /dev/null+++ b/src/layers/src/raster-tile/url.ts@@ -0,0 +1,224 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++/* Utilities for creating request urls */+/* global URLSearchParams */++import {StacTypes} from '@kepler.gl/types';+import {getApplicationConfig} from '@kepler.gl/utils';++import {DEFAULT_BAND_MAPPINGS} from './config';+import {+ AssetIds,+ BandIndexes,+ CompleteSTACItem,+ CompleteSTACCollection,+ GetTileDataProps+} from './types';++type Item = StacTypes.STACItem;+type Collection = StacTypes.STACCollection;++const TILE_SIZE: 256 | 512 = 256;++interface StacSearchInfo {+ sentinelCollectionName: string[];+ stacSearchUrl: string;+}++const STAC_SEARCH_DATA: Record<string, StacSearchInfo> = {+ // Commenting out Microsoft for now+ // microsoft: {+ // sentinelCollectionName: ['sentinel-2-l2a'],+ // stacSearchUrl: 'https://planetarycomputer.microsoft.com/api/stac/v1/search'+ // },+ 'earth-search': {+ sentinelCollectionName: ['sentinel-s2-l2a-cogs'],+ stacSearchUrl: 'https://earth-search.aws.element84.com/v0/search'+ }+};++/**+ * Construct query parameters to be sent to STAC API instance+ */+function constructStacApiQuery(options: {+ stac: Item | Collection;+ startDate: string;+ endDate: string;+ stacSearchProvider: string;+}): {collections: string[]; datetime: string} {+ const {stac, startDate, endDate, stacSearchProvider} = options;++ // This is a quick hack to support the same Sentinel 2 STAC object for searching both microsoft+ // and AWS+ const collections = STAC_SEARCH_DATA[stacSearchProvider]?.sentinelCollectionName || [stac.id];++ return {+ collections,+ datetime: `${startDate}T00:00:00Z/${endDate}T23:59:59Z`+ };+}++/**+ * Perform lookup to find url of desired STAC search provider+ */+function getStacApiUrl(stacSearchProvider: string): string {+ return STAC_SEARCH_DATA[stacSearchProvider].stacSearchUrl;+}++export function getStacApiUrlParams(options: {+ stac: CompleteSTACCollection;+ stacSearchProvider: string;+ startDate: string;+ endDate: string;+ mask?: boolean;+ loadAssetIds: AssetIds;+ _stacQuery?: string;+}): URLSearchParams | null {+ const {stac, loadAssetIds, stacSearchProvider, mask = false} = options;+ const query = options._stacQuery || JSON.stringify(constructStacApiQuery(options));+ const searchUrl = getStacApiUrl(stacSearchProvider);++ if (!searchUrl) {+ return null;+ }++ const bandIndexAssets = loadAssetIds.map(assetId => {+ const mapping = DEFAULT_BAND_MAPPINGS[stac.id];+ if (!mapping) {+ // TODO provide a UI to setup custom band mapping+ return assetId;+ }++ const bandIndex = mapping[assetId];+ if (bandIndex) {+ return bandIndex;+ }++ // This is most likely incorrect as BXX is expected, not common name+ return assetId;+ });++ return new URLSearchParams({+ assets: bandIndexAssets.join(','),+ return_mask: String(mask),+ url: searchUrl,+ query+ });+}++export function bandIndexesToURLParams(+ urlParams: URLSearchParams,+ bandIndexes: BandIndexes+): URLSearchParams {+ if (getApplicationConfig().rasterServerUseLatestTitiler) {+ // for newer titiler versions+ bandIndexes.forEach(bandIndex => {+ urlParams.append('bidx', String(bandIndex + 1));+ });+ } else {+ // The parameter in titiler is `bands` for landsat/sentinel and `bidx` for COG+ // GDAL/Rasterio/rio-tiler start band indexing at one+ // older titiler versions+ urlParams.append('bidx', bandIndexes.map(val => val + 1).join(','));+ }++ return urlParams;+}++export function getSingleCOGUrlParams(options: {+ stac: CompleteSTACItem;+ loadAssetId: string;+ loadBandIndexes: BandIndexes;+ mask?: boolean;+}): URLSearchParams | null {+ const {stac, loadAssetId, loadBandIndexes, mask = false} = options;+ const url = stac.assets[loadAssetId].href;++ if (!url) {+ return null;+ }++ const urlParams = new URLSearchParams({+ return_mask: String(mask),+ url+ });+ return bandIndexesToURLParams(urlParams, loadBandIndexes);+}++/**+ * Construct full URL to load tile from a Titiler-based backend+ */+export function getTitilerUrl(options: {+ stac: GetTileDataProps['stac'];+ useSTACSearching: boolean;+ x: number;+ y: number;+ z: number;+}): string {+ // mask Set to false for mosaics because entire image is assumed to be valid+ const {stac, useSTACSearching, x, y, z} = options;++ if (!stac.rasterTileServerUrls?.length) {+ throw new Error('No raster tile servers');+ }++ const pathStem = getTitilerPathMapping(stac, useSTACSearching);+ const scale = TILE_SIZE === 512 ? '@2x' : '';+ const domain = chooseDomain(stac.rasterTileServerUrls, x, y);+ return `${domain}/${pathStem}/tiles/WebMercatorQuad/${z}/${x}/${y}${scale}.npy`;+}++export function getTitilerPathMapping(+ stac: GetTileDataProps['stac'],+ useSTACSearching = false+): string {+ if (useSTACSearching) {+ return 'stac/mosaic';+ }++ return 'cog';+}++/**+ * Choose from available domains to load images from+ *+ * @param x x tile index+ * @param y y tile index+ *+ * @return domain+ */+function chooseDomain(domains: string[], x: number, y: number): string {+ const index = Math.abs(x + y) % domains.length;+ return domains[index];+}++export function getTerrainUrl(+ rasterTileServerUrls: string[],+ x: number,+ y: number,+ z: number,+ meshMaxError: number+): string {+ const scale = TILE_SIZE === 512 ? '@2x' : '';++ const params = new URLSearchParams({+ url: 'terrarium',+ mesh_max_error: meshMaxError.toFixed(2)+ });+ const domain = chooseDomain(rasterTileServerUrls, x, y);+ const baseUrl = `${domain}/mesh/tiles/${z}/${x}/${y}${scale}.terrain?`;+ return baseUrl + params.toString();+}++/**+ * get mesh max error for z value+ * @param z mercator tile z coord+ * @param multiplier multipler applied to default error+ *+ * Uses suggestion from here+ * https://www.linkedin.com/pulse/fast-cesium-terrain-rendering-new-quantized-mesh-output-alvaro-huarte/+ */+export function getMeshMaxError(z: number, multiplier: number): number {+ return (77067.34 / (1 << z)) * multiplier;+}diff --git a/src/layers/src/vector-tile/vector-tile-layer.ts b/src/layers/src/vector-tile/vector-tile-layer.tsindex 879a77373..90f194d4b 100644--- a/src/layers/src/vector-tile/vector-tile-layer.ts+++ b/src/layers/src/vector-tile/vector-tile-layer.ts@@ -12,6 +12,7 @@ import GL from '@luma.gl/constants';import {notNullorUndefined} from '@kepler.gl/common-utils';import {+ getLoaderOptions,DatasetType,LAYER_TYPES,RemoteTileFormat,@@ -23,7 +24,6 @@ import {} from '@kepler.gl/constants';import {getTileUrl,- getLoaderOptions,KeplerTable as KeplerDataset,Datasets as KeplerDatasets,GpuFilter,@@ -237,7 +237,7 @@ export default class VectorTileLayer extends AbstractTileLayer<VectorTile, Featu});}- get type() {+ get type(): string {return LAYER_TYPES.vectorTile;}diff --git a/src/localization/src/translations/en.ts b/src/localization/src/translations/en.tsindex 6ae1b8001..6e3e465eb 100644--- a/src/localization/src/translations/en.ts+++ b/src/localization/src/translations/en.ts@@ -112,7 +112,8 @@ export default {trip: 'trip',s2: 'S2','3d': '3D',- vectortile: 'vector tile'+ vectortile: 'vector tile',+ rastertile: 'raster tile'},layerUpdateError:'An error occurred during layer update: {errorMessage}. Make sure the format of the input data is valid.',@@ -209,7 +210,8 @@ export default {},datasetInfo: {rowCount: '{rowCount} rows',- vectorTile: 'Vector tile'+ vectorTile: 'Vector tile',+ rasterTile: 'Raster tile'},tooltip: {hideLayer: 'Hide layer',@@ -320,7 +322,7 @@ export default {unfilteredData: 'Unfiltered Data',fileCount: '{fileCount} Files',rowCount: '{rowCount} Rows',- vectorTileWarning: "* Export Data for Vector Tile datasets isn't supported"+ tiledDatasetWarning: "* Export Data for Tiled datasets isn't supported"},deleteData: {warning: 'you are going to delete this dataset. It will affect {length} layers'@@ -604,6 +606,7 @@ ${'```'}},tilesetSetup: {header: 'Setup Vector Tiles',+ rasterTileHeader: 'Setup Raster Tiles',addTilesetText: 'Add Tileset'},geocoder: {diff --git a/src/reducers/src/vis-state-updaters.ts b/src/reducers/src/vis-state-updaters.tsindex c8e0118f0..8131ae5a3 100644--- a/src/reducers/src/vis-state-updaters.ts+++ b/src/reducers/src/vis-state-updaters.ts@@ -2347,6 +2347,14 @@ export const updateVisDataUpdater = (return updatedState;}+ if (datasets.length) {+ // indicate that dataset setup / loading is in progress+ const setIsLoadingTask = ACTION_TASK().map(() => {+ return setLoadingIndicator({change: 1});+ });+ updatedState = withTask(updatedState, setIsLoadingTask);+ }+const createDatasetTasks: Task[] = [];const notificationTasks: Task[] = [];diff --git a/src/table/src/dataset-utils.ts b/src/table/src/dataset-utils.tsindex c11a9fb30..5b51527e5 100644--- a/src/table/src/dataset-utils.ts+++ b/src/table/src/dataset-utils.ts@@ -6,7 +6,13 @@ import KeplerTable, {Datasets} from './kepler-table';import {ProtoDataset, RGBColor} from '@kepler.gl/types';import Task from 'react-palm/tasks';-import {DatasetType, RemoteTileFormat, VectorTileDatasetMetadata} from '@kepler.gl/constants';+import {+ DatasetType,+ RasterTileDatasetMetadata,+ PMTilesType,+ RemoteTileFormat,+ VectorTileDatasetMetadata+} from '@kepler.gl/constants';import {hexToRgb,validateInputData,@@ -17,7 +23,12 @@ import {PMTilesSource, PMTilesMetadata} from '@loaders.gl/pmtiles';import {/* MVTSource,*/ TileJSON} from '@loaders.gl/mvt';import {getMVTMetadata} from './tileset/tileset-utils';-import {parseVectorMetadata, getFieldsFromTile} from './tileset/vector-tile-utils';+import {parseRasterMetadata} from './tileset/raster-tile-utils';+import {+ parseVectorMetadata,+ getFieldsFromTile,+ VectorTileMetadata+} from './tileset/vector-tile-utils';// apply a color for each dataset// to use as label colors@@ -109,7 +120,9 @@ async function createTable(datasetInfo: CreateTableProps) {let metadata = opts.metadata;if (refreshedMetadata) {metadata = {...opts.metadata, ...refreshedMetadata};- data.fields = metadata?.fields;+ if (metadata.fields) {+ data.fields = metadata.fields;+ }}const TableClass = getApplicationConfig().table ?? KeplerTable;@@ -131,12 +144,21 @@ const CREATE_TABLE_TASK = Task.fromPromise(createTable, 'CREATE_TABLE_TASK');* @param datasetInfo* @returns*/-async function refreshRemoteData(datasetInfo: CreateTableProps) {- // so far only vector tile layers should refresh metadata- if (datasetInfo.info.type !== DatasetType.VECTOR_TILE) {- return null;+async function refreshRemoteData(datasetInfo: CreateTableProps): Promise<object | null> {+ const {type} = datasetInfo.info;+ switch (type) {+ case DatasetType.VECTOR_TILE:+ return await refreshVectorTileMetadata(datasetInfo);+ case DatasetType.RASTER_TILE:+ return await refreshRasterTileMetadata(datasetInfo);+ default:+ return null;}+}+async function refreshVectorTileMetadata(+ datasetInfo: CreateTableProps+): Promise<VectorTileMetadata | null> {const {remoteTileFormat, tilesetMetadataUrl, tilesetDataUrl} =(datasetInfo.opts.metadata as VectorTileDatasetMetadata) || {};@@ -170,8 +192,43 @@ async function refreshRemoteData(datasetInfo: CreateTableProps) {return metadata;}} catch (err) {- // ignore for now, and use old metadata?+ // ignore for now, and use old metadata}+ return null;+}++async function refreshRasterTileMetadata(datasetInfo: CreateTableProps): Promise<any | null> {+ const {metadataUrl, pmtilesType} = (datasetInfo.opts.metadata as RasterTileDatasetMetadata) || {};+ if (typeof metadataUrl !== 'string') {+ return null;+ }++ try {+ if (pmtilesType === PMTilesType.RASTER) {+ const tileSource = PMTilesSource.createDataSource(metadataUrl, {});+ const rawMetadata: PMTilesMetadata = await tileSource.metadata;++ if (rawMetadata) {+ return parseVectorMetadata(rawMetadata);+ }+ } else {+ // it's stac raster tiles+ const response = await fetch(metadataUrl);+ if (!response.ok) {+ throw new Error(`Failed Fetch ${metadataUrl}`);+ }+ const rawMetadata = await response.json();++ const metadata = parseRasterMetadata(rawMetadata, {allowCollections: true});+ if (metadata instanceof Error) {+ throw new Error(`Failed to parse metadata ${metadata.message}`);+ }++ return metadata;+ }+ } catch (err) {+ // ignore for now, and use old metadata+ }return null;}diff --git a/src/table/src/index.ts b/src/table/src/index.tsindex dfffca942..4a551a39f 100644--- a/src/table/src/index.ts+++ b/src/table/src/index.ts@@ -25,3 +25,4 @@ export * from './gpu-filter-utils';export * from './dataset-utils';export * from './tileset/tileset-utils';export * from './tileset/vector-tile-utils';+export * from './tileset/raster-tile-utils';diff --git a/src/table/src/tileset/raster-tile-utils.ts b/src/table/src/tileset/raster-tile-utils.tsnew file mode 100644index 000000000..67db45aad--- /dev/null+++ b/src/table/src/tileset/raster-tile-utils.ts@@ -0,0 +1,91 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++import {JsonObjectOrArray, StacTypes} from '@kepler.gl/types';++// Define these as regex to allow any semver major version 1+const EO_EXT_ID = /https:\/\/stac-extensions.github.io\/eo\/v1[\d.]+\/schema.json/;+const RASTER_EXT_ID = /https:\/\/stac-extensions.github.io\/raster\/v1[\d.]+\/schema.json/;+const ITEM_ASSETS_EXT_ID =+ /https:\/\/stac-extensions.github.io\/item-assets\/v1[\d.]+\/schema.json/;++export function parseRasterMetadata(+ metadata: JsonObjectOrArray,+ options: {allowCollections: boolean}+): StacTypes.CompleteSTACObject | Error | null {+ if (!metadata || typeof metadata !== 'object') {+ return null;+ }++ const error = validateSTAC(metadata, options);+ if (error !== null) {+ return error;+ }++ return metadata as StacTypes.CompleteSTACObject;+}++// eslint-disable-next-line complexity+function validateSTAC(stac: JsonObjectOrArray, options: {allowCollections: boolean}): Error | null {+ const {allowCollections = false} = options;++ // Note: If a STAC object fails validation, it will silently fail+ if (+ typeof stac === 'string' ||+ typeof stac === 'boolean' ||+ typeof stac === 'number' ||+ Array.isArray(stac) ||+ !stac+ ) {+ return Error('Metadata must be an object.');+ }++ if ((!allowCollections && stac?.type === 'Collection') || stac?.type === 'Catalog') {+ return Error('Custom STAC Collections and Catalogs are not supported.');+ }++ if (stac?.stac_version?.[0] !== '1') {+ return Error('STAC versions before 1.0.0 not supported.');+ }++ if (stac?.type !== 'Feature' && stac?.type !== 'Collection') {+ return Error('Not a STAC Item or Collection.');+ }++ const isCollection = stac?.type === 'Collection';++ const required_extensions = [EO_EXT_ID, RASTER_EXT_ID];+ if (isCollection) {+ required_extensions.push(ITEM_ASSETS_EXT_ID);+ }++ if (+ !Array.isArray(stac.stac_extensions) ||+ !stac.stac_extensions.some(ext => typeof ext === 'string' && EO_EXT_ID.exec(ext)) ||+ !stac.stac_extensions.some(ext => typeof ext === 'string' && RASTER_EXT_ID.exec(ext))+ ) {+ return Error('EO and Raster STAC extensions are required.');+ }++ if (+ isCollection &&+ !stac.stac_extensions.some(ext => typeof ext === 'string' && EO_EXT_ID.exec(ext))+ ) {+ return Error('item-assets STAC extension is required.');+ }++ const assets = isCollection ? stac?.item_assets : stac?.assets;+ if (!assets || typeof assets !== 'object') {+ return Error('STAC object is missing asset information.');+ }++ if (+ !Object.values(assets).some(+ asset => Array.isArray(asset?.['eo:bands']) && Array.isArray(asset?.['raster:bands'])+ )+ ) {+ return Error('At least one STAC asset must have both eo:bands and raster:bands data.');+ }++ return null;+}diff --git a/src/table/src/tileset/vector-tile-utils.spec.ts b/src/table/src/tileset/vector-tile-utils.spec.tsindex 6de089733..06c1e2422 100644--- a/src/table/src/tileset/vector-tile-utils.spec.ts+++ b/src/table/src/tileset/vector-tile-utils.spec.ts@@ -156,7 +156,8 @@ test('parseMetadata, PMTiles from PMTileSource', () => {gpu: false}}- ]+ ],+ pmtilesType: 'mvt'});});diff --git a/src/table/src/tileset/vector-tile-utils.ts b/src/table/src/tileset/vector-tile-utils.tsindex edc02fe4d..11a93f568 100644--- a/src/table/src/tileset/vector-tile-utils.ts+++ b/src/table/src/tileset/vector-tile-utils.ts@@ -17,7 +17,13 @@ import {parseUri,getFieldsFromData} from '@kepler.gl/common-utils';-import {DatasetType, ALL_FIELD_TYPES, FILTER_TYPES, RemoteTileFormat} from '@kepler.gl/constants';+import {+ DatasetType,+ ALL_FIELD_TYPES,+ FILTER_TYPES,+ PMTilesType,+ RemoteTileFormat+} from '@kepler.gl/constants';import {Feature, Field as KeplerField, KeplerLayer} from '@kepler.gl/types';import {clamp, formatNumberByStep, getNumericStepSize, timeToUnixMilli} from '@kepler.gl/utils';@@ -30,14 +36,6 @@ import {default as KeplerDataset} from '../kepler-table';-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);}@@ -61,6 +59,9 @@ export type VectorTileMetadata = {name?: string;description?: string;fields: VectorTileField[];++ // if the tileset is of pmtiles format then include info about type of pmtiles+ pmtilesType?: PMTilesType;};type TilesetMetadata = VectorTileMetadata;@@ -223,6 +224,14 @@ function getMetaUrlMapbox(tileUrl = ''): string {function parseMetadataTileJSON(metadata: PMTilesMetadata | TileJSON): TilesetMetadata | null {const parsed = parseMetadataTippecanoeFromDataSource(metadata);if (!parsed) return null;++ // PMTiles can potentially be in RasterTile format+ const mimeType = (metadata as PMTilesMetadata).tileMIMEType;+ if (mimeType) {+ parsed.pmtilesType =+ mimeType === 'application/vnd.mapbox-vector-tile' ? PMTilesType.MVT : PMTilesType.RASTER;+ }+// Fields already parsed from `json` propertyif (parsed.fields?.length) {return parsed;@@ -700,7 +709,8 @@ export const getFieldsFromTile = async ({metadata &&metadata.fields?.length === 0 &&metadata.minZoom &&- metadata.bounds?.length === 4+ metadata.bounds?.length === 4 &&+ metadata.pmtilesType === PMTilesType.MVT) {const lon = (metadata.bounds[0] + metadata.bounds[2]) / 2;const lat = (metadata.bounds[1] + metadata.bounds[3]) / 2;diff --git a/src/types/index.d.ts b/src/types/index.d.tsindex 25425171e..155feb9d6 100644--- a/src/types/index.d.ts+++ b/src/types/index.d.ts@@ -9,3 +9,5 @@ export * from './types';export * from './components';export * from './effects';export * from './datasets';++export * as StacTypes from './stac';diff --git a/src/types/layers.d.ts b/src/types/layers.d.tsindex 33fddf6bb..8e3423b7b 100644--- a/src/types/layers.d.ts+++ b/src/types/layers.d.ts@@ -250,12 +250,25 @@ export type VisConfigBoolean = VisConfig & {defaultValue: boolean;};+export type VisConfigInput = VisConfig & {+ type: 'input';+ defaultValue: string | null;+};+export type VisConfigSelection = VisConfig & {type: 'select';defaultValue: string;options: string[];};+export type VisConfigObjectSelection = VisConfig & {+ type: 'object-select';+ // the value is id of one of the options+ defaultValue: string | null;+ // an array of objects with ids+ options: object[];+};+export type VisConfigRange = VisConfig & {type: 'number';isRanged: boolean;@@ -274,6 +287,16 @@ export type VisConfigColorRange = VisConfig & {defaultValue: ColorRange;};+export type LayerVisConfigTypes =+ | VisConfigBoolean+ | VisConfigNumber+ | VisConfigRange+ | VisConfigColorRange+ | VisConfigColorSelect+ | VisConfigInput+ | VisConfigSelect+ | VisConfigObjectSelection;+export type LayerVisConfigSettings = {thickness: VisConfigNumber;strokeWidthRange: VisConfigRange;@@ -318,6 +341,7 @@ export type LayerVisConfigSettings = {allowHover: VisConfigBoolean;showNeighborOnHover: VisConfigBoolean;showHighlightColor: VisConfigBoolean;+ [key: string]: LayerVisConfigTypes;};// TODO: Move this to individual layersdiff --git a/src/types/stac.d.ts b/src/types/stac.d.tsnew file mode 100644index 000000000..fda45bb45--- /dev/null+++ b/src/types/stac.d.ts@@ -0,0 +1,830 @@+// SPDX-License-Identifier: MIT+// Copyright contributors to the kepler.gl project++/**+ * STAC EO Extension for STAC Items.+ */+export type EOExtension =+ | ({+ type: 'Feature';+ properties: Fields;+ assets: {+ [k: string]: Fields;+ };+ [k: string]: unknown;+ } & StacExtensions)+ | ({+ type: 'Collection';+ assets?: {+ [k: string]: Fields;+ };+ item_assets?: {+ [k: string]: Fields;+ };+ [k: string]: unknown;+ } & StacExtensions);+export type CloudCover = number;+export type Bands = [Band, ...Band[]];+export type NameOfTheBand = string;+export type CommonNameOfTheBand =+ | 'coastal'+ | 'blue'+ | 'green'+ | 'red'+ | 'rededge'+ | 'yellow'+ | 'pan'+ | 'nir'+ | 'nir08'+ | 'nir09'+ | 'cirrus'+ | 'swir16'+ | 'swir22'+ | 'lwir'+ | 'lwir11'+ | 'lwir12';+export type CenterWavelength = number;+export type FullWidthHalfMaxFWHM = number;++export interface Fields {+ 'eo:cloud_cover'?: CloudCover;+ 'eo:bands'?: Bands;+ /**+ * This interface was referenced by `Fields`'s JSON-Schema definition+ * via the `patternProperty` "^(?!eo:)".+ */+ [k: string]: unknown;+}+export interface Band {+ name?: NameOfTheBand;+ common_name?: CommonNameOfTheBand;+ center_wavelength?: CenterWavelength;+ full_width_half_max?: FullWidthHalfMaxFWHM;+ [k: string]: unknown;+}+export interface StacExtensions {+ stac_extensions: unknown[];+ [k: string]: unknown;+}++export type STACItem = Core;+export type Core = GeoJSONFeature &+ (+ | {+ geometry: GeoJSONGeometry;+ bbox: {+ [k: string]: unknown;+ } & number[];+ [k: string]: unknown;+ }+ | {+ geometry: null;+ bbox?: {+ [k: string]: unknown;+ };+ [k: string]: unknown;+ }+ ) & {+ stac_version: STACVersion;+ stac_extensions?: STACExtensions;+ id: ProviderID;+ links: ItemLinks;+ assets: AssetLinks;+ properties: CommonMetadata &+ (+ | {+ datetime: {+ [k: string]: unknown;+ };+ [k: string]: unknown;+ }+ | {+ [k: string]: unknown;+ }+ );+ [k: string]: unknown;+ };+export type GeoJSONGeometry =+ | GeoJSONPoint2+ | GeoJSONLineString2+ | GeoJSONPolygon2+ | GeoJSONMultiPoint2+ | GeoJSONMultiLineString2+ | GeoJSONMultiPolygon2;+export type STACVersion = '1.0.0';+export type ReferenceToAJSONSchema = string;+export type STACExtensions = ReferenceToAJSONSchema[];+/**+ * Provider item ID+ */+export type ProviderID = string;+export type LinkReference = string;+export type LinkRelationType = string;+export type LinkType = string;+export type LinkTitle = string;+/**+ * Links to item relations+ */+export type ItemLinks = Link[];+export type Asset = {+ href: AssetReference;+ title?: AssetTitle;+ description?: AssetDescription;+ type?: AssetType;+ roles?: AssetRoles;+ [k: string]: unknown;+} & CommonMetadata;+export type AssetReference = string;+export type AssetTitle = string;+export type AssetDescription = string;+export type AssetType = string;+export type AssetRoles = string[];+export type CommonMetadata = BasicDescriptiveFields &+ DateAndTimeFields &+ InstrumentFields &+ LicensingFields &+ ProviderFields;+/**+ * A human-readable title describing the Item.+ */+export type ItemTitle = string;+/**+ * Detailed multi-line description to fully explain the Item.+ */+export type ItemDescription = string;+/**+ * The searchable date/time of the assets, in UTC (Formatted in RFC 3339)+ */+export type DateAndTime = string | null;+/**+ * The searchable start date/time of the assets, in UTC (Formatted in RFC 3339)+ */+export type StartDateAndTime = string;+/**+ * The searchable end date/time of the assets, in UTC (Formatted in RFC 3339)+ */+export type EndDateAndTime = string;+export type CreationTime = string;+export type LastUpdateTime = string;+export type Platform = string;+export type Instruments = string[];+export type Constellation = string;+export type Mission = string;+export type GroundSampleDistance = number;+export type OrganizationName = string;+export type OrganizationDescription = string;+export type OrganizationRoles = ('producer' | 'licensor' | 'processor' | 'host')[];+export type OrganizationHomepage = string;+export type Providers = {+ name: OrganizationName;+ description?: OrganizationDescription;+ roles?: OrganizationRoles;+ url?: OrganizationHomepage;+ [k: string]: unknown;+}[];++export interface GeoJSONFeature {+ type: 'Feature';+ id?: number | string;+ properties: null | {+ [k: string]: unknown;+ };+ geometry:+ | null+ | GeoJSONPoint+ | GeoJSONLineString+ | GeoJSONPolygon+ | GeoJSONMultiPoint+ | GeoJSONMultiLineString+ | GeoJSONMultiPolygon+ | GeoJSONGeometryCollection;+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONPoint {+ type: 'Point';+ coordinates: [number, number, ...number[]];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONLineString {+ type: 'LineString';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONPolygon {+ type: 'Polygon';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONMultiPoint {+ type: 'MultiPoint';+ coordinates: [number, number, ...number[]][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONMultiLineString {+ type: 'MultiLineString';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONMultiPolygon {+ type: 'MultiPolygon';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ][][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONGeometryCollection {+ type: 'GeometryCollection';+ geometries: (+ | GeoJSONPoint1+ | GeoJSONLineString1+ | GeoJSONPolygon1+ | GeoJSONMultiPoint1+ | GeoJSONMultiLineString1+ | GeoJSONMultiPolygon1+ )[];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONPoint1 {+ type: 'Point';+ coordinates: [number, number, ...number[]];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONLineString1 {+ type: 'LineString';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONPolygon1 {+ type: 'Polygon';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONMultiPoint1 {+ type: 'MultiPoint';+ coordinates: [number, number, ...number[]][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONMultiLineString1 {+ type: 'MultiLineString';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONMultiPolygon1 {+ type: 'MultiPolygon';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ][][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONPoint2 {+ type: 'Point';+ coordinates: [number, number, ...number[]];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONLineString2 {+ type: 'LineString';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONPolygon2 {+ type: 'Polygon';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONMultiPoint2 {+ type: 'MultiPoint';+ coordinates: [number, number, ...number[]][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONMultiLineString2 {+ type: 'MultiLineString';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface GeoJSONMultiPolygon2 {+ type: 'MultiPolygon';+ coordinates: [+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ [number, number, ...number[]],+ ...[number, number, ...number[]][]+ ][][];+ bbox?: [number, number, number, number, ...number[]];+ [k: string]: unknown;+}+export interface Link {+ href: LinkReference;+ rel: LinkRelationType;+ type?: LinkType;+ title?: LinkTitle;+ [k: string]: unknown;+}+/**+ * Links to assets+ */+export interface AssetLinks {+ [k: string]: Asset;+}+export interface BasicDescriptiveFields {+ title?: ItemTitle;+ description?: ItemDescription;+ [k: string]: unknown;+}+export interface DateAndTimeFields {+ datetime?: DateAndTime;+ start_datetime?: StartDateAndTime;+ end_datetime?: EndDateAndTime;+ created?: CreationTime;+ updated?: LastUpdateTime;+ [k: string]: unknown;+}+export interface InstrumentFields {+ platform?: Platform;+ instruments?: Instruments;+ constellation?: Constellation;+ mission?: Mission;+ gsd?: GroundSampleDistance;+ [k: string]: unknown;+}+export interface LicensingFields {+ license?: string;+ [k: string]: unknown;+}+export interface ProviderFields {+ providers?: Providers;+ [k: string]: unknown;+}++/**+ * This object represents Collections in a SpatioTemporal Asset Catalog.+ */+export type STACCollectionSpecification = STACCollection;+export type STACVersion = '1.0.0';+export type ReferenceToAJSONSchema = string;+export type STACExtensions = ReferenceToAJSONSchema[];+export type TypeOfSTACEntity = 'Collection';+export type Identifier = string;+export type Title = string;+export type Description = string;+export type Keywords = string[];+export type CollectionLicenseName = string;+export type OrganizationName = string;+export type OrganizationDescription = string;+export type OrganizationRoles = ('producer' | 'licensor' | 'processor' | 'host')[];+export type OrganizationHomepage = string;+export type SpatialExtents = [SpatialExtent, ...SpatialExtent3[]];+export type SpatialExtent = SpatialExtent1 & SpatialExtent2;+export type SpatialExtent1 =+ | {+ [k: string]: unknown;+ }+ | {+ [k: string]: unknown;+ };+export type SpatialExtent2 = number[];+export type SpatialExtent3 = SpatialExtent1 & SpatialExtent2;+export type TemporalExtents = [TemporalExtent, ...TemporalExtent[]];+export type TemporalExtent = [string | null, string | null];+export type AssetReference = string;+export type AssetTitle = string;+export type AssetDescription = string;+export type AssetType = string;+export type AssetRoles = string[];+/**+ * A human-readable title describing the Item.+ */+export type ItemTitle = string;+/**+ * Detailed multi-line description to fully explain the Item.+ */+export type ItemDescription = string;+/**+ * The searchable date/time of the assets, in UTC (Formatted in RFC 3339)+ */+export type DateAndTime = string | null;+/**+ * The searchable start date/time of the assets, in UTC (Formatted in RFC 3339)+ */+export type StartDateAndTime = string;+/**+ * The searchable end date/time of the assets, in UTC (Formatted in RFC 3339)+ */+export type EndDateAndTime = string;+export type CreationTime = string;+export type LastUpdateTime = string;+export type Platform = string;+export type Instruments = string[];+export type Constellation = string;+export type Mission = string;+export type GroundSampleDistance = number;+export type OrganizationName1 = string;+export type OrganizationDescription1 = string;+export type OrganizationRoles1 = ('producer' | 'licensor' | 'processor' | 'host')[];+export type OrganizationHomepage1 = string;+export type Providers = {+ name: OrganizationName1;+ description?: OrganizationDescription1;+ roles?: OrganizationRoles1;+ url?: OrganizationHomepage1;+ [k: string]: unknown;+}[];+export type LinkReference = string;+export type LinkRelationType = string;+export type LinkType = string;+export type LinkTitle = string;+export type Links = Link[];+export type JSONSchema = CoreSchemaMetaSchema;+export type CoreSchemaMetaSchema =+ | {+ $id?: string;+ $schema?: string;+ $ref?: string;+ $comment?: string;+ title?: string;+ description?: string;+ default?: true;+ readOnly?: boolean;+ writeOnly?: boolean;+ examples?: true[];+ multipleOf?: number;+ maximum?: number;+ exclusiveMaximum?: number;+ minimum?: number;+ exclusiveMinimum?: number;+ maxLength?: number;+ minLength?: number;+ pattern?: string;+ additionalItems?: CoreSchemaMetaSchema;+ items?: CoreSchemaMetaSchema | [CoreSchemaMetaSchema, ...CoreSchemaMetaSchema[]];+ maxItems?: number;+ minItems?: number;+ uniqueItems?: boolean;+ contains?: CoreSchemaMetaSchema;+ maxProperties?: number;+ minProperties?: number;+ required?: string[];+ additionalProperties?: CoreSchemaMetaSchema;+ definitions?: {+ [k: string]: CoreSchemaMetaSchema;+ };+ properties?: {+ [k: string]: CoreSchemaMetaSchema;+ };+ patternProperties?: {+ [k: string]: CoreSchemaMetaSchema;+ };+ dependencies?: {+ [k: string]: CoreSchemaMetaSchema | string[];+ };+ propertyNames?: CoreSchemaMetaSchema;+ const?: true;+ enum?: [true, ...unknown[]];+ type?:+ | ('array' | 'boolean' | 'integer' | 'null' | 'number' | 'object' | 'string')+ | [+ 'array' | 'boolean' | 'integer' | 'null' | 'number' | 'object' | 'string',+ ...('array' | 'boolean' | 'integer' | 'null' | 'number' | 'object' | 'string')[]+ ];+ format?: string;+ contentMediaType?: string;+ contentEncoding?: string;+ if?: CoreSchemaMetaSchema;+ then?: CoreSchemaMetaSchema;+ else?: CoreSchemaMetaSchema;+ allOf?: [CoreSchemaMetaSchema, ...CoreSchemaMetaSchema[]];+ anyOf?: [CoreSchemaMetaSchema, ...CoreSchemaMetaSchema[]];+ oneOf?: [CoreSchemaMetaSchema, ...CoreSchemaMetaSchema[]];+ not?: CoreSchemaMetaSchema;+ [k: string]: unknown;+ }+ | boolean;+export type MinimumValue = number | string;+export type MaximumValue = number | string;+export type SetOfValues = [+ {+ [k: string]: unknown;+ },+ ...{+ [k: string]: unknown;+ }[]+];++/**+ * These are the fields specific to a STAC Collection. All other fields are inherited from STAC Catalog.+ */+export interface STACCollection {+ stac_version: STACVersion;+ stac_extensions?: STACExtensions;+ type: TypeOfSTACEntity;+ id: Identifier;+ title?: Title;+ description: Description;+ keywords?: Keywords;+ license: CollectionLicenseName;+ providers?: {+ name: OrganizationName;+ description?: OrganizationDescription;+ roles?: OrganizationRoles;+ url?: OrganizationHomepage;+ [k: string]: unknown;+ }[];+ extent: Extents;+ assets?: AssetLinks;+ links: Links;+ summaries?: Summaries;+ [k: string]: unknown;+}+export interface Extents {+ spatial: SpatialExtentObject;+ temporal: TemporalExtentObject;+ [k: string]: unknown;+}+export interface SpatialExtentObject {+ bbox: SpatialExtents;+ [k: string]: unknown;+}+export interface TemporalExtentObject {+ interval: TemporalExtents;+ [k: string]: unknown;+}+/**+ * Links to assets+ */+export interface AssetLinks {+ [k: string]: {+ href: AssetReference;+ title?: AssetTitle;+ description?: AssetDescription;+ type?: AssetType;+ roles?: AssetRoles;+ [k: string]: unknown;+ } & (BasicDescriptiveFields &+ DateAndTimeFields &+ InstrumentFields &+ LicensingFields &+ ProviderFields);+}+export interface BasicDescriptiveFields {+ title?: ItemTitle;+ description?: ItemDescription;+ [k: string]: unknown;+}+export interface DateAndTimeFields {+ datetime?: DateAndTime;+ start_datetime?: StartDateAndTime;+ end_datetime?: EndDateAndTime;+ created?: CreationTime;+ updated?: LastUpdateTime;+ [k: string]: unknown;+}+export interface InstrumentFields {+ platform?: Platform;+ instruments?: Instruments;+ constellation?: Constellation;+ mission?: Mission;+ gsd?: GroundSampleDistance;+ [k: string]: unknown;+}+export interface LicensingFields {+ license?: string;+ [k: string]: unknown;+}+export interface ProviderFields {+ providers?: Providers;+ [k: string]: unknown;+}+export interface Link {+ href: LinkReference;+ rel: LinkRelationType;+ type?: LinkType;+ title?: LinkTitle;+ [k: string]: unknown;+}+export interface Summaries {+ [k: string]: JSONSchema | Range | SetOfValues;+}+export interface Range {+ minimum: MinimumValue;+ maximum: MaximumValue;+ [k: string]: unknown;+}++export type AssetTitle = string;+export type AssetDescription = string;+export type AssetType = string;+export type AssetRoles = string[];++/**+ * STAC Item Assets Definition Extension for STAC Collections.+ */+export interface ItemAssetsDefinitionExtension {+ stac_extensions: unknown[];+ type: 'Collection';+ item_assets: {+ [k: string]: {+ href?: DisallowHref;+ title?: AssetTitle;+ description?: AssetDescription;+ type?: AssetType;+ roles?: AssetRoles;+ [k: string]: unknown;+ };+ };+ [k: string]: unknown;+}+export interface DisallowHref {+ [k: string]: unknown;+}++/**+ * STAC Raster Extension for STAC Items.+ */+export type RasterExtension =+ | ({+ type: 'Feature';+ assets: {+ [k: string]: Assetfields;+ };+ [k: string]: unknown;+ } & StacExtensions)+ | {+ type: 'Collection';+ assets?: {+ [k: string]: Assetfields;+ };+ item_assets?: {+ [k: string]: Assetfields;+ };+ [k: string]: unknown;+ };+export type Bands = [Band, ...Band[]];+export type DataTypeOfTheBand =+ | 'int8'+ | 'int16'+ | 'int32'+ | 'int64'+ | 'uint8'+ | 'uint16'+ | 'uint32'+ | 'uint64'+ | 'float16'+ | 'float32'+ | 'float64'+ | 'cint16'+ | 'cint32'+ | 'cfloat32'+ | 'cfloat64'+ | 'other';+export type UnitDenominationOfThePixelValue = string;+export type TheActualNumberOfBitsUsedForThisBand = number;+export type PixelSamplingInTheBand = 'area' | 'point';+export type NoDataPixelValue = number | ('nan' | 'inf' | '-inf');+export type MultiplicatorFactorOfThePixelValueToTransformIntoTheValue = number;+export type NumberToBeAddedToThePixelValueToTransformIntoTheValue = number;+export type AverageSpatialResolutionInMetersOfThePixelsInTheBand = number;+export type MeanValueOfAllThePixelsInTheBand = number;+export type MinimumValueOfAllThePixelsInTheBand = number;+export type MaximumValueOfAllThePixelsInTheBand = number;+export type StandardDeviationValueOfAllThePixelsInTheBand = number;+export type PercentageOfValidNotNodataPixel = number;+export type NumberOfBuckets = number;+export type MinimumValueOfTheBuckets = number;+export type MaximumValueOfTheBuckets = number;+export type DistributionBuckets = [+ NumberOfPixelsInTheBucket,+ NumberOfPixelsInTheBucket,+ NumberOfPixelsInTheBucket,+ ...NumberOfPixelsInTheBucket[]+];+export type NumberOfPixelsInTheBucket = number;++export interface Assetfields {+ 'raster:bands'?: Bands;+ /**+ * This interface was referenced by `Assetfields`'s JSON-Schema definition+ * via the `patternProperty` "^(?!raster:)".+ */+ [k: string]: {+ [k: string]: unknown;+ };+}+export interface Band {+ data_type?: DataTypeOfTheBand;+ unit?: UnitDenominationOfThePixelValue;+ bits_per_sample?: TheActualNumberOfBitsUsedForThisBand;+ sampling?: PixelSamplingInTheBand;+ nodata?: NoDataPixelValue;+ scale?: MultiplicatorFactorOfThePixelValueToTransformIntoTheValue;+ offset?: NumberToBeAddedToThePixelValueToTransformIntoTheValue;+ spatial_resolution?: AverageSpatialResolutionInMetersOfThePixelsInTheBand;+ statistics?: Statistics;+ histogram?: Histogram;+ [k: string]: unknown;+}+export interface Statistics {+ mean?: MeanValueOfAllThePixelsInTheBand;+ minimum?: MinimumValueOfAllThePixelsInTheBand;+ maximum?: MaximumValueOfAllThePixelsInTheBand;+ stddev?: StandardDeviationValueOfAllThePixelsInTheBand;+ valid_percent?: PercentageOfValidNotNodataPixel;+}+export interface Histogram {+ count: NumberOfBuckets;+ min: MinimumValueOfTheBuckets;+ max: MaximumValueOfTheBuckets;+ buckets: DistributionBuckets;+}+export interface StacExtensions {+ stac_extensions: unknown[];+ [k: string]: unknown;+}++/**+ * STAC Item or Collection+ */+export type STACObject = STACItem | STACCollection;++/**+ * STAC Item including our required extensions: EO and Raster+ */+export type CompleteSTACItem = STACItem & EOExtension & RasterExtension;++/**+ * STAC Collection including our required extensions: Item Assets, EO, and Raster+ */+export type CompleteSTACCollection = STACCollection &+ ItemAssetsDefinitionExtension &+ EOExtension &+ RasterExtension;++export type CompleteSTACObject = CompleteSTACItem | CompleteSTACCollection;diff --git a/src/utils/src/application-config.ts b/src/utils/src/application-config.tsindex 40144ef70..1167a6f99 100644--- a/src/utils/src/application-config.ts+++ b/src/utils/src/application-config.ts@@ -46,6 +46,16 @@ export type KeplerApplicationConfig = {// Enabling this option may cause performance issues when dealing with a large number of layers or sublayers,// especially if large Arrow files are split into relatively small batches (should be fixed in the future).useOnFilteredItemsChange?: boolean;++ // Raster Tile layer config+ // Raster Tile layer is under development and not ready for production use. Disabled by default.+ enableRasterTileLayer?: boolean;+ /** Titiler v0.11 vs v0.21 */+ rasterServerUseLatestTitiler?: boolean;+ /** An array of URLs to shards of the raster tile server to be used by the raster tile layer. */+ rasterServerUrls?: string[];+ /** If true then try to fetch quantized elevation meshes from raster servers */+ rasterServerSupportsElevation?: boolean;};const DEFAULT_APPLICATION_CONFIG: Required<KeplerApplicationConfig> = {@@ -82,7 +92,14 @@ const DEFAULT_APPLICATION_CONFIG: Required<KeplerApplicationConfig> = {useArrowProgressiveLoading: true,showReleaseBanner: true,- useOnFilteredItemsChange: false+ useOnFilteredItemsChange: false,++ // Raster Tile layer config+ enableRasterTileLayer: false,+ rasterServerUseLatestTitiler: true,+ // TODO: provide a default free server or leave blank+ rasterServerUrls: ['http://localhost:8000'],+ rasterServerSupportsElevation: true};const applicationConfig: Required<KeplerApplicationConfig> = DEFAULT_APPLICATION_CONFIG;