Announcement: All noncommercial projects registered to use Earth Engine before April 15, 2025 must verify noncommercial eligibility to maintain Earth Engine access.
Stay organized with collections
Save and categorize content based on your preferences.
Creates an array image where each array-valued pixel is masked with another array-valued pixel, retaining only the elements where the mask is non-zero. If the mask image has one band it will be applied to all the bands of 'input', otherwise they must have the same number of bands.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-06 UTC."],[[["\u003cp\u003e\u003ccode\u003earrayMask\u003c/code\u003e creates a masked array image by retaining elements where the mask image is non-zero.\u003c/p\u003e\n"],["\u003cp\u003eIt can be applied to 1D or 2D array images, using either single-band or multi-band masks.\u003c/p\u003e\n"],["\u003cp\u003eIf the mask has one band, it's applied to all bands of the input array image; otherwise, they must have the same number of bands.\u003c/p\u003e\n"],["\u003cp\u003eThe masking operation effectively filters out array elements corresponding to zero values in the mask.\u003c/p\u003e\n"],["\u003cp\u003eThis function is useful for selectively manipulating or analyzing specific elements within array images.\u003c/p\u003e\n"]]],[],null,["Creates an array image where each array-valued pixel is masked with another array-valued pixel, retaining only the elements where the mask is non-zero. If the mask image has one band it will be applied to all the bands of 'input', otherwise they must have the same number of bands.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|-------------------------|---------|\n| Image.arrayMask`(mask)` | Image |\n\n| Argument | Type | Details |\n|---------------|-------|---------------------------|\n| this: `input` | Image | Array image to mask. |\n| `mask` | Image | Array image to mask with. |\n\nExamples\n\nCode Editor (JavaScript) \n\n```javascript\n// A function to print arrays for a selected pixel in the following examples.\nfunction sampArrImg(arrImg) {\n var point = ee.Geometry.Point([-121, 42]);\n return arrImg.sample(point, 500).first().get('array');\n}\n\n// Create a 1D array image with length 6.\nvar arrayImg1D = ee.Image([0, 1, 2, 4, 0, 5]).toArray();\nprint('1D array image (pixel)', sampArrImg(arrayImg1D));\n// [0, 1, 2, 4, 0, 5]\n\n// Create a mask using a relational operator to mask values greater than 2.\nvar mask1D = arrayImg1D.lte(2);\nprint('1D mask for greater than value 2 (pixel)', sampArrImg(mask1D));\n// [1, 1, 1, 0, 1, 0]\n\nvar arrayImg1DMask = arrayImg1D.arrayMask(mask1D);\nprint('1D array image mask (pixel)', sampArrImg(arrayImg1DMask));\n// [0, 1, 2, 0]\n\n// Self mask the 1D array image. Value zero will be masked out.\nvar arrayImg1DselfMask = arrayImg1D.arrayMask(arrayImg1D);\nprint('1D array image self mask (pixel)', sampArrImg(arrayImg1DselfMask));\n// [1, 2, 4, 5]\n\n// Create a 2D array image.\nvar arrayImg2D = arrayImg1D.arrayReshape(ee.Image([2, 3]).toArray(), 2);\nprint('2D 2x3 array image (pixel)', sampArrImg(arrayImg2D));\n// [[0, 1, 2],\n// [4, 0, 5]]\n\n// Slice out a row to use as a column mask.\nvar rowAsMaskForCols = arrayImg2D.arraySlice(0, 1, 2);\nprint('2D mask for cols (pixel)', sampArrImg(rowAsMaskForCols));\n// [[4, 0, 5]]\n\nvar arrayImg2DMaskCols = arrayImg2D.arrayMask(rowAsMaskForCols);\nprint('2D array image cols masked (pixel)', sampArrImg(arrayImg2DMaskCols));\n// [[0, 2],\n// [4, 5]]\n\n// Slice out a column to use as a row mask.\nvar colAsMaskForRows = arrayImg2D.arraySlice(1, 1, 2);\nprint('2D mask for rows (pixel)', sampArrImg(colAsMaskForRows));\n// [[1],\n// [0]]\n\nvar arrayImg2DMaskRows = arrayImg2D.arrayMask(colAsMaskForRows);\nprint('2D array image rows masked (pixel)', sampArrImg(arrayImg2DMaskRows));\n// [[0, 1, 2]]\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\nColab (Python) \n\n```python\n# A function to print arrays for a selected pixel in the following examples.\ndef samp_arr_img(arr_img):\n point = ee.Geometry.Point([-121, 42])\n return arr_img.sample(point, 500).first().get('array')\n\n# Create a 1D array image with length 6.\narray_img_1d = ee.Image([0, 1, 2, 4, 0, 5]).toArray()\nprint('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())\n# [0, 1, 2, 4, 0, 5]\n\n# Create a mask using a relational operator to mask values greater than 2.\nmask_1d = array_img_1d.lte(2)\nprint(\n '1D mask for greater than value 2 (pixel):',\n samp_arr_img(mask_1d).getInfo()\n)\n# [1, 1, 1, 0, 1, 0]\n\narray_img1d_mask = array_img_1d.arrayMask(mask_1d)\nprint('1D array image mask (pixel):', samp_arr_img(array_img1d_mask).getInfo())\n# [0, 1, 2, 0]\n\n# Self mask the 1D array image. Value zero will be masked out.\narray_img_1d_self_mask = array_img_1d.arrayMask(array_img_1d)\nprint(\n '1D array image self mask (pixel):',\n samp_arr_img(array_img_1d_self_mask).getInfo()\n)\n# [1, 2, 4, 5]\n\n# Create a 2D array image.\narray_img_2d = array_img_1d.arrayReshape(ee.Image([2, 3]).toArray(), 2)\nprint('2D 2x3 array image (pixel):', samp_arr_img(array_img_2d).getInfo())\n# [[0, 1, 2],\n# [4, 0, 5]]\n\n# Slice out a row to use as a column mask.\nrow_as_mask_for_cols = array_img_2d.arraySlice(0, 1, 2)\nprint('2D mask for cols (pixel):', samp_arr_img(row_as_mask_for_cols).getInfo())\n# [[4, 0, 5]]\n\narray_img_2d_mask_cols = array_img_2d.arrayMask(row_as_mask_for_cols);\nprint(\n '2D array image cols masked (pixel):',\n samp_arr_img(array_img_2d_mask_cols).getInfo()\n)\n# [[0, 2],\n# [4, 5]]\n\n# Slice out a column to use as a row mask.\ncol_as_mask_for_rows = array_img_2d.arraySlice(1, 1, 2)\nprint('2D mask for rows (pixel):', samp_arr_img(col_as_mask_for_rows).getInfo())\n# [[1],\n# [0]]\n\narray_img_2d_mask_rows = array_img_2d.arrayMask(col_as_mask_for_rows)\nprint(\n '2D array image rows masked (pixel):',\n samp_arr_img(array_img_2d_mask_rows).getInfo()\n)\n# [[0, 1, 2]]\n```"]]