ee.Image.clip

Cắt một hình ảnh thành một Hình học hoặc Đối tượng.

Các dải đầu ra hoàn toàn tương ứng với các dải đầu vào, ngoại trừ dữ liệu không thuộc hình học sẽ bị che. Hình ảnh đầu ra giữ lại siêu dữ liệu của hình ảnh đầu vào.

Sử dụng clipToCollection để cắt một hình ảnh thành một FeatureCollection.

Trả về hình ảnh bị cắt.

Cách sử dụngGiá trị trả về
Image.clip(geometry)Hình ảnh
Đối sốLoạiThông tin chi tiết
this: imageHình ảnhĐối tượng Image.
geometryĐối tượng|Hình học|Đối tượngHình học hoặc Đối tượng để cắt.

Ví dụ

Trình soạn thảo mã (JavaScript)

// A digital elevation model.
var dem = ee.Image('NASA/NASADEM_HGT/001');
var demVis = {bands: 'elevation', min: 0, max: 1500};

// Clip the DEM by a polygon geometry.
var geomPoly = ee.Geometry.BBox(-121.55, 39.01, -120.57, 39.38);
var demClip = dem.clip(geomPoly);
print('Clipped image retains metadata and band names', demClip);
Map.setCenter(-121.12, 38.13, 8);
Map.addLayer(demClip, demVis, 'Polygon clip');
Map.addLayer(geomPoly, {color: 'green'}, 'Polygon geometry', false);

// Clip the DEM by a line geometry.
var geomLine = ee.Geometry.LinearRing(
    [[-121.19, 38.10], [-120.53, 38.54], [-120.22, 37.83], [-121.19, 38.10]]);
Map.addLayer(dem.clip(geomLine), demVis, 'Line clip');
Map.addLayer(geomLine, {color: 'orange'}, 'Line geometry', false);

// Images have geometry; clip the dem image by the geometry of an S2 image.
var s2Img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');
var geomS2Img = s2Img.geometry();
Map.addLayer(dem.clip(geomS2Img), demVis, 'Image geometry clip');
Map.addLayer(geomS2Img, {color: 'blue'}, 'Image geometry', false);

// Don't use ee.Image.clip prior to ee.Image.regionReduction, the "geometry"
// parameter handles it more efficiently.
var zonalMax = dem.select('elevation').reduceRegion({
  reducer: ee.Reducer.max(),
  geometry: geomPoly
});
print('Max elevation (m)', zonalMax.get('elevation'));

// Don't use ee.Image.clip to clip an image by a FeatureCollection, use
// ee.Image.clipToCollection(collection).
var watersheds = ee.FeatureCollection('USGS/WBD/2017/HUC10')
    .filterBounds(ee.Geometry.Point(-122.754, 38.606).buffer(2e4));
Map.addLayer(dem.clipToCollection(watersheds), demVis, 'Watersheds clip');
Map.addLayer(watersheds, {color: 'red'}, 'Watersheds', false);

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về API Python và cách sử dụng geemap cho quá trình phát triển tương tác.

import ee
import geemap.core as geemap

Colab (Python)

# A digital elevation model.
dem = ee.Image('NASA/NASADEM_HGT/001')
dem_vis = {'bands': 'elevation', 'min': 0, 'max': 1500}

# Clip the DEM by a polygon geometry.
geom_poly = ee.Geometry.BBox(-121.55, 39.01, -120.57, 39.38)
dem_clip = dem.clip(geom_poly)
display('Clipped image retains metadata and band names', dem_clip)
m = geemap.Map()
m.set_center(-121.12, 38.13, 8)
m.add_layer(dem_clip, dem_vis, 'Polygon clip')
m.add_layer(geom_poly, {'color': 'green'}, 'Polygon geometry', False)

# Clip the DEM by a line geometry.
geom_line = ee.Geometry.LinearRing(
    [[-121.19, 38.10], [-120.53, 38.54], [-120.22, 37.83], [-121.19, 38.10]]
)
m.add_layer(dem.clip(geom_line), dem_vis, 'Line clip')
m.add_layer(geom_line, {'color': 'orange'}, 'Line geometry', False)

# Images have geometry clip the dem image by the geometry of an S2 image.
s_2_img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')
geom_s_2_img = s_2_img.geometry()
m.add_layer(dem.clip(geom_s_2_img), dem_vis, 'Image geometry clip')
m.add_layer(geom_s_2_img, {'color': 'blue'}, 'Image geometry', False)

# Don't use ee.Image.clip prior to ee.Image.regionReduction, the "geometry"
# parameter handles it more efficiently.
zonal_max = dem.select('elevation').reduceRegion(
    reducer=ee.Reducer.max(), geometry=geom_poly
)
display('Max elevation (m)', zonal_max.get('elevation'))

# Don't use ee.Image.clip to clip an image by a FeatureCollection, use
# ee.Image.clipToCollection(collection).
watersheds = ee.FeatureCollection('USGS/WBD/2017/HUC10').filterBounds(
    ee.Geometry.Point(-122.754, 38.606).buffer(2e4)
)
m.add_layer(dem.clipToCollection(watersheds), dem_vis, 'Watersheds clip')
m.add_layer(watersheds, {'color': 'red'}, 'Watersheds', False)
m