Skip to content

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

fix(layers): reject truncated raster selection statistics (#3965)

Igor Dykhtaeeb7ee403831
diff --git a/src/layers/src/raster-tile/raster-tile-identify.ts b/src/layers/src/raster-tile/raster-tile-identify.ts
index 6a235ece3..39230ad1c 100644
--- a/src/layers/src/raster-tile/raster-tile-identify.ts
+++ b/src/layers/src/raster-tile/raster-tile-identify.ts
@@ -501,6 +501,8 @@ function isInsidePolygon(
* Uses the highest-zoom tile at each location (tiles are scanned independently;
* overlapping parent tiles are skipped when a child of higher zoom exists for that pixel
* by only using tiles at the maximum zoom present in the intersecting set).
+ * Throws rather than returning partial statistics if the scan limit is exceeded.
+ * The limit includes inspected pixels outside the polygon and NoData pixels.
*/
export function computeRasterZonalStats(
tiles: Array<TileLike | null | undefined>,
@@ -541,7 +543,9 @@ export function computeRasterZonalStats(
for (let row = 0; row < size.height; row++) {
for (let col = 0; col < size.width; col++) {
if (scanned >= MAX_ZONAL_PIXELS) {
- break;
+ throw new Error(
+ 'Raster statistics exceeded the 2,000,000-pixel scan limit. Select a smaller area or zoom out.'
+ );
}
scanned += 1;
const [lng, lat] = tilePixelToLngLat(col, row, size.width, size.height, index);
@@ -571,9 +575,6 @@ export function computeRasterZonalStats(
addStat(derivedAcc, derived.value);
}
}
- if (scanned >= MAX_ZONAL_PIXELS) {
- break;
- }
}
}
diff --git a/test/browser/layer-tests/raster-tile-layer-specs.js b/test/browser/layer-tests/raster-tile-layer-specs.js
index d1b2ad214..00cfa505f 100644
--- a/test/browser/layer-tests/raster-tile-layer-specs.js
+++ b/test/browser/layer-tests/raster-tile-layer-specs.js
@@ -14,6 +14,7 @@ import {
filterAvailablePresets,
getDataSourceParams,
computeDerivedValue,
+ computeRasterZonalStats,
formatRasterIdentifyRows,
lngLatToTileUV,
mercatorPixelSizeForBbox,
@@ -29,6 +30,7 @@ import {testCreateCases} from 'test/helpers/layer-utils';
import {getModules, loadNpyArray} from '../../../src/layers/src/raster-tile/gpu-utils';
import * as rasterGpuUtils from '../../../src/layers/src/raster-tile/gpu-utils';
import * as rasterImages from '../../../src/layers/src/raster-tile/image';
+import * as rasterExtract from '../../../src/layers/src/raster-tile/raster-tile-extract';
const {RasterTileLayer} = KeplerGlLayers;
@@ -1402,6 +1404,104 @@ test('#RasterTileLayer -> hover, picking, and extract from loaded tiles', t => {
t.end();
});
+test('#RasterTileLayer -> scan limit never returns partial statistics', async t => {
+ const polygon = {
+ type: 'Feature',
+ properties: {shape: 'Rectangle'},
+ geometry: {
+ type: 'Polygon',
+ coordinates: [
+ [
+ [-180, -86],
+ [180, -86],
+ [180, 86],
+ [-180, 86],
+ [-180, -86]
+ ]
+ ]
+ }
+ };
+ const context = {isPMTiles: false, bandCombination: 'single', loadAssetIds: ['value']};
+ const band = {width: 2000, height: 1000, data: new Uint8Array(2_000_000).fill(1)};
+ band.data[band.data.length - 1] = 100;
+ const tile = {index: {x: 0, y: 0, z: 0}, data: {images: {imageBands: [band]}}};
+ const exact = computeRasterZonalStats([tile], polygon, context);
+ t.equal(exact.pixelCount, 2_000_000, 'exactly the scan limit is supported');
+ t.equal(exact.bands.value.max, 100, 'the final pixel is included');
+ const extra = {
+ ...tile,
+ data: {images: {imageBands: [{width: 1, height: 1, data: new Uint8Array([200])}]}}
+ };
+ const scanLimit = /2,000,000-pixel scan limit/;
+ t.throws(
+ () => computeRasterZonalStats([tile, extra], polygon, context),
+ scanLimit,
+ 'one more pixel in a later tile fails instead of returning truncated totals'
+ );
+ const tinyPolygon = {
+ ...polygon,
+ geometry: {
+ type: 'Polygon',
+ coordinates: [
+ [
+ [0, 0],
+ [0.01, 0],
+ [0.01, 0.01],
+ [0, 0.01],
+ [0, 0]
+ ]
+ ]
+ }
+ };
+ t.throws(
+ () => computeRasterZonalStats([tile, extra], tinyPolygon, context),
+ scanLimit,
+ 'the work limit counts scanned pixels, including those outside the selection'
+ );
+
+ const layer = new RasterTileLayer({id: 'raster-scan-limit', dataId: 'raster'});
+ layer._identifyContext = context;
+ layer._loadedTiles = [
+ {
+ ...tile,
+ data: {
+ images: {imageBands: [{...band, height: 1001, data: new Uint8Array(2_002_000).fill(1)}]}
+ }
+ }
+ ];
+ t.throws(
+ () => layer.extractInsideFeature(polygon),
+ scanLimit,
+ 'extraction also fails when additional pixels are in the same tile'
+ );
+
+ const download = sinon.stub(rasterExtract, 'downloadBlob');
+ const mosaic = sinon.stub(rasterExtract, 'mosaicTilesToPng').resolves(null);
+ try {
+ await layer.downloadClip(polygon);
+ t.fail('oversized clip statistics must reject');
+ } catch (error) {
+ t.ok(scanLimit.test(error.message), 'clip download propagates the actionable limit error');
+ } finally {
+ t.equal(download.callCount, 0, 'no partial statistics file is downloaded');
+ t.equal(mosaic.callCount, 0, 'no clip image work starts after a statistics failure');
+ download.restore();
+ mosaic.restore();
+ }
+
+ layer._loadedTiles = [extra];
+ const ordinary = layer.extractInsideFeature(polygon);
+ t.equal(ordinary.rowCount, 1, 'ordinary selections still extract after a failed attempt');
+ t.equal(ordinary.features[0].properties.pixel_count, 1, 'ordinary counts are unchanged');
+ t.equal(ordinary.features[0].properties.value_max, 200, 'ordinary extrema are unchanged');
+ t.deepEqual(
+ computeRasterZonalStats([], polygon, context),
+ {pixelCount: 0, bands: {}},
+ 'empty tiles are unchanged'
+ );
+ t.end();
+});
+
test('#RasterTileLayer -> raster stats sidecar is GeoJSON with bbox rectangle', t => {
const geometry = {
type: 'Polygon',
diff --git a/test/node/reducers/vis-state-test.js b/test/node/reducers/vis-state-test.js
index 9dc163692..93724832b 100644
--- a/test/node/reducers/vis-state-test.js
+++ b/test/node/reducers/vis-state-test.js
@@ -8137,6 +8137,49 @@ test('#visStateReducer -> EXTRACT_DATA_FROM_FEATURE raster awaits clip download'
t.end();
});
+test('#visStateReducer -> oversized raster statistics show an error before download', t => {
+ drainTasksForTesting();
+ const layer = new KeplerGlLayers.RasterTileLayer({id: 'raster-limit', dataId: 'raster-data'});
+ layer._loadedTiles = [
+ {
+ index: {x: 0, y: 0, z: 0},
+ data: {
+ images: {imageBands: [{width: 2000, height: 1001, data: new Uint8Array(2_002_000).fill(1)}]}
+ }
+ }
+ ];
+ const download = sinon.stub(layer, 'downloadClip').resolves({png: true, json: true});
+ const startState = {
+ ...INITIAL_VIS_STATE,
+ layers: [layer],
+ datasets: {
+ 'raster-data': {id: 'raster-data', fields: [], dataContainer: createDataContainer([])}
+ },
+ editor: {...INITIAL_VIS_STATE.editor, selectedFeature: mockPolygonFeature}
+ };
+ try {
+ const state = reducer(startState, VisStateActions.extractDataFromFeature({layerId: layer.id}));
+ const tasks = drainTasksForTesting();
+ t.equal(tasks.length, 1, 'queues one notification');
+ const notification = succeedTaskInTest(tasks[0], {png: true, json: true});
+ t.equal(notification.payload.type, 'error', 'the user receives an error instead of success');
+ t.ok(
+ notification.payload.message.includes('2,000,000-pixel scan limit'),
+ 'the notification explains the scan limit'
+ );
+ t.ok(
+ notification.payload.message.includes('zoom out'),
+ 'the notification includes a recovery action'
+ );
+ t.equal(download.callCount, 0, 'no partial clip or statistics download starts');
+ t.equal(state.datasets, startState.datasets, 'no partial dataset is installed');
+ } finally {
+ download.restore();
+ drainTasksForTesting();
+ }
+ t.end();
+});
+
test('#visStateReducer -> CONVERT_EDITOR_FEATURES_TO_LAYER disabled by config', t => {
initApplicationConfig({enableDrawOnMapSketches: false});