Search code examples
javascriptgoogle-earth-engine

Earth Engine: Mapping over an ee.ImageCollection returns an ee.FeatureCollection


I am trying to remove images with clouds from a Sentinel-2 image collection with the following script:

// Remove images with clouds
var cloud_removal = function(image){

  // save the quality assessment band QA60 as a variable
  var cloud_mask = image.select('QA60');
  
  // calculate the sum of the QA60-Band (because QA60 != 0 means cloud or cirrus presence)
  var cloud_pixels = cloud_mask.reduceRegion( // reduceRegion computes a single object value pair out of an image
    {reducer:ee.Reducer.sum(), // calculates the sum of all pixels..
    geometry:aoi, // inside the specified geometry
    scale:60}) // at this scale (matching the band resolution of the QA60 band)
    .getNumber('QA60'); // extracts the values as a number
  
  return ee.Algorithms.If(cloud_pixels.eq(0), image);
};

var s2_collection_noclouds = s2_collection_clipped.map(cloud_removal, true);
print('The clipped Sentinel-2 image collection without cloudy images: ', s2_collection_noclouds);

The problem is that the output ("s2_collection_noclouds") is an ee.FeatureCollection. I have already tried to cast the output as an image collection but it stays a feature collection:

var s2_collection_noclouds = ee.ImageCollection(s2_collection_clipped.map(cloud_removal, true));

What am i missing?


Solution

  • The resulting object is indeed an Image Collection, and can be displayed on the map. For example:

    var visualization = {
      min: 0,
      max: 3000,
      bands: ['B4', 'B3', 'B2'],
    };
    Map.addLayer(s2_collection_noclouds.median(), visualization, "Sentinel-2")
    

    Side note: I do see that the Earth Engine Code Editor console labels the object type as "FeatureCollection" and the collection contains features that are Image objects. This appears to be caused because ee.Algorithms.If() in the mapped function returns a null object for cloudy images. If you instead return a masked out image:

    return ee.Algorithms.If(cloud_pixels.eq(0), image, image.mask(0));
    

    then the collection is correctly described as an ImageCollection.