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
| Parameter | Type | Default | Description |
|---|---|---|---|
raster_obj | raster | — | The raster to convert. |
extent | box | — | The spatial extent of the output image. Uses the geographic coordinate system by default. |
pyramidLevel | integer | 0 | The pyramid level of the image. Valid values start from 0. |
bands | cstring | '' | 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. |
option | cstring | '' | Rendering options as a JSON string. See the option fields table below. |
Option fields
| Field | Type | Default | Description |
|---|---|---|---|
nodata | bool | false | Specifies whether to apply NoData handling. true: NoData values are processed. false: NoData values are processed as normal values. |
nodataValue | integer | 0 | The NoData value of the raster. Required when nodata is true. |
rast_coord | bool | false | Specifies whether the extent box uses pixel coordinates. When true, x is the column number and y is the row number, both starting from 0. |
strength | string | 'none' | Specifies the display enhancement mode. none: no enhancement. stats: contrast stretching based on statistical values. |
quality | integer | 75 | The PNG compression quality. Valid values: 1–100. |
Usage notes
Returns a
byteavalue 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_sizeparameter.The
bandsparameter controls the output color mode:Band count Output 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;