问题是这样的,有时候我们明明看到结果是一致的,但是为啥运算出来的东西却不一样呢?我们可以具体看看到底是为啥,有朋友闻到了这样的问题,我们可以具体看看这个问题:
这个结果是 True 我该怎么做?
修正前的代码:
var points=ee.Geometry.MultiPoint([[35.13382554493316, 42.01690078898458],
[35.127720870209764, 42.023546081055784]])
Map.addLayer(points)
var p=points.coordinates().size()
print(p)
print(p==ee.Number(2))
这里我们所需要用到的就是我么判断是否相等的算法,具体算法见如下
ee.Algorithms.IsEqual(left, right)
Returns whether two objects are equal.返回两个对象是否相等。
left (Object, default: null)
right (Object, default: null)
修正后代码:
var points=ee.Geometry.MultiPoint([[35.13382554493316, 42.01690078898458],
[35.127720870209764, 42.023546081055784]])
Map.addLayer(points)
var p=points.coordinates().size()
print(p)
var ff= ee.Algorithms.IsEqual(p,ee.Number(2))
print(ff)
这里还有一种方式:
getInfo(callback)
Retrieves the value of this object from the server.
If no callback function is provided, the request is made synchronously. If a callback is provided, the request is made asynchronously.
The asynchronous mode is preferred because the synchronous mode stops all other code (for example, the EE Code Editor UI) while waiting for the server. To make an asynchronous request, evaluate() is preferred over getInfo().
Returns the computed value of this object.
this:computedobject (ComputedObject):
The ComputedObject instance.
callback (Function, optional):
An optional callback. If not supplied, the call is made synchronously.
getInfo(callback)
从服务器上检索此对象的值。
如果没有提供回调函数,请求是同步进行的。如果提供了一个回调函数,请求是异步进行的。
异步模式是首选,因为同步模式在等待服务器时停止所有其他代码(例如,EE代码编辑器用户界面)。为了进行异步请求,evaluate()比getInfo()更适合。
返回这个对象的计算值。
参数。
this:computedobject (ComputedObject)。
计算对象的实例。
callback(函数,可选)。
一个可选的回调。如果不提供,调用将同步进行。
返回。对象
var points=ee.Geometry.MultiPoint([[35.13382554493316, 42.01690078898458],
[35.127720870209764, 42.023546081055784]])
Map.addLayer(points)
var s1=ee.Number(points.coordinates().size())
print(s1.eq(ee.Number(2)))
print(s1.eq(ee.Number(2))==1)
print(s1.eq(ee.Number(2)).getInfo()==1)
结果:
当然除了用上面判断是否相等之外,还可以使用下面的函数来进行判断:条件,真假的情况是什么都可以的。
ee.Algorithms.If(condition, trueCase, falseCase)
Selects one of its inputs based on a condition, similar to an if-then-else construct.
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.
这里还有一种情况,就是当我们反映真假得时候,我们可以用到0和1而不是true和法拉瑟,其实很简单。
var points=ee.Geometry.MultiPoint([[35.13382554493316, 42.01690078898458],
[35.127720870209764, 42.023546081055784]])
Map.addLayer(points)
var s1=ee.Number(points.coordinates().size())
print('s1', s1)
print('s1.eq(ee.Number(2))', s1.eq(ee.Number(2)))
print('s1.eq(ee.Number(3))', s1.eq(ee.Number(3)))