3

I want to create a threshold for the NDVIs in this collection. I would like to say something along the lines of:

If pixel is less than 0, assign pixel a value of 0; otherwise assign the pixel a value of 1.

This should turn each raster NDVI into a raster that only contains ones and zeroes.

//NDVI functions for all satellites
var mergedAllFunction = function(mergedCollection) {
  var ndvi = mergedCollection.normalizedDifference(['NIR', 'red']).rename('NDVI');
  return mergedCollection.addBands(ndvi);
};

// find all data and filter them by date and add NDVI
var mergedCollectionNDVI = mergedCollection
    .filterDate('1988-01-01', '2018-10-01')
    .filterBounds(studyarea)  // filter to area-of-interest; 
    .map(mergedAllFunction).select('NDVI');
print ('mergedCollectionNDVI', mergedCollectionNDVI);
  • Could you include the code that defines the object "mergedCollection" in the code example? – Tyler Erickson Oct 9 '18 at 19:17
3

I think that the confusion part of your code is the argument you use in the mapping function. Technically, you can name it as you want, and it works anyway, but most people just call it image or img because the argument in a mapping function over an ImageCollection is actually the image (every image of the loop). Plus, you named the argument equally to the collection mergedCollection which leads me to think that you may have not well understood the map method.

This would be the code you want (as I understand):

//NDVI functions for all satellites
var mergedAllFunction = function(image) {
  var ndvi = image.normalizedDifference(['NIR', 'red']).rename('NDVI');

  // THRESHOLD
  // if NDVI less or equal to 0 => 0 else 1
  var thres = ndvi.gte(0).rename('thres')

  return image.addBands(ndvi).addBands(thres);
}

// find all data and filter them by date and add NDVI
var mergedCollectionNDVI = mergedCollection
    .filterDate('1988-01-01', '2018-10-01')
    .filterBounds(studyarea)  // filter to area-of-interest; 
    .map(mergedAllFunction)


print ('mergedCollectionNDVI', mergedCollectionNDVI);
Map.addLayer(ee.Image(mergedCollectionNDVI.first()), {bands:['thres']})
Map.addLayer(ee.Image(mergedCollectionNDVI.first()), {
  bands:['NDVI'],
  palette:['brown', 'green'],
  min:-1, max:1
})

Here is a working example: https://code.earthengine.google.com/6902a7c6980014d14263aac5f71b7e03

  • Thank you for your help. I am sorry if I used improper notation. I am extremely new to any form of coding and my existing code, most of which is not shown, is the product of tinkering with existing code from other research projects in GEE and look up and adapting other people's question on stackexchange. I am going to try this method in the office and see how it turns out. – Eric Deutsch Oct 10 '18 at 4:54
  • Don't be sorry, it's fine. It was just a comment. You're welcome =) – Rodrigo E. Principe Oct 10 '18 at 15:29

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.