名称

ST_MapAlgebraExpr — 双栅格波段版本:创建一个新的单波段栅格,通过对两个输入栅格波段应用有效的 PostgreSQL 代数运算,并使用提供的像素类型生成。如果未指定波段编号,则假定为每个栅格的波段 1。结果栅格将与第一个栅格定义的网格对齐(比例、倾斜和像素角),并具有由 "extenttype" 参数定义的范围。"extenttype" 的值可以是:INTERSECTION、UNION、FIRST、SECOND。

概要

raster ST_MapAlgebraExpr(raster rast1, raster rast2, text expression, text pixeltype=same_as_rast1_band, text extenttype=INTERSECTION, text nodata1expr=NULL, text nodata2expr=NULL, double precision nodatanodataval=NULL);

raster ST_MapAlgebraExpr(raster rast1, integer band1, raster rast2, integer band2, text expression, text pixeltype=same_as_rast1_band, text extenttype=INTERSECTION, text nodata1expr=NULL, text nodata2expr=NULL, double precision nodatanodataval=NULL);

描述

[Warning]

自 2.1.0 版本起,ST_MapAlgebraExpr 已被弃用。请改用 ST_MapAlgebra(表达式版本)

创建一个新的单波段栅格,通过将有效的 PostgreSQL 代数运算应用于由 expression 定义的两个波段,这两个波段来自输入栅格波段 rast1rast2。 如果没有指定 band1band2,则假定为波段 1。结果栅格将与第一个栅格定义的网格对齐(比例、倾斜和像素角)。结果栅格将具有由 extenttype 参数定义的范围。

表达式

一个 PostgreSQL 代数表达式,涉及两个栅格和 PostgreSQL 定义的函数/运算符,当像素相交时,将定义像素值。 例如:(([rast1] + [rast2])/2.0)::integer

像素类型

输出栅格的像素类型。必须是 ST_BandPixelType 中列出的类型之一,可以省略或设置为 NULL。如果未传入或设置为 NULL,则默认为第一个栅格的像素类型。

范围类型

控制结果栅格的范围

  1. INTERSECTION - 新栅格的范围是两个栅格的交集。这是默认值。

  2. UNION - 新栅格的范围是两个栅格的并集。

  3. FIRST - 新栅格的范围与第一个栅格的范围相同。

  4. SECOND - 新栅格的范围与第二个栅格的范围相同。

nodata1expr

一个只涉及 rast2 或常数的代数表达式,用于定义当 rast1 的像素为 nodata 值并且空间上对应的 rast2 像素有值时返回的内容。

nodata2expr

一个只涉及 rast1 或常数的代数表达式,用于定义当 rast2 的像素为 nodata 值并且空间上对应的 rast1 像素有值时返回的内容。

nodatanodataval

当空间上对应的 rast1 和 rast2 像素都为 nodata 值时返回的数值常数。

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

使用术语 [rast1.val][rast2.val] 来引用原始栅格波段的像素值,使用 [rast1.x][rast1.y] 等来引用像素的列/行位置。

可用性:2.0.0

示例:2 波段交集和并集

从我们的原始栅格创建一个新的单波段栅格,它是原始栅格波段对 2 取模的函数。

--Create a cool set of rasters --
DROP TABLE IF EXISTS fun_shapes;
CREATE TABLE fun_shapes(rid serial PRIMARY KEY, fun_name text, rast raster);

-- Insert some cool shapes around Boston in Massachusetts state plane meters --
INSERT INTO fun_shapes(fun_name, rast)
VALUES ('ref', ST_AsRaster(ST_MakeEnvelope(235229, 899970, 237229, 901930,26986),200,200,'8BUI',0,0));

INSERT INTO fun_shapes(fun_name,rast)
WITH ref(rast) AS (SELECT rast FROM fun_shapes WHERE fun_name = 'ref' )
SELECT 'area' AS fun_name, ST_AsRaster(ST_Buffer(ST_SetSRID(ST_Point(236229, 900930),26986), 1000),
            ref.rast,'8BUI', 10, 0) As rast
FROM ref
UNION ALL
SELECT 'rand bubbles',
            ST_AsRaster(
            (SELECT ST_Collect(geom)
    FROM (SELECT ST_Buffer(ST_SetSRID(ST_Point(236229 + i*random()*100, 900930 + j*random()*100),26986), random()*20) As geom
            FROM generate_series(1,10) As i, generate_series(1,10) As j
            ) As foo ), ref.rast,'8BUI', 200, 0)
FROM ref;

--map them -
SELECT  ST_MapAlgebraExpr(
        area.rast, bub.rast, '[rast2.val]', '8BUI', 'INTERSECTION', '[rast2.val]', '[rast1.val]') As interrast,
        ST_MapAlgebraExpr(
            area.rast, bub.rast, '[rast2.val]', '8BUI', 'UNION', '[rast2.val]', '[rast1.val]') As unionrast
FROM
  (SELECT rast FROM fun_shapes WHERE
 fun_name = 'area') As area
CROSS JOIN  (SELECT rast
FROM fun_shapes WHERE
 fun_name = 'rand bubbles') As bub
                    

地图代数交集

地图代数并集

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

-- we use ST_AsPNG to render the image so all single band ones look grey --
WITH mygeoms
    AS ( SELECT 2 As bnum, ST_Buffer(ST_Point(1,5),10) As geom
            UNION ALL
            SELECT 3 AS bnum,
                ST_Buffer(ST_GeomFromText('LINESTRING(50 50,150 150,150 50)'), 10,'join=bevel') As geom
            UNION ALL
            SELECT 1 As bnum,
                ST_Buffer(ST_GeomFromText('LINESTRING(60 50,150 150,150 50)'), 5,'join=bevel') As geom
            ),
   -- define our canvas to be 1 to 1 pixel to geometry
   canvas
    AS (SELECT ST_AddBand(ST_MakeEmptyRaster(200,
        200,
        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
            ),
   rbands AS (SELECT ARRAY(SELECT ST_MapAlgebraExpr(canvas.rast, ST_AsRaster(m.geom, canvas.rast, '8BUI', 100),
                 '[rast2.val]', '8BUI', 'FIRST', '[rast2.val]', '[rast1.val]') As rast
                FROM mygeoms AS m CROSS JOIN canvas
                ORDER BY m.bnum) As rasts
                )
          SELECT rasts[1] As rast1 , rasts[2] As rast2, rasts[3] As rast3, ST_AddBand(
                    ST_AddBand(rasts[1],rasts[2]), rasts[3]) As final_rast
            FROM rbands;
                    

rast1

rast2

rast3

final_rast

示例:将选定宗地的 2 米边界覆盖在航空影像上

-- Create new 3 band raster composed of first 2 clipped bands, and overlay of 3rd band with our geometry
-- This query took 3.6 seconds on PostGIS windows 64-bit install
WITH pr AS
-- Note the order of operation: we clip all the rasters to dimensions of our region
(SELECT ST_Clip(rast,ST_Expand(geom,50) ) As rast, g.geom
    FROM aerials.o_2_boston AS r INNER JOIN
-- union our parcels of interest so they form a single geometry we can later intersect with
        (SELECT ST_Union(ST_Transform(geom,26986)) AS geom
          FROM landparcels WHERE pid IN('0303890000', '0303900000')) As g
        ON ST_Intersects(rast::geometry, ST_Expand(g.geom,50))
),
-- we then union the raster shards together
-- ST_Union on raster is kinda of slow but much faster the smaller you can get the rasters
-- therefore we want to clip first and then union
prunion AS
(SELECT ST_AddBand(NULL, ARRAY[ST_Union(rast,1),ST_Union(rast,2),ST_Union(rast,3)] ) As clipped,geom
FROM pr
GROUP BY geom)
-- return our final raster which is the unioned shard with
-- with the overlay of our parcel boundaries
-- add first 2 bands, then mapalgebra of 3rd band + geometry
SELECT ST_AddBand(ST_Band(clipped,ARRAY[1,2])
    , ST_MapAlgebraExpr(ST_Band(clipped,3), ST_AsRaster(ST_Buffer(ST_Boundary(geom),2),clipped, '8BUI',250),
     '[rast2.val]', '8BUI', 'FIRST', '[rast2.val]', '[rast1.val]') ) As rast
FROM prunion;
                    

蓝色线条是选定宗地的边界