TransWikia.com

Renaming bands using Google Earth Engine JavaScript API

Geographic Information Systems Asked by Iago Mendes on July 7, 2021

I would like to rename the band "B4_stdDev" to "Red_stdDev", and the band "B10_stdDev" to "Temperature_stdDev".

Example code:

var b4_stdDev = ee.Image().rename('B4_stdDev');
var b10_stdDev = ee.Image().rename('B10_stdDev');

var img = ee.Image.cat(b4_stdDev, b10_stdDev);
print(img);

This is a simple example. I actually have an image with approximately 60 bands with names like "B2_mean", "B2_savg", "B2_ent", etc.

One Answer

For doing this task you need the list of original band names (approximately 60 bands) and a dictionary with your desired changes for producing the list with strings for renaming.

For testing my approach, I first used USGS Landsat 8 Surface Reflectance Tier 1 product with 12 bands; where it was used multiple renaming with 'select' and 'rename' methods (only 4 bands involved). Code snippet looks as follows.

img = img.select(['B1', 'B2', 'B3', 'B4'])
  .rename(['ultra blue', 'blue', 'green', 'red']);

print("multiple replace in img", img);

Afterward, I created a list (only 32 elements) for simulating your possible result of application of 'bandNames' method in your image with approximately 60 bands. This list was mapped with a function where substitutions based in distinct values in referred dictionary were produced as expected. Following code snippet summarizes above criteria.

var names_dict = ee.Dictionary({'B1':'ultra blue', 'B2':'blue', 'B3':'green',
  'B4':'red', 'B5':'near infrared', 'B6':'shortwave infrared 1', 'B7':'shortwave infrared 2', 
  'B10':'brightness temperature' 
});

var band_names2 = ee.List(["B1_mean", "B1_stdDev", "B1_savg", "B1_ent",
  "B2_mean", "B2_stdDev", "B2_savg", "B2_ent",
  "B3_mean", "B3_stdDev", "B3_savg", "B3_ent",
  "B4_mean", "B4_stdDev", "B4_savg", "B4_ent",
  "B5_mean", "B5_stdDev", "B5_savg", "B5_ent",
  "B6_mean", "B6_stdDev", "B6_savg", "B6_ent",
  "B7_mean", "B7_stdDev", "B7_savg", "B7_ent",
  "B10_mean", "B10_stdDev", "B10_savg", "B10_ent"]);

print("band_names2 is simulating application of bandNames in your image with 60 bands");
print("band_names2", band_names2);

print("band_names2 size", band_names2.size());

var forRename = band_names2.map(function (ele) {
  
  return ee.String(ele).replace(ee.String(ele).slice(0, ee.String(ele).index('_')), 
    names_dict.get(ee.String(ele).slice(0, ee.String(ele).index('_'))));
  
});

print(forRename);

In your situation, multiple renaming can be produced with following line by using band_names2 and forRename lists.

img = img.select(band_names2).rename(forRename);

Complete code can be find here and result of running it in GEE code editor can be observed in following image.

enter image description here

Correct answer by xunilk on July 7, 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