ee.Image.clip

Clips an image to a Geometry or Feature.

The output bands correspond exactly to the input bands, except data not covered by the geometry is masked. The output image retains the metadata of the input image.

Use clipToCollection to clip an image to a FeatureCollection.

Returns the clipped image.

UsageReturns
Image.clip(geometry)Image
ArgumentTypeDetails
this: imageImageThe Image instance.
geometryFeature|Geometry|ObjectThe Geometry or Feature to clip to.

Examples

Code Editor (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);