1

I am new in Earth Engine. Here is an image collection [img1,img2,img3,img4,img5]. I want to get List or image collection [img1+img2 , img2+img3, img3+img4, img4+img5]. I tried to complete the following code https://code.earthengine.google.com/1256826da91886b9242f29f20339d604 But the result is incorrect, I don't know where the error is. I try not to use for loops because I will have a lot of data to calculate.

var img1 = ee.Image(1);//3
var img2 = ee.Image(2);//5   
var img3 = ee.Image(3);//7   
var img4 = ee.Image(4);//9  
var img5 = ee.Image(5);
var image=ee.ImageCollection([img1,img2,img3,img4,img5]);

var list = image.toList(image.size());

var newList = list.slice(1);
print(list.size());
newList = newList.map(function(data) {
  data = ee.Image(data);
  var l = data.add(ee.Image(list.get(list.size().subtract(1))));
  return l;
})
print(newList) 

1 Answer 1

2

You can do it this way:

var img1 = ee.Image(1);
var img2 = ee.Image(2);
var img3 = ee.Image(3);
var img4 = ee.Image(4);
var img5 = ee.Image(5);

var image=ee.ImageCollection([img1,img2,img3,img4,img5]);

var list = image.toList(image.size());

var upList = list.slice(0, ee.Number(list.size()).subtract(1))
var downList = list.slice(1, list.size())

var outList = upList.zip(downList).map(function(listElem) {return ee.Image(ee.List(listElem).get(0)).add(ee.Image(ee.List(listElem).get(1)))})

Create upList with last element missing, downList with first element missing, zip them together to make a list of 2 element lists, and map a function to sum the images over that list.

I hope this helps.

3
  • Thank you very much for your answer, it completely solved my problem
    – YYu
    Commented Mar 9, 2020 at 14:52
  • 1
    @YYu gis.stackexchange.com/help/someone-answers
    – user2856
    Commented Mar 11, 2020 at 11:11
  • @blizzardus okay.I have marked for an acceptable answer
    – YYu
    Commented Mar 15, 2020 at 17:11

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.