ST_Transform

更新时间:
复制 MD 格式

Reprojects a raster object from one spatial reference system (SRS) to another, transforming the underlying coordinates and resampling pixel values accordingly.

Syntax

raster ST_Transform(raster rast,
           integer outSrid,
           cstring processExpr default '',
           cstring storageOption default '')

Parameters

ParameterTypeDefaultDescription
rastrasterThe source raster object to reproject.
outSridintegerThe SRID of the target spatial reference system. Must be a valid SRID in the spatial_ref_sys table.
processExprcstring''JSON string controlling the resampling algorithm and nodata handling.
storageOptioncstring''JSON string controlling how the output raster is stored.

processExpr fields

FieldTypeDefaultDescription
resampleTextNearResampling 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 not resampled. If false, those pixels are resampled.
nodataValuefloat8 or float8[]NULLNodata value to assign to the output raster. Specify a single value to apply it to all bands, or an array of values—one per band. The array length must match the number of bands.
Warning

Use nodata and nodataValue with care. If the source raster has no nodata pixels, set nodata to false and omit nodataValue. Misconfiguring these fields can introduce image artifacts.

storageOption fields

FieldTypeDefaultDescription
chunkingBooleanSame as sourceSpecifies whether to store the output raster as chunks.
chunkdimStringSame as sourceChunk dimensions. Takes effect only when chunking is true.
chunktableString''Name of the chunk table for the output raster. If set to NULL or an empty string, a temporary chunk table with a random name is created and exists only for the current session. To persist the output raster beyond the session, specify a table name.
compressionStringSame as sourceCompression format. Valid values: None, JPEG, Zlib, PNG, LZO, LZ4.
qualityIntegerSame as sourceImage quality level. Takes effect only when compression is JPEG.
interleavingStringSame as sourcePixel interleaving type. Valid values: bip (band interleaved by pixel, BIP), bil (band interleaved by line, BIL), bsq (band sequential, BSQ).
endianStringSame as sourceByte order. NDR for little-endian, XDR for big-endian.

Examples

All examples reproject a raster from its source SRS to EPSG:32652 using nearest-neighbor resampling.

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

Persist the output to a permanent chunk table

Specify a chunktable name to save the output raster across sessions.

CREATE TABLE rat_transform_result (id integer, rast raster);

-- Without nodata handling
INSERT INTO rat_transform_result (id, rast)
SELECT 10, ST_Transform(
    rast,
    32652,
    '{"resample":"Near","nodata":false}',
    '{"chunking":true,"chunkdim":"(256,256,1)","compression":"none","interleaving":"bsq","chunktable":"result_rbt"}'
)
FROM datasource_table
WHERE id = 1;

-- With nodata enabled and a single nodata value (applies to all bands)
INSERT INTO rat_transform_result (id, rast)
SELECT 11, ST_Transform(
    rast,
    32652,
    '{"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;

-- With multiple nodata values (one per band)
INSERT INTO rat_transform_result (id, rast)
SELECT 12, ST_Transform(
    rast,
    32652,
    '{"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 temporary chunk table. The table is deleted when the session ends.

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

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