Eine ImageCollection als Overlay verwenden

Wenn Sie eine Funktion auf jede Image in einer ImageCollection anwenden möchten, verwenden Sie imageCollection.map(). Das einzige Argument für map() ist eine Funktion, die einen Parameter annimmt: einen ee.Image. Mit dem folgenden Code wird beispielsweise jedem Bild in der Sammlung ein Zeitstempel hinzugefügt.

Code-Editor (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));

Python einrichten

Auf der Seite Python-Umgebung finden Sie Informationen zur Python API und zur Verwendung von geemap für die interaktive Entwicklung.

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))

In der vordefinierten Funktion wird die Methode getNumber() verwendet, um aus dem numerischen Wert eines Attributs ein neues Image zu erstellen. Wie in den Abschnitten Reduzieren und Compositing beschrieben, ist das Zeitband nützlich für die lineare Modellierung von Veränderungen und für die Erstellung von Kompositionen.

Die zugewiesene Funktion ist in den ausführbaren Vorgängen eingeschränkt. Insbesondere können damit keine Variablen außerhalb der Funktion geändert, nichts gedruckt und keine JavaScript- und Python-„if“- oder „for“-Anweisungen verwendet werden. Sie können ee.Algorithms.If() jedoch verwenden, um bedingte Vorgänge in einer zugeordneten Funktion auszuführen. Beispiel:

Code-Editor (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));

Python einrichten

Auf der Seite Python-Umgebung finden Sie Informationen zur Python API und zur Verwendung von geemap für die interaktive Entwicklung.

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))

Sehen Sie sich die Liste der Bilder in der Ausgabe „ImageCollection“ an. Wenn die vom If()-Algorithmus ausgewertete Bedingung wahr ist, enthält die Ausgabe ein konstantes Bild. Hier wird eine serverseitige bedingte Funktion gezeigt (Weitere Informationen zu Client und Server in Earth Engine). Verwenden Sie If() jedoch im Allgemeinen nicht, sondern Filter.