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.Array.firstNonZero

  • Array.firstNonZero(right) is a method that returns an Array.

  • This method selects the first value on an element-wise basis if it is non-zero and the second value otherwise.

  • It takes two Array arguments:left (the left-hand value) andright (the right-hand value).

On an element-wise basis, selects the first value if it is non-zero, and the second value otherwise.

UsageReturns
Array.firstNonZero(right)Array
ArgumentTypeDetails
this:leftArrayThe left-hand value.
rightArrayThe right-hand value.

Examples

Code Editor (JavaScript)

varempty=ee.Array([],ee.PixelType.int8());print(empty.firstNonZero(empty));// []print(ee.Array([0]).firstNonZero(0));// [0]print(ee.Array([0]).firstNonZero([0]));// [0]print(ee.Array([0]).firstNonZero([1]));// [1]print(ee.Array([2]).firstNonZero([3]));// [2]print(ee.Array([1]).firstNonZero([0]));// [1]print(ee.Array([-1,0,1]).firstNonZero([2,-1,2]));// [-1,-1,1]// [[1,2],[3,4]]print(ee.Array([[1,2],[0,0]]).firstNonZero([[0,0],[3,4]]));

Python setup

See the Python Environment page for information on the Python API and usinggeemap for interactive development.

importeeimportgeemap.coreasgeemap

Colab (Python)

empty=ee.Array([],ee.PixelType.int8())display(empty.firstNonZero(empty))# []display(ee.Array([0]).firstNonZero(0))# [0]display(ee.Array([0]).firstNonZero([0]))# [0]display(ee.Array([0]).firstNonZero([1]))# [1]display(ee.Array([2]).firstNonZero([3]))# [2]display(ee.Array([1]).firstNonZero([0]))# [1]display(ee.Array([-1,0,1]).firstNonZero([2,-1,2]))# [-1, -1, 1]# [[1, 2], [3, 4]]display(ee.Array([[1,2],[0,0]]).firstNonZero([[0,0],[3,4]]))

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.