Skip to content

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

fix: apply the SAVI soil adjustment multiplier correctly (#3942)

Igor Dykhta11774eb154bc
diff --git a/src/deckgl-layers/src/raster/webgl/spectral-indices/savi.ts b/src/deckgl-layers/src/raster/webgl/spectral-indices/savi.ts
index e3342ac0f..97b19ca73 100644
--- a/src/deckgl-layers/src/raster/webgl/spectral-indices/savi.ts
+++ b/src/deckgl-layers/src/raster/webgl/spectral-indices/savi.ts
@@ -5,14 +5,14 @@ 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
+// https://www.usgs.gov/landsat-missions/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;
+ float numerator = (band5 - band4) * 1.5;
+ float denominator = band5 + band4 + 0.5;
return numerator / denominator;
}
`;
diff --git a/src/layers/src/raster-tile/raster-tile-identify.ts b/src/layers/src/raster-tile/raster-tile-identify.ts
index c3c8f8401..a786ea261 100644
--- a/src/layers/src/raster-tile/raster-tile-identify.ts
+++ b/src/layers/src/raster-tile/raster-tile-identify.ts
@@ -252,11 +252,11 @@ export function computeDerivedValue(
if (!Number.isFinite(g)) {
return null;
}
- const denominator = (r + g + 0.5) * 1.5;
+ const denominator = r + g + 0.5;
if (denominator === 0) {
return null;
}
- return {name: derivedLabel || 'SAVI', value: (r - g) / denominator};
+ return {name: derivedLabel || 'SAVI', value: (1.5 * (r - g)) / denominator};
}
case BandCombination.ModifiedSoilAdjustedVegetationIndex:
case 'modifiedSoilAdjustedVegetationIndex': {
diff --git a/test/browser/layer-tests/raster-tile-layer-specs.js b/test/browser/layer-tests/raster-tile-layer-specs.js
index 8c509f760..2b921b9ea 100644
--- a/test/browser/layer-tests/raster-tile-layer-specs.js
+++ b/test/browser/layer-tests/raster-tile-layer-specs.js
@@ -1258,6 +1258,62 @@ test('#RasterTileLayer -> replacing a PMTiles URL reloads cached tile content',
t.end();
});
+test('#RasterTileLayer -> SAVI uses the soil adjustment multiplier in CPU and shader arithmetic', t => {
+ // Evaluate this scalar GLSL function as JavaScript to check the shipped expression.
+ // This is an arithmetic parity test, not a WebGL rendering test.
+ const body = RasterWebGL.soilAdjustedVegetationIndex.fs.match(/\{([\s\S]*)\}/)[1];
+ const shaderSavi = new Function('image', body.replace(/\bfloat\b/g, 'const'));
+ for (const [nir, red, expected] of [
+ [0.8, 0.2, 0.6],
+ [0.2, 0.8, -0.6],
+ [0.2, 0.2, 0],
+ [0, 0, 0],
+ [0.5, 0, 0.75]
+ ]) {
+ const cpu = computeDerivedValue([nir, red], 'soilAdjustedVegetationIndex');
+ t.equal(cpu.name, 'SAVI', 'derived value retains its label');
+ t.ok(Math.abs(cpu.value - expected) < 1e-10, `CPU SAVI for ${nir}, ${red} is ${expected}`);
+ t.ok(
+ Math.abs(shaderSavi({r: nir, g: red}) - expected) < 1e-10,
+ 'GLSL arithmetic matches the reference'
+ );
+ }
+ for (const values of [[NaN, 0.2], [0.8, Infinity], [0.8], [-0.5, 0]]) {
+ t.equal(
+ computeDerivedValue(values, 'soilAdjustedVegetationIndex'),
+ null,
+ 'invalid inputs or zero denominator stay unavailable'
+ );
+ }
+ const context = {isPMTiles: false, bandCombination: 'soilAdjustedVegetationIndex'};
+ const sample = {
+ lng: 0,
+ lat: 0,
+ tileIndex: {x: 0, y: 0, z: 0},
+ col: 0,
+ row: 0,
+ bandValues: [0.8, 0.2],
+ bandLabels: ['nir', 'red'],
+ nodata: false
+ };
+ t.ok(
+ formatRasterIdentifyRows(sample, context).some(
+ row => row.name === 'SAVI' && row.value === '0.600000'
+ ),
+ 'identify displays the corrected derived value'
+ );
+ const nodataRows = formatRasterIdentifyRows({...sample, nodata: true}, context);
+ t.ok(
+ nodataRows.some(row => row.value === 'NoData'),
+ 'masked samples remain NoData'
+ );
+ t.notOk(
+ nodataRows.some(row => row.name === 'SAVI'),
+ 'masked samples do not emit a derived value'
+ );
+ t.end();
+});
+
test('#RasterTileLayer -> switching STAC datasets with matching bands reloads cached imagery', async t => {
const layer = new RasterTileLayer({id: 'stac-cache', dataId: 'raster'});
layer.updateLayerVisConfig({enableTerrain: false, preset: 'trueColor'});