公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
保存最佳联接
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如需仅保存集合中每个元素的最佳匹配项,请使用 ee.Join.saveBest()
。saveBest()
联接的运作方式与 saveAll()
联接等效,但对于 primary
集合中的每个元素,它会保存 secondary
集合中与其最匹配的元素。主要集合中不匹配的元素会被舍弃。假设您希望找到与 primary
集合中的每张 Landsat 图片时间最接近的气象图片。如需执行此联接,必须针对单个联接条件重新定义 ee.Filter
(组合过滤条件不适用于 saveBest()
,因为无法确定如何组合来自多个子过滤条件的排名):
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: 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 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: 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 图像中的最小值。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003e\u003ccode\u003eee.Join.saveBest()\u003c/code\u003e is used to efficiently match elements from two collections, keeping only the best match for each element in the primary collection based on a specified metric.\u003c/p\u003e\n"],["\u003cp\u003eUnmatched elements in the primary collection are excluded from the results of a \u003ccode\u003esaveBest()\u003c/code\u003e join.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003esaveBest()\u003c/code\u003e joins require a single filter condition for determining the best match, unlike \u003ccode\u003esaveAll()\u003c/code\u003e joins that can utilize combined filters.\u003c/p\u003e\n"],["\u003cp\u003eThe resulting collection from a \u003ccode\u003esaveBest()\u003c/code\u003e join includes the best matching element stored under a specified property (e.g., 'bestImage') along with a property indicating the match quality (e.g., 'timeDiff').\u003c/p\u003e\n"]]],["`ee.Join.saveBest()` saves the best-matching element from a secondary collection to each element in a primary collection. It requires a single `ee.Filter` for matching. The provided example joins Landsat imagery with GRIDMET meteorological data, finding the closest meteorological image in time to each Landsat image. The join stores the best-matching image in a property named 'bestImage' and the time difference in a property named 'timeDiff'. Unmatched elements from the primary are dropped.\n"],null,["# Save-Best Joins\n\nTo save only the best match for each element in a collection, use an\n`ee.Join.saveBest()`. The `saveBest()` join functions in an\nequivalent way to the `saveAll()` join, except for each element in the\n`primary` collection, it saves the element from the `secondary`\ncollection with the best match. Unmatched elements in the primary collection are\ndropped. Suppose the intention is to find a meteorological image closest in time to each\nLandsat image in the `primary` collection. To perform this join, the\n`ee.Filter` must be redefined for a single join condition (combined filters\nwill not work with `saveBest()` since it is ambiguous how to combine ranks\nfrom multiple sub-Filters):\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: GRIDMET meteorological data\nvar gridmet = ee.ImageCollection('IDAHO_EPSCOR/GRIDMET');\n\n// Define a max difference filter to compare timestamps.\nvar maxDiffFilter = ee.Filter.maxDifference({\n difference: 2 * 24 * 60 * 60 * 1000,\n leftField: 'system:time_start',\n rightField: 'system:time_start'\n});\n\n// Define the join.\nvar saveBestJoin = ee.Join.saveBest({\n matchKey: 'bestImage',\n measureKey: 'timeDiff'\n});\n\n// Apply the join.\nvar landsatMet = saveBestJoin.apply(primary, gridmet, maxDiffFilter);\n\n// Print the result.\nprint(landsatMet);\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: GRIDMET meteorological data\ngridmet = ee.ImageCollection('IDAHO_EPSCOR/GRIDMET')\n\n# Define a max difference filter to compare timestamps.\nmax_diff_filter = ee.Filter.maxDifference(\n difference=2 * 24 * 60 * 60 * 1000,\n leftField='system:time_start',\n rightField='system:time_start',\n)\n\n# Define the join.\nsave_best_join = ee.Join.saveBest(matchKey='bestImage', measureKey='timeDiff')\n\n# Apply the join.\nlandsat_met = save_best_join.apply(primary, gridmet, max_diff_filter)\n\n# Print the result.\ndisplay(landsat_met)\n```\n\nNote that a `saveBest()` join defines the name of the property with which to\nstore the best match (`'bestImage'`) and the name of the property with which\nto store the goodness of the match metric (`'timeDiff'`). Inspection of the\nresults indicates that a matching DAYMET image has been added to the property\n`bestImage` for each Landsat scene in the `primary` collection. Each\nof these DAYMET images has the property `timeDiff` indicating the time\ndifference in milliseconds between the DAYMET image and the Landsat image, which will be\nminimum among the DAYMET images passing the condition in the filter."]]