5

I have created code which accesses Sentinel 2 images, clip them according to a shapefile I have and calculate NDVI for the whole image collection. Now, I want to visualize the images I have created, to see them as a layer and maybe after even export them. I tried to change the ImageCollection into a list and then use "get" in order to create variable out of the images, but it didn't work and I get this error message anytime I try to visualize the images:

 Cannot add an object of type <ComputedObject> to the map.

I tried it also on the image collection before all the calculations, when it was a bunch of Sentinel images, but I got the same error.

This is my code:

/**
 * Function to mask clouds using the Sentinel-2 QA band
 * @param {ee.Image} image Sentinel-2 image
 * @return {ee.Image} cloud masked Sentinel-2 image
 */
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)
  .copyProperties(image, ['system:time_start']);
}

// Map the function over one year of data 
// Load Sentinel-2 TOA reflectance data.
var dataset = ee.ImageCollection('COPERNICUS/S2')
                  .filterDate('2015-06-23', '2019-07-02')
                  // Pre-filter to get less cloudy granules.
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                  .select('B2','B3','B4','B8','QA60')
                  .filterBounds(geometry)
                  .map(maskS2clouds);

var rgbVis = {
  min: 0.0,
  max: 0.3,
  bands: ['B4', 'B3', 'B2'],
};
var clippedCol=dataset.map(function(im){ 
   return im.clip(geometry);
});

print(clippedCol);


//function to calculate NDVI
var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4'])
  .rename('NDVI')
  .copyProperties(image,['system:time_start']);
  return image.addBands(ndvi);

};

//NDVI to the clipped image collection
var withNDVI = clippedCol.map(addNDVI).select('NDVI');

var colorizedVis = {
  min: 0.0,
  max: 1.0,
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
};

var listOfImages = withNDVI.toList(withNDVI.size());
print('List:',listOfImages);
var img1 = listOfImages.get(0);

Map.addLayer(img1,colorizedVis,'img1');

The end goal is to be able to visualize any image I want from the ImageCollection and to be able to export it.

1 Answer 1

12

For Earth Engine the elements inside an ImageCollection are Image but inside a List are just Element, so the system doesn't know it is an Image what you are getting from it. You can read about it here.

To solve this you just have to "cast" it:

var img1 = ee.Image(listOfImages.get(0));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.