1

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);
});
4
  • Please provide a link to a functioning EE script, with all used assets shared. Commented Jan 22, 2020 at 12:58
  • i am quite new to this... i am not able to create a URL that works for others .. i am pasting the code though in the question .. please find it.
    – rao
    Commented Jan 22, 2020 at 13:45
  • Hi @rao, to share scripts via Code Editor URLs, just click the "Get Link" at the top of the code editor panel. A dialog box will appear providing a "copy link" button (the URL is also automatically added to the clipboard). Commented Jan 22, 2020 at 16:17
  • Related question and answer: gis.stackexchange.com/a/348061/68792 Commented Jan 22, 2020 at 16:23

1 Answer 1

1

You can just loop over your years client-side and add them to the map. To make this easier, you might want to set the year as a property of each image.

var startYear = 2016
var endYear = 2018
var dataset = ee.ImageCollection('COPERNICUS/S2');
var years = ee.List.sequence(startYear, endYear);
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
      .set('year', y)
  }));
print(collectYear);

function addImage(image) { // display each image in collection
  var id = image.id;
  Map.addLayer(image);
}

for (var year = startYear; year <= endYear; year++) {
  var image = collectYear
    .filterMetadata('year', 'equals', year)
    .first()
  var visParams = {bands: 'B4,B3,B2', min: 0.03, max: 0.3, gamma: 1.5}
  Map.addLayer(image, visParams, '' + year)
}

https://code.earthengine.google.com/5239181e2b7ac82e3377faa00dade9c1

0

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.