2

I'm trying to perform ANIR index calculation on my image collection. I'm having trouble writing the code in the right way. ANIR index formula is :

cos^-1[((a^2)+(b^2)+(c^2))/(2*a*b)] 

(a2, b2 and c1, are Euclidean distances between bands B4, B8, B11/B12 respectively).

This is the function I have written so far-

var addANIR = function(image) {
  var ANIR = image.expression(
  '(1/cosin)',
  {
    'B4': image.select('B4'),
    'B8': image.select('B8'),
    'B11': image.select('B11'),
    'A':((ee.Number(832.8-644.6).pow(2)).add(image.select('B8').subtract(image.select('B4')).pow(2))).sqrt(),
    'B':((ee.Number(1613.7-832.8).pow(2)).add(image.select('B11').subtract(image.select('B8')).pow(2))).sqrt(),
    'C':((ee.Number(1613.7-644.6).pow(2)).add(image.select('B11').subtract(image.select('B4')).pow(2))).sqrt(),
    'top':A.pow(2).add(B.pow(2)).add((C).pow(2))/ (ee.Number(2).multiply(A).multiply(B)),
    'cosin': top.cos()

  }).rename('ANIR');
  //NDVI index
  
  return image.addBands(ANIR.add(ee.Image(0.0)));
  //Add 0.0 value to NDVI (currently not in use)
  
 };

This function gives me an Error. What is wrong with the function/code?

0

I've never calculated this index, and am not convinced I got it completely right. But based on what you provided, it could look something like this:

https://code.earthengine.google.com/c94ed6bb713d414a265d4817244f287a

var addANIR = function(image) {
  var a = ee.Image().expression(
    'sqrt((832.8 - 644.6)**2 + (i.B8 - i.B4)**2)', {i: image}
  )
  var b = ee.Image().expression(
    'sqrt((1613.7 - 832.8)**2 + (i.B11 - i.B8)**2)', {i: image}
  )
  var c = ee.Image().expression(
    'sqrt((1613.7 - 644.6)**2 + (i.B11 - i.B4)**2)', {i: image}
  )
  var anir = ee.Image().expression(
    '1 / cos((a**2 + b**2 + c**2) / (2 * a * b))', {a: a, b: b, c: c}
  ).rename('ANIR')
  
  return image.addBands(anir)  
 }
| improve this answer | |
  • Thanks a lot, it works! – user166621 Sep 16 at 12:28

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.