Per applicare una funzione a ogni Image
in un ImageCollection
, utilizza
imageCollection.map()
. L'unico argomento di map()
è una
funzione che accetta un parametro: un ee.Image
. Ad esempio, il seguente
codice aggiunge una banda di timestamp a ogni immagine della raccolta.
Editor di codice (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))
Tieni presente che nella funzione predefinita viene utilizzato il metodo getNumber()
per creare un nuovo Image
dal valore numerico di una proprietà. Come discusso nelle sezioni Riduzione e Composizione, la fascia temporale è utile per la modellazione lineare del cambiamento e per la creazione di composizioni.
La funzione mappata è limitata nelle operazioni che può eseguire. Nello specifico, non può
modificare le variabili al di fuori della funzione; non può stampare nulla; non può utilizzare le istruzioni "if" o "for" di JavaScript e Python. Tuttavia, puoi utilizzare ee.Algorithms.If()
per eseguire operazioni condizionali in una funzione mappata. Ad esempio:
Editor di codice (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))
Esamina l'elenco di immagini nella raccolta di immagini di output e tieni presente che quando la condizione valutata dall'algoritmo If()
è vera, l'output contiene un'immagine costante. Sebbene questa demo mostri una funzione condizionale lato server
(scopri di più sulla differenza tra client e server in Earth Engine),
evita If()
in generale e utilizza i filtri.