3

I am a Java script and EE novice but I'm already a great fan of Google Earth Engine. I am simply trying to create a single NDVI and NDWI masked layer to work on. Below you'll find my Frankenstein of a script where I've pieced code together, and it has worked well to create and NDVI and NDWI layer,I can do it with a single image but I just can't figure out how to apply it to the image collection, as it returns the error- 'S2 masked: Layer error: ImageCollection.mosaic: Error in map(ID=20160801T095532_20160801T133428_T32SQD): Image.select: Pattern 'NDVI' did not match any bands'.

I know why it returns this error, the code is wrong, but I just can't figure it out for find an example to help me out.

FYI: I am using Sentinel-2 MSI: Multispectral Instrument, Level-1C for the image collection.

var S2collection =  imageCollection.filterDate('2016-08-01','2019-04-10')
            .filterBounds(aoi)
            .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'equals', 0);
                Map.addLayer(S2collection, {bands:['B4', 'B3', 'B2'], max:3000});

  // Function to calculate and add an NDVI band
  var addNDVI = function(image) {
   return image.addBands(image.normalizedDifference(['B8', 'B4']));
  };
  // Add NDVI band to image collection
  var S2 = S2collection.map(addNDVI);
  // Extract NDVI band and create NDVI median composite image
  var NDVI = S2.select(['nd']);
   var NDVImed = NDVI.median(); //I just changed the name of this variable ;)
    var ndvi_vis = ['#d73027', '#f46d43', '#fdae61', '#fee08b', '#d9ef8b',
       '#a6d96a'];
       Map.addLayer(NDVI, {min:0, max:0.7, palette: ndvi_vis},'NDVI');

  // Add NDWI band to image collection
  var addNDWI = function(image) {
    return image.addBands(image.normalizedDifference(['B3', 'B5']));
  };
  // Add NDWI band to image collection
  var S2S = S2collection.map(addNDWI);
  // Extract NDWI band and create NDWI median composite image
  var NDWI = S2S.select(['nd']);
   var NDWImed = NDVI.median();
    var NDWIViz = {min: 0, max: 0.5, palette: ['00FFFF', '0000FF']};
      Map.addLayer(NDWI, NDWIViz, 'NDWI', false);


  // Function to mask out NDVI
  var S2maskedVeg = function(image) {
   var NDVI = image.select(['NDVI']);
     return image.updateMask(NDVI.lte(0.15));
  };
  // Apply water and veg masking functions to image collection
  var S2MASKNDVI = S2collection.map(S2maskedVeg);

  // Function to mask out NDWI
  var S2maskedWater = function(image) {
   var NDWI = image.select(['NDWI']);
    return image.updateMask(NDWI.lte(0.15));
  };
  // Apply water and veg masking functions to image collection
  var S2maskedW = S2MASKNDVI.map(S2maskedWater);
    Map.addLayer(S2maskedW,{max:1},'S2 masked');
3

The error occurs cause you did not rename the band to 'NDVI' and you did not use the actual variable containing the NDVI band (you are now using the original collection using 'S2collection.map(S2maskedVeg)'.

You would (i think) best first define all you functions. Then apply them in the required order to your image collection, so the one where NDVI is needed in the NDVI mask, NDVI is already present. That would look something like this:

var S2collection =  imageCollection.filterDate('2016-08-01','2019-04-10')
            .filterBounds(aoi)
            .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'equals', 0);
                Map.addLayer(S2collection, {bands:['B4', 'B3', 'B2'], max:3000});

// ####### DEFINE ALL FUNCTIONS #####
  // Function to calculate and add an NDVI band
  var addNDVI = function(image) {
   return image.addBands(image.normalizedDifference(['B8', 'B4']).rename('NDVI'));
  };
  // Add NDWI band to image collection
  var addNDWI = function(image) {
    return image.addBands(image.normalizedDifference(['B3', 'B5']).rename('NDWI'));
  }; 
  // Function to mask out NDVI
  var S2maskedVeg = function(image) {
   var NDVI = image.select(['NDVI']);
     return image.addBands(ee.Image(1).updateMask(NDVI.lte(0.15)).rename('NDVI_mask'));
  };
  // Function to mask out NDWI
  var S2maskedWater = function(image) {
   var NDWI = image.select(['NDWI']);
    return image.addBands(ee.Image(1).updateMask(NDWI.lte(0.15)).rename('NDWI_mask'));
  };

// ####### APPLY ALL FUNCTION TO THE IMAGE COLLECTION ####
// Apply water and veg masking functions to image collection
S2collection = S2collection.map(addNDWI).map(addNDVI).map(S2maskedVeg).map(S2maskedWater);
print(S2collection)

I was not sure which band of the image you would like to mask using the NDVI and NDWI, so I added a constant image (ee.Image(1)) and masked that image in the functions where masking is applied.

Here is an example how to use the mask NDVI and NDWI bands:

// define function
var maskBands = function(image) {
  return image.select(['B3', 'B4', 'B5', 'B8']).updateMask(image.select('NDVI_mask'))
                                .updateMask(image.select('NDWI_mask'));
};

// map over the collection and use the function
var maskedCollection = S2collection.map(maskBands);

Link code

  • Hello, thanks for your time and effort to help me out! I am trying to create a single layer that depicts both NDVI and NDWI masks, so one layer that masks both NDVI and NDWI. I will then use band ratios to display different types of minerals on top of that. Again, I am a massive novice but I am trying to mask bands B8 & B4, as well as B3 & B5 over an image collection that stretches across the Mediterranean (it's pretty big!). - I'll give this code a try and come back to you shortly, cheers :) – Richie Apr 23 at 15:23
  • code.earthengine.google.com/f141f6e4e3381fe0b0824c132e67c736 . See code added below – Kuik Apr 24 at 7:32
  • This has worked a charm! Thank you so much for helping, I shall rejoice in this code and learn what I can from it. I am trying add another band to the map below, but keep getting SyntaxError: Unexpected token (78:74) Code is as follows: var addFeO = function(image) { return image.addBands(image.select(['B4']).divide(image.select(['B2']));.rename('FeO')); }; – Richie Apr 24 at 12:38
  • Fixed the above error, got to the point where it says FeO bands do not exist but I will follow your above steps. Thanks again - Richie – Richie Apr 24 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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