1

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

Map.setCenter(-2.063,6.088,12)

//Importing SENTINEL-2 collection 
var St2 = ee.ImageCollection('COPERNICUS/S2')
    .filterDate('2015-01-01','2020-07-15')
    .filterBounds(galamsey_aoi)
    .select(['B1','B2','B3','B4','B8','B9','B11','B12'])
      .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',5))
      .median()
      .clip(galamsey_aoi);

//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)
                    .reduceToImage({
                    properties:['id'],reducer:ee.Reducer.first()
                    }
                    );
                    
//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)
                    .reduceToImage({
                    properties:['id'],reducer:ee.Reducer.first()
                    }
                    );
                    

//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)

print(builtup)
print(ndviFinal,"ndviFinal")

// 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)

https://code.earthengine.google.com/60f9cc570c405145c1e572ce8efd9384

3

1 Answer 1

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())

https://code.earthengine.google.com/cd28e0647e0c0b40125c518fd55b7177

2
  • 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.