resolutionmaskgoogle-earth-enginelandsat

Masking datasets with different resolutions in GEE


I've calculated the NDMI index based on the MCD43A4 (spatial resolution 500m) collection for an area where various water bodies exist. What I want to do, is to mask out these water bodies from my collection, based on the Landsat Global Inland Water dataset (spatial resolution 30m), but I have no idea how to do this. The first thing I must do, is to change the spatial resolution of Landsat in order to match it with the MODIS one but I do not understand how to this, should I use a type of Reduce?

Thanks

var geometry = /* color: #d63000 */ee.Geometry.Polygon(
        [[[69.75758392503599, 50.151303763817786],
          [71.60328705003599, 40.18192251959151],
          [93.70777923753599, 41.54446477874571],
          [91.86207611253599, 51.09912927236651]]]);
var dataset = ee.ImageCollection('GLCF/GLS_WATER')
.filterBounds(geometry)
    .map(function(image){return image.clip(geometry)}) ;
var water = dataset.select('water');
var imageCollection = ee.ImageCollection("MODIS/006/MCD43A4")
 .filterBounds(geometry)
    .map(function(image){return image.clip(geometry)}) 
    .filter(ee.Filter.calendarRange(6,8,'month'));
   var modNDMI = imageCollection.select("Nadir_Reflectance_Band2","Nadir_Reflectance_Band6","BRDF_Albedo_Band_Mandatory_Quality_Band2","BRDF_Albedo_Band_Mandatory_Quality_Band6");
/////////////////////////////////////////////////

var quality = function(image){ 
  var mask1 = image.select("BRDF_Albedo_Band_Mandatory_Quality_Band2").eq(0);
  var mask2 = image.select("BRDF_Albedo_Band_Mandatory_Quality_Band6").eq(0);
  return image.updateMask(mask1).updateMask(mask2);
};

var clean_collection = modNDMI.map(quality);

var addNDMI = function(image) {
  var ndmi = image.normalizedDifference(['Nadir_Reflectance_Band2', 'Nadir_Reflectance_Band6']).rename('NDMI');
  return image.addBands(ndmi);
};
var ndmi = clean_collection.map(addNDMI);

var NDMI=ndmi.select('NDMI')
print(water)
//And from this point, I have no idea how to mask the water bodies based on the
//Landsat collection

Solution

  • It's not entirely obvious what you mean by "mask the waterbodies," but if this is not what you intend, then just use water_mask.not().

    var gsw = ee.Image('JRC/GSW1_0/GlobalSurfaceWater');
    var occurrence = gsw.select('occurrence');
    // Create a water mask layer, and set the image mask so that non-water areas
    // are opaque.
    var water_mask = occurrence.gt(90).unmask(0);
    Map.addLayer(water_mask)
    
    var dataset = ee.ImageCollection('GLCF/GLS_WATER')
    var water = dataset.select('water');
    
    var imageCollection = ee.ImageCollection("MODIS/006/MCD43A4")
        .filterDate('2017-01-01', '2018-12-31')
        .filter(ee.Filter.calendarRange(6,8,'month'));
    var modNDMI = imageCollection.select("Nadir_Reflectance_Band2","Nadir_Reflectance_Band6","BRDF_Albedo_Band_Mandatory_Quality_Band2","BRDF_Albedo_Band_Mandatory_Quality_Band6");
    
    var quality = function(image){ 
      var mask1 = image.select("BRDF_Albedo_Band_Mandatory_Quality_Band2").eq(0);
      var mask2 = image.select("BRDF_Albedo_Band_Mandatory_Quality_Band6").eq(0);
      return image.updateMask(mask1).updateMask(mask2);
    };
    
    var clean_collection = modNDMI.map(quality);
    
    var addNDMI = function(image) {
      var ndmi = image.normalizedDifference(['Nadir_Reflectance_Band2', 'Nadir_Reflectance_Band6']).rename('NDMI');
      return image.addBands(ndmi).updateMask(water_mask);
    };
    var ndmi = clean_collection.map(addNDMI);
    
    var NDMI=ndmi.select('NDMI')
    Map.addLayer(NDMI)
    

    See also this tutorial.