名称

ST_MapAlgebraFctNgb — 1-波段版本:使用用户定义的 PostgreSQL 函数映射代数最近邻。 返回一个栅格,其值是涉及输入栅格波段值的邻域的 PLPGSQL 用户函数的结果。

大纲

raster ST_MapAlgebraFctNgb(raster rast, integer band, text pixeltype, integer ngbwidth, integer ngbheight, regprocedure onerastngbuserfunc, text nodatamode, text[] VARIADIC args);

Description

[警告]

ST_MapAlgebraFctNgb 自 2.1.0 起已弃用。 使用ST_MapAlgebra (callback function version)(回调函数版本)代替。

(一个栅格版本)返回一个栅格,其值是涉及输入栅格波段值的邻域的 PLPGSQL 用户函数的结果。 用户函数将像素值的邻域作为数字数组,对于每个像素,返回用户函数的结果,用函数结果替换当前检查像素的像素值。

rast

评估用户函数的栅格。

band

要评估的栅格的波段号。 默认为 1。

pixeltype

输出栅格的结果像素类型。 必须是ST_BandPixelType 中列出的一项,或者省略或设置为 NULL。 如果未传入或设置为 NULL,将默认为 rast 的像素类型。 如果结果大于像素类型允许的值,则会被截断。

ngbwidth

邻域的宽度(以单元格为单位)。

ngbheight

邻域的高度(以单元格为单位)。

onerastngbuserfunc

PLPGSQL/psql 用户函数应用于栅格单个波段的邻域像素。 第一个元素是表示矩形像素邻域的二维数字数组

nodatamode

定义要为无数据或 NULL 的邻域像素传递给函数的值

'ignore':计算忽略在邻域中遇到的任何 NODATA 值 - 该标志必须发送到用户回调函数,并且用户函数决定如何忽略它。

“NULL”:在邻域中遇到的任何 NODATA 值都将导致结果像素为 NULL——在这种情况下将跳过用户回调函数。

“value”:在邻域中遇到的任何 NODATA 值都将替换为参考像素(邻域中心的像素)。 请注意,如果该值为 NODATA,则行为与“NULL”相同(对于受影响的邻域)

args

要传递给用户函数的参数。

可用性: 2.0.0

示例

示例利用 https://gdal.org/user/drivers/raster/postgisraster.html 中描述的作为单个图块加载的卡特里娜栅格,然后在 ST_Rescale 示例中准备

A simple 'callback' user function that averages up all the values in a neighborhood.

Code
CREATE OR REPLACE FUNCTION rast_avg(matrix float[][], nodatamode text, variadic args text[])
    RETURNS float AS
    $$
    DECLARE
        _matrix float[][];
        x1 integer;
        x2 integer;
        y1 integer;
        y2 integer;
        sum float;
    BEGIN
        _matrix := matrix;
        sum := 0;
        FOR x in array_lower(matrix, 1)..array_upper(matrix, 1) LOOP
            FOR y in array_lower(matrix, 2)..array_upper(matrix, 2) LOOP
                sum := sum + _matrix[x][y];
            END LOOP;
        END LOOP;
        RETURN (sum*1.0/(array_upper(matrix,1)*array_upper(matrix,2) ))::integer ;
    END;
    $$
LANGUAGE 'plpgsql' IMMUTABLE COST 1000;
                    

Apply the raster averaging callback within two pixels of each input pixel in the X and Y directions.

Code
SELECT ST_MapAlgebraFctNgb(
        rast,
        1,
        '8BUI',
        4,
        4,
        'rast_avg(float[][], text, text[])'::regprocedure,
        'NULL',
        NULL) As nn_with_border
    FROM katrinas_rescaled
    limit 1;
                    

A self-contained neighborhood average. The source raster has a sharp central square, while the result spreads those values across neighboring pixels. The source uses a non-NODATA background so nodatamode ignore averages background values with the square values, instead of treating the background as absent.

Code
WITH source AS (
    SELECT ST_SetValues(
        ST_AddBand(
            ST_MakeEmptyRaster(16, 16, 0, 16, 1, -1, 0, 0, 0),
            1, '8BUI', 25, NULL
        ),
        1, 6, 6,
        ARRAY[
            [240, 240, 240, 240, 240, 240],
            [240, 240, 240, 240, 240, 240],
            [240, 240, 240, 240, 240, 240],
            [240, 240, 240, 240, 240, 240],
            [240, 240, 240, 240, 240, 240],
            [240, 240, 240, 240, 240, 240]
        ]::double precision[][]
    ) AS rast
), variants AS (
    SELECT 'source' AS title, rast AS rendered FROM source
    UNION ALL
    SELECT 'averaged', ST_SetBandNoDataValue(ST_MapAlgebraFctNgb(
        rast,
        1,
        '8BUI',
        2,
        2,
        'ST_Mean4ma(float[][],text,text[])'::regprocedure,
        'ignore',
        NULL
    ), NULL) FROM source
)
SELECT title, ST_AsPNG(rendered) AS image
FROM variants
ORDER BY title DESC;
栅格输出
title    | image
----------+-------------------------
 source   | PNG image, 16 x 16 pixels
 averaged | PNG image, 16 x 16 pixels
Figure
Geometry figure for visual-rt-st-mapalgebrafctngb-01

Neighborhood averaging.

我们栅格的第一个波段

Averaged result