Returns the color table of a raster band as a JSON string, or null if the band has no color table.
Syntax
text ST_ColorTable(raster raster_obj, integer band);Parameters
| Parameter | Description |
|---|---|
raster_obj | The raster object. |
band | The band index, starting from 0. |
Description
ST_ColorTable retrieves the color lookup table attached to a specific band of a raster object. If the band does not have a color table, the function returns null.
The returned JSON contains a compsCount field and an entries array. Each entry maps a pixel value to its color components:
| Field | Description |
|---|---|
compsCount | Number of color components: 3 for three-component color, 4 for four-component color. |
value | The pixel value in the color table. |
c1 | First color component value. |
c2 | Second color component value. |
c3 | Third color component value. |
c4 | Fourth color component value. Present only when compsCount is 4. |
Three-component format:
{
"compsCount": 3,
"entries": [
{"value": 0, "c1": 0, "c2": 0, "c3": 0},
{"value": 1, "c1": 0, "c2": 0, "c3": 85},
{"value": 2, "c1": 0, "c2": 0, "c3": 170}
]
}Four-component format:
{
"compsCount": 4,
"entries": [
{"value": 0, "c1": 0, "c2": 0, "c3": 0, "c4": 255},
{"value": 1, "c1": 0, "c2": 0, "c3": 85, "c4": 255},
{"value": 2, "c1": 0, "c2": 0, "c3": 170, "c4": 255}
]
}Examples
Example 1: Query a band with a color table
SELECT ST_ColorTable(raster_obj, 0)
FROM raster_table
WHERE id = 1;Result:
{
"compsCount": 3,
"entries": [
{"value": 0, "c1": 0, "c2": 0, "c3": 0},
{"value": 1, "c1": 0, "c2": 0, "c3": 85},
{"value": 2, "c1": 0, "c2": 0, "c3": 170}
]
}Example 2: Filter rows that have a color table on band 0
Use IS NOT NULL to skip bands that do not have a color table:
SELECT id, ST_ColorTable(raster_obj, 0)
FROM raster_table
WHERE ST_ColorTable(raster_obj, 0) IS NOT NULL;该文章对您有帮助吗?