公告 :凡是在
2025 年 4 月 15 日 前註冊使用 Earth Engine 的非商業專案,都必須
驗證非商業用途資格 ,才能繼續存取 Earth Engine。
提供意見
ee.Image.arrayReduce
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
減少每個陣列像素的元素。
用量 傳回 Image. arrayReduce (reducer, axes, fieldAxis )
圖片
引數 類型 詳細資料 這個:input
圖片 輸入圖片。 reducer
縮減函式 要套用的縮減函式。 axes
清單 要在每個像素中縮減的陣列軸清單。在所有這些軸中,輸出內容的長度都會是 1。 fieldAxis
整數,預設值為 null 縮減器輸入和輸出欄位的軸。如果縮減器有多個輸入或輸出,才需要填寫這項資訊。
範例
程式碼編輯器 (JavaScript)
// A function to print arrays for a selected pixel in the following examples.
function sampArrImg ( arrImg ) {
var point = ee . Geometry . Point ([ - 121 , 42 ]);
return arrImg . sample ( point , 500 ). first (). get ( 'array' );
}
// Create a 1D array image with length 6.
var arrayImg1D = ee . Image ([ 0 , 1 , 2 , 3 , 4 , 5 ]). toArray ();
print ( '1D array image (pixel)' , sampArrImg ( arrayImg1D ));
// [0, 1, 2, 3, 4, 5]
// Sum the elements in the 1D array image.
var arrayImg1Dsum = arrayImg1D . arrayReduce ( ee . Reducer . sum (), [ 0 ]);
print ( '1D array image sum (pixel)' , sampArrImg ( arrayImg1Dsum ));
// [15]
// Create a 2D array image with 2 rows and 3 columns.
var arrayImg2D = arrayImg1D . arrayReshape ( ee . Image ([ 2 , 3 ]). toArray (), 2 );
print ( '2D array image (pixel)' , sampArrImg ( arrayImg2D ));
// [[0, 1, 2],
// [3, 4, 5]]
// Sum 2D array image along 0-axis.
var arrayImg2DsumRow = arrayImg2D . arrayReduce ( ee . Reducer . sum (), [ 0 ]);
print ( '2D array image sum rows (pixel)' , sampArrImg ( arrayImg2DsumRow ));
// [[3, 5, 7]]
// Sum 2D array image along 1-axis.
var arrayImg2DsumCol = arrayImg2D . arrayReduce ( ee . Reducer . sum (), [ 1 ]);
print ( '2D array image sum columns (pixel)' , sampArrImg ( arrayImg2DsumCol ));
// [[3],
// [12]]
// Sum 2D array image 0-axis and 1-axis.
var arrayImg2DsumRowCol = arrayImg2D . arrayReduce ( ee . Reducer . sum (), [ 0 , 1 ]);
print ( '2D array image sum columns (pixel)' , sampArrImg ( arrayImg2DsumRowCol ));
// [[15]]
// For reducers that provide several outputs (like minMax and percentile),
// you need to ensure you have a dimension to hold the results. For instance,
// if you want minMax for a 1D array, add a second dimension.
var arrayImg1Dto2D = arrayImg1D . toArray ( 1 );
print ( '1D array image to 2D' , sampArrImg ( arrayImg1Dto2D ));
// [[0],
// [1],
// [2],
// [3],
// [4],
// [5]]
// Calculate min and max for 2D array, use the fieldAxis parameter.
var minMax1D = arrayImg1Dto2D . arrayReduce ( ee . Reducer . minMax (), [ 0 ], 1 );
print ( '1D array image minMax (pixel)' , sampArrImg ( minMax1D ));
// [[0, 5]]
// If your array image is 2D and you want min and max, add a third dimension.
var arrayImg2Dto3D = arrayImg2D . toArray ( 2 );
print ( '2D array image to 3D' , sampArrImg ( arrayImg2Dto3D ));
// [[[0], [1], [2]],
// [[3], [4], [5]]]
// Calculate min and max along the 0-axis, store results in 2-axis.
var minMax2D = arrayImg2Dto3D . arrayReduce ( ee . Reducer . minMax (), [ 0 ], 2 );
print ( '2D array image minMax (pixel)' , sampArrImg ( minMax2D ));
// [[[0, 3],
// [1, 4],
// [2, 5]]]
Python 設定
請參閱
Python 環境 頁面,瞭解 Python API 和如何使用 geemap
進行互動式開發。
import ee
import geemap.core as geemap
Colab (Python)
# A function to print arrays for a selected pixel in the following examples.
def samp_arr_img ( arr_img ):
point = ee . Geometry . Point ([ - 121 , 42 ])
return arr_img . sample ( point , 500 ) . first () . get ( 'array' )
# Create a 1D array image with length 6.
array_img_1d = ee . Image ([ 0 , 1 , 2 , 3 , 4 , 5 ]) . toArray ()
print ( '1D array image (pixel):' , samp_arr_img ( array_img_1d ) . getInfo ())
# [0, 1, 2, 3, 4, 5]
# Sum the elements in the 1D array image.
array_img_1d_sum = array_img_1d . arrayReduce ( ee . Reducer . sum (), [ 0 ])
print ( '1D array image sum (pixel):' , samp_arr_img ( array_img_1d_sum ) . getInfo ())
# [15]
# Create a 2D array image with 2 rows and 3 columns.
array_img_2d = array_img_1d . arrayReshape ( ee . Image ([ 2 , 3 ]) . toArray (), 2 )
print ( '2D array image (pixel):' , samp_arr_img ( array_img_2d ) . getInfo ())
# [[0, 1, 2],
# [3, 4, 5]]
# Sum 2D array image along 0-axis.
array_img_2d_sum_row = array_img_2d . arrayReduce ( ee . Reducer . sum (), [ 0 ])
print (
'2D array image sum rows (pixel):' ,
samp_arr_img ( array_img_2d_sum_row ) . getInfo ()
)
# [[3, 5, 7]]
# Sum 2D array image along 1-axis.
array_img_2d_sum_col = array_img_2d . arrayReduce ( ee . Reducer . sum (), [ 1 ])
print (
'2D array image sum columns (pixel):' ,
samp_arr_img ( array_img_2d_sum_col ) . getInfo ()
)
# [[3],
# [12]]
# Sum 2D array image 0-axis and 1-axis.
array_img_2d_sum_row_col = array_img_2d . arrayReduce ( ee . Reducer . sum (), [ 0 , 1 ])
print (
'2D array image sum columns (pixel):' ,
samp_arr_img ( array_img_2d_sum_row_col ) . getInfo ()
)
# [[15]]
# For reducers that provide several outputs (like minMax and percentile),
# you need to ensure you have a dimension to hold the results. For instance,
# if you want minMax for a 1D array, add a second dimension.
array_img_1d_to_2d = array_img_1d . toArray ( 1 )
print ( '1D array image to 2D:' , samp_arr_img ( array_img_1d_to_2d ) . getInfo ())
# [[0],
# [1],
# [2],
# [3],
# [4],
# [5]]
# Calculate min and max for 2D array, use the fieldAxis parameter.
min_max_1d = array_img_1d_to_2d . arrayReduce ( ee . Reducer . minMax (), [ 0 ], 1 )
print ( '1D array image minMax (pixel):' , samp_arr_img ( min_max_1d ) . getInfo ())
# [[0, 5]]
# If your array image is 2D and you want min and max, add a third dimension.
array_img_2d_to_3d = array_img_2d . toArray ( 2 )
print ( '2D array image to 3D:' , samp_arr_img ( array_img_2d_to_3d ) . getInfo ())
# [[[0], [1], [2]],
# [[3], [4], [5]]]
# Calculate min and max along the 0-axis, store results in 2-axis.
min_max_2d = array_img_2d_to_3d . arrayReduce ( ee . Reducer . minMax (), [ 0 ], 2 )
print ( '2D array image minMax (pixel):' , samp_arr_img ( min_max_2d ) . getInfo ())
# [[[0, 3],
# [1, 4],
# [2, 5]]]
提供意見
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權 ,程式碼範例則為阿帕契 2.0 授權 。詳情請參閱《Google Developers 網站政策 》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
想進一步說明嗎?
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[[["`Image.arrayReduce()` reduces elements of each array pixel along the specified axes using a provided reducer."],["It takes an input image, a reducer function, a list of axes to reduce, and an optional field axis for reducers with multiple inputs or outputs."],["The output image has a length of 1 in all reduced axes."],["For reducers with multiple outputs, you may need to add dimensions to the input array to store the results."],["Examples show reducing 1D and 2D array images using `ee.Reducer.sum()` and `ee.Reducer.minMax()`."]]],["The `arrayReduce` method reduces elements within each array pixel of an image. It takes a `reducer`, a list of `axes` to reduce, and optionally, a `fieldAxis` for reducers with multiple inputs or outputs. The method collapses the specified axes to a length of 1. Example operations include summing elements in 1D or 2D arrays along specified axes or calculating the min/max with multiple outputs, requiring adjusted dimensions. The results are displayed as array pixels.\n"]]