公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
保存所有联接
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在 Earth Engine 中,保存联接是表示一对多关系的一种方式。
与内连接不同,保存联接会将 secondary
集合中的匹配项存储为 primary
集合中地图项的命名属性。如需保存所有此类匹配,请使用 ee.Join.saveAll()
。如果存在一对多关系,saveAll()
联接会将所有匹配的特征存储为 ee.List
。primary
集合中的不匹配元素会被舍弃。例如,假设您需要获取某个合集中每个 Landsat 图像前两天获取的所有 MODIS 图像。以下示例使用 saveAll()
联接来实现此目的:
Code Editor (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: 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 设置
如需了解 Python API 以及如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
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: 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 图片列表。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eSaving joins represent one-to-many relationships in Earth Engine by storing matches from a secondary collection as a property in the primary collection.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eee.Join.saveAll()\u003c/code\u003e stores all matching features from the secondary collection as a list in a new property of the primary collection's features, dropping unmatched primary features.\u003c/p\u003e\n"],["\u003cp\u003eThis approach is useful for scenarios like associating all MODIS images within a specific time window to each Landsat image in a collection.\u003c/p\u003e\n"],["\u003cp\u003eThe example code demonstrates filtering and joining Landsat and MODIS collections based on their timestamps, storing the matching MODIS images in a 'terra' property on each Landsat image.\u003c/p\u003e\n"]]],["Saving joins represent one-to-many relationships by storing matches from a secondary collection as a property within the primary collection's features. `ee.Join.saveAll()` stores all matching features as an `ee.List`. The example filters Landsat and MODIS imagery collections, defining a two-day time difference as the matching condition using a time filter. A `saveAll()` join, named `'terra'`, is applied, sorting matches by `system:time_start`. The result has the added property `terra` with a list of matching MODIS images. Unmatched primary collection elements are dropped.\n"],null,["# Save-All Joins\n\nSaving joins are one way of representing one-to-many relationships in Earth Engine.\nUnlike an [inner join](/earth-engine/guides/joins_inner), a saving join stores matches from the\n`secondary` collection as a named property of the features in the\n`primary` collection. To save all such matches, use an\n`ee.Join.saveAll()`. If there is a one-to-many relationship, a\n`saveAll()` join stores all matching features as an\n`ee.List`. Unmatched elements in the `primary` collection are\ndropped. For example, suppose there is a need to get all MODIS imagery acquired\nwithin two days of each Landsat image in a collection. This example uses a\n`saveAll()` join for that purpose:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load a primary collection: Landsat imagery.\nvar primary = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')\n .filterDate('2014-04-01', '2014-06-01')\n .filterBounds(ee.Geometry.Point(-122.092, 37.42));\n\n// Load a secondary collection: MODIS imagery.\nvar modSecondary = ee.ImageCollection('MODIS/006/MOD09GA')\n .filterDate('2014-03-01', '2014-07-01');\n\n// Define an allowable time difference: two days in milliseconds.\nvar twoDaysMillis = 2 * 24 * 60 * 60 * 1000;\n\n// Create a time filter to define a match as overlapping timestamps.\nvar timeFilter = ee.Filter.or(\n ee.Filter.maxDifference({\n difference: twoDaysMillis,\n leftField: 'system:time_start',\n rightField: 'system:time_end'\n }),\n ee.Filter.maxDifference({\n difference: twoDaysMillis,\n leftField: 'system:time_end',\n rightField: 'system:time_start'\n })\n);\n\n// Define the join.\nvar saveAllJoin = ee.Join.saveAll({\n matchesKey: 'terra',\n ordering: 'system:time_start',\n ascending: true\n});\n\n// Apply the join.\nvar landsatModis = saveAllJoin.apply(primary, modSecondary, timeFilter);\n\n// Display the result.\nprint('Join.saveAll:', landsatModis);\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Load a primary collection: Landsat imagery.\nprimary = (\n ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')\n .filterDate('2014-04-01', '2014-06-01')\n .filterBounds(ee.Geometry.Point(-122.092, 37.42))\n)\n\n# Load a secondary collection: MODIS imagery.\nmod_secondary = ee.ImageCollection('MODIS/006/MOD09GA').filterDate(\n '2014-03-01', '2014-07-01'\n)\n\n# Define an allowable time difference: two days in milliseconds.\ntwo_days_millis = 2 * 24 * 60 * 60 * 1000\n\n# Create a time filter to define a match as overlapping timestamps.\ntime_filter = ee.Filter.Or(\n ee.Filter.maxDifference(\n difference=two_days_millis,\n leftField='system:time_start',\n rightField='system:time_end',\n ),\n ee.Filter.maxDifference(\n difference=two_days_millis,\n leftField='system:time_end',\n rightField='system:time_start',\n ),\n)\n\n# Define the join.\nsave_all_join = ee.Join.saveAll(\n matchesKey='terra', ordering='system:time_start', ascending=True\n)\n\n# Apply the join.\nlandsat_modis = save_all_join.apply(primary, mod_secondary, time_filter)\n\n# Display the result.\ndisplay('Join.saveAll:', landsat_modis)\n```\n\nIn this example, note that the `secondary` MODIS collection is pre-filtered to be\nchronologically similar to the `primary` Landsat collection for efficiency. To\ncompare the Landsat acquisition time to the MODIS composite time, which has a daily range,\nthe filter compares the endpoints of the image timestamps. The join is defined with the\nname of the property used to store the list of matches for each Landsat image\n(`'terra'`) and optional parameter to sort the list of matches by the\n`system:time_start` property\n\nInspection of the result indicates that images within the primary collection have the\nadded `terra` property which stores a list of the matching MODIS images."]]