保存最佳联接

如需仅保存集合中每个元素的最佳匹配项,请使用 ee.Join.saveBest()saveBest() 联接的运作方式与 saveAll() 联接等效,但对于 primary 集合中的每个元素,它会保存 secondary 集合中与其最匹配的元素。主要集合中不匹配的元素会被舍弃。假设您希望找到与 primary 集合中的每张 Landsat 图片时间最接近的气象图片。如需执行此联接,必须针对单个联接条件重新定义 ee.Filter(组合过滤条件不适用于 saveBest(),因为无法确定如何组合来自多个子过滤条件的排名):

// 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 API 以及如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

import ee
import geemap.core as geemap
# 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’)。检查结果表明,系统已将匹配的 DAYMET 图片添加到 primary 集合中每个 Landsat 场景的 bestImage 属性。这些 DAYMET 图像中的每张图像都有 timeDiff 属性,该属性表示 DAYMET 图像与 Landsat 图像之间的时间差(以毫秒为单位),该值将是通过过滤器中条件的 DAYMET 图像中的最小值。