0

I am trying to calculate NDWI from Sentinel 2 data in Google Earth Engine but keep getting the error "Line 29 is not a function" (the line where I am trying to run the ndwi function on all images). I have casted the object but the error still pertains.

PLease find GEE code here

// filter data and location
var images = s2.filterDate('2021-09-19', '2021-10-18')
                     .filterBounds(SSD);
                     
print(images);

// cloudMask
function cloudMask(im) {
  // Opaque and cirrus cloud masks cause bits 10 and 11 in QA60 to be set,
  // so values less than 1024 are cloud-free
  var mask = ee.Image(0).where(im.select('QA60').gte(1024), 1).not();
  return im.updateMask(mask);
}
 
// remove clouds for all images
images = ee.ImageCollection(images.map(cloudMask));

// Add to map
//Map.centerObject(NgheAn, 8);
//Map.addLayer(images.min().clip(NgheAn), {bands: ['??', '??', '??'], max: 2048}, 's2 image Feb-Mar 2016');

// calculate ndwi from sentinel
function s2ndwi (img){
  var ndwi = img.normalizedDifference(['B3','B11']).rename('NDWI');
  return img.addBands(ndwi);
}

// run ndwi on all images
images = ee.ImageCollection(images.map(s2ndwi));

// select maximum NDWI
var s2ndwi = ee.ImageCollection(images.select(ndwi).max);

// set the threshold
var THRESHOLD = 0.1;
// select pixels greater than threshold
s2ndwi = s2ndwi.gt(THRESHOLD);

// set visualization
var ndwi_viz = {min: -1, max: 1, palette: ['000000','0000FF']};
// add the map as a layer
Map.addLayer(s2ndwi.updateMask(s2ndwi).clip(SSD), ndwi_viz , 'ndwi');

1
  • 1
    Do you have a link to your script? The error tells you where your problem is (line 29) but we don't know which one that is from your example. I suspect though that your line: s2ndwi = s2ndwi.gt(THRESHOLD); Is the problem because you apply a image function (.gt()) to your s2ndwi imageCollection
    – Jobbo90
    Commented Nov 19, 2021 at 9:37

1 Answer 1

1

You're reusing/redefining the name s2ndwi to mean both a function (line 23) and a collection (line 32). The later definition is taking precedence over the former.

1
  • Thank you for your answer. However, I have done the same above (line 8 and 13) and this has not caused an issue here.
    – Simba06
    Commented Nov 19, 2021 at 12:51

Your Answer

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