I found this code Add/display all images of mycollection in google earth engine and tried it, but the output displays four layers each with one band called constant.
Also, can you please tell me what is to be given as image.id
- do I have to set any property in it?
My code is below. Note that collectYear
is my image collection. How should I proceed?
geometry = /* color: #d63000 */ee.Geometry.Polygon(
[[[76.15934177863937, 9.45738504612055],
[76.85697361457687, 9.45738504612055],
[76.85148045051437, 9.993390779673321],
[76.14011570442062, 9.990685858146769]]]);
function maskS2clouds(image) {
var qa = image.select('QA60');
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000);
}
var dataset = ee.ImageCollection('COPERNICUS/S2');
var years = ee.List.sequence(2016, 2018);
print (years);
var collectYear = ee.ImageCollection(years
.map(function(y) {
var start = ee.Date.fromYMD(y, 1, 1);
var end = start.advance(12, 'month');
var d2 = dataset.filterDate(start, end).filterBounds(geometry)
// Pre-filter to get less cloudy granules.
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
.map(maskS2clouds).first();
//Applying clip to image
var ls_clipped = ee.Image(d2).clip(geometry);
var resamp_img = ls_clipped.resample('bilinear');
var B2_B11 = resamp_img.select('B2').divide(resamp_img.select('B11'));
var ratio_image = resamp_img.addBands(B2_B11);
return ratio_image;
}));
print(collectYear);
function addImage(image) { // display each image in collection
var id = image.id;
var image = ee.Image(image.id)
Map.addLayer(image);
}
collectYear.evaluate(function(collectYear) { // use map on client-side
collectYear.features.map(addImage);
});