ST_Rescale

更新时间:
复制 MD 格式

Rescales the pixel range of a raster object by adjusting its resolution. The geospatial extent remains unchanged.

Syntax

raster ST_Rescale(raster rast,
           f8 scale_x,
           f8 scale_y,
             cstring processexpr default '',
             cstring storageOption default '');

raster ST_Rescale(raster raster,
           f8 scale_xy,
             cstring processexpr default '',
             cstring storageOption default '')

Parameters

ParameterDescription
rastThe raster object to rescale.
scale_xThe scale ratio in the x direction.
scale_yThe scale ratio in the y direction.
scale_xyThe scale ratio applied uniformly to both x and y directions.
processexprA JSON string that controls resampling behavior and nodata handling. Defaults to '' (uses default values for all fields). See processexpr fields.
storageOptionA JSON string that controls how the output raster is stored. Defaults to '' (inherits settings from the input raster). See storageOption fields.

processexpr fields

FieldTypeDefaultDescription
resampletextNearResampling algorithm. Valid values: Near, Average, Cubic, Bilinear.
nodataBooleanfalseSpecifies whether nodata values are treated as valid. If true, pixels with nodata values are not resampled. If false, they are resampled.
nodataValuefloat8 or float8[]NULLThe nodata value(s) to assign in the output raster. Specify a single value to apply to all bands, or one value per band (the count must match the number of bands in the output raster).
Warning

Set nodata to false and omit nodataValue if the input raster has no nodata pixels. Specifying these fields unnecessarily can cause image artifacts.

storageOption fields

FieldTypeDefaultDescription
chunkingBooleanSame as inputSpecifies whether to store the output raster as chunks.
chunkdimStringSame as inputChunk dimensions. Takes effect only when chunking is true.
chunktableString''The name of the chunk table for the output raster. If set to NULL or '', a temporary chunk table with a random name is created and is valid only for the current session. To retain the output raster permanently, specify a table name.
compressionStringSame as inputCompression format. Valid values: None, JPEG, Zlib, PNG, LZO, LZ4.
qualityIntegerSame as inputImage quality. Takes effect only when compression is JPEG.
interleavingStringSame as inputInterleaving type. Valid values: bip (band interleaved by pixel, BIP), bil (band interleaved by line, BIL), bsq (band sequential, BSQ).
endianStringSame as inputByte order. Valid values: NDR (little-endian), XDR (big-endian).
Note

If chunktable is NULL or '', the output raster is stored in a temporary chunk table that is deleted when the session ends. Specify a permanent chunk table name to persist the output across sessions.

Examples

The following examples use a 512x512 single-band raster imported from a GeoTIFF file and rescale it to 1024x1024 pixels.

-- Set up the source table
CREATE TABLE IF NOT EXISTS datasource_table (id integer, rast raster);
INSERT INTO datasource_table VALUES (
    1,
    ST_ImportFrom('rbt', '$(RAST_DATA_DIR)/512_512_1_bsq_8u_geo.tif', '{}')
);

Store output in a permanent chunk table

Specify chunktable in storageOption to retain the output raster after the session ends.

CREATE TABLE rat_rescale_result (id integer, rast raster);

-- Rescale with default nodata handling (nodata field omitted, uses default false)
INSERT INTO rat_rescale_result (id, rast)
SELECT 10, ST_Rescale(
    rast, 1024, 1024,
    '{"resample":"Near","nodata":false}',
    '{"chunking":true,"chunkdim":"(256,256,1)","compression":"none","interleaving":"bsq","chunktable":"result_rbt"}'
)
FROM datasource_table
WHERE id = 1;

-- Rescale with a single nodata value; nodata pixels are not resampled
INSERT INTO rat_rescale_result (id, rast)
SELECT 11, ST_Rescale(
    rast, 1024, 1024,
    '{"resample":"Near","nodata":true,"nodatavalue":255}',
    '{"chunking":true,"chunkdim":"(256,256,1)","compression":"none","interleaving":"bsq","chunktable":"result_rbt"}'
)
FROM datasource_table
WHERE id = 1;

-- Rescale with one nodata value per band; nodata pixels are resampled
INSERT INTO rat_rescale_result (id, rast)
SELECT 12, ST_Rescale(
    rast, 1024, 1024,
    '{"resample":"Near","nodata":false,"nodatavalue":[255,255,255]}',
    '{"chunking":true,"chunkdim":"(256,256,1)","compression":"none","interleaving":"bsq","chunktable":"result_rbt"}'
)
FROM datasource_table
WHERE id = 1;

Use a temporary chunk table for in-session computation

Omit chunktable to store the output in a session-scoped temporary table. The output is available for nested computations within the current session but is discarded when the session ends.

CREATE TEMP TABLE rat_rescale_result_temp (id integer, rast raster);

INSERT INTO rat_rescale_result_temp (id, rast)
SELECT 1, ST_Rescale(
    rast, 1024, 1024,
    '{"resample":"Near","nodata":false,"nodataValue":[255,255,255]}',
    '{"chunking":true,"chunkdim":"(256,256,1)","compression":"none","interleaving":"bsq"}'
)
FROM datasource_table
WHERE id = 1;