Name

ST_Clip — 返回由输入几何图形裁剪的栅格。如果未指定波段号,则处理所有波段。如果未指定 crop 或其值为 TRUE,则输出栅格将被裁剪。如果 touched 设置为 TRUE,则包括接触到的像素;否则,仅当像素的中心位于几何图形内时,才会包括该像素。

Synopsis

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 生成的栅格必须为剪切区域分配一个节点数据值,每个波段一个。 如果未提供任何内容,并且输入栅格未定义 nodata 值,则生成的栅格的 nodata 值将设置为 ST_MinPossibleValue(ST_BandPixelType(rast, band))。 当数组中的nodata值的数量小于波段的数量时,数组中的最后一个值用于剩余的波段。 如果节点数据值的数量大于波段数量,则忽略多余的节点数据值。 所有接受节点数据值数组的变体也接受将分配给每个波段的单个值。

如果未指定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 Aerial Orthos页面上找到。

示例:比较选择所有相交的和不选择所有相交的情况

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
                    

裁剪前的完整栅格图块

裁剪后

示例:裁剪未裁剪的单波段,并将其他波段不变添加回去

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

裁剪前的完整栅格图块

裁剪后