0

I want to define a function for an index MMRI using the formula given below enter image description here

I'm relatively new to JS and I'm having trouble defining it correctly.

First, I defined NDVI and MNDWI. This works fine.

var addIndicesL5 = function(img) {
      var ndvi = img.normalizedDifference(['B4','B3']).rename('NDVI');
      var mndwi = img.normalizedDifference(['B2','B5']).rename('MNDWI');
return img
    .addBands(ndvi)
    .addBands(mndwi)
};

Then, I defined a separate function to get their absolute values using

var ndvi_mndwi_abs = function(img){
  var ndvi_abs = (img.select('NDVI')).abs.rename('NDVI_ABS');
  var mndwi_abs = (img.select('MNDWI')).abs.rename('MNDWI_ABS');
  
            return img
    .addBands(ndvi_abs)
    .addBands(mndwi_abs);
};

(Following this, I plan to take the normalizedDifference of 'MNDWI_ABS' and 'NDVI_ABS' bands to get MMRI)

However, when I map ndvi_mndwi_abs function to the Image Collection, I get an error saying " img.select(...).abs.rename is not a function".

How can I rewrite this function?

1 Answer 1

0

You forgot the function call parentheses:

  //                                   vv
  var ndvi_abs = img.select('NDVI').abs().rename('NDVI_ABS');
  var mndwi_abs = img.select('MNDWI').abs().rename('MNDWI_ABS');
  //                                     ^^

Every operation on an Earth Engine Image is a method, which must be called (.abs()), not just named (.abs).

For future troubleshooting: The error you then saw was because the effect of the incorrect code was to try to call a method named rename on the method abs, rather than the result of calling abs(). No such method exists, so execution stopped with that error.

Your Answer

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

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