ee.Image.reduceRegion

將縮減函式套用至特定區域中的所有像素。

縮減器必須與輸入圖片的波段數相同,或是只有單一輸入,並針對每個波段重複執行。

傳回縮減器輸出的字典。

用量傳回
Image.reduceRegion(reducer, geometry, scale, crs, crsTransform, bestEffort, maxPixels, tileScale)字典
引數類型詳細資料
這個:image圖片要縮小的圖片。
reducer縮減函式要套用的縮減函式。
geometry幾何圖形,預設值:null要減少資料的區域。預設為圖片第一個波段的足跡。
scale浮點值,預設值為空值以公尺為單位的投影名義比例,用於工作。
crs投影,預設值:null要使用的投影機。如未指定,系統會使用圖片第一個波段的投影。如果除了比例外還指定了其他值,系統會重新調整為指定比例。
crsTransform清單,預設值為空值CRS 轉換值清單。這是 3x2 轉換矩陣的列優先順序。這個選項與「scale」互斥,且會取代投影上已設定的任何變形。
bestEffort布林值,預設值為 false如果多邊形在指定比例下包含的像素過多,請計算並使用較大的比例,這樣作業就能成功。
maxPixelsLong,預設值:10000000要減少的像素數量上限。
tileScale浮點值,預設值為 1介於 0.1 和 16 之間的縮放比例,用於調整聚合圖塊大小;設定較大的 tileScale (例如 2 或 4) 使用較小的圖塊,可啟用預設值會導致記憶體不足的運算作業。

範例

程式碼編輯器 (JavaScript)

// A Landsat 8 surface reflectance image with SWIR1, NIR, and green bands.
var img = ee.Image('LANDSAT/LC08/C02/T1_L2/LC08_044034_20210508')
              .select(['SR_B6', 'SR_B5', 'SR_B3']);

// Santa Cruz Mountains ecoregion geometry.
var geom = ee.FeatureCollection('EPA/Ecoregions/2013/L4')
               .filter('us_l4name == "Santa Cruz Mountains"').geometry();

// Display layers on the map.
Map.setCenter(-122.08, 37.22, 9);
Map.addLayer(img, {min: 10000, max: 20000}, 'Landsat image');
Map.addLayer(geom, {color: 'white'}, 'Santa Cruz Mountains ecoregion');

// Calculate median band values within Santa Cruz Mountains ecoregion. It is
// good practice to explicitly define "scale" (or "crsTransform") and "crs"
// parameters of the analysis to avoid unexpected results from undesired
// defaults when e.g. reducing a composite image.
var stats = img.reduceRegion({
  reducer: ee.Reducer.median(),
  geometry: geom,
  scale: 30,  // meters
  crs: 'EPSG:3310',  // California Albers projection
});

// A dictionary is returned; keys are band names, values are the statistic.
print('Median band values, Santa Cruz Mountains ecoregion', stats);

// You can combine reducers to calculate e.g. mean and standard deviation
// simultaneously. The output dictionary keys are the concatenation of the band
// names and statistic names, separated by an underscore.
var reducer = ee.Reducer.mean().combine({
  reducer2: ee.Reducer.stdDev(),
  sharedInputs: true
});
var multiStats = img.reduceRegion({
  reducer: reducer,
  geometry: geom,
  scale: 30,
  crs: 'EPSG:3310',
});
print('Mean & SD band values, Santa Cruz Mountains ecoregion', multiStats);

Python 設定

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

import ee
import geemap.core as geemap

Colab (Python)

# A Landsat 8 surface reflectance image with SWIR1, NIR, and green bands.
img = ee.Image('LANDSAT/LC08/C02/T1_L2/LC08_044034_20210508').select(
    ['SR_B6', 'SR_B5', 'SR_B3']
)

# Santa Cruz Mountains ecoregion geometry.
geom = (
    ee.FeatureCollection('EPA/Ecoregions/2013/L4')
    .filter('us_l4name == "Santa Cruz Mountains"')
    .geometry()
)

# Display layers on the map.
m = geemap.Map()
m.set_center(-122.08, 37.22, 9)
m.add_layer(img, {'min': 10000, 'max': 20000}, 'Landsat image')
m.add_layer(geom, {'color': 'white'}, 'Santa Cruz Mountains ecoregion')
display(m)

# Calculate median band values within Santa Cruz Mountains ecoregion. It is
# good practice to explicitly define "scale" (or "crsTransform") and "crs"
# parameters of the analysis to avoid unexpected results from undesired
# defaults when e.g. reducing a composite image.
stats = img.reduceRegion(
    reducer=ee.Reducer.median(),
    geometry=geom,
    scale=30,  # meters
    crs='EPSG:3310',  # California Albers projection
)

# A dictionary is returned keys are band names, values are the statistic.
display('Median band values, Santa Cruz Mountains ecoregion', stats)

# You can combine reducers to calculate e.g. mean and standard deviation
# simultaneously. The output dictionary keys are the concatenation of the band
# names and statistic names, separated by an underscore.
reducer = ee.Reducer.mean().combine(
    reducer2=ee.Reducer.stdDev(), sharedInputs=True
)
multi_stats = img.reduceRegion(
    reducer=reducer,
    geometry=geom,
    scale=30,
    crs='EPSG:3310',
)
display('Mean & SD band values, Santa Cruz Mountains ecoregion', multi_stats)