名称

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);

Description

colormap应用于 rastnband 处的波段,从而生成最多由四个 8BUI 波段组成的新栅格。 新栅格中的 8BUI 波段数量由colormap中定义的颜色分量数量决定。

如果未指定 nband,则假定为波段 1。

colormap 可以是预定义颜色图的关键字或定义值和颜色分量的一组线。

有效的预定义colormap关键字:

  • grayscalegreyscale的一个 8BUI 波段栅格的灰度。

  • pseudocolor四 8BUI (RGBA) 波段栅格的伪彩色,颜色从蓝色到绿色再到红色。

  • fire为四 8BUI (RGBA) 波段栅格,颜色从黑色到红色到浅黄色。

  • bluered 表示四 8BUI (RGBA) 波段栅格,颜色从蓝色到浅白色再到红色。

用户可以通过 colormap 参数传递一组条目(每行一个)来指定自定义的颜色映射。每个条目通常包括五个值:像素值以及相应的红色、绿色、蓝色、Alpha 组件(颜色组件的取值范围在 0 到 255 之间)。可以使用百分比值代替像素值,其中 0% 和 100% 分别表示栅格带中找到的最小值和最大值。值可以使用逗号(',')、制表符、冒号(':')和/或空格分隔。像素值可以设置为 nvnullnodata,表示 NODATA 值。以下是一个示例。

Code
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 使用其值最接近像素值的颜色图条目

[注意]

ColorBrewer 是色彩图的一个很好的参考。

[警告]

新栅格的结果波段将没有设置 NODATA 值。 如果需要,请使用 ST_SetBandNoDataValue 设置 NODATA 值。

可用性:2.1.0

示例

This example builds a raster from rotated buffered lines and compares the original values with predefined and custom color maps.

Code
WITH ref AS (
    SELECT ST_MakeEmptyRaster(200, 200, 0, 200, 1, -1, 0, 0) AS rast
), source AS (
    SELECT ST_Union(rast) AS rast
    FROM (
    SELECT
        ST_AsRaster(ST_Rotate(ST_Buffer('LINESTRING(0 2,50 50,150 150,125 50)'::geometry,
                    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 title, ST_AsPNG(rendered) AS image
FROM source
CROSS JOIN LATERAL (VALUES
    ('original', rast),
    ('grayscale', ST_ColorMap(rast, 1, 'greyscale')),
    ('pseudocolor', ST_ColorMap(rast, 1, 'pseudocolor')),
    ('fire', ST_ColorMap(rast, 1, 'fire')),
    ('blue-red', ST_ColorMap(rast, 1, 'bluered')),
    ('custom red', ST_ColorMap(
        rast,
        1,
        E'100% 255 0 0 255\n80% 160 0 0 255\n50% 130 0 0 255\n' ||
        E'30% 30 0 0 255\n20% 60 0 0 255\n0% 0 0 0 255\n' ||
        E'nv 255 255 255 0'
    ))
) AS variants(title, rendered);
栅格输出
title      | image
------------+--------------------------------
 original   | PNG image, 224 x 295 pixels
 grayscale | PNG image, 224 x 295 pixels
 pseudocolor| PNG image, 224 x 295 pixels
 fire       | PNG image, 224 x 295 pixels
 blue-red   | PNG image, 224 x 295 pixels
 custom red | PNG image, 224 x 295 pixels
                    
Figure
Geometry figure for visual-rt-st-colormap-01