1
var L8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_TOA") 
.filterDate('2016-06-01', '2016-07-31') // you can change date here
.filter(ee.Filter.lt("CLOUD_COVER", 0.75))
.filterBounds(collection[i])
.map(maskLandsatclouds)
.sort('DATE_ACQUIRED')
.select(L8bands)

 var count = L8.size();
 for (var j=0;j< 10 ;j++){  
 // Get the number of images.
 print(j);

Correct if wrong; the code below is taking the median of all available images in a collection.

 var mosaic_L8 =L8.median().clip(collection[i]); // here we are taking the 
 median at each pixel in the collection
 Map.addLayer(mosaic_L8, L8vis, "mosaic_L8")

...and here, it is calculating NDWI of an image:

 // Create an NDWI image, define visualization parameters and display.
 var ndwi = mosaic_L8.normalizedDifference(['B3', 'B5']);

 var ndwiViz = {min: 0.5, max: 1, palette: ['44c9f1', '1637f1']};
 //Map.addLayer(ndwi, ndwiViz, 'NDWI', false);

 // Mask the non-watery parts of the image, where NDWI < 0.10.
 var ndwiMasked = ndwi.updateMask(ndwi.gte(0.10));
 Map.addLayer(ndwiMasked, ndwiViz,'NDWI masked '+ ee.Number(j).getInfo());

 Map.centerObject(ndwiMasked);

I have enclosed this code in a for-loop to perform the operation of NDWI on each image in a collection, but am unable to access individual images. It should be something like L8.normalizedDifference(['B3', 'B5']); but this is not a correct command.

0

It's best to avoid for loops in cases like this. Using the NDWI calculation described here (which is also what you list above) you can map an NDWI calculator to your imageCollection. I had to remove some of your functions that were mapped to the image collection because they weren't defined in the code you provide, but this should give you the general gist of how to calculate NDWI for an imageCollection (withNDWI is a collection of the NDWI images).

// Define a region of interest
var MECounties = ee.FeatureCollection('TIGER/2016/Counties')
  .filter(ee.Filter.eq('NAME', 'Waldo'));
print(MECounties);
var geometry = MECounties;
Map.centerObject(geometry);

// Grab your imagery
var L8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_TOA") 
  .filterDate('2018-02-01', '2018-04-30') // you can change date here
  .filterBounds(geometry)
  .sort('DATE_ACQUIRED');


// function to calculate NDWI in LANDSAT8
var addNDWI = function(image) {
  var NDWI = image.normalizedDifference(['B3', 'B5'])
  .rename('NDWI')
  .copyProperties(image,['system:time_start']);
  return image.addBands(NDWI);

};

// Add NDWI to the clipped image collection
var withNDWI = L8.map(addNDWI).select('NDWI');
print("NDWI collection", withNDWI);

// Test the addNDWI function on a single image.
var ndwi1 = withNDWI.select('NDWI').median();
print("NDWI image", ndwi1);

// Visualize an NDWI image
var NDWIcolor = {
  min: -1,
  max:1,
  palette: ['0000ff', '00ffff', 'ffff00', 'ff0000', 'ffffff'],
};
Map.addLayer(ndwi1,NDWIcolor,'test');
| improve this answer | |
  • there is confusion, L8 is a collection which retrieves 13 images after certain filters applied. What I am trying is to calculate NDWI of all 13 images individually, the code you have written, correct me if wrong, is taking the median of all retrieved images. – rAm Aug 19 '19 at 6:24
  • NDWI1 is taking the median of all images, for the sake of visualization. But, as I note above, withNDWI is a collection of NDWI images - each image therein being NDWI calculated for each image of the L8 imageCollection. – JepsonNomad Aug 19 '19 at 16:08
0

There are at least two other ways to calculate the NDWI for the image other than the .normalizedDifference function. Here are examples adapted from http://www.malfihasan.com

I'm assuming you want the NDWI fomula from McFeeters (1996) using green and near IR bands.

// // NDWI expression
var ndwiL8 = mosaic_L8.expression(
'(G - NIR) / (G + NIR)' , {
'NIR' : mosaic_L8.select('B5'),
'G' : mosaic_L8.select('B3')
} ) ;
// NDWI math operation
var ndwi_L8 = mosaic_L8.select('B3').subtract(mosaic_L8.select('B5'))
.divide(mosaic_L8.select('B3').add(mosaic_L8.select('B5')));
| improve this answer | |

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.