6

I'm using the Javascript console on Google Earth Engine, and I'm stumped by a simple problem. I want to concatenate and print two values on one line. One of them is an object property.

When I use a comma to separate the parts in the print statement, I get the output I want, except that it is printed on separate lines. When I use a plus ('+'), the object's toString method is invoked and I get vastly more output than I want.

var img = ee.Image(img_list.get(0)) //grab an image
var date_index = img.get('system:index'); //get its date index property

print('MOD13Q_', date_index);
//Output (basically what I want, except on two lines instead of one)
//'MOD13Q_'
//'2018-01-17' 


print('MOD13Q_' + date_index); //Oops, '+' invoked the toString method
//'MOD13Q_ComputedObject({
  "type": "Invocation",
  "arguments": {
    "object": {
      "type": "Invocation",
      "arguments": {
        "list": {
          "type"....etc.

Is there any way to override the toString method, or to prevent it from being invoked? This question describes something similar, but does not show how to solve my problem.

1
  • 1
    The first print works because print(date_index) (where date_index is an ee Object) is a shortcut of print(date_index.getInfo()). You can still use your second print statement using the + operator, but you have to use .getInfo(): print('MOD13Q_'+date_index.getInfo()) Commented Apr 6, 2023 at 11:35

1 Answer 1

10

Use server-side string method cat instead of JavaScript concatenation operator +.

var outStr = ee.String('MOD13Q_').cat(date_index);
print(outStr);

// OUT: 'MOD13Q_2018-01-17'
2
  • Brilliant, thank you! Does that work because the ee.Image toString method gets called when evaluatingget(property) on the client side, whereas on the server side the get(property) method just returns the property directly?
    – John
    Commented Apr 6, 2023 at 5:22
  • I have no idea. Commented Apr 6, 2023 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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