javascript - Google Earth Engine ee.Number to integer - Stack Overflow

This is probably a simple question for people familiarized with the Code Editor of Google Earth Engine

This is probably a simple question for people familiarized with the Code Editor of Google Earth Engine (/) or generally Javascript.

In my code, I need to use the size of an object for a boolean conditional (e.g. n>0). However, the output of .size() which I would store in n does not return a plain integer, but a ee.Number structure and I am not being able to transform it into an integer to properly evaluate the conditional.

Example with the structure ee.Number of Earth Engine:

var n=ee.Number(1)
print(n)
print(n.int())
print(n==1)
print(n===1)
print(n.int()==1)
print(n.int()===1)
print(n.int()== parseInt(1))

This outputs these evaluate as false, even when I try to tast the number structure into an int.

1
1
false
false
false
false
false

note:

print(typeof n)

returns an object (JSON):

object

Any help very much appreciated. Thanks

This is probably a simple question for people familiarized with the Code Editor of Google Earth Engine (https://code.earthengine.google./) or generally Javascript.

In my code, I need to use the size of an object for a boolean conditional (e.g. n>0). However, the output of .size() which I would store in n does not return a plain integer, but a ee.Number structure and I am not being able to transform it into an integer to properly evaluate the conditional.

Example with the structure ee.Number of Earth Engine:

var n=ee.Number(1)
print(n)
print(n.int())
print(n==1)
print(n===1)
print(n.int()==1)
print(n.int()===1)
print(n.int()== parseInt(1))

This outputs these evaluate as false, even when I try to tast the number structure into an int.

1
1
false
false
false
false
false

note:

print(typeof n)

returns an object (JSON):

object

Any help very much appreciated. Thanks

Share Improve this question asked Dec 5, 2018 at 9:50 Moisés Expósito AlonsoMoisés Expósito Alonso 351 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

This is due to how GEE works. Processing steps are constructed locally as objects and then only evaluated by the server once another function requires it.

print is one of the functions that requires execution, this is why it shows as integer in your console.

You can force evaluation with .getInfo()... this however should be used with caution, because everything is pulled to the client side, which can be problematic with big objects.

So this works:

var n=ee.Number(1)

print(n)
print(n.getInfo()==1)

giving

1
true

This section of the documentation explains the background.

If the value of n indeed is JSON, try to parse it:

n = JSON.parse(n);

Then convert it into an integer:

n = parseInt(n);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745424188a4627098.html

相关推荐

  • javascript - Google Earth Engine ee.Number to integer - Stack Overflow

    This is probably a simple question for people familiarized with the Code Editor of Google Earth Engine

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信