GEE NDVI function that removes negative values

google-earth-enginendvi

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

Best Answer

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)
    )

}

Related Question