圖片區域的統計資料

如要取得 FeatureCollection 中儲存的多個區域中的圖片統計資料,您可以使用 image.reduceRegions() 一次減少多個區域。reduceRegions() 的輸入內容為 ImageFeatureCollection。輸出結果是另一個 FeatureCollection,其中 reduceRegions() 輸出內容已設為每個 Feature 的屬性。在這個範例中,每個地圖幾何圖形中的 Landsat 7 年度合成頻帶平均值會新增為輸入地圖項目的屬性:

程式碼編輯器 (JavaScript)

// Load input imagery: Landsat 7 5-year composite.
var image = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012');

// Load a FeatureCollection of counties in Maine.
var maineCounties = ee.FeatureCollection('TIGER/2016/Counties')
  .filter(ee.Filter.eq('STATEFP', '23'));

// Add reducer output to the Features in the collection.
var maineMeansFeatures = image.reduceRegions({
  collection: maineCounties,
  reducer: ee.Reducer.mean(),
  scale: 30,
});

// Print the first feature, to illustrate the result.
print(ee.Feature(maineMeansFeatures.first()).select(image.bandNames()));

Python 設定

請參閱「 Python 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

import ee
import geemap.core as geemap

Colab (Python)

# Load input imagery: Landsat 7 5-year composite.
image = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012')

# Load a FeatureCollection of counties in Maine.
maine_counties = ee.FeatureCollection('TIGER/2016/Counties').filter(
    ee.Filter.eq('STATEFP', '23')
)

# Add reducer output to the Features in the collection.
maine_means_features = image.reduceRegions(
    collection=maine_counties, reducer=ee.Reducer.mean(), scale=30
)

# Print the first feature, to illustrate the result.
display(ee.Feature(maine_means_features.first()).select(image.bandNames()))

請注意,以頻帶名稱做為索引的新屬性已新增至 FeatureCollection,用於儲存每個 Feature 幾何圖形中的複合值平均值。因此,print 陳述式的輸出內容應如下所示:

Feature (Polygon, 7 properties)
  type: Feature
  geometry: Polygon, 7864 vertices
  properties: Object (7 properties)
    B1: 24.034822192925134
    B2: 19.40202233717122
    B3: 13.568454303016292
    B4: 63.00423784301736
    B5: 29.142707062821305
    B6_VCID_2: 186.18172376827042
    B7: 12.064469664746415