保存所有联接

在 Earth Engine 中,保存联接是表示一对多关系的一种方式。 与内连接不同,保存联接会将 secondary 集合中的匹配项存储为 primary 集合中地图项的命名属性。如需保存所有此类匹配,请使用 ee.Join.saveAll()。如果存在一对多关系,saveAll() 联接会将所有匹配的特征存储为 ee.Listprimary 集合中的不匹配元素会被舍弃。例如,假设您需要获取某个合集中每个 Landsat 图像前两天获取的所有 MODIS 图像。以下示例使用 saveAll() 联接来实现此目的:

// 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: MODIS imagery.
var modSecondary = ee.ImageCollection('MODIS/006/MOD09GA')
    .filterDate('2014-03-01', '2014-07-01');

// Define an allowable time difference: two days in milliseconds.
var twoDaysMillis = 2 * 24 * 60 * 60 * 1000;

// Create a time filter to define a match as overlapping timestamps.
var timeFilter = ee.Filter.or(
  ee.Filter.maxDifference({
    difference: twoDaysMillis,
    leftField: 'system:time_start',
    rightField: 'system:time_end'
  }),
  ee.Filter.maxDifference({
    difference: twoDaysMillis,
    leftField: 'system:time_end',
    rightField: 'system:time_start'
  })
);

// Define the join.
var saveAllJoin = ee.Join.saveAll({
  matchesKey: 'terra',
  ordering: 'system:time_start',
  ascending: true
});

// Apply the join.
var landsatModis = saveAllJoin.apply(primary, modSecondary, timeFilter);

// Display the result.
print('Join.saveAll:', landsatModis);

如需了解 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: MODIS imagery.
mod_secondary = ee.ImageCollection('MODIS/006/MOD09GA').filterDate(
    '2014-03-01', '2014-07-01'
)

# Define an allowable time difference: two days in milliseconds.
two_days_millis = 2 * 24 * 60 * 60 * 1000

# Create a time filter to define a match as overlapping timestamps.
time_filter = ee.Filter.Or(
    ee.Filter.maxDifference(
        difference=two_days_millis,
        leftField='system:time_start',
        rightField='system:time_end',
    ),
    ee.Filter.maxDifference(
        difference=two_days_millis,
        leftField='system:time_end',
        rightField='system:time_start',
    ),
)

# Define the join.
save_all_join = ee.Join.saveAll(
    matchesKey='terra', ordering='system:time_start', ascending=True
)

# Apply the join.
landsat_modis = save_all_join.apply(primary, mod_secondary, time_filter)

# Display the result.
display('Join.saveAll:', landsat_modis)

在此示例中,请注意,为了提高效率,secondary MODIS 集合已预过滤,以使其时间顺序与 primary Landsat 集合相似。为了将 Landsat 获取时间与 MODIS 复合时间(具有每日范围)进行比较,过滤器会比较图片时间戳的端点。联接是使用用于存储每个 Landsat 图像的匹配项列表的属性的名称 (‘terra’) 和用于按 system:time_start 属性对匹配项列表进行排序的可选参数定义的

检查结果表明,主要集合中的图片添加了 terra 属性,该属性用于存储匹配的 MODIS 图片列表。