Arthur BAYLE
Arthur BAYLE

Reputation: 33

object type as a condition in if else - from JS to rgee

I'm trying to convert this if/else function from JS (GEE) to rgee :

if ((typeof band) === 'string')

obviously, the "typeof" does not work in rgee, so I tried to find an alternative. I came up with the following code :

if (ee$Algorithms$ObjectType(band) == 'String')

The problem is that in that case, ee$Algorithms$ObjectType will not give me the object type of band, but gives EarthEngine Object: String

For example, if I try ee$Algorithms$ObjectType(ee$Number(2)), it results in : EarthEngine Object: String, while the good answer is "Integer". I can get the true information using ee$Algorithms$ObjectType(ee$Number(2))$getInfo(), but this is not applicable to script because it is client-sided.

Do you have any ideas ? Thanks a lot !

I tried to convert if ((typeof band) === 'string') to if (ee$Algorithms$ObjectType(band) == 'String') but it does not work

Upvotes: 1

Views: 167

Answers (1)

Daniel Wiell
Daniel Wiell

Reputation: 268

A regular if-else is a client-side operations. You cannot apply it to a server-side object, like the output of ObjectType(). See here for more details. You can do conditionals on server-side objects with ee.Algorithms.If(), though it's something you typically try to avoid.

print(
  ee.Algorithms.If(
    ee.Algorithms.ObjectType(ee.String('A string')).equals('String'), 
    'This is a String', 
    'This is not a String'
  )
)

print(
  ee.Algorithms.If(
    ee.Algorithms.ObjectType(ee.Number(123)).equals('String'), 
    'This is a String', 
    'This is not a String'
  )
)

https://code.earthengine.google.com/dcbdb8b99e97bb7dd11c236b147bc7fa

Upvotes: 0

Related Questions