ST_ClipToRast

更新时间:
复制 MD 格式

Clips a raster object using a geometry object and returns the clipped result as a new raster object.

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 at which to perform the clip.
bandscstring''The bands to clip, in the format '0-2' or '1,2,3'. Band index starts at 0. An empty string clips all bands.
nodatafloat8[]NULLThe nodata values for clipped areas, one per band. If fewer values are provided than the number of bands, the remaining bands use 0 as the nodata value.
clipOptioncstring''Clipping behavior, specified as a JSON string. See clipOption parameters.
storageOptioncstring''Storage configuration for the returned raster, specified as a JSON string. See storageOption parameters.

clipOption parameters

ParameterTypeDefaultDescription
window_clipboolfalseSpecifies whether to clip using the minimum bounding rectangle (MBR) of the geometry instead of the geometry shape. Set to true to use the MBR; set to false to use the geometry shape.
rast_coordboolfalseSpecifies whether the geometry uses pixel coordinates. When true, the x coordinate is the column number and the y coordinate is the row number.

storageOption parameters

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

Usage notes

  • By default, window_clip is false, so clipping follows the geometry shape exactly. Set window_clip to true to clip using the minimum bounding rectangle (MBR) instead. MBR clipping is faster but produces a rectangular result regardless of the geometry shape.

  • By default, chunktable is blank, so the clipped raster is stored in a temporary chunk table that exists only for the duration of the current session. To retain the clipped raster across sessions, set chunktable to a persistent table name.

  • The default cache size for a clip operation is 100 MB. If the clipped result exceeds this limit, adjust the limit using the ganos.raster.clip_max_buffer_size parameter.

Examples

The following example clips a raster object from raster_table using a polygon geometry, then stores the result in a temporary table.

DO $$
declare
    rast raster;
    new_rast raster;
begin
    -- Create a permanent table.
    CREATE TEMP TABLE rast_clip_result(id integer, rast raster);

    select raster_obj into rast from raster_table where id = 1;
    new_rast = ST_ClipToRast(rast, ST_geomfromtext('Polygon((0 0, 45 45, 90 45, 45 0, 0 0))', 4326), 0);
    Insert into rast_clip_result values(1, new_rast);
end;
$$ LANGUAGE 'plpgsql';