0

I have a stack of Sentinel 1 images and I want to display all of them one by one and want the label to be the date of corresponding image. I am trying a for loop for that but I am unable to visualize the results.

Link :https://code.earthengine.google.com/e58e28ed2fd14cc2b8cbaf4ef666b5bb

1 Answer 1

2

Use ee.Image.get() option to retrieve the specific property of interest (in your case it is system:time_start which corresponds to the date of acquisition.)

The new loop should look something like this:

for (var i=0; i <=2; i++)
{
  var image = ee.Image(listOfImages.get(i));
  var label = ee.Date(image.get("system:time_start")).format('YYYY-MM-dd HH:mm:ss').getInfo();
  Map.addLayer(image,vis_params2,label,true)
}

Explanation:

  1. ee.Date() function converts the acquisition times from milliseconds to date-time object.
  2. I have used the .format() option to convert the ee.Date object to a string but you can use any other method too.
  3. The .getInfo() is important because Map.addLayer() is a client-side function. So the label property to be passed needs to be retrieved from server-side to client-side.

Link to corrected code.

3
  • Works perfectly. Thankyou so much :) I had no idea about client side function. I shall look for it now. A follow-up query: I defined a variable in the for loop condition and the code stopped working. I am wondering what could be the problem (Line 19) Link: code.earthengine.google.com/e4bf667ba4cb028613020989c80e7795
    – Shagun
    Jan 10, 2021 at 6:31
  • the for loop runs on the client-side. Its arguments must be retrieved to the client-side first before running the loop. iff variable resides in the server-side. To retrieve it use .getInfo(). So use for (var i=0; i <=iff.getInfo()-1; i++). If you want to read more about server-side and client-side, checkout this link: developers.google.com/earth-engine/guides/client_server.
    – kkrao
    Jan 11, 2021 at 7:13
  • Thankyou so much @kkrao. worked perfectly :)
    – Shagun
    Jan 14, 2021 at 14:35

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