ST_MapAlgebra(回调函数版本)——回调函数版本 - 给定一个或多个输入光栅、波段索引和一个用户指定的回调函数,返回一个单波段光栅。
raster ST_MapAlgebra(
rastbandarg[] rastbandargset, regprocedure callbackfunc, text pixeltype=NULL, text extenttype=INTERSECTION, raster customextent=NULL, integer distancex=0, integer distancey=0, text[] VARIADIC userargs=NULL)
;
raster ST_MapAlgebra(
raster rast, integer[] nband, regprocedure callbackfunc, text pixeltype=NULL, text extenttype=FIRST, raster customextent=NULL, integer distancex=0, integer distancey=0, text[] VARIADIC userargs=NULL)
;
raster ST_MapAlgebra(
raster rast, integer nband, regprocedure callbackfunc, text pixeltype=NULL, text extenttype=FIRST, raster customextent=NULL, integer distancex=0, integer distancey=0, text[] VARIADIC userargs=NULL)
;
raster ST_MapAlgebra(
raster rast1, integer nband1, raster rast2, integer nband2, regprocedure callbackfunc, text pixeltype=NULL, text extenttype=INTERSECTION, raster customextent=NULL, integer distancex=0, integer distancey=0, text[] VARIADIC userargs=NULL)
;
raster ST_MapAlgebra(
raster rast, integer nband, regprocedure callbackfunc, float8[] mask, boolean weighted, text pixeltype=NULL, text extenttype=INTERSECTION, raster customextent=NULL, text[] VARIADIC userargs=NULL)
;
给定一个或多个输入光栅、波段索引和一个用户指定的回调函数,返回一个单波段光栅。
评估地图代数过程的栅格。
rastbandargset
允许对多个栅格和/或多个波段使用地图代数操作。请参见示例变体 1。
要评估的栅格的波段号。nband 可以是表示波段的整数或整数[]。对于 2 栅格/2 波段的情况,nband1 是 rast1 上的波段,nband2 是 rast2 上的波段。
callbackfunc
参数必须是 SQL 或 PL/pgSQL 函数的名称和签名,转换为 regprocedure。PL/pgSQL 函数示例如下:
CREATE OR REPLACE FUNCTION sample_callbackfunc(value double precision[][][], position integer[][], VARIADIC userargs text[]) RETURNS double precision AS $$ BEGIN RETURN 0; END; $$ LANGUAGE 'plpgsql' IMMUTABLE;
callbackfunc
必须有三个参数:一个 3 维双精度数组、一个 2 维整数数组和一个可变的 1 维文本数组。第一个参数 value
是来自所有输入栅格的值集(作为双精度)。三个维度(其中索引从 1 开始)是:栅格 #、行 y、列 x。第二个参数 position
是来自输出栅格和输入栅格的像素位置集。外部维度(其中索引从 0 开始)是栅格 #。外部维度索引 0 处的位置是输出栅格的像素位置。对于每个外部维度,内部维度中有两个元素,分别表示 X 和 Y。第三个参数 userargs
用于传递任何用户指定参数。
将 regprocedure 参数传递给 SQL 函数需要传递完整的函数签名,然后转换为 regprocedure 类型。要将上述 PL/pgSQL 函数示例作为参数传递,该参数的 SQL 为:
'sample_callbackfunc(double precision[], integer[], text[])'::regprocedure
请注意,参数包含函数的名称、函数参数的类型、名称和参数类型周围的引号以及转换为 regprocedure。
用于过滤传递给地图代数回调函数的像元的数字的 n 维数组(矩阵)。0 表示应将相邻像元值视为无数据,1 表示应将值视为数据。如果 weight 设置为 true,则这些值用作乘数,以乘以邻域位置中该值的像素值。
布尔值 (真/假) 表示是否对掩码值进行加权(乘以原始值),或不进行加权(仅适用于采用掩码的 proto)。
如果传入 pixeltype
,则新栅格的一条波段将采用该像素类型。如果 pixeltype 传入 NULL 或未传入,则新栅格波段将采用与第一栅格指定波段相同的像素类型(对于范围类型:INTERSECTION、UNION、FIRST、CUSTOM)或适当栅格的指定波段(对于范围类型:SECOND、LAST)。如有疑问,请始终指定 pixeltype
。
输出栅格的最终像素类型必须列在 ST_BandPixelType 中,或未传入或设置为 NULL。
可能的值为 INTERSECTION(默认值)、UNION、FIRST(一个栅格变体的默认值)、SECOND、LAST、CUSTOM。
如果 extentype
为 CUSTOM,则必须为 customextent
提供一个栅格。请参见变体 1 的示例 4。
参考像元在 x 方向的像素距离。因此,结果矩阵的宽度将为 2*distancex + 1
。如果未指定,则仅考虑参考像元(邻域为 0)。
参考像元在 y 方向的像素距离。结果矩阵的高度将为 2*distancey + 1
。如果未指定,则仅考虑参考像元(邻域为 0)。
传递给 callbackfunc
的第三个参数是一个 可变文本 数组。所有尾随文本参数都传递到指定的 callbackfunc
,并包含在 userargs
参数中。
有关 VARIADIC 关键字的更多信息,请参阅 PostgreSQL 文档和 查询语言 (SQL) 函数 中的“具有可变数量参数的 SQL 函数”部分。 |
传递给 |
变体 1 接受 rastbandarg
数组,允许对多个栅格和/或多个波段使用地图代数运算。请参见变体 1 示例。
变体 2 和 3 对一个栅格的一个或多个波段进行操作。请参见变体 2 和 3 示例。
变体 4 对每个栅格带一个波段的两个栅格进行操作。请参见变体 4 示例。
可用性:2.2.0:添加掩码的能力
可用性:2.1.0
一个栅格,一个波段
WITH foo AS ( SELECT 1 AS rid, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '16BUI', 1, 0) AS rast ) SELECT ST_MapAlgebra( ARRAY[ROW(rast, 1)]::rastbandarg[], 'sample_callbackfunc(double precision[], int[], text[])'::regprocedure ) AS rast FROM foo
一个栅格,多个波段
WITH foo AS ( SELECT 1 AS rid, ST_AddBand(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '16BUI', 1, 0), 2, '8BUI', 10, 0), 3, '32BUI', 100, 0) AS rast ) SELECT ST_MapAlgebra( ARRAY[ROW(rast, 3), ROW(rast, 1), ROW(rast, 3), ROW(rast, 2)]::rastbandarg[], 'sample_callbackfunc(double precision[], int[], text[])'::regprocedure ) AS rast FROM foo
多个栅格,多个波段
WITH foo AS ( SELECT 1 AS rid, ST_AddBand(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '16BUI', 1, 0), 2, '8BUI', 10, 0), 3, '32BUI', 100, 0) AS rast UNION ALL SELECT 2 AS rid, ST_AddBand(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 1, 1, -1, 0, 0, 0), 1, '16BUI', 2, 0), 2, '8BUI', 20, 0), 3, '32BUI', 300, 0) AS rast ) SELECT ST_MapAlgebra( ARRAY[ROW(t1.rast, 3), ROW(t2.rast, 1), ROW(t2.rast, 3), ROW(t1.rast, 2)]::rastbandarg[], 'sample_callbackfunc(double precision[], int[], text[])'::regprocedure ) AS rast FROM foo t1 CROSS JOIN foo t2 WHERE t1.rid = 1 AND t2.rid = 2
带邻域的覆盖面切片的完整示例。此查询仅适用于 PostgreSQL 9.1 或更高版本。
WITH foo AS ( SELECT 0 AS rid, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '16BUI', 1, 0) AS rast UNION ALL SELECT 1, ST_AddBand(ST_MakeEmptyRaster(2, 2, 2, 0, 1, -1, 0, 0, 0), 1, '16BUI', 2, 0) AS rast UNION ALL SELECT 2, ST_AddBand(ST_MakeEmptyRaster(2, 2, 4, 0, 1, -1, 0, 0, 0), 1, '16BUI', 3, 0) AS rast UNION ALL SELECT 3, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, -2, 1, -1, 0, 0, 0), 1, '16BUI', 10, 0) AS rast UNION ALL SELECT 4, ST_AddBand(ST_MakeEmptyRaster(2, 2, 2, -2, 1, -1, 0, 0, 0), 1, '16BUI', 20, 0) AS rast UNION ALL SELECT 5, ST_AddBand(ST_MakeEmptyRaster(2, 2, 4, -2, 1, -1, 0, 0, 0), 1, '16BUI', 30, 0) AS rast UNION ALL SELECT 6, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, -4, 1, -1, 0, 0, 0), 1, '16BUI', 100, 0) AS rast UNION ALL SELECT 7, ST_AddBand(ST_MakeEmptyRaster(2, 2, 2, -4, 1, -1, 0, 0, 0), 1, '16BUI', 200, 0) AS rast UNION ALL SELECT 8, ST_AddBand(ST_MakeEmptyRaster(2, 2, 4, -4, 1, -1, 0, 0, 0), 1, '16BUI', 300, 0) AS rast ) SELECT t1.rid, ST_MapAlgebra( ARRAY[ROW(ST_Union(t2.rast), 1)]::rastbandarg[], 'sample_callbackfunc(double precision[], int[], text[])'::regprocedure, '32BUI', 'CUSTOM', t1.rast, 1, 1 ) AS rast FROM foo t1 CROSS JOIN foo t2 WHERE t1.rid = 4 AND t2.rid BETWEEN 0 AND 8 AND ST_Intersects(t1.rast, t2.rast) GROUP BY t1.rid, t1.rast
与前一个类似的示例,适用于带邻域的覆盖面切片,但适用于 PostgreSQL 9.0。
WITH src AS ( SELECT 0 AS rid, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '16BUI', 1, 0) AS rast UNION ALL SELECT 1, ST_AddBand(ST_MakeEmptyRaster(2, 2, 2, 0, 1, -1, 0, 0, 0), 1, '16BUI', 2, 0) AS rast UNION ALL SELECT 2, ST_AddBand(ST_MakeEmptyRaster(2, 2, 4, 0, 1, -1, 0, 0, 0), 1, '16BUI', 3, 0) AS rast UNION ALL SELECT 3, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, -2, 1, -1, 0, 0, 0), 1, '16BUI', 10, 0) AS rast UNION ALL SELECT 4, ST_AddBand(ST_MakeEmptyRaster(2, 2, 2, -2, 1, -1, 0, 0, 0), 1, '16BUI', 20, 0) AS rast UNION ALL SELECT 5, ST_AddBand(ST_MakeEmptyRaster(2, 2, 4, -2, 1, -1, 0, 0, 0), 1, '16BUI', 30, 0) AS rast UNION ALL SELECT 6, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, -4, 1, -1, 0, 0, 0), 1, '16BUI', 100, 0) AS rast UNION ALL SELECT 7, ST_AddBand(ST_MakeEmptyRaster(2, 2, 2, -4, 1, -1, 0, 0, 0), 1, '16BUI', 200, 0) AS rast UNION ALL SELECT 8, ST_AddBand(ST_MakeEmptyRaster(2, 2, 4, -4, 1, -1, 0, 0, 0), 1, '16BUI', 300, 0) AS rast ) WITH foo AS ( SELECT t1.rid, ST_Union(t2.rast) AS rast FROM src t1 JOIN src t2 ON ST_Intersects(t1.rast, t2.rast) AND t2.rid BETWEEN 0 AND 8 WHERE t1.rid = 4 GROUP BY t1.rid ), bar AS ( SELECT t1.rid, ST_MapAlgebra( ARRAY[ROW(t2.rast, 1)]::rastbandarg[], 'raster_nmapalgebra_test(double precision[], int[], text[])'::regprocedure, '32BUI', 'CUSTOM', t1.rast, 1, 1 ) AS rast FROM src t1 JOIN foo t2 ON t1.rid = t2.rid ) SELECT rid, (ST_Metadata(rast)), (ST_BandMetadata(rast, 1)), ST_Value(rast, 1, 1, 1) FROM bar;
一个栅格,多个波段
WITH foo AS ( SELECT 1 AS rid, ST_AddBand(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '16BUI', 1, 0), 2, '8BUI', 10, 0), 3, '32BUI', 100, 0) AS rast ) SELECT ST_MapAlgebra( rast, ARRAY[3, 1, 3, 2]::integer[], 'sample_callbackfunc(double precision[], int[], text[])'::regprocedure ) AS rast FROM foo
一个栅格,一个波段
WITH foo AS ( SELECT 1 AS rid, ST_AddBand(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '16BUI', 1, 0), 2, '8BUI', 10, 0), 3, '32BUI', 100, 0) AS rast ) SELECT ST_MapAlgebra( rast, 2, 'sample_callbackfunc(double precision[], int[], text[])'::regprocedure ) AS rast FROM foo
两个栅格,两个波段
WITH foo AS ( SELECT 1 AS rid, ST_AddBand(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '16BUI', 1, 0), 2, '8BUI', 10, 0), 3, '32BUI', 100, 0) AS rast UNION ALL SELECT 2 AS rid, ST_AddBand(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 1, 1, -1, 0, 0, 0), 1, '16BUI', 2, 0), 2, '8BUI', 20, 0), 3, '32BUI', 300, 0) AS rast ) SELECT ST_MapAlgebra( t1.rast, 2, t2.rast, 1, 'sample_callbackfunc(double precision[], int[], text[])'::regprocedure ) AS rast FROM foo t1 CROSS JOIN foo t2 WHERE t1.rid = 1 AND t2.rid = 2
WITH foo AS (SELECT ST_SetBandNoDataValue( ST_SetValue(ST_SetValue(ST_AsRaster( ST_Buffer( ST_GeomFromText('LINESTRING(50 50,100 90,100 50)'), 5,'join=bevel'), 200,200,ARRAY['8BUI'], ARRAY[100], ARRAY[0]), ST_Buffer('POINT(70 70)'::geometry,10,'quad_segs=1') ,50), 'LINESTRING(20 20, 100 100, 150 98)'::geometry,1),0) AS rast ) SELECT 'original' AS title, rast FROM foo UNION ALL SELECT 'no mask mean value' AS title, ST_MapAlgebra(rast,1,'ST_mean4ma(double precision[], int[], text[])'::regprocedure) AS rast FROM foo UNION ALL SELECT 'mask only consider neighbors, exclude center' AS title, ST_MapAlgebra(rast,1,'ST_mean4ma(double precision[], int[], text[])'::regprocedure, '{{1,1,1}, {1,0,1}, {1,1,1}}'::double precision[], false) As rast FROM foo UNION ALL SELECT 'mask weighted only consider neighbors, exclude center multi otehr pixel values by 2' AS title, ST_MapAlgebra(rast,1,'ST_mean4ma(double precision[], int[], text[])'::regprocedure, '{{2,2,2}, {2,0,2}, {2,2,2}}'::double precision[], true) As rast FROM foo;
|
|
|
|