All Products
Search
Document Center

PolarDB:ST_AsPNG

Last Updated:Mar 28, 2026

Converts a raster to a bytea PNG image by clipping it to a specified extent.

Syntax

bytea ST_AsPNG(raster raster_obj, box extent,
               integer pyramidLevel default 0,
               cstring bands default '',
               cstring option default '');

Parameters

ParameterDescription
raster_objThe raster to convert.
extentThe extent to clip. Uses the geographic coordinate system by default.
pyramidLevelThe pyramid level of the image. Valid values start from 0. Default value: 0.
bandsThe bands to include in the output image. Valid values start from 0. Use a range ('0-2') or a comma-separated list ('1,2,3'). Leave blank to use the first three bands. PNG supports 1, 2, 3, or 4 bands — see Usage notes for details.
optionOptions for the conversion, as a JSON string.

option fields

FieldTypeDefaultDescription
nodataboolfalseSpecifies whether to treat NoData values differently from normal values. true: processes NoData values as NoData. false: treats NoData values as normal values.
nodataValueinteger0The NoData value of the raster. Required when nodata is true.
rast_coordboolfalseSpecifies whether the extent box uses pixel coordinates. When true, x is the column number and y is the row number, both starting from 0.
strengthstringnoneDisplay enhancement mode. none: no enhancement. stats: stretching based on statistical values.
qualityinteger75PNG compression quality. Valid values: 1 to 100.

Usage notes

  • Returns a bytea PNG image.

  • By default, up to 100 MB of clipped data is cached, which is also the maximum size of the returned image. To adjust this limit, set the ganos.raster.clip_max_buffer_size parameter.

  • The bands parameter determines the PNG image type:

    Band countImage type
    1Grayscale
    2Grayscale with Alpha band
    3R, G, B
    4R, G, B, Alpha band

Examples

Return a PNG image clipped to a geographic extent:

SELECT ST_AsPNG(raster_obj,
                '(-180,-90), (0,0)'::Box)
FROM raster_table
WHERE id = 1;

Specify the pyramid level:

SELECT ST_AsPNG(raster_obj,
                '(-180,-90), (0,0)'::Box,
                1)
FROM raster_table
WHERE id = 1;

Specify the band range:

SELECT ST_AsPNG(raster_obj,
                '(-180,-90), (0,0)'::Box,
                1,
                '0-2')
FROM raster_table
WHERE id = 1;

Apply statistical stretch enhancement:

SELECT ST_AsPNG(rast,
                '(-180,-90), (0,0)'::Box,
                0,
                '',
                '{"nodata":"false", "nodatavalue":"0","rast_coord":"false", "strength":"stats", "quality":"75"}')
FROM raster_table
WHERE id = 1;