Para aplicar uma função a cada Image
em um ImageCollection
, use
imageCollection.map()
. O único argumento para map()
é uma
função que usa um parâmetro: um ee.Image
. Por exemplo, o código
abaixo adiciona uma faixa de carimbo de data/hora a cada imagem da coleção.
Editor de código (JavaScript)
// Load a Landsat 8 collection for a single path-row, 2021 images only. var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') .filterDate('2021', '2022') .filter(ee.Filter.eq('WRS_PATH', 44)) .filter(ee.Filter.eq('WRS_ROW', 34)); // This function adds a band representing the image timestamp. var addTime = function(image) { return image.addBands(image.getNumber('system:time_start')); }; // Map the function over the collection and display the result. print(collection.map(addTime));
import ee import geemap.core as geemap
Colab (Python)
# Load a Landsat 8 collection for a single path-row, 2021 images only. collection = ( ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') .filterDate('2021', '2022') .filter(ee.Filter.eq('WRS_PATH', 44)) .filter(ee.Filter.eq('WRS_ROW', 34)) ) # This function adds a band representing the image timestamp. def add_time(image): return image.addBands(image.getNumber('system:time_start')) # Map the function over the collection and display the result. display(collection.map(add_time))
Na função predefinida, o método getNumber()
é usado
para criar um novo Image
com base no valor numérico de uma propriedade. Conforme discutido nas seções Redução e Composição, ter a faixa de tempo é útil para modelagem linear de mudanças e para fazer composições.
A função mapeada é limitada nas operações que pode realizar. Especificamente, não é possível
modificar variáveis fora da função, imprimir nada e usar instruções JavaScript e
Python "if" ou "for". No entanto, é possível usar ee.Algorithms.If()
para
realizar operações condicionais em uma função mapeada. Exemplo:
Editor de código (JavaScript)
// Load a Landsat 8 collection for a single path-row, 2021 images only. var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') .filterDate('2021', '2022') .filter(ee.Filter.eq('WRS_PATH', 44)) .filter(ee.Filter.eq('WRS_ROW', 34)); // This function uses a conditional statement to return the image if // the solar elevation > 40 degrees. Otherwise it returns a "zero image". var conditional = function(image) { return ee.Algorithms.If(ee.Number(image.get('SUN_ELEVATION')).gt(40), image, ee.Image(0)); }; // Map the function over the collection and print the result. Expand the // collection and note that 7 of the 22 images are now "zero images'. print('Expand this to see the result', collection.map(conditional));
import ee import geemap.core as geemap
Colab (Python)
# Load a Landsat 8 collection for a single path-row, 2021 images only. collection = ( ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') .filterDate('2021', '2022') .filter(ee.Filter.eq('WRS_PATH', 44)) .filter(ee.Filter.eq('WRS_ROW', 34)) ) # This function uses a conditional statement to return the image if # the solar elevation > 40 degrees. Otherwise it returns a "zero image". def conditional(image): return ee.Algorithms.If( ee.Number(image.get('SUN_ELEVATION')).gt(40), image, ee.Image(0) ) # Map the function over the collection and print the result. Expand the # collection and note that 7 of the 22 images are now "zero images'. display('Expand this to see the result', collection.map(conditional))
Inspecione a lista de imagens na ImageCollection de saída e observe que, quando a
condição avaliada pelo algoritmo If()
é verdadeira, a saída contém uma
imagem constante. Embora isso demonstre uma função condicional do lado do servidor, saiba mais sobre o cliente e o servidor no Earth Engine. Evite If()
em geral e use filtros.