Liên kết trên ImageCollection

Để áp dụng một hàm cho mọi Image trong ImageCollection, hãy sử dụng imageCollection.map(). Đối số duy nhất cho map() là một hàm nhận một tham số: ee.Image. Ví dụ: mã sau đây sẽ thêm một dải dấu thời gian vào mọi hình ảnh trong bộ sưu tập.

Trình soạn thảo mã (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));

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về API Python và cách sử dụng geemap để phát triển tương tác.

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

Xin lưu ý rằng trong hàm được xác định trước, phương thức getNumber() được dùng để tạo một Image mới từ giá trị số của một thuộc tính. Như đã thảo luận trong phần GiảmKết hợp, việc có dải thời gian sẽ hữu ích cho việc lập mô hình tuyến tính về sự thay đổi và để tạo thành phần kết hợp.

Hàm được ánh xạ bị giới hạn trong các thao tác mà hàm đó có thể thực hiện. Cụ thể, hàm này không thể chỉnh sửa các biến bên ngoài hàm; không thể in bất kỳ nội dung nào; không thể sử dụng JavaScript và câu lệnh Python "if" hoặc "for". Tuy nhiên, bạn có thể sử dụng ee.Algorithms.If() để thực hiện các phép toán có điều kiện trong một hàm được liên kết. Ví dụ:

Trình soạn thảo mã (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));

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về API Python và cách sử dụng geemap để phát triển tương tác.

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

Kiểm tra danh sách hình ảnh trong ImageCollection đầu ra và lưu ý rằng khi điều kiện được thuật toán If() đánh giá là đúng, kết quả sẽ chứa một hình ảnh không đổi. Mặc dù ví dụ này minh hoạ một hàm có điều kiện phía máy chủ (tìm hiểu thêm về máy khách so với máy chủ trong Earth Engine), nhưng nói chung, hãy tránh sử dụng If() và sử dụng bộ lọc.