名称

ST_MapAlgebraFct — 2 波段版本 - 通过对 2 个输入栅格波段应用有效的 PostgreSQL 函数,并使用提供的像素类型创建新的单波段栅格。如果未指定波段,则假定为波段 1。如果未指定,则范围类型默认为 INTERSECTION。

概要

raster ST_MapAlgebraFct(raster rast1, raster rast2, regprocedure tworastuserfunc, text pixeltype=same_as_rast1, text extenttype=INTERSECTION, text[] VARIADIC userargs);

raster ST_MapAlgebraFct(raster rast1, integer band1, raster rast2, integer band2, regprocedure tworastuserfunc, text pixeltype=same_as_rast1, text extenttype=INTERSECTION, text[] VARIADIC userargs);

描述

[Warning]

从 2.1.0 版本开始,ST_MapAlgebraFct 已被弃用。请改用 ST_MapAlgebra(回调函数版本)

通过对输入栅格 rast1rast2 应用由 tworastuserfunc 指定的有效 PostgreSQL 函数,创建一个新的单波段栅格。如果未指定 band1band2,则假定为波段 1。新栅格将具有与原始栅格相同的地理参考、宽度和高度,但仅包含一个波段。

如果传递了 pixeltype,则新栅格将具有该像素类型的波段。如果传递了 NULL 或省略了 pixeltype,则新栅格波段将具有与输入 rast1 波段相同的像素类型。

tworastuserfunc 参数必须是 SQL 或 PL/pgSQL 函数的名称和签名,强制转换为 regprocedure。PL/pgSQL 函数示例如下

CREATE OR REPLACE FUNCTION simple_function_for_two_rasters(pixel1 FLOAT, pixel2 FLOAT, pos INTEGER[], VARIADIC args TEXT[])
    RETURNS FLOAT
    AS $$ BEGIN
        RETURN 0.0;
    END; $$
    LANGUAGE 'plpgsql' IMMUTABLE;

函数 tworastuserfunc 可以接受三个或四个参数:一个双精度值,一个双精度值,一个可选的整数数组和一个可变文本数组。第一个参数是 rast1 中单个栅格单元的值(与栅格数据类型无关)。第二个参数是 rast2 中单个栅格单元的值。第三个参数是当前处理单元的位置,形式为“{x,y}”。第四个参数表示所有剩余的参数将传递给 ST_MapAlgebraFct,并传递给 tworastuserfunc

regprodedure 参数传递给 SQL 函数需要传递完整的函数签名,然后将其转换为 regprocedure 类型。要将上面的 PL/pgSQL 函数示例作为参数传递,SQL 参数代码如下:

'simple_function(double precision, double precision, integer[], text[])'::regprocedure

请注意,参数包含函数名称、函数参数的类型、名称和参数类型的引号以及转换为 regprocedure 的强制转换。

函数 tworastuserfunc 的第四个参数是 variadic text 数组。任何 ST_MapAlgebraFct 调用的所有尾随文本参数都将传递给指定的 tworastuserfunc,并包含在 userargs 参数中。

[Note]

有关 VARIADIC 关键字的更多信息,请参阅 PostgreSQL 文档和 查询语言 (SQL) 函数 中的“具有可变参数数量的 SQL 函数”部分。

[Note]

函数 tworastuserfunctext[] 参数是必需的,无论您是否选择将任何参数传递给您的用户函数进行处理。

可用性:2.0.0

示例:将栅格叠加在画布上作为单独的波段

-- define our user defined function --
CREATE OR REPLACE FUNCTION raster_mapalgebra_union(
    rast1 double precision,
    rast2 double precision,
    pos integer[],
    VARIADIC userargs text[]
)
    RETURNS double precision
    AS $$
    DECLARE
    BEGIN
        CASE
            WHEN rast1 IS NOT NULL AND rast2 IS NOT NULL THEN
                RETURN ((rast1 + rast2)/2.);
            WHEN rast1 IS NULL AND rast2 IS NULL THEN
                RETURN NULL;
            WHEN rast1 IS NULL THEN
                RETURN rast2;
            ELSE
                RETURN rast1;
        END CASE;

        RETURN NULL;
    END;
    $$ LANGUAGE 'plpgsql' IMMUTABLE COST 1000;

-- prep our test table of rasters
DROP TABLE IF EXISTS map_shapes;
CREATE TABLE map_shapes(rid serial PRIMARY KEY, rast raster, bnum integer, descrip text);
INSERT INTO map_shapes(rast,bnum, descrip)
WITH mygeoms
    AS ( SELECT 2 As bnum, ST_Buffer(ST_Point(90,90),30) As geom, 'circle' As descrip
            UNION ALL
            SELECT 3 AS bnum,
                ST_Buffer(ST_GeomFromText('LINESTRING(50 50,150 150,150 50)'), 15) As geom, 'big road' As descrip
            UNION ALL
            SELECT 1 As bnum,
                ST_Translate(ST_Buffer(ST_GeomFromText('LINESTRING(60 50,150 150,150 50)'), 8,'join=bevel'), 10,-6) As geom, 'small road' As descrip
            ),
   -- define our canvas to be 1 to 1 pixel to geometry
   canvas
    AS ( SELECT ST_AddBand(ST_MakeEmptyRaster(250,
        250,
        ST_XMin(e)::integer, ST_YMax(e)::integer, 1, -1, 0, 0 ) , '8BUI'::text,0) As rast
        FROM (SELECT ST_Extent(geom) As e,
                    Max(ST_SRID(geom)) As srid
                    from mygeoms
                    ) As foo
            )
-- return our rasters aligned with our canvas
SELECT ST_AsRaster(m.geom, canvas.rast, '8BUI', 240) As rast, bnum, descrip
                FROM mygeoms AS m CROSS JOIN canvas
UNION ALL
SELECT canvas.rast, 4, 'canvas'
FROM canvas;

-- Map algebra on single band rasters and then collect with ST_AddBand
INSERT INTO map_shapes(rast,bnum,descrip)
SELECT ST_AddBand(ST_AddBand(rasts[1], rasts[2]),rasts[3]), 4, 'map bands overlay fct union (canvas)'
    FROM (SELECT ARRAY(SELECT ST_MapAlgebraFct(m1.rast, m2.rast,
            'raster_mapalgebra_union(double precision, double precision, integer[], text[])'::regprocedure, '8BUI', 'FIRST')
                FROM map_shapes As m1 CROSS JOIN map_shapes As m2
    WHERE m1.descrip = 'canvas' AND m2.descrip <> 'canvas' ORDER BY m2.bnum) As rasts) As foo;
                    

映射波段叠加(画布)(R:小路,G:圆形,B:大路)

接受额外参数的用户定义函数

CREATE OR REPLACE FUNCTION raster_mapalgebra_userargs(
    rast1 double precision,
    rast2 double precision,
    pos integer[],
    VARIADIC userargs text[]
)
    RETURNS double precision
    AS $$
    DECLARE
    BEGIN
        CASE
            WHEN rast1 IS NOT NULL AND rast2 IS NOT NULL THEN
                RETURN least(userargs[1]::integer,(rast1 + rast2)/2.);
            WHEN rast1 IS NULL AND rast2 IS NULL THEN
                RETURN userargs[2]::integer;
            WHEN rast1 IS NULL THEN
                RETURN greatest(rast2,random()*userargs[3]::integer)::integer;
            ELSE
                RETURN greatest(rast1, random()*userargs[4]::integer)::integer;
        END CASE;

        RETURN NULL;
    END;
    $$ LANGUAGE 'plpgsql' VOLATILE COST 1000;

SELECT ST_MapAlgebraFct(m1.rast, 1, m1.rast, 3,
            'raster_mapalgebra_userargs(double precision, double precision, integer[], text[])'::regprocedure,
                '8BUI', 'INTERSECT', '100','200','200','0')
                FROM map_shapes As m1
    WHERE m1.descrip = 'map bands overlay fct union (canvas)';
                    

具有额外参数和来自同一栅格的不同波段的用户定义函数

另请参阅

ST_MapAlgebraExprST_BandPixelTypeST_GeoReferenceST_SetValue