ST_ColorMap — 从源栅格和指定的波段创建一个新的最多包含四个 8BUI 波段(灰度、RGB、RGBA)的栅格。如果未指定,则假定为波段 1。
raster ST_ColorMap(raster rast, integer nband=1, text colormap=grayscale, text method=INTERPOLATE);
raster ST_ColorMap(raster rast, text colormap, text method=INTERPOLATE);
将 colormap 应用于 rast 的 nband 波段,生成一个最多包含四个 8BUI 波段的新栅格。新栅格中 8BUI 波段的数量由 colormap 中定义的颜色分量数量决定。
如果未指定 nband,则假定为波段 1。
                        colormap 可以是预定义的颜色映射的关键字,也可以是一组定义值和颜色分量的行。
有效的预定义 colormap 关键字
                                grayscale 或 greyscale 用于由灰度阴影组成的单个 8BUI 波段栅格。
                                pseudocolor 用于具有从蓝色到绿色到红色的颜色的四个 8BUI (RGBA) 波段栅格。
                                fire 用于具有从黑色到红色到淡黄色的颜色的四个 8BUI (RGBA) 波段栅格。
                                bluered 用于具有从蓝色到淡白色到红色的颜色的四个 8BUI (RGBA) 波段栅格。
用户可以将一组条目(每行一个)传递给 colormap 来指定自定义颜色映射。每个条目通常由五个值组成:像素值和对应的红色、绿色、蓝色、Alpha 分量(颜色分量介于 0 和 255 之间)。可以使用百分比值代替像素值,其中 0% 和 100% 是栅格波段中找到的最小值和最大值。值可以用逗号(“,”)、制表符、冒号(“:”)和/或空格分隔。可以将像素值设置为 nv、null 或 nodata 表示 NODATA 值。下面提供一个示例。
5 0 0 0 255
4 100:50 55 255
1 150,100 150 255
0% 255 255 255 255
nv 0 0 0 0
                    
        colormap 的语法类似于 GDAL gdaldem 的颜色浮雕模式。
method 的有效关键字
                                INTERPOLATE 使用线性插值平滑混合给定像素值之间的颜色
                                EXACT 严格匹配仅在颜色映射中找到的那些像素值。像素值与颜色映射条目不匹配的像素将被设置为 0 0 0 0 (RGBA)
                                NEAREST 使用值最接近像素值的颜色映射条目
| ![[Note]](../images/note.png)  | |
| 一个很好的颜色映射参考是 ColorBrewer。 | 
| ![[Warning]](../images/warning.png)  | |
| 新栅格的生成波段将不设置 NODATA 值。如果需要 NODATA 值,请使用 ST_SetBandNoDataValue 设置 NODATA 值。 | 
可用性:2.1.0
这是一个用来练习的垃圾表格
-- setup test raster table --
DROP TABLE IF EXISTS funky_shapes;
CREATE TABLE funky_shapes(rast raster);
INSERT INTO funky_shapes(rast)
WITH ref AS (
    SELECT ST_MakeEmptyRaster( 200, 200, 0, 200, 1, -1, 0, 0) AS rast
)
SELECT
    ST_Union(rast)
FROM (
    SELECT
        ST_AsRaster(
            ST_Rotate(
                ST_Buffer(
                    ST_GeomFromText('LINESTRING(0 2,50 50,150 150,125 50)'),
                    i*2
                ),
                pi() * i * 0.125, ST_Point(50,50)
            ),
            ref.rast, '8BUI'::text, i * 5
        ) AS rast
    FROM ref
    CROSS JOIN generate_series(1, 10, 3) AS i
) AS shapes;
                    
        SELECT
    ST_NumBands(rast) As n_orig,
    ST_NumBands(ST_ColorMap(rast,1, 'greyscale')) As ngrey,
    ST_NumBands(ST_ColorMap(rast,1, 'pseudocolor')) As npseudo,
    ST_NumBands(ST_ColorMap(rast,1, 'fire')) As nfire,
    ST_NumBands(ST_ColorMap(rast,1, 'bluered')) As nbluered,
    ST_NumBands(ST_ColorMap(rast,1, '
100% 255   0   0
 80% 160   0   0
 50% 130   0   0
 30%  30   0   0
 20%  60   0   0
  0%   0   0   0
  nv 255 255 255
    ')) As nred
FROM funky_shapes;
                    
         n_orig | ngrey | npseudo | nfire | nbluered | nred
--------+-------+---------+-------+----------+------
      1 |     1 |       4 |     4 |        4 |    3
                    
      SELECT
    ST_AsPNG(rast) As orig_png,
    ST_AsPNG(ST_ColorMap(rast,1,'greyscale')) As grey_png,
    ST_AsPNG(ST_ColorMap(rast,1, 'pseudocolor')) As pseudo_png,
    ST_AsPNG(ST_ColorMap(rast,1, 'nfire')) As fire_png,
    ST_AsPNG(ST_ColorMap(rast,1, 'bluered')) As bluered_png,
    ST_AsPNG(ST_ColorMap(rast,1, '
100% 255   0   0
 80% 160   0   0
 50% 130   0   0
 30%  30   0   0
 20%  60   0   0
  0%   0   0   0
  nv 255 255 255
    ')) As red_png
FROM funky_shapes;
                    
        | 
  orig_png 
 | 
  grey_png 
 | 
  pseudo_png 
 | 
| 
  fire_png 
 | 
  bluered_png 
 | 
  red_png 
 |