6

I have created mosaics by date from an ImageCollection and now want to label each mosaic with its corresponding date. This is my function to label the ImageCollection:

var date_label_pos = ee.Geometry.Point([32.55, -15.42]);
var scale = 200;

function annotate(collection) {
  return collection.map(function(img) {
    // define text properties
    var props = {
      fontFamily: 'Calibri', 
      fontStyle: 'bold', 
      textColor: '1D4180', 
      outlineColor: 'ffffff', 
      outlineWidth: 2.5, 
      outlineOpacity: 0.6
    };
    var center = ee.List(date_label_pos.coordinates());
    var pos = translate(center, 0, 0);
    var s = ee.String(' DATE SHOULD BE HERE!');
    var date_label = Text.draw(s, pos, scale * 1.5, props);
    // merge results
    var image = img.visualize({forceRgbOutput: true, min: 500, max: 1800, bands: ['B4', 'B3', 'B2']});
    return ee.ImageCollection([image, date_label]).mosaic(); 
  });
}

The full code is HERE. What I'm missing is the part where I can extract the date from the image mosaic. img.date() does not work.


To provide a little bit more info: I needed to set a new property date to the mosaics, which I can see when I print the ImageCollection to the console. I can also access that date outside of the annotate function using:

print(ee.Image(mosaics.toList(mosaics.size()).get(0)).get('date'));

where mosaics is my ImageCollection. But within the function, it seems that img is something different and I can't figure out what it is and how I can access the date property I set.

2
  • You should post or share the full code example using the "Get Link (shareable snapshot)" link, not the "Copy script Path" link. Looking at the small piece of code in your question body, it is difficult to evaluate what you are trying to do.. especially the part where you are doing ee.ImageCollection([image, date_label]).mosaic -- that doesn't seem to make sense Apr 17, 2023 at 12:34
  • I fear I lost access to the full script (probably due to the exact mistake you pointed out), so although I'd love see this solved, I cannot provide any further information.
    – s6hebern
    Apr 17, 2023 at 15:01

1 Answer 1

1

This question was asked a long time ago, but nevertheless, here is the answer. The main questions is related to the following line:

var s = ee.String(' DATE SHOULD BE HERE!');

The img.date() can't be passed here as an argument because it is of an ee.Date() type, to turn it into a string you need to use ee.Date.format(), e.g., img.date().format('YYYY-MM-dd HH:mm'). which would return a string and that can be passed as an argument to the draw() function to turn that string into an image.

If you'd like to use something like mosaics.first().date() after you apply the annotate() function, you need to propagate the system:time_start property to the resulting images, you can do it by making use of copyProperties(img, ['system:time_start']) function. The date() function is just a helper function passing that property to the ee.Date().

Furthermore, the code miscalculates the text position. It's always a constant value based on the date_label_pos. I've replaced it with the image centroid and added missing pieces to generate a working example: https://code.earthengine.google.com/4cb8c1c370d4ecbe62fc728391877275. Notice that the code copies the system:time_start property from the original image to ensure that the date() still works. The final code adds image times as annotations.

output

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.