การแมปใน ImageCollection

หากต้องการใช้ฟังก์ชันกับ Image ทั้งหมดใน ImageCollection ให้ใช้ imageCollection.map() อาร์กิวเมนต์เดียวของ map() คือฟังก์ชันที่รับพารามิเตอร์ 1 รายการ ซึ่งเป็น ee.Image ตัวอย่างเช่น โค้ดต่อไปนี้จะเพิ่มแถบการประทับเวลาให้กับรูปภาพทุกรูปในคอลเล็กชัน

เครื่องมือแก้ไขโค้ด (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 API และการใช้ geemap สําหรับการพัฒนาแบบอินเทอร์แอกทีฟได้ที่หน้า สภาพแวดล้อม Python

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() เพื่อดำเนินการแบบมีเงื่อนไขในฟังก์ชันที่แมปได้ เช่น

เครื่องมือแก้ไขโค้ด (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 API และการใช้ geemap สําหรับการพัฒนาแบบอินเทอร์แอกทีฟได้ที่หน้า สภาพแวดล้อม Python

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() และใช้ตัวกรองแทน