1

I calculated an image collection with the property "date". Now I want to add the property "date" as a new band but I got the error "Actual value for parameter 'id' must be a constant". Instead of .getString I also tried .get and .getNumber.

GEEscript

// Add band:
var add_band = function(img) {
  var bandDate = img.getString('Date');
  return img.addBands(bandDate);
}

var new_mask = s1_mask.map(add_band);
print('ImageCollection new mask', new_mask)

And is it possible to transform the new band in a date format such as YYYY_MM_DD?


---EDIT--- -> solution

var addDate = function(image){
  var dateNum = ee.Image(image).date().millis()
  var doyBand = ee.Image.constant(dateNum).rename('date').toFloat()
  doyBand = doyBand.updateMask(image.select('VV').mask())
  
  return image.addBands(doyBand);
};

var withDate = s1.map(addDate);
print(withDate);

1 Answer 1

1

simple answer: no. The error in your script already tells you that .addBands() requires a constant value so no character/string. See for example: Add a date (day of year) band to each image in a collection using Google Earth Engine

  var dateNum = ee.Image(img).date().millis() // date in milli seconds
  var dateBand = ee.Image.constant(dateNum).uint16().rename('dateMillis')

https://code.earthengine.google.com/0013a119e3d19bf254196afd5418263b


EDIT

Sorry bit of a typo there:

var dateBand = ee.Image.constant(dateNum).rename('dateMillis') 

You can check all your dates by retrieving the computed value from the properties (see example) or add your images and use the inquiry to see the different date bands.

https://code.earthengine.google.com/6f72be06fac2ac0b71e2815d155c2e7b

4
  • Thank you. The link to the other post helped (see updated question). But I don't know how to change the format in a way that I have the information about day, month and year.
    – lena
    Commented Jun 3, 2022 at 8:38
  • If you read the documentation you'll notice that .millis() returns the exact date but expressed in a different format: developers.google.com/earth-engine/apidocs/ee-date-millis
    – Jobbo90
    Commented Jun 3, 2022 at 11:25
  • Ok, I replaced "var doy = image.date().getRelative('day', 'year');" with "var doy = ee.Image(image).date().millis()". Now I have a new band "dateMillis" but the value is always 65535 although the images were taken on different dates. In your script, this happens as well but you add it also as a new property and this seems to work. So why not with the addBands function?
    – lena
    Commented Jun 3, 2022 at 11:56
  • Thanks! With your edit it worked but I got another error when I added the image collection to the map. I could fix that with the function .toFloat(). The final code is as an update in my question.
    – lena
    Commented Jun 3, 2022 at 17:48

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.