ee.Image.arrayAccum Stay organized with collections Save and categorize content based on your preferences.
Page Summary
Image.arrayAccumaccumulates elements of each array pixel along a given axis.The function calculates the reduction of elements in a pixel along the axis, up to and including the current position.
It can be used to create cumulative sums or monotonically increasing sequences.
The method takes the axis and an optional reducer as arguments, returning an Image.
| Usage | Returns |
|---|---|
Image.arrayAccum(axis,reducer) | Image |
| Argument | Type | Details |
|---|---|---|
this:input | Image | Input image. |
axis | Integer | Axis along which to perform the cumulative sum. |
reducer | Reducer, default: null | Reducer to accumulate values. Default is SUM, to produce the cumulative sum of each vector along the given axis. |
Examples
Code Editor (JavaScript)
// A function to print the array for a selected pixel in the following examples.functionsampArrImg(arrImg){varpoint=ee.Geometry.Point([-121,42]);returnarrImg.sample(point,500).first().get('array');}// Create a 1D array image.vararrayImg1D=ee.Image([1,2,3]).toArray();print('1D array image (pixel)',sampArrImg(arrayImg1D));// [1, 2, 3]// Perform accumulation procedures along axes using ee.Reducer functions.// Here we calculate the cumulative sum along the 0-axis for a 1D array.varaccumSum1DAx0=arrayImg1D.arrayAccum(0,ee.Reducer.sum());print('Cumulative sum along 0-axis',sampArrImg(accumSum1DAx0));// [1, 3, 6]// Create a 2D 3x3 array image.vararrayImg2D=ee.Image([1,2,3,4,5,6,7,8,9]).toArray().arrayReshape(ee.Image([3,3]).toArray(),2);print('2D 3x3 array image (pixel)',sampArrImg(arrayImg2D));// [[1, 2, 3],// [4, 5, 6],// [7, 8, 9]]// Calculate the cumulative sum along the 0-axis for a 2D array.varaccumSum2DAx0=arrayImg2D.arrayAccum(0,ee.Reducer.sum());print('Cumulative sum along 0-axis',sampArrImg(accumSum2DAx0));// [[ 1, 2, 3],// [ 5, 7, 9],// [12, 15, 18]]// Calculate the cumulative sum along the 1-axis for a 2D array.varaccumSum2DAx1=arrayImg2D.arrayAccum(1,ee.Reducer.sum());print('Cumulative sum along 1-axis',sampArrImg(accumSum2DAx1));// [[1, 3, 6],// [4, 9, 15],// [7, 15, 24]]
Python setup
See the Python Environment page for information on the Python API and usinggeemap for interactive development.
importeeimportgeemap.coreasgeemap
Colab (Python)
# A function to print the array for a selected pixel in the following examples.defsamp_arr_img(arr_img):point=ee.Geometry.Point([-121,42])returnarr_img.sample(point,500).first().get('array')# Create a 1D array image.array_img_1d=ee.Image([1,2,3]).toArray()display('1D array image (pixel):',samp_arr_img(array_img_1d))# [1, 2, 3]# Perform accumulation procedures along axes using ee.Reducer functions.# Here we calculate the cumulative sum along the 0-axis for a 1D array.accum_sum_1d_ax0=array_img_1d.arrayAccum(0,ee.Reducer.sum())display('Cumulative sum along 0-axis:',samp_arr_img(accum_sum_1d_ax0))# [1, 3, 6]# Create a 2D 3x3 array image.array_img_2d=ee.Image([1,2,3,4,5,6,7,8,9]).toArray().arrayReshape(ee.Image([3,3]).toArray(),2)display('2D 3x3 array image (pixel):',samp_arr_img(array_img_2d))# [[1, 2, 3],# [4, 5, 6],# [7, 8, 9]]# Calculate the cumulative sum along the 0-axis for a 2D array.accum_sum_2d_ax0=array_img_2d.arrayAccum(0,ee.Reducer.sum())display('Cumulative sum along 0-axis:',samp_arr_img(accum_sum_2d_ax0))# [[ 1, 2, 3],# [ 5, 7, 9],# [12, 15, 18]]# Calculate the cumulative sum along the 1-axis for a 2D array.accum_sum_2d_ax1=array_img_2d.arrayAccum(1,ee.Reducer.sum())display('Cumulative sum along 1-axis:',samp_arr_img(accum_sum_2d_ax1))# [[1, 3, 6],# [4, 9, 15],# [7, 15, 24]]
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-10-06 UTC.