ST_Resize

更新时间:
复制 MD 格式

Resizes the pixel dimensions of a raster object to a new width and height while keeping the geospatial extent unchanged.

Syntax

raster ST_Resize(raster rast,
           integer outWidth,
           integer outHeight,
           cstring processExpr default '',
           cstring storageOption default '')

Parameters

ParameterTypeDescription
rastrasterThe source raster object to resize.
outWidthintegerThe width of the output raster, in pixels.
outHeightintegerThe height of the output raster, in pixels.
processExprcstringA JSON string that controls resampling and nodata handling. Defaults to an empty string.
storageOptioncstringA JSON string that controls how the output raster is stored. Defaults to an empty string.

processExpr fields

FieldTypeDefaultDescription
resampletextNearThe resampling algorithm. Valid values: Near, Average, Cubic, Bilinear.
nodataBooleanfalseSpecifies whether nodata values in the source raster are treated as valid. If true, pixels with nodata values are preserved and not resampled. If false, they are resampled like normal pixels.
nodataValuefloat8 or float8[]NULLThe nodata value to assign in the output raster. Specify a single value to apply it to all bands, or an array whose length matches the number of bands.
If the source raster has no nodata pixels, set nodata to false and omit nodataValue. Otherwise, image artifacts may occur.

storageOption fields

FieldTypeDefaultDescription
chunkingBooleanSame as sourceSpecifies whether to store the output raster as chunks.
chunkdimStringSame as sourceThe chunk dimensions, for example (256,256,1). Takes effect only when chunking is true.
chunktableString'' (empty)The name of the chunk table. If left blank or NULL, a temporary chunk table with a random name is created for the current session and deleted when the session ends. Specify a name to create a permanent chunk table.
compressionStringSame as sourceThe compression format. Valid values: None, JPEG, Zlib, PNG, LZO, LZ4.
qualityIntegerSame as sourceThe JPEG quality level. Takes effect only when compression is JPEG.
interleavingStringSame as sourceThe interleaving type: bip (band interleaved by pixel, BIP), bil (band interleaved by line, BIL), or bsq (band sequential, BSQ).
endianStringSame as sourceThe byte order: NDR (little endian) or XDR (big endian).
If chunktable is NULL or an empty string, the output raster is stored in a session-only temporary chunk table. The table is deleted when the session ends. To persist the output across sessions, specify a chunk table name in chunktable.

Examples

The following examples resize a 512x512 raster to 1024x1024 pixels using nearest neighbor resampling. All examples that write to a permanent chunk table use result_rbt as the chunk table name.

First, set up the source data:

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', '{}')
);

CREATE TABLE rat_resize_result (id integer, rast raster);

Example 1: Resize without nodata handling

Use this when the source raster has no nodata pixels.

INSERT INTO rat_resize_result (id, rast)
SELECT
    10,
    ST_Resize(
        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;

Example 2: Resize with a single nodata value

Pixels with nodata values are preserved, and a nodata value of 255 is assigned to all bands in the output.

INSERT INTO rat_resize_result (id, rast)
SELECT
    11,
    ST_Resize(
        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;

Example 3: Resize with per-band nodata values

Specify one nodata value per band. The array length must match the number of bands in the output raster.

INSERT INTO rat_resize_result (id, rast)
SELECT
    12,
    ST_Resize(
        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;

Example 4: Resize to a session-only temporary raster

Omit chunktable to store the output in a temporary chunk table. The result is available only within the current session and is useful for intermediate computations.

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

INSERT INTO rat_resize_result_temp (id, rast)
SELECT
    1,
    ST_Resize(
        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;