4

I am using logical judgement in GEE. Here is my toy code:

    var aa = ee.Number(0);
    print(aa.neq(0) );
    print(aa.eq(0) );
    if (aa.eq(0)){
        print('01 is zeros');
    }
    if (aa.neq(0)){
        print('02 is not zeros');
    }

Rather simple, but I got the following feedback:

Ridiculous feedback from above simple code

Is there any recommended and STABLE logical judgement form in GEE?

By the way, this code doesn't work either; it returns the same feedback as shown in the figure.

var aa = ee.Number(0);

print(aa.neq(0) );
print(aa.eq(0) );

if (aa.eq(ee.Number(0))){
  print('01 is zeros');
}
if (aa.neq(ee.Number(0))){
  print('02 is not zeros');
}

7

This is a Client vs. Server problem.

aa.eq(0) is not yet evaluated — it is a JavaScript object that represents a computation that hasn't been performed by Earth Engine yet. The print function automatically performs the evaluation and displays the response. JavaScript's if does not know about Earth Engine objects and will always consider them to be ‘true’.

Generally, you should try to keep things server-side as much as possible — this results in more efficient computations because you're not having to send separate requests to Earth Engine for the next part of the computation. The direct correspondence to if is ee.Algorithms.If, but it is rarely needed. Instead, you can use operations such as filtering a collection.

When you definitely need a result to be used in JavaScript, you should call evaluate to fetch it:

aa.eq(0).evaluate(function (answer) {
  if (answer) {
    ...
  }
});

But, again, this is rarely needed and if you think you need it, there is likely a better way. (Unless you're writing user-interface code for an interactive EE app.)

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.