ST_Transform

Updated at:
Copy as MD

Reprojects a raster object from one spatial reference system to another using a specified resampling algorithm. Available resampling methods are Near (default), Average, Cubic, and Bilinear.

Syntax

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

Parameters

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

processExpr fields

FieldTypeDefaultDescription
resampleTextNearThe resampling algorithm. Valid values: Near, Average, Cubic, Bilinear.
nodataBooleanfalseSpecifies whether nodata pixels in the source raster are treated as valid. Set to true to skip resampling nodata pixels. Set to false to resample them.
nodataValuefloat8 or float8[]NULLThe nodata value(s) to assign in the output raster. Specify one value to apply to all bands, or one value per band. If specifying one value per band, the count must match the number of bands.
Warning

If the source raster contains no nodata pixels, set nodata to false and omit nodataValue to avoid image artifacts.

Choosing a resampling algorithm

AlgorithmCharacteristicsUse when
NearFastest; preserves original pixel values exactlyCategorical or classified data (land cover, labels)
BilinearSmooth interpolation using 4 surrounding pixelsContinuous data where smoothness matters
CubicHigher-quality interpolation using 16 surrounding pixelsHigh-resolution imagery requiring fine detail
AverageComputes the mean of contributing pixelsDownsampling or reducing noise

storageOption fields

FieldTypeDefaultDescription
chunkingBooleanSame as sourceWhether to store the output raster as chunks.
chunkdimStringSame as sourceChunk dimensions. Takes effect only when chunking is true.
chunktableString''The name of the chunk table. If 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 persist the output raster permanently.
compressionStringSame as sourceCompression format. Supported values: None, JPEG, Zlib, PNG, LZO, LZ4.
qualityIntegerSame as sourceImage quality. Applies only when compression is JPEG.
interleavingStringSame as sourceBand interleaving format. 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.
Warning

If chunktable is blank or NULL, the output raster exists only for the current session. To save the raster permanently, specify a chunk table name in the chunktable field.

Examples

The examples below load source data and write transformed results in two ways: to a permanent chunk table, and to a session-only temporary table.

-- Set up the source table and load a sample raster
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', '{}')
);

Write to a permanent chunk table

All three INSERT statements below use the chunktable field to persist the output raster. They differ in how nodata is handled.

CREATE TABLE rat_transform_result (id integer, rast raster);

-- No nodataValue specified
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;

-- Single nodataValue; nodata pixels are skipped during resampling
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;

-- Multiple nodataValues (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;

Write to a session-only temporary table

Omit chunktable to store the result in a temporary chunk table. Use this approach for intermediate computations within a session.

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

INSERT INTO rast_clip_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;