Name

ST_MapAlgebraExpr — 래스터 밴드 2개 버전: 입력 래스터 2개에 대해 유효한 PostgreSQL 대수 연산을 적용해서 형성되고, 설정한 픽셀 유형을 가진, 밴드 1개를 가진 새 래스터를 생성합니다. 따로 밴드를 설정하지 않을 경우, 각 래스터의 밴드 1로 가정합니다. 출력 래스터는 첫 번째 래스터가 정의하는 그리드 상에 (축척, 기울기 및 픽셀 모서리가) 정렬될 것입니다. extenttype 파라미터가 출력 래스터의 범위를 정의할 것입니다. extenttype 의 값은 INTERSECTION, UNION, FIRST, SECOND가 될 수 있습니다.

Synopsis

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]

ST_MapAlgebraExpr 2.1.0 버전부터 더 이상 이 함수를 지원하지 않습니다. 대신 ST_MapAlgebra (expression version) 함수를 이용하십시오.

입력 래스터 밴드 rast1, (rast2)에 대한 expression 이 정의하는 밴드 2개에 대해, 유효한 PostgreSQL 대수 연산을 적용해서 형성된, 밴드 1개를 가진 새 래스터를 생성합니다. band1, band2 를 설정하지 않을 경우, 밴드 1로 가정합니다. 출력 래스터는 첫 번째 래스터가 정의하는 그리드 상에 (축척, 기울기 및 픽셀 모서리가) 정렬될 것입니다. extenttype 파라미터가 출력 래스터의 범위를 정의할 것입니다.

표현식(expression)

래스터 2개가 관련된 PostgreSQL 대수 표현식 및 픽셀들이 교차할 경우 픽셀 값을 정의할 PostgreSQL 정의 함수/연산자입니다. 예: (([rast1] + [rast2])/2.0)::integer

pixeltype

출력 래스터의 픽셀 유형입니다. 이 유형은 ST_BandPixelType 목록에 존재하는 유형 가운데 하나이거나, 생략되거나, NULL로 설정돼야만 합니다. 따로 설정하지 않거나 NULL로 설정하지 않으면, 첫 번째 래스터의 픽셀 유형을 기본값으로 삼을 것입니다.

extenttype

출력 래스터의 범위 제어

  1. INTERSECTION - 새 래스터의 범위는 두 래스터의 교차 부분입니다. 기본값입니다.

  2. UNION - 새 래스터의 범위는 두 래스터를 통합한 범위입니다.

  3. FIRST - 새 래스터의 범위는 첫 번째 래스터의 범위와 동일합니다.

  4. SECOND - 새 래스터의 범위는 두 번째 래스터의 범위와 동일합니다.

nodata1expr

rast1 의 픽셀들이 NODATA 값이고 공간적으로 상응하는 rast2 의 픽셀들이 값을 가지고 있을 때 어떤 것을 반환할지 정의하는 상수만, 또는 rast2 와만 관련된 대수 표현식입니다.

nodata2expr

rast2 의 픽셀들이 NODATA 값이고 공간적으로 상응하는 rast1 의 픽셀들이 값을 가지고 있을 때 어떤 것을 반환할지 정의하는 상수만, 또는 rast1 과만 관련된 대수 표현식입니다.

nodatanodataval

공간적으로 상응하는 rast1rast2 의 픽셀들이 모두 NODATA 값일 경우 반환하는 숫자 상수입니다.

pixeltype 을 설정할 경우, 새 래스터의 밴드가 해당 픽셀 유형이 될 것입니다. pixeltype 이 NULL이거나 따로 설정하지 않을 경우, 새 래스터의 밴드는 입력 rast1 의 밴드와 동일한 픽셀 유형이 될 것입니다.

원본 밴드의 픽셀 값을 참조하는 데 [rast1.val], [rast2.val], 픽셀의 열/행 위치를 참조하는 데 [rast1.x], [rast1.y] 등의 용어를 사용하십시오.

2.0.0 버전부터 사용할 수 있습니다.

예시: 밴드 2개의 교차 및 통합

원본 래스터 2개를 입력받는 모듈로(modulo) 함수인 원본으로부터 밴드 1개를 가진 새 래스터를 생성합니다.

--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
                    

맵 대수 교차(intersection)

맵 대수 통합(union)

예시: 캔버스 상에 래스터들을 개별 밴드로서 오버레이

-- 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;
                    

파란색 라인이 선택된 획지의 경계선입니다.

참고

ST_MapAlgebraExpr, ST_AddBand, ST_AsPNG, ST_AsRaster, ST_MapAlgebraFct, ST_BandPixelType, ST_GeoReference, ST_Value, ST_Union, ST_Union