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 使用其值最接近像素值的颜色图条目
|
|
|
ColorBrewer 是色彩图的一个很好的参考。 |
|
|
|
新栅格的结果波段将没有设置 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
|