All Products
Search
Document Center

ApsaraDB RDS:ST_LinearStretch

Last Updated:Mar 28, 2026

Stretches the pixel values of a raster linearly and returns a new raster. Linear stretching redistributes pixel values across the full output range to improve image brightness and contrast, making features easier to distinguish visually.

Two stretching methods are available:

  • Percentile-based stretch — clips extreme pixel values at the specified low and high percentiles, then redistributes the remaining values across the full output range (0–255). Use this method when the raster's actual value distribution matters more than absolute thresholds.

  • Fixed-range stretch — maps a specified minimum and maximum value directly to the output range. Use this method when you know the exact input bounds for each band.

Syntax

Syntax 1: Percentile-based stretch

raster ST_LinearStretch(raster raster_obj,
                        integer pyramidLevel DEFAULT 0,
                        cstring bands DEFAULT '',
                        integer minRatio DEFAULT 0,
                        integer maxRatio DEFAULT 100,
                        cstring storageOption DEFAULT '',
                        cstring parallelOption DEFAULT '{}')

Syntax 2: Fixed-range stretch

raster ST_LinearStretch(raster raster_obj,
                        float8[] minValues,
                        float8[] maxValues,
                        integer pyramidLevel DEFAULT 0,
                        cstring bands DEFAULT '',
                        cstring storageOption DEFAULT '',
                        cstring parallelOption DEFAULT '{}')

Parameters

Common parameters

ParameterDescriptionDefault
raster_objThe raster to stretch.
pyramidLevelThe pyramid level to stretch. Valid values start from 0.0
bandsThe band IDs to stretch. Use '0-2' for a range or '1,2,3' for individual bands. Valid IDs start from 0. An empty string selects all bands.'' (all bands)
storageOptionStorage options for the output raster, as a JSON string. See Storage options.''
parallelOptionParallel execution options, as a JSON string. See Parallel options.'{}'

Syntax 1 parameters (percentile-based)

ParameterDescription
minRatioThe low-end percentile cutoff. Pixel values below this percentile are mapped to 0. Valid values: 0–100.
maxRatioThe high-end percentile cutoff. Pixel values above this percentile are mapped to 255. Valid values: 0–100. Must be greater than minRatio.
To use this syntax, you can use ST_BuildPercentiles or ST_ComputeStatistics to compute the raster's percentile statistics first.

Syntax 2 parameters (fixed-range)

ParameterDescription
minValuesAn array of minimum values, one per band. The array length must equal the number of bands being stretched.
maxValuesAn array of maximum values, one per band. The array length must equal the number of bands being stretched.

Storage options

Pass these fields as a JSON string in the storageOption parameter.

FieldTypeDefaultDescription
chunkingbooleanSame as input rasterSpecifies whether to store the output raster as chunks.
chunkdimstringSame as input rasterThe chunk dimensions. Valid only when chunking is true.
chunktablestring''The name of the chunk table for the output raster. If empty or NULL, a temporary chunk table with a random name is created and is valid only for the current session. Specify a name to create a permanent chunk table.
compressionstringSame as input rasterThe compression format. Valid values: NONE, JPEG, ZLIB, PNG, LZO, LZ4.
qualityintegerSame as input rasterThe image quality after compression. Valid values: 1–99. Valid only when compression is JPEG.
interleavingstringSame as input rasterThe data interleaving method. Valid values: bip (band interleaved by pixel, BIP), bil (band interleaved by line, BIL), bsq (band sequential, BSQ).
endianstringSame as input rasterThe byte order. Valid values: NDR (little endian), XDR (big endian).
celltypestringSame as input rasterThe pixel type. Valid values: 1bb, 2bui, 4bui, 8bsi, 8bui, 16bsi, 16bui, 32bsi, 32bui, 64bsi, 64bui, 32bf, 64bf.

Parallel options

Pass these fields as a JSON string in the parallelOption parameter.

FieldTypeDefaultDescription
parallelintegerValue of ganos.parallel.degreeThe degree of parallelism. Valid values: 1–64.

How it works

Percentile-based stretch

Pixel values in the bottom minRatio percentile are clamped to 0, and pixel values in the top (100 - maxRatio) percentile are clamped to 255. All remaining values are linearly redistributed across the full 0–255 range.

Example: A raster's histogram spans values 33–206. With minRatio=2 and maxRatio=98:

  • The bottom 2% of values (33–45) are mapped to 0.

  • The top 2% of values (198–206) are mapped to 255.

  • All values between 45 and 198 are redistributed linearly across 0–255.

Fixed-range stretch

The pixel values of each band are linearly mapped from the [minValues[i], maxValues[i]] range to 0–255. This redistributes values across the full histogram range, improving brightness and contrast.

Example: In an 8-bit dataset, setting minValues=[35] and maxValues=[206] maps value 35 to 0 and value 206 to 255, with all intermediate values distributed proportionally.

Examples

Example 1: Percentile-based stretch

Stretch bands 0–2 at pyramid level 1, clipping the bottom 2% and top 2% of pixel values.

INSERT INTO raster_table
SELECT 100, ST_LinearStretch(rast,
                             1,        -- pyramid level
                             '0-2',    -- bands
                             2,        -- minRatio: clip bottom 2%
                             98,       -- maxRatio: clip top 2%
                             '{"chunktable":"chunk_table", "chunking":true}',
                             '{"parallel":4}')
FROM raster_table
WHERE id = 1;

Example 2: Fixed-range stretch

Stretch bands 0–2 at pyramid level 0, mapping the fixed range [100, 200] to [0, 255] for all three bands.

INSERT INTO raster_table
SELECT 200, ST_LinearStretch(rast,
                             ARRAY[100, 100, 100]::float8[],  -- minValues per band
                             ARRAY[200, 200, 200]::float8[],  -- maxValues per band
                             0,        -- pyramid level
                             '0-2',    -- bands
                             '{"chunktable":"chunk_table", "chunking":true}',
                             '{"parallel":4}')
FROM raster_table
WHERE id = 1;

What's next