Crops a 256 or 512 pixel tile from a raster object within the specified geometry bounds and returns it as a binary stream. Outputs PNG format with all bands included by default.
Syntax
record ST_AsTile(raster raster_obj, geometry geom, cstring export_options default '');Return values
Returns a record with two fields:
| Field | Type | Description |
|---|---|---|
ext | — | The image format of the output tile |
data | bytea | The binary tile data (256 or 512 pixels) |
Parameters
| Parameter | Type | Description |
|---|---|---|
raster_obj | raster | The input grid object |
geom | geometry | The geometry object that defines the tile bounds |
export_options | cstring | A JSON string of export options. Defaults to '' (empty string). |
Export options
Pass export_options as a JSON string. All fields are optional.
| Option | Type | Default | Description |
|---|---|---|---|
bands | cstring | '' | The bands to include in the output, separated by commas. By default, all bands are included. |
format | cstring | PNG | The output image format. Valid values: PNG, JPEG, GTiff. |
pyramid_level | integer | -1 | The pyramid level to use. Defaults to -1, which selects the optimal level automatically. |
dim | integer | 256 | The pixel size of the output tile. Valid values: 256, 512. |
nodata | boolean | true | Specifies whether to apply the nodata value. If you do not specify this option, the nodata value of the object is used. |
nodatavalue | f8 | 0 | The nodata value to apply. Takes effect only when nodata is true. |
strength | string | none | The display enhancement method. Valid values: none (no enhancement), stats (stretch based on statistical values), ratio (stretch based on ratios). |
ratio_offset | integer | 2 | The percentage offset for ratio-based enhancement. Stretches the output from the nth to the (100-n)th percentile. Takes effect only when strength is ratio. |
alpha | boolean | false | Specifies whether to add an alpha channel for transparency. |
quality | integer | 75 | The compression quality. Valid values: 0-100. The value 0 indicates the poorest compression quality, and the value 100 indicates the optimal compression quality. |
Examples
Export bands 0, 1, and 2 as PNG
SELECT ST_AsTile(rast,
ST_GeomFromText('POLYGON((-80 30,-100 30, -100 100, -80 100, -80 30))',4326),
'{"format":"PNG","bands":"0,1,2"}')
FROM raster_table;Export bands 0, 1, and 2 as GTiff at pyramid level 7
SELECT ST_AsTile(rast,
ST_GeomFromText('POLYGON((-80 30,-100 30, -100 100, -80 100, -80 30))',4326),
'{"format":"GTiff","bands":"0,1,2","pyramid_level":7}')
FROM raster_table;Export a PNG tile with statistical enhancement and transparency
SELECT ST_AsTile(rast,
ST_GeomFromText('POLYGON((-80 30,-100 30, -100 100, -80 100, -80 30))',4326),
'{"format":"PNG","bands":"0,1,2","strength":"stats","alpha":true}')
FROM raster_table;