0

I am trying to iterate through an image collection and add a property to each image that is taken from a list. For example I want image 1 in the collection to have the property list item 1.

I first get the collection like so

var collection = ee.ImageCollection('COPERNICUS/S1_GRD')
.filterBounds(AOI)
.filterDate(startDate,endDate)
.filter(ee.Filter.eq('instrumentMode', 'IW'))
.filter(ee.Filter.eq('resolution_meters', 10)) // orbital path needs to change
.select('VV');

then I create a list and convert to a date

var index = collection.aggregate_array('system:index');
function extract(title){
  var cal = ee.String(title).slice(17, 25);
  var time = ee.String(title).slice(26, 32);
  
  var imgdate = ee.Date.parse('yyyyMMddHHmmss',cal.cat(time));
  return imgdate;
} 
var dates = index.map(extract);

then I wanted to create a new property called date and add the corresponding list item to the image.

var i = 0;
function joindate (img){
  img.set({'Date': dates.get(i)});
  i = i+1;
  return img;
}

collection = collection.map(joindate);

I get no errors but when I look at the image properties, no new properties are made.

Here is a link to my code, but there are a lot more processes going on https://code.earthengine.google.com/381595639a2961eb100cdfde05625e2f

1 Answer 1

0

For starters, the images in the collection already contains the acquisition time, through the system:time_start property. It contains millis since Unix epoch. The easiest way to get it as a date is to simply call image.date(). So I don't really see a reason to extract the date from the image id. You do have to be a bit careful, as some image operations will remove the image properties. If that happen, you can simply add them back from the original image.

Still, your implementation shows a couple of misconceptions on how EE works, that probably is worthwhile looking at.

When you map over a collection, like you do with collection.map(joindate), the function will actually not be invoked once for every image in the collection. You're creating a "recipe" on how the Google servers should process your imagery. So you cannot rely on a counter like you do with i. There are usually ways to get around such constructs. In your case, you can skip generating the dates list altogether and extract the date directly when mapping.

Another issue is the way you're setting the date on the image. Most operations in EE doesn't actually change the objects involved. A new object is returned instead. In your case, img.set({'Date': dates.get(i)}) doesn't actually add a property to img, but returns a new image, identical to img, but with the Date property set.

Below is how you could implement it. But, again, I wouldn't do this at all and instead rely on the pre-existing date.

var AOI = ee.Geometry.Polygon([[
  [-2.476123901062024, 51.76830784649942],
  [-2.476123901062024, 51.72281788891212],
  [-2.377246947937024, 51.72281788891212],
  [-2.377246947937024, 51.76830784649942]
]])
var startDate = '2017-01-01'
var endDate = '2017-12-31'

var collection = ee.ImageCollection('COPERNICUS/S1_GRD')
  .filterBounds(AOI)
  .filterDate(startDate, endDate)
  .filter(ee.Filter.eq('instrumentMode', 'IW'))
  .filter(ee.Filter.eq('resolution_meters', 10)) // orbital path needs to change
  .select('VV')
  .map(preProcess)

print(collection.first().get('date'))


function preProcess(img) {
  var date = extract(img.id())
  return img
    .set('date', date)
}

function extract(title) {
  var cal = ee.String(title).slice(17, 25)
  var time = ee.String(title).slice(26, 32)

  var imgdate = ee.Date.parse('yyyyMMddHHmmss', cal.cat(time))
  return imgdate
}

https://code.earthengine.google.com/17e787a793a6ac0b3f6ab0409c5d0ce6

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.