[GIS] GEE ImageCollection.map() with multiple-input function

functiongoogle-earth-enginegoogle-earth-engine-javascript-api

I have an Image Collection, I want to Calculate a Normalized difference and got this Function called ND

/////normalized Differense///////
function ND(Im,B1,B2){
var four = Im.select(B1)
var eight = Im.select(B2)
return eight.subtract(four).divide(four.add(eight)).copyProperties(Im);
}

this way I would call ND but miss the additional Arguments :

ImageCollection.map(ND)

this way I would get an error regarding the second argument of .map:

ImageCollection.map(ND,'B4','B8')

this way I will get "a is not a function":

ImageCollection.map((ND,'B4','B8'),)
ImageCollection.map([ND,'B4','B8'],)

I know there is a ".NormalizedDifference" Function, and I will use it. It's more of a general question
how do I map over an ImageCollection with a function that needs multiple inputs?

Best Answer

You can make a nested function.

/////normalized Differense///////
var ND = function(B1,B2) {
  var wrap = function(Im) {
    var four = Im.select(B1)
    var eight = Im.select(B2)
    return eight.subtract(four).divide(four.add(eight)).copyProperties(Im);
  }
  return wrap
}

And to call it:

ImageCollection.map(ND('B4','B8'))
Related Question