TransWikia.com

how to name the band created with ee.Image.expression?

Geographic Information Systems Asked on April 3, 2021

I’m computing the bands of the ‘UMD/hansen/global_forest_change_2019_v1_7’ by creating a new composite band :

var aoi = table //any aoi/country/polygon avalaible
var dataset = ee.Image('UMD/hansen/global_forest_change_2019_v1_7');

//clip the dataset on the aoi 
var clip = dataset.clip(aoi)

//create a composite band bassed on the user threshold 
var threshold = 30


var calc = "(A<=threshold)*((C==1)*50 + (C==0)*30) + " //Non forest 
calc += "(A>threshold)*(C==1)*(B>0)*51 + "         //gain + loss 
calc += "(A>threshold)*(C==1)*(B==0)*50 + "        //gain                                             
calc += "(A>threshold)*(C==0)*(B>0)*B + "          //loss
calc += "(A>threshold)*(C==0)*(B==0)*40"           //stable forest

calc = calc.replace(/threshold/g, threshold)

var gfc = clip.expression(calc,{
  'A': clip.select('treecover2000'),
  'C': clip.select('gain'),
  'B': clip.select('lossyear')
});

print(gfc)

The obtained band in gfc is named treecover2000. I’d like to use gfc instead. according to the doc :

If the result of an expression is a single band, it can be assigned a name using the ‘=’ operator (e.g.: x = a + b).

But I don’t understand where to put the ‘=’ statement. Anyone that has already used it can give me a clue ?

One Answer

You need to put the = at the beginning of your expression, like this:

var calc = "gfc = (A<=threshold)*((C==1)*50 + (C==0)*30) + " //Non forest 
calc += "(A>threshold)*(C==1)*(B>0)*51 + "         //gain + loss 
calc += "(A>threshold)*(C==1)*(B==0)*50 + "        //gain                                             
calc += "(A>threshold)*(C==0)*(B>0)*B + "          //loss
calc += "(A>threshold)*(C==0)*(B==0)*40"           //stable forest

Alternatively you can also rename afterwards by passing two lists to .select().

gfc.select([0], ["someName"])

This selects the band at index 0 and renames it to someName

Correct answer by JonasV on April 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP