ee.Image.clipToCollection

将图片裁剪为 FeatureCollection。输出波段与输入波段完全对应,但集合中至少一个要素的几何图形未覆盖的数据会被遮盖。输出图片会保留输入图片的元数据。

用法返回
Image.clipToCollection(collection)图片
参数类型详细信息
此:input图片要剪辑的图片。
collection对象要剪裁到的 FeatureCollection。

示例

代码编辑器 (JavaScript)

// A digital elevation model.
var dem = ee.Image('NASA/NASADEM_HGT/001');

// A FeatureCollection defining Southeast Asia boundary.
var fc = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017')
             .filter('wld_rgn == "SE Asia"');

// Clip the DEM by the Southeast Asia boundary FeatureCollection.
var demClip = dem.clipToCollection(fc);
print('Clipped image retains metadata and band names', demClip);

// Add layers to the map.
Map.setCenter(110.64, 9.16, 4);
Map.addLayer(dem, {bands: 'elevation', min: 0, max: 2500}, 'Original DEM');
Map.addLayer(fc, {color: 'blue'}, 'FeatureCollection');
Map.addLayer(demClip,
 {bands: 'elevation', min: 0, max: 2500, palette: ['green', 'yellow', 'brown']},
 'Clipped DEM');

Python 设置

如需了解 Python API 和如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# A digital elevation model.
dem = ee.Image('NASA/NASADEM_HGT/001')

# A FeatureCollection defining Southeast Asia boundary.
fc = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filter(
    'wld_rgn == "SE Asia"'
)

# Clip the DEM by the Southeast Asia boundary FeatureCollection.
dem_clip = dem.clipToCollection(fc)
display('Clipped image retains metadata and band names', dem_clip)

# Add layers to the map.
m = geemap.Map()
m.set_center(110.64, 9.16, 4)
m.add_layer(dem, {'bands': 'elevation', 'min': 0, 'max': 2500}, 'Original DEM')
m.add_layer(fc, {'color': 'blue'}, 'FeatureCollection')
m.add_layer(
    dem_clip,
    {
        'bands': 'elevation',
        'min': 0,
        'max': 2500,
        'palette': ['green', 'yellow', 'brown'],
    },
    'Clipped DEM',
)
m