addConstantBands(value, *names, **pairs)

Adds bands with a constant value

  • names: final names for the additional bands
  • value: constant value
  • pairs: keywords for the bands (see example)

return the function for ee.ImageCollection.map()


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

In [2]:
from geetools import tools

In [3]:
col = ee.ImageCollection('COPERNICUS/S2').select(['B1', 'B2', 'B3']).limit(10)

helper function to print values for the centroid of the first image of a collection


In [4]:
def print_center(collection):
    first = ee.Image(collection.first())
    p = first.geometry().centroid()
    return tools.image.getValue(first, p, scale=10, side='client')

Option 1 - arguments


In [5]:
newcol = col.map(lambda i: tools.image.addConstantBands(i, 0, "a", "b", "c"))

In [6]:
print_center(newcol)


Out[6]:
{'B1': 1730, 'B2': 1615, 'B3': 1769, 'a': 0, 'b': 0, 'c': 0}

Option 2 - keyword arguments


In [7]:
newcolCK = col.map(lambda i: tools.image.addConstantBands(i, a=0, b=1, c=2))

In [8]:
print_center(newcolCK)


Out[8]:
{'B1': 1730, 'B2': 1615, 'B3': 1769, 'a': 0, 'b': 1, 'c': 2}

Option 3 - combined


In [9]:
newcolCC = col.map(lambda i:tools.image.addConstantBands(i, 0, "a", "b", "c", d=1, e=2))

In [10]:
print_center(newcolCC)


Out[10]:
{'B1': 1730, 'B2': 1615, 'B3': 1769, 'a': 0, 'b': 0, 'c': 0, 'd': 1, 'e': 2}

In [ ]: