Search code examples
google-earth-engine

Applying function to image bands based on table values in Earth Engine?


firstly apologies: I am a beginner in Earth Engine, but googling my question hasn't yielded any results. I have a reasonable amount of experience with other languages/platforms.

I have a table of data with headers 'name' and 'value' with N entries, I also have a multiband images in which the bands are named the same as the 'name' column in my table.

I want to apply a functions to each band in the image, based on its corresponding value in the table.

I'm struggling to find a way to do this without using loops and getInfo(), both of which I understand are not efficient and generally frowned upon.

I think perhaps I'm missing something fundamental here regarding the interaction between local variables and things occuring serverside - help would be greatly appreciated!


Solution

  • You could perhaps iterate over the band names in the image:

    var updatedImage = ee.Image(
      image.bandNames().iterate(  
        function (bandName, acc) {
          bandName = ee.String(bandName) // Must cast from ee.Element to actual type
          var feature = features
            .filter(ee.Filter.eq('name', bandName))
            .first()
          var value = feature.getNumber('value')
          var bandWithFunctionApplied = image.select(bandName)
            .add(value) // Apply some function
          return ee.Image(acc)
            .addBands(bandWithFunctionApplied)
        },
        ee.Image([])
      )
    )
    

    https://code.earthengine.google.com/082411908a7525a4c8a87916b5ea88fc