1

I have two image collections (ndvi and spei) with 20 features each (one image per year). I want to combine them into one image collection with one image per year with two bands (ndvi and spei). To do so, I tried to apply an inner Join based on the system property 'year' that I defined for each image collection. As a result, I obtain an image collection with 400 features instead of 20, and I am not able to see where the problem is.

Here is my code:

var ndvi= medianCompResiduals.select('ndvi');
var spei= spei3m.map(function(img){
  return img.rename('spei')
}); 

// Add year as a system property to match with SPEI
var ndvi=ndvi.map(function(img){
  var date= ee.Date(img.get('system:time_start'))
  return img.set('year', ee.Number(date.get('year')))
})
print (ndvi, 'ndvi')

var spei=spei.map(function(img){
  var date= ee.String(img.get('system:index')).slice(1,5)
  return img.set('year', ee.Number(date))
})
print (spei, 'spei')

// Use an equals filter to specify how the collections match.

var filter = ee.Filter.equals({
  leftField: 'system:year',
  rightField: 'system:year'
});

// Define the join.
var innerJoin = ee.Join.inner('primary', 'secondary');

// Applt join
var collectionJoin = ee.ImageCollection(innerJoin.apply(ndvi, spei, filter )); 

// Flatten joined images into a single image with 2 bands
var Combined = collectionJoin.map(function(feature) {
  return ee.Image.cat(feature.get('primary'), feature.get('secondary'));
});


// Print the result.
print('Inner join:', Combined);

1 Answer 1

1

This is a small enough problem that I'd just convert to lists and zip them together (as long as they're both the same length and order). Something like:

merged = collection1.toList(100).zip(collection2.toList(100))
    .map(function(row) {
        return ee.Image(ee.List(row).get(0))
              .addBands(ee.List(row).get(1))
})

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.