儲存最佳彙整

如要只儲存集合中每個元素的最佳比對結果,請使用 ee.Join.saveBest()saveBest() 彙整函式與 saveAll() 彙整函式運作方式相同,但 primary 集合中的每個元素都會儲存 secondary 集合中相符程度最高的元素。主要集合中不相符的元素會遭到捨棄。假設您想找出與 primary 集合中每張 Landsat 圖像最接近的氣象圖像。如要執行這項彙整作業,必須針對單一彙整條件重新定義 ee.Filter (彙整篩選器無法與 saveBest() 搭配使用,因為彙整多個子篩選器的排名方式不夠明確):

程式碼編輯器 (JavaScript)

// Load a primary collection: Landsat imagery.
var primary = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
    .filterDate('2014-04-01', '2014-06-01')
    .filterBounds(ee.Geometry.Point(-122.092, 37.42));

// Load a secondary collection: GRIDMET meteorological data
var gridmet = ee.ImageCollection('IDAHO_EPSCOR/GRIDMET');

// Define a max difference filter to compare timestamps.
var maxDiffFilter = ee.Filter.maxDifference({
  difference: 2 * 24 * 60 * 60 * 1000,
  leftField: 'system:time_start',
  rightField: 'system:time_start'
});

// Define the join.
var saveBestJoin = ee.Join.saveBest({
  matchKey: 'bestImage',
  measureKey: 'timeDiff'
});

// Apply the join.
var landsatMet = saveBestJoin.apply(primary, gridmet, maxDiffFilter);

// Print the result.
print(landsatMet);

Python 設定

請參閱「 Python 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

import ee
import geemap.core as geemap

Colab (Python)

# Load a primary collection: Landsat imagery.
primary = (
    ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
    .filterDate('2014-04-01', '2014-06-01')
    .filterBounds(ee.Geometry.Point(-122.092, 37.42))
)

# Load a secondary collection: GRIDMET meteorological data
gridmet = ee.ImageCollection('IDAHO_EPSCOR/GRIDMET')

# Define a max difference filter to compare timestamps.
max_diff_filter = ee.Filter.maxDifference(
    difference=2 * 24 * 60 * 60 * 1000,
    leftField='system:time_start',
    rightField='system:time_start',
)

# Define the join.
save_best_join = ee.Join.saveBest(matchKey='bestImage', measureKey='timeDiff')

# Apply the join.
landsat_met = save_best_join.apply(primary, gridmet, max_diff_filter)

# Print the result.
display(landsat_met)

請注意,saveBest() 彙整會定義用來儲存最佳相符項目的屬性名稱 (‘bestImage’),以及用來儲存相符指標優良度的屬性名稱 (‘timeDiff’)。檢查結果後,我們發現 primary 集合中每個 Landsat 影像場景的屬性 bestImage 已加入相符的 DAYMET 影像。每張 DAYMET 圖片都有 timeDiff 屬性,用於指出 DAYMET 圖片和 Landsat 圖片之間的時間差異 (以毫秒為單位),且這個時間差異會是通過篩選器條件的 DAYMET 圖片中最低的時間差異。