All Products
Search
Document Center

PolarDB:ST_ClipToRast

Last Updated:Mar 28, 2026

Clips a raster object to a geometry boundary and returns the result as a new raster object. By default, all bands are clipped using the exact geometry shape, and the result is stored in a session-scoped temporary chunk table.

Syntax

raster ST_ClipToRast(raster raster_obj,
                     geometry geom,
                     integer pyramidLevel default 0,
                     cstring bands default '',
                     float8[] nodata default NULL,
                     cstring clipOption default '',
                     cstring storageOption default '')

Parameters

ParameterTypeDefaultDescription
raster_objrasterThe raster object to clip.
geomgeometryThe geometry object used for clipping.
pyramidLevelinteger0The pyramid level of the raster to clip.
bandscstring''The bands to clip. Use '0-2' for a range or '1,2,3' for a list. Band index starts at 0. Leave blank to clip all bands.
nodatafloat8[]NULLThe nodata values for pixels outside the geometry, specified as a float8[] array. If fewer values are provided than the number of bands, the remaining bands use 0.
clipOptioncstring''Clipping options as a JSON string. See clipOption parameters.
storageOptioncstring''Storage options for the returned raster as a JSON string. See storageOption parameters.

clipOption parameters

ParameterTypeDefaultDescription
window_clipboolfalseSpecifies whether to clip using the bounding box of the geometry instead of its exact shape. When set to true, uses the minimum bounding rectangle (MBR); when set to false, uses the geometry object.
rast_coordboolfalseSpecifies whether the geometry coordinates are in pixel space. When set to true, the x coordinate is the column number and the y coordinate is the row number.

storageOption parameters

ParameterTypeDefaultDescription
chunkingbooleanSame as original rasterSpecifies whether to store the result as chunks.
chunkdimstringSame as original rasterThe chunk dimension. Takes effect only when chunking is true.
chunktablestring''The name of the chunk table for storing the result. If left blank, a temporary chunk table with a random name is created and is valid only for the current session. To retain the clipped raster across sessions, specify a table name.
compressionstringlz4The compression algorithm. Valid values: none, jpeg, zlib, png, lzo, lz4.
qualityinteger75The compression quality. Applies only to the jpeg algorithm.
interleavingstringSame as original rasterThe interleaving method. Valid values: bip (band interleaved by pixel, BIP), bil (band interleaved by line, BIL), bsq (band sequential, BSQ).
endianstringSame as original rasterThe byte order. Valid values: NDR (little endian), XDR (big endian).

Usage notes

  • Session persistence: If chunktable is left blank or set to '', the result is stored in a temporary chunk table that is automatically dropped at the end of the session. Specify a chunk table name to retain the clipped raster across sessions.

  • Result size limit: The default cache size for clipping is 100 MB. To adjust this limit, set the ganos.raster.clip_max_buffer_size parameter.

Examples

All examples clip from a source table named clip_table using a polygon geometry.

Clip to a temporary table using default settings

Uses the exact geometry shape, clips all bands, and stores the result in a session-scoped temporary table.

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

INSERT INTO rast_clip_result_temp(id, rast)
SELECT 1, ST_ClipToRast(rast, ST_geomfromtext('Polygon((0 0, 45 45, 90 45, 45 0, 0 0))', 4326), 0)
FROM clip_table
WHERE id = 1;

Clip with a custom nodata fill and persist the result

Uses white (RGB 254, 254, 254) as the background fill for clipped areas and stores the result in a named permanent chunk table clip_rbt.

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

INSERT INTO rast_clip_result(id, rast)
SELECT 2, ST_ClipToRast(rast, ST_geomfromtext('Polygon((0 0, 45 45, 90 45, 45 0, 0 0))', 4326), 0, '', ARRAY[254,254,254], '', '{"chunktable":"clip_rbt"}')
FROM clip_table
WHERE id = 1;

Clip using the bounding box of the geometry

Sets window_clip to true to clip using the minimum bounding rectangle (MBR) of the geometry instead of its exact shape.

INSERT INTO rast_clip_result_temp(id, rast)
SELECT 3, ST_ClipToRast(rast, ST_geomfromtext('Polygon((0 0, 45 45, 90 45, 45 0, 0 0))', 4326), 0, '', NULL, '{"window_clip":true}', '')
FROM clip_table
WHERE id = 1;