ST_MapAlgebraFct — 单波段版本-通过在输入栅格波段上应用有效的PostgreSQL函数和所生成的像素类型创建新的单波段栅格。如果未指定波段,则假定为波段1。
raster
ST_MapAlgebraFct
(
raster
rast
, regprocedure
onerasteruserfunc
)
;
raster
ST_MapAlgebraFct
(
raster
rast
, regprocedure
onerasteruserfunc
, text[]
VARIADIC args
)
;
raster
ST_MapAlgebraFct
(
raster
rast
, text
pixeltype
, regprocedure
onerasteruserfunc
)
;
raster
ST_MapAlgebraFct
(
raster
rast
, text
pixeltype
, regprocedure
onerasteruserfunc
, text[]
VARIADIC args
)
;
raster
ST_MapAlgebraFct
(
raster
rast
, integer
band
, regprocedure
onerasteruserfunc
)
;
raster
ST_MapAlgebraFct
(
raster
rast
, integer
band
, regprocedure
onerasteruserfunc
, text[]
VARIADIC args
)
;
raster
ST_MapAlgebraFct
(
raster
rast
, integer
band
, text
pixeltype
, regprocedure
onerasteruserfunc
)
;
raster
ST_MapAlgebraFct
(
raster
rast
, integer
band
, text
pixeltype
, regprocedure
onerasteruserfunc
, text[]
VARIADIC args
)
;
![]() |
|
ST_MapAlgebraFct 从2.1.0开始不建议使用。使用 ST_MapAlgebra(回调函数版本) 取而代之的是。 |
通过应用由指定的有效PostgreSQL函数创建新的单波段栅格
onerasteruserfunc
在输入栅格上(
rast
)。如果没有
band
如果指定,则假定为带1。新栅格将具有与原始栅格相同的地理参考、宽度和高度,但只有一个波段。
如果
pixeltype
则新栅格将具有该像素类型的带。如果将像素类型传递为空,则新栅格波段将具有与输入相同的像素类型
rast
乐队。
The
onerasteruserfunc
parameter must be the name and signature of a SQL or PL/pgSQL function, cast to a regprocedure. A very simple and quite useless PL/pgSQL function example is:
CREATE OR REPLACE FUNCTION simple_function(pixel FLOAT, pos INTEGER[], VARIADIC args TEXT[])
RETURNS FLOAT
AS $$ BEGIN
RETURN 0.0;
END; $$
LANGUAGE 'plpgsql' IMMUTABLE;
The
userfunction
may accept two or three arguments: a float value, an optional integer array, and a variadic text array. The first argument is the value of an individual raster cell (regardless of the raster datatype). The second argument is the position of the current processing cell in the form '{x,y}'. The third argument indicates that all remaining parameters to
ST_MapAlgebraFct
shall be passed through to the
userfunction
.
传递一个 重新生产 参数传递给SQL函数需要传递完整的函数签名,然后强制转换为 注册表程序 键入。要将上述示例PL/pgSQL函数作为参数传递,参数的SQL为:
‘Simple_Function(Float,Integer[],Text[])’::regprocess
请注意,参数包含函数的名称、函数参数的类型、名称两边的引号和参数类型,以及转换为 注册表程序 。
的第三个参数
userfunction
是一种
多变文本
数组。的所有尾随文本参数
ST_MapAlgebraFct
调用被传递到指定的
userfunction
,并包含在
args
论点。
![]() |
|
有关VARIADIC关键字的详细信息,请参阅的PostgreSQL文档和的“参数数目可变的SQL函数”一节 查询语言(SQL)函数 。 |
![]() |
|
这个
文本[]
参数设置为
|
可用性:2.0.0
从我们的原始创建一个新的1波段栅格,它是原始栅格波段的模2的函数。
ALTER TABLE dummy_rast ADD COLUMN map_rast raster;
CREATE FUNCTION mod_fct(pixel float, pos integer[], variadic args text[])
RETURNS float
AS $$
BEGIN
RETURN pixel::integer % 2;
END;
$$
LANGUAGE 'plpgsql' IMMUTABLE;
UPDATE dummy_rast SET map_rast = ST_MapAlgebraFct(rast,NULL,'mod_fct(float,integer[],text[])'::regprocedure) WHERE rid = 2;
SELECT ST_Value(rast,1,i,j) As origval, ST_Value(map_rast, 1, i, j) As mapval
FROM dummy_rast CROSS JOIN generate_series(1, 3) AS i CROSS JOIN generate_series(1,3) AS j
WHERE rid = 2;
origval | mapval
---------+--------
253 | 1
254 | 0
253 | 1
253 | 1
254 | 0
254 | 0
250 | 0
254 | 0
254 | 0
从重新分类的原始图像创建像素类型为2BUI的新的1波段栅格,并将nodata值设置为传递给用户函数(0)的参数。
ALTER TABLE dummy_rast ADD COLUMN map_rast2 raster;
CREATE FUNCTION classify_fct(pixel float, pos integer[], variadic args text[])
RETURNS float
AS
$$
DECLARE
nodata float := 0;
BEGIN
IF NOT args[1] IS NULL THEN
nodata := args[1];
END IF;
IF pixel < 251 THEN
RETURN 1;
ELSIF pixel = 252 THEN
RETURN 2;
ELSIF pixel > 252 THEN
RETURN 3;
ELSE
RETURN nodata;
END IF;
END;
$$
LANGUAGE 'plpgsql';
UPDATE dummy_rast SET map_rast2 = ST_MapAlgebraFct(rast,'2BUI','classify_fct(float,integer[],text[])'::regprocedure, '0') WHERE rid = 2;
SELECT DISTINCT ST_Value(rast,1,i,j) As origval, ST_Value(map_rast2, 1, i, j) As mapval
FROM dummy_rast CROSS JOIN generate_series(1, 5) AS i CROSS JOIN generate_series(1,5) AS j
WHERE rid = 2;
origval | mapval
---------+--------
249 | 1
250 | 1
251 |
252 | 2
253 | 3
254 | 3
SELECT ST_BandPixelType(map_rast2) As b1pixtyp
FROM dummy_rast WHERE rid = 2;
b1pixtyp
----------
2BUI
![]() 原始(柱面栅格-视图) |
![]() rast_view_ma |
创建一个新的3波段栅格相同的像素类型从我们原来的3波段栅格与第一个波段由地图代数改变,其余2个波段没有改变。
CREATE FUNCTION rast_plus_tan(pixel float, pos integer[], variadic args text[])
RETURNS float
AS
$$
BEGIN
RETURN tan(pixel) * pixel;
END;
$$
LANGUAGE 'plpgsql';
SELECT ST_AddBand(
ST_AddBand(
ST_AddBand(
ST_MakeEmptyRaster(rast_view),
ST_MapAlgebraFct(rast_view,1,NULL,'rast_plus_tan(float,integer[],text[])'::regprocedure)
),
ST_Band(rast_view,2)
),
ST_Band(rast_view, 3) As rast_view_ma
)
FROM wind
WHERE rid=167;