名稱

ST_Clip — 回傳以輸入幾何圖形裁剪過的網格。如果未指定波段編號,則會處理所有波段。如果未指定 crop 或設為 TRUE,則會裁剪輸出網格。如果 touched 設為 TRUE,則會包含接觸到的像素,否則只有當像素中心在幾何圖形內時才會包含。

概要

raster ST_Clip(raster rast, integer[] nband, geometry geom, double precision[] nodataval=NULL, boolean crop=TRUE, boolean touched=FALSE);

raster ST_Clip(raster rast, integer nband, geometry geom, double precision nodataval, boolean crop=TRUE, boolean touched=FALSE);

raster ST_Clip(raster rast, integer nband, geometry geom, boolean crop, boolean touched=FALSE);

raster ST_Clip(raster rast, geometry geom, double precision[] nodataval=NULL, boolean crop=TRUE, boolean touched=FALSE);

raster ST_Clip(raster rast, geometry geom, double precision nodataval, boolean crop=TRUE, boolean touched=FALSE);

raster ST_Clip(raster rast, geometry geom, boolean crop, boolean touched=FALSE);

描述

回傳以輸入幾何圖形 geom 裁剪過的網格。如果未指定波段索引,則會處理所有波段。

從 ST_Clip 產生的網格必須為裁剪的區域指定一個無資料值,每個波段一個。如果沒有提供任何值,且輸入網格沒有定義無資料值,則結果網格的無資料值會設定為 ST_MinPossibleValue(ST_BandPixelType(rast, band))。當陣列中的無資料值數量小於波段數量時,陣列中的最後一個值會用於剩餘的波段。如果無資料值的數量大於波段數量,則會忽略額外的無資料值。所有接受無資料值陣列的變體也接受單一值,該值將會指派給每個波段。

如果未指定 crop,則會假設為 true,表示輸出網格會裁剪到 geomrast 範圍的交集。如果 crop 設定為 false,則新網格會取得與 rast 相同的範圍。如果 touched 設定為 true,則會選取 rast 中與幾何圖形相交的所有像素。

[Note]

預設行為是 touched=false,這只會選取像素中心被幾何圖形覆蓋的像素。

增強功能:3.5.0 - 新增 touched 參數。

可用性:2.0.0

增強功能:2.1.0 以 C 語言重新編寫

此處的範例使用 MassGIS 網站上提供的麻薩諸塞州航空資料 MassGIS 航空正射影像

範例:比較選取所有接觸與不選取所有接觸

SELECT ST_Count(rast) AS count_pixels_in_orig, ST_Count(rast_touched) AS all_touched_pixels, ST_Count(rast_not_touched) AS default_clip
FROM ST_AsRaster(ST_Letters('R'), scalex => 1.0, scaley => -1.0) AS r(rast) 
    INNER JOIN ST_GeomFromText('LINESTRING(0 1, 5 6, 10 10)') AS g(geom)
 ON ST_Intersects(r.rast,g.geom)
 , ST_Clip(r.rast, g.geom, touched => true) AS rast_touched
 , ST_Clip(r.rast, g.geom, touched => false) AS rast_not_touched;
 
 count_pixels_in_orig | all_touched_pixels | default_clip
----------------------+--------------------+--------------
                 2605 |                 16 |           10
(1 row)
 

範例:1 波段裁剪(未接觸)

-- Clip the first band of an aerial tile by a 20 meter buffer.
SELECT ST_Clip(rast, 1,
        ST_Buffer(ST_Centroid(ST_Envelope(rast)),20)
    ) from aerials.boston
WHERE rid = 4;
                    
-- Demonstrate effect of crop on final dimensions of raster
-- Note how final extent is clipped to that of the geometry
-- if crop = true
SELECT ST_XMax(ST_Envelope(ST_Clip(rast, 1, clipper, true))) As xmax_w_trim,
    ST_XMax(clipper) As xmax_clipper,
    ST_XMax(ST_Envelope(ST_Clip(rast, 1, clipper, false))) As xmax_wo_trim,
    ST_XMax(ST_Envelope(rast)) As xmax_rast_orig
FROM (SELECT rast, ST_Buffer(ST_Centroid(ST_Envelope(rast)),6) As clipper
    FROM aerials.boston
WHERE rid = 6) As foo;

   xmax_w_trim    |   xmax_clipper   |   xmax_wo_trim   |  xmax_rast_orig
------------------+------------------+------------------+------------------
 230657.436173996 | 230657.436173996 | 230666.436173996 | 230666.436173996
                    

裁剪前的完整網格圖塊

裁剪後

範例:1 波段裁剪,不裁剪,並將其他波段新增回未變更的狀態

-- Same example as before, but we need to set crop to false to be able to use ST_AddBand
-- because ST_AddBand requires all bands be the same Width and height
SELECT ST_AddBand(ST_Clip(rast, 1,
        ST_Buffer(ST_Centroid(ST_Envelope(rast)),20),false
    ), ARRAY[ST_Band(rast,2),ST_Band(rast,3)] ) from aerials.boston
WHERE rid = 6;
                    

裁剪前的完整網格圖塊

裁剪後 - 超現實

範例:裁剪所有波段

-- Clip all bands of an aerial tile by a 20 meter buffer.
-- Only difference is we don't specify a specific band to clip
-- so all bands are clipped
SELECT ST_Clip(rast,
      ST_Buffer(ST_Centroid(ST_Envelope(rast)), 20),
      false
    ) from aerials.boston
WHERE rid = 4;
                    

裁剪前的完整網格圖塊

裁剪後