# Problem with a function to create a unique image starting to image collection

Starting from Global Hansen dataset and in particular lossyear map, with the following function I obtained an image collection. Each different image is given by the overlap of image for the current year and the image of the following year. These two maps are obtained following two different condition as report.

``````var mylist = ee.List.sequence(1, 16, 1);

var combine_img = ee.ImageCollection.fromImages(mylist.map(function(y) {
y = ee.Number(y).toInt();
var a = gfc.select(['lossyear']).eq(y).rename('a'); //Current year
var aP = gfc.select(['lossyear']).eq(y.add(1)).rename('aP'); //Next year
var b = mapB.select(y.subtract(1)).rename('b');
var bP = mapB.select(y).rename('bP');
var c = mapC.select(y.subtract(1)).rename('c');
// mapC is the discriminating factor.
// In the map of following year I would like to have pixels that result by filtering for this map.

// First condition
// Values of map b  are greater than map bP
// .gt mean "greater than".
// Event in the current year

// Second condition
// Values of bP map are greater than map b
// .lte means "less than equals"
// Event in the following year.

return b_gt_bP.blend(bP_lte_b).set('imageNumber', y);
// the first map is overlap to the other map.
// Obtain a map with pixel value equal to imageNumber
}));
``````

How can I modify this function to obtain not an image collection but a unique image? In this map the pixels values are equals to imageNumber?

EDIT: In my mind the pixel values have only integer number starting from 1 to 16.

I attached a picture above. The image shows the proprieties of the first image that makes up my collection. The pixels in this image have value of 1. In my final map I want that these pixels have a value of 1 because it is referred to 2001. In the same map I would like also the pixels of other image of my collection that have a values between 2 to 16 according the year.

• Do you mean extract any image from the given collection? For instance,`combine_img.first()` will do the job. Commented Mar 17, 2020 at 19:21
• I have tried this solution but it is not correct because if I understand the help well this function only uses the first band. I, on the other hand, would need a function that uses all bands. Commented Mar 18, 2020 at 10:05

I think you're asking how to combine the single-band images in the image collection to a single, multi-band image. If so, you simply call `ee.ImageCollection.toBands()`. You might want to rename the generated band names to something cleaner. Here's an example how you could do that:

``````var gfc = ee.Image('UMD/hansen/global_forest_change_2018_v1_6');
var mapB = ee.Image('users/mattelearning85/mapB') //Harvest rate
var mapC = ee.Image('users/mattelearning85/mapC') //wind critical speed

var mylist = ee.List.sequence(1, 16, 1);
var combine_img = ee.ImageCollection.fromImages(mylist.map(function(y) {
y = ee.Number(y).toInt();
var a = gfc.select(['lossyear']).eq(y).rename('a'); //Current year
var aP = gfc.select(['lossyear']).eq(y.add(1)).rename('aP'); //Next year
var b = mapB.select(y.subtract(1)).rename('b');
var bP = mapB.select(y).rename('bP');
var c = mapC.select(y.subtract(1)).rename('c');
return b_gt_bP.blend(bP_lte_b).set('imageNumber', y)