ee.Image.clipToBoundsAndScale

Clips an image to the bounds of a Geometry, and scales the clipped image to a particular size or scale.

UsageReturns
Image.clipToBoundsAndScale(geometry, width, height, maxDimension, scale)Image
ArgumentTypeDetails
this: inputImageThe image to clip and scale.
geometryGeometry, default: nullThe Geometry to clip the image to. The image will be clipped to the bounding box, in the image's projection, of this geometry.
widthInteger, default: nullThe width to scale the image to, in pixels. Must be provided along with "height". Exclusive with "maxDimension" and "scale".
heightInteger, default: nullThe height to scale the image to, in pixels. Must be provided along with "width". Exclusive with "maxDimension" and "scale".
maxDimensionInteger, default: nullThe maximum dimension to scale the image to, in pixels. Exclusive with "width", "height" and "scale".
scaleFloat, default: nullIf scale is specified, then the projection is scaled by dividing the specified scale value by the nominal size of a meter in the image's projection. Exclusive with "width", "height" and "maxDimension".

Examples

Code Editor (JavaScript)

// A digital elevation model.
var dem = ee.Image('NASA/NASADEM_HGT/001');
var demVis = {bands: 'elevation', min: 0, max: 2000};
print('DEM', dem);
Map.setCenter(-121.38, 46.51, 8);
Map.addLayer(dem, demVis, 'DEM');

// Clip DEM by a single polygon geometry, specify width and height parameters.
var geom1 = ee.Geometry.BBox(-123.55, 46.61, -122.57, 46.98);
var demClip1 = dem.clipToBoundsAndScale({
  geometry: geom1,
  width: 20,  // pixels
  height: 10  // pixels
});
print('Clipped image retains metadata and band names', demClip1);
Map.addLayer(demClip1, demVis, 'Single geometry clip (width, height)');
Map.addLayer(geom1, {color: 'red'}, 'Single geometry (width, height)');

// Clip DEM by a single polygon geometry, specify maxDimension parameter.
var geom2 = ee.Geometry.BBox(-120.79, 46.58, -120.16, 46.81);
var demClip2 = dem.clipToBoundsAndScale({
  geometry: geom2,
  maxDimension: 5,  // pixels
});
Map.addLayer(demClip2, demVis, 'Single polygon clip (maxDimension)');
Map.addLayer(geom2, {color: 'yellow'}, 'Single polygon (maxDimension)');

// Clip DEM by a single polygon geometry, specify scale parameter.
var geom3 = ee.Geometry.BBox(-120.79, 46.18, -120.16, 46.41);
var demClip3 = dem.clipToBoundsAndScale({
  geometry: geom3,
  scale: 1e4,  // meters
});
Map.addLayer(demClip3, demVis, 'Single polygon clip (scale)');
Map.addLayer(geom3, {color: 'blue'}, 'Single polygon (scale)');