ee.Algorithms.If
Selects one of its inputs based on a condition, similar to an if-then-else construct.
Usage | Returns | ee.Algorithms.If(condition, trueCase, falseCase) | Object |
Argument | Type | Details | condition | Object, default: null | The condition that determines which result is returned. If this is not a boolean, it is interpreted as a boolean by the following rules:
- Numbers that are equal to 0 or a NaN are false.
- Empty strings, lists and dictionaries are false.
- Null is false.
- Everything else is true.
|
trueCase | Object, default: null | The result to return if the condition is true. |
falseCase | Object, default: null | The result to return if the condition is false. |
Examples
Code Editor (JavaScript)
print(ee.Algorithms.If(false, '*true*', '*false*')); // The string "*false*"
print(ee.Algorithms.If(true, '*true*', '*false*')); // The string "*true*"
// Consider using remap rather than If for tasks like numbers for classes.
print(ee.Algorithms.If(ee.String('Tree').compareTo('Tree'), 0, 1));
print(ee.Algorithms.If(ee.String('NotTree').compareTo('Tree'), 0, 1));
Python setup
See the
Python Environment page for information on the Python API and using
geemap
for interactive development.
import ee
import geemap.core as geemap
Colab (Python)
# The string "*false*"
print(ee.Algorithms.If(False, '*true*', '*false*').getInfo())
# The string "*true*"
print(ee.Algorithms.If(True, '*true*', '*false*').getInfo())
# Consider using remap rather than If for tasks like numbers for classes.
print(ee.Algorithms.If(ee.String('Tree').compareTo('Tree'), 0, 1).getInfo())
print(ee.Algorithms.If(ee.String('NotTree').compareTo('Tree'), 0, 1).getInfo())
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-09-19 UTC.
[null,null,["Last updated 2024-09-19 UTC."],[[["`ee.Algorithms.If` selects one of two input values based on a given condition, similar to an if-then-else statement."],["The `condition` input determines the output, treating non-boolean values as true or false based on specific rules (e.g., 0 is false, empty strings are false)."],["`trueCase` and `falseCase` represent the values returned when the condition is true or false, respectively."],["While `ee.Algorithms.If` is versatile, using `remap` might be more efficient for tasks like assigning numerical values to classes."]]],[]]