1

Within my NDVI function, I want to either reclassify negative values (-1 to 0) as 0, or change them into 'no data'. Specifically, I want to the do this calculation/reclassification in the code provided in response to this question: Google Earth Engine - Get Image with highest max Scene NDVI from a time period.

I suspect I need to alter the NDVI function, but so far any changes have given me error messages or inhibit the previous functions.

function toNdvi(image) {
  return image
    .normalizedDifference(['B8', 'B4'])
    .rename('ndvi')
    .updateMask(
      image.select('QA60').not()
    )}

Code in question found here: https://code.earthengine.google.com/013a113d3f01c192ff7370d236197a8a

2
  • You are missing a closing curly brace } from your function, is that just a typo?
    – Matt
    Mar 2, 2022 at 11:27
  • sorry yeah typo, updated Mar 2, 2022 at 11:51

1 Answer 1

2

You can mask the unwanted values directly in the same function using updateMask.

function toNdvi(image) {
  var newImg = image
    .normalizedDifference(['B8', 'B4'])
    .rename('ndvi')
    .updateMask(
      image.select('QA60').not()
    )
    return newImg.updateMask(
      newImg.select('ndvi').gte(0)
    )

}

3
  • thank you for your response. this addition gives me the error message: 'Image.select: Pattern 'ndvi' did not match any bands'. i have updated the question. Mar 2, 2022 at 11:06
  • This solution is almost there. but slightly wrong. Do not try to return in one go but make a variable and then return using the second updateMask and it will work. I've edited the code as per. Mar 2, 2022 at 17:32
  • Yeah, I missed that detail. Thanks for the edit. Mar 2, 2022 at 18:04

Your Answer

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

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