名称

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;
                    

裁剪之前的完整栅格切片

裁剪之后