Announcement: All noncommercial projects registered to use Earth Engine beforeApril 15, 2025 mustverify noncommercial eligibility to maintain access. If you have not verified by September 26, 2025, your access may be on hold.

ee.Image.arrayLengths

  • TheImage.arrayLengths() method returns a 1D array image where each pixel contains the length of each axis of the array in the corresponding input pixel.

  • The method takes an input image as an argument.

  • The output is an Image object.

Returns a 1D array image with the length of each array axis.

UsageReturns
Image.arrayLengths()Image
ArgumentTypeDetails
this:inputImageInput image.

Examples

Code Editor (JavaScript)

// A function to print arrays for a selected pixel in the following examples.functionsampArrImg(arrImg){varpoint=ee.Geometry.Point([-121,42]);returnarrImg.sample(point,500).first().get('array');}// A 3-band image of constants.varimg=ee.Image([0,1,2]);print('3-band image',img);// Convert the 3-band image to a 2D array image.vararrayImg2D=img.toArray().toArray(1);print('2D array image (pixel)',sampArrImg(arrayImg2D));// [[0],//  [1],//  [2]]// Get the number of dimensions in each pixel's array.vararrayImg2Ddim=arrayImg2D.arrayDimensions();print('N dimensions in array',sampArrImg(arrayImg2Ddim));// 2// Get the array length per dimension per pixel.vararrayImg2DdimLen=arrayImg2D.arrayLengths();print('Array length per dimension',sampArrImg(arrayImg2DdimLen));// [3, 1]// Get the array length for 0-axis (rows).vararrayImg2Daxis0Len=arrayImg2D.arrayLength(0);print('Array length 0-axis (rows)',sampArrImg(arrayImg2Daxis0Len));// 3// Get the array length for 1-axis (columns).vararrayImg2Daxis1Len=arrayImg2D.arrayLength(1);print('Array length 1-axis (columns)',sampArrImg(arrayImg2Daxis1Len));// 1

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 arrays 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')# A 3-band image of constants.img=ee.Image([0,1,2])display('3-band image:',img)# Convert the 3-band image to a 2D array image.array_img_2d=img.toArray().toArray(1)display('2D array image (pixel):',samp_arr_img(array_img_2d))# [[0],#  [1],#  [2]]# Get the number of dimensions in each pixel's array.array_img2d_dim=array_img_2d.arrayDimensions()display('N dimensions in array:',samp_arr_img(array_img2d_dim))# 2# Get the array length per dimension per pixel.array_img_2d_dim_len=array_img_2d.arrayLengths()display('Array length per dimension:',samp_arr_img(array_img_2d_dim_len))# [3, 1]# Get the array length for 0-axis (rows).array_img2d_axis0_len=array_img_2d.arrayLength(0)display('Array length 0-axis (rows):',samp_arr_img(array_img2d_axis0_len))# 3# Get the array length for 1-axis (columns).array_img_2d_axis1_len=array_img_2d.arrayLength(1)display('Array length 1-axis (columns):',samp_arr_img(array_img_2d_axis1_len))# 1

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.