ST_ColorTable

更新时间:
复制 MD 格式

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

ParameterDescription
raster_objThe raster object.
bandThe 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:

FieldDescription
compsCountNumber of color components: 3 for three-component color, 4 for four-component color.
valueThe pixel value in the color table.
c1First color component value.
c2Second color component value.
c3Third color component value.
c4Fourth 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;