ST_SummaryStats

Updated at:
Copy as MD

Calculates statistics for a band set of a raster object.

Syntax

raster ST_SummaryStats(raster raster_obj)
raster ST_SummaryStats(raster raster_obj, cstring statsOption)
raster ST_SummaryStats(raster raster_obj, cstring bands, cstring statsOption)

Parameters

ParameterDescription
raster_objThe raster object to analyze.
bandsThe bands to include. Accepts an integer array or an integer range. Band indexes start at 0. Examples: '0', '1-3', '1,2,3'.
statsOptionsA JSON string specifying calculation options. See the statsOptions fields below.

statsOptions fields

FieldTypeDefaultDescription
approxBooleantrueSpecifies whether to use sampling to calculate statistics. Set to false to calculate exact statistics across all pixels.
By default, approx is true, which means statistics are calculated from a sample of pixels and may not be exact. To get precise results, pass '{"approx": false}' as statsOption.

Examples

Basic usage

Calculate statistics for all bands using the default sampling method.

DO $$
DECLARE
    rast raster;
BEGIN
    SELECT raster_obj INTO rast FROM raster_table WHERE id = 1;
    rast = ST_SummaryStats(rast);
    UPDATE raster_table SET raster_obj = rast WHERE id = 1;
END;
$$ LANGUAGE 'plpgsql';

Exact statistics with band selection

Calculate exact statistics (no sampling) for bands 0 through 2.

DO $$
DECLARE
    rast raster;
BEGIN
    SELECT raster_obj INTO rast FROM raster_table WHERE id = 1;
    rast = ST_SummaryStats(rast, '0-2', '{"approx": false}');
    UPDATE raster_table SET raster_obj = rast WHERE id = 1;
END;
$$ LANGUAGE 'plpgsql';