I am trying to mask out part of an image(tiff) with another image(tiff) using the 'updateMask' in google earth engine but I keep getting an empty output. The '.not()' is supposed to give the opposite of the output. I want the cleanup to be masked out from the ndviFinal


//Importing SENTINEL-2 collection 
var St2 = ee.ImageCollection('COPERNICUS/S2')

//Display SENTINEL-2 image
var s2_palette = {bands: ['B11', 'B8', 'B4'], min:300, max: 4550}; // specify false color palette
Map.addLayer(St2,s2_palette,'Sentinel 2 (2015-2020)', false);

// NDVI Composite
var ndvi = St2.normalizedDifference(['B8','B4']);

var ndvi1 = ndvi.gt(0.3).not().selfMask();

var ndvi2 = ndvi.lte(0.018).and(ndvi.gt(-0.07));

var ndviFinal = ndvi2.blend(ndvi1).selfMask();

//Preparing masking

//Adding an ID to a feature collection
var addID = function(feature) {
  return feature.set({id: 1});

// Map the ID function over the FeatureCollection and converting to Image (osm settlement).
var osm_settlement = osm_settlement.map(addID)
//Clipping osm_settlement to project area
var mask1 = osm_settlement.clip(galamsey_aoi)

//Creating a buffer of 40m 
var buffer = function(feature) {
  return feature.buffer(40);
var road_buffer = table.map(buffer)

var road = road_buffer.map(addID)

//Merging or combining all the mask
var cleanup = mask1.blend(builtup).blend(road).mask().clip(galamsey_aoi).selfMask();
Map.addLayer(cleanup,{min:0 , max:1, palette:['yellow']},'cleanup', false)
Map.addLayer(builtup,{min:0 , max:1, palette:['yellow']},'builtup', false)


// Creating of final mask
var maskComposite = ndviFinal.updateMask(cleanup.not()).selfMask()

//Display of NDVI composite
Map.addLayer(ndviFinal, {min:0, max:1,  palette:['green']},'ndviFinal', false)
Map.addLayer(maskComposite, {min:0, max:1,  palette:['red']},'maskComposite', false)



1 Answer 1


Masks ain't supposed to be self masked. The layer cleanup has pixels with value 1 and other pixels masked, so when you inverse using .not() you get pixels with value 0 and other pixels masked. Therefore, when you apply that mask, you get all pixels masked.

You have to unmask cleanup:

var maskComposite = ndviFinal.updateMask(cleanup.unmask().not())


  • 1
    Thank you Rodrigo Feb 4, 2021 at 9:17
  • This might be a bit beside the question, but a simpler solution would of been replacing the gt() function with lte() which would be the opposite of gt(), same as using .not()
    – M. Nicolas
    Feb 4, 2021 at 20:34

Your Answer

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

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