I am having difficulties with masks in Google Earth Engine (GEE).

I need to classify a sentinel image of the GEE. But I don't want the eucalyptus areas in my image to be classified. I have a shapefile that contains all the areas of eucalyptus planted and I would like the GEE to disregard the eucalyptus shapefile area during classification.

How can I do it?

var S2_1 = ee.ImageCollection("COPERNICUS/S2")
    .filterMetadata('MGRS_TILE', 'equals', '21KYT')
    .filterMetadata('CLOUDY_PIXEL_PERCENTAGE','less_than', 1) 

var classnames = veg1.merge(past1).merge(past2).merge(agua).merge(past3).merge(veg2);

var bands = ['B4', 'B3', 'B2'];

var training = S2_1.select(bands).sampleRegions({
  collection: classnames,
  scale: 10

print (training);

var cart = ee.Classifier.cart().train({
  features: training,
  classProperty: 'classe',
  inputProperties: bands

var cart_class = cart.select(bands).classify(cart)

1 Answer 1


It's not entirely clear in the snippet provided which is the shapefile containing the eucalyptus, but masking is an easy task. You might want to transform your shapefile into a raster first where 1 is eucalyptus and everything else is 0. You can do it directly on GEE (https://developers.google.com/earth-engine/guides/reducers_reduce_to_image) or it's easy to do on ArcMap or Qgis. Once you have your shapefile as a raster applying the mask is very simple.

var mask = eucalyptusImage.eq(1); // Finding which pixels equal 1 thus indicating eucalyptus
var S2_2 = S2_1.updateMask(mask); // Masking the image with the predefined mask


  • through your answer i managed to solve my problem. Convert my raster to ee.Image () // Upload my raster_mask var mask = ee.Image (raster_mask) .eq (0) // convert raster in ee.Image () var S2_2_mask = S2_2.updateMask (mask) //aply the mask in my study area Dec 16, 2020 at 15:08
  • Great! Happy coding!
    – M. Nicolas
    Dec 16, 2020 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.