All Products
Search
Document Center

ApsaraDB RDS:ST_AsPNG

Last Updated:Mar 28, 2026

Converts a raster into a bytea PNG image. For other raster output formats, use ST_AsGDALRaster.

Syntax

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

Parameters

ParameterTypeDefaultDescription
raster_objrasterThe raster to convert.
extentboxThe spatial extent of the output image. Uses the geographic coordinate system by default.
pyramidLevelinteger0The pyramid level of the image. Valid values start from 0.
bandscstring''The bands to include. Valid values start from 0. Accepted formats: '0-2' (range) and '1,2,3' (list). PNG supports 1, 2, 3, or 4 bands; defaults to the first three bands.
optioncstring''Rendering options as a JSON string. See the option fields table below.

Option fields

FieldTypeDefaultDescription
nodataboolfalseSpecifies whether to apply NoData handling. true: NoData values are processed. false: NoData values are processed 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.
strengthstring'none'Specifies the display enhancement mode. none: no enhancement. stats: contrast stretching based on statistical values.
qualityinteger75The PNG compression quality. Valid values: 1–100.

Usage notes

  • Returns a bytea value containing the PNG image data.

  • By default, up to 100 MB of cropped data is cached. To adjust the limit, set the ganos.raster.clip_max_buffer_size parameter.

  • The bands parameter controls the output color mode:

    Band countOutput mode
    1Grayscale
    2Grayscale + Alpha
    3R, G, B
    4R, G, B, Alpha

Examples

Basic: export a geographic region as PNG

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

Specify a pyramid level

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

Specify a band range

-- Export bands 0, 1, and 2 (mapped to R, G, B)
SELECT ST_AsPNG(raster_obj, '(-180,-90), (0,0)'::box, 1, '0-2')
FROM raster_table
WHERE id = 1;

Apply contrast stretching with rendering options

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;