公告:凡是在
2025 年 4 月 15 日前註冊使用 Earth Engine 的非商業專案,都必須
驗證非商業用途資格,才能繼續存取 Earth Engine。
儲存最佳彙整
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如要只儲存集合中每個元素的最佳比對結果,請使用 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 圖片中最低的時間差異。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間: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."]]