מיפוי על גבי ImageCollection

כדי להחיל פונקציה על כל Image ב-ImageCollection, משתמשים ב-imageCollection.map(). הארגומנט היחיד של map() הוא פונקציה שמקבלת פרמטר אחד: ee.Image. לדוגמה, הקוד הבא מוסיף פס של חותמת זמן לכל תמונה באוסף.

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

בדף סביבת Python מפורט מידע על Python API ועל השימוש ב-geemap לפיתוח אינטראקטיבי.

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

שימו לב שבפונקציה שהוגדרה מראש, השיטה getNumber() משמשת ליצירת Image חדש מהערך המספרי של נכס. כפי שצוין בקטעים צמצום ושילוב, הפס הזמן שימושי ליצירת מודלים ליניאריים של שינוי וליצירת קומפוזיציות.

הפונקציה הממופה מוגבלת בפעולות שהיא יכולה לבצע. באופן ספציפי, אי אפשר לשנות משתנים מחוץ לפונקציה, אי אפשר להדפיס שום דבר, אי אפשר להשתמש ב-JavaScript ובמשפטי if או for של Python. עם זאת, אפשר להשתמש ב-ee.Algorithms.If() כדי לבצע פעולות מותנות בפונקציה ממופה. לדוגמה:

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

בדף סביבת Python מפורט מידע על Python API ועל השימוש ב-geemap לפיתוח אינטראקטיבי.

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

בודקים את רשימת התמונות ב-ImageCollection של הפלט, ומבחינים שבמקרה שהתנאי שמוערך על ידי האלגוריתם If() נכון, הפלט מכיל תמונה קבועה. הדוגמה הזו מציגה פונקציה מותנית בצד השרת (מידע נוסף על לקוח לעומת שרת ב-Earth Engine), אבל באופן כללי מומלץ להימנע משימוש ב-If() ולהשתמש במקום זאת במסננים.