Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix: draw float32 raster tiles (#3704)
Javier Andres Garciabcf42616e3cf
diff --git a/src/deckgl-layers/src/raster/webgl/texture/mask.ts b/src/deckgl-layers/src/raster/webgl/texture/mask.tsindex 0f1e9acc0..0260ff809 100644--- a/src/deckgl-layers/src/raster/webgl/texture/mask.ts+++ b/src/deckgl-layers/src/raster/webgl/texture/mask.ts@@ -4,7 +4,10 @@import type {Texture} from '@luma.gl/core';import {GetUniformsOutput, ShaderModule} from '../types';-const inf = Math.pow(2, 62);+// The "no bound" default for keepMin/keepMax. A tile server writes the mask in the band's own+// data type and marks valid pixels with that type's maximum — 255 for uint8, 65535 for uint16,+// FLT_MAX for float32 — so a smaller default discards every valid pixel of a float32 mask.+const inf = 3.4028234663852886e38;function getUniforms(opts: {imageMask?: Texture; maskKeepMin?: number; maskKeepMax?: number} = {}diff --git a/src/layers/src/raster-tile/raster-tile-utils.ts b/src/layers/src/raster-tile/raster-tile-utils.tsindex 867a6f42f..ee59d799c 100644--- a/src/layers/src/raster-tile/raster-tile-utils.ts+++ b/src/layers/src/raster-tile/raster-tile-utils.ts@@ -156,19 +156,15 @@ function getPixelRange(const minPixelValue = 0;const maxPixelValue = dtypeMaxValue[dtype];- // TODO check if this early return is expected- if (!maxPixelValue) {+ // Data types without a fixed maximum, such as floats, can only be rescaled from the+ // statistics reported by the STAC metadata.+ if (maxPixelValue === null || !Number.isFinite(maxPixelValue)) {+ if (typeof minRasterStatsValue === 'number' && typeof maxRasterStatsValue === 'number') {+ return [minRasterStatsValue, maxRasterStatsValue];+ }return null;}- if (- !Number.isFinite(maxPixelValue) &&- typeof minRasterStatsValue === 'number' &&- typeof maxRasterStatsValue === 'number'- ) {- return [minRasterStatsValue, maxRasterStatsValue];- }-return [minPixelValue, maxPixelValue];}diff --git a/test/browser/layer-tests/raster-tile-layer-specs.js b/test/browser/layer-tests/raster-tile-layer-specs.jsindex 89638edd3..fa5e62a7f 100644--- a/test/browser/layer-tests/raster-tile-layer-specs.js+++ b/test/browser/layer-tests/raster-tile-layer-specs.js@@ -8,8 +8,10 @@ import {findAssetWithName,getUsableAssets,getRasterStatisticsMinMax,- filterAvailablePresets+ filterAvailablePresets,+ getDataSourceParams} from '@kepler.gl/layers';+import {RasterWebGL} from '@kepler.gl/deckgl-layers';import {parseRasterMetadata} from '@kepler.gl/table';import {testCreateCases} from 'test/helpers/layer-utils';@@ -739,3 +741,83 @@ test('#RasterTileLayer -> showTileBorders default config', t => {);t.end();});++const MOCK_STAC_FLOAT32_METADATA = {+ type: 'Feature',+ stac_version: '1.0.0',+ stac_extensions: ['https://stac-extensions.github.io/raster/v1.1.0/schema.json'],+ id: 'test-float32-cog',+ geometry: {type: 'Point', coordinates: [0, 0]},+ bbox: [-122.5, 37.5, -122.0, 38.0],+ links: [],+ properties: {datetime: '2023-01-01T00:00:00Z'},+ assets: {+ ndvi: {+ href: 'https://example.com/ndvi.tif',+ type: 'image/tiff; application=geotiff',+ 'raster:bands': [+ {+ data_type: 'float32',+ statistics: {minimum: -0.2, maximum: 0.92}+ }+ ]+ }+ }+};++test('#RasterTileLayer -> getDataSourceParams float32 band with statistics', t => {+ const params = getDataSourceParams(MOCK_STAC_FLOAT32_METADATA, 'singleBand', {+ singleBand: {assetId: 'ndvi'}+ });++ t.ok(params, 'should return data source params for a float32 band');+ t.equal(params?.dataType, 'float32', 'should keep the float32 data type');+ t.equal(params?.minPixelValue, -0.2, 'should take the minimum from the band statistics');+ t.equal(params?.maxPixelValue, 0.92, 'should take the maximum from the band statistics');+ t.end();+});++test('#RasterTileLayer -> getDataSourceParams float32 band without statistics', t => {+ const noStatsStac = {+ ...MOCK_STAC_FLOAT32_METADATA,+ assets: {+ ndvi: {+ ...MOCK_STAC_FLOAT32_METADATA.assets.ndvi,+ 'raster:bands': [{data_type: 'float32'}]+ }+ }+ };++ const params = getDataSourceParams(noStatsStac, 'singleBand', {singleBand: {assetId: 'ndvi'}});++ t.equal(params, null, 'should return null when there is no range to rescale against');+ t.end();+});++test('#RasterTileLayer -> getDataSourceParams integer band uses the data type range', t => {+ const params = getDataSourceParams(MOCK_STAC_110_METADATA, 'singleBand', {+ singleBand: {assetId: 'data'}+ });++ t.ok(params, 'should return data source params for a uint8 band');+ t.equal(params?.minPixelValue, 0, 'should keep the data type minimum');+ t.equal(params?.maxPixelValue, 255, 'should keep the data type maximum');+ t.end();+});++test('#RasterTileLayer -> float mask keeps the pixels a float32 mask marks as valid', t => {+ // A tile server writes the mask in the band's own data type, marking valid pixels with that+ // type's maximum: 255 for uint8, 65535 for uint16, and FLT_MAX for float32. The default upper+ // bound therefore has to sit at or above FLT_MAX, or every valid pixel of a float raster is+ // discarded and the layer draws nothing.+ const FLOAT32_MAX = 3.4028234663852886e38;+ const uniforms = RasterWebGL.maskFloat.getUniforms({imageMask: {}, maskKeepMin: 1});++ t.ok(uniforms, 'should return uniforms for a mask texture');+ t.equal(uniforms.keepMin, 1, 'should keep the given lower bound');+ t.ok(+ uniforms.keepMax >= FLOAT32_MAX,+ `should not discard valid float pixels: keepMax ${uniforms.keepMax} < ${FLOAT32_MAX}`+ );+ t.end();+});