Rename bands using a dict


In [1]:
import ee
ee.Initialize()

In [2]:
from geetools import tools

In [3]:
import ipygee as ui

In [4]:
i = ee.Image('COPERNICUS/S2/20181122T142749_20181122T143353_T18GYT').select('B.')

In [5]:
bands = i.bandNames()

In [6]:
ui.eprint(bands)



In [7]:
renamed = tools.image.renameDict(i, {'B2':'BLUE', 'B3':'GREEN', 'B4':'RED'})

In [8]:
renamed_bands = renamed.bandNames()

In [9]:
ui.eprint(renamed_bands)


In collection


In [10]:
col = ee.ImageCollection('COPERNICUS/S2').limit(5)

In [11]:
def print_bands(col):
    info = col.getInfo()
    images = info['features']
    for image in images:
        bands = image['bands']
        print([band['id'] for band in bands])

In [12]:
print_bands(col)


['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']

In [13]:
renamed_col = col.map(lambda img: tools.image.renameDict(img, {'B2':'BLUE', 'B3':'GREEN', 'B4':'RED'}))

In [14]:
print_bands(renamed_col)


['B1', 'BLUE', 'GREEN', 'RED', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'BLUE', 'GREEN', 'RED', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'BLUE', 'GREEN', 'RED', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'BLUE', 'GREEN', 'RED', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'BLUE', 'GREEN', 'RED', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']

In [ ]: