I am now using fmask algorithm to eliminate the cloud covered areas on Landsat images.

I choose a period of time (Jan 1st 2017 to Dec 31st 2017), and selected the study area (Florida). I want to get all of the Landsat 8 images under the above mentioned conditions, then apply the algorithm to mask the cloud.

I hoped the images taken for the same area but on different observation days could all get cloud masked, overlapped together and then I can do some statistical calculation.

However, I can only get the cloud masked images for Dec 31st 2017. I have tried a lot but still cannot overlap the images for the whole year.

My code is attached below

//Choose country using GEE Feature Collection

var region = ee.FeatureCollection('TIGER/2016/States')
        ee.Filter.eq('NAME', 'Florida')));

//Add region outline to layer ‐ for selected states

//Map.addLayer(region,{}, 'Florida');

var landsat8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR');

// Fmask classification values var FMASK_CLEAR_GROUND = 0; var FMASK_WATER = 2; var FMASK_CLOUD_SHADOW = 3; var FMASK_SNOW = 4; var FMASK_CLOUD = 5;

var mosaic = landsat8 .filterBounds(region) .filterDate('2017-01-01', '2017-12-31') .mosaic();

var getQABits = function(image, start, end, newName) {
    // Compute the bits we need to extract.
    var pattern = 0;
    for (var i = start; i <= end; i++) {
       pattern += Math.pow(2, i);
    // Return a single band image of the extracted QA bits, giving the band
    // a new name.
    return image.select([0], [newName])

// A function to mask out cloudy pixels.
var cloud_shadows = function(image) {
  // Select the QA band.
  var QA = image.select(['pixel_qa']);
  // Get the internal_cloud_algorithm_flag bit.
  return getQABits(QA, 3,3, 'Cloud_shadows').eq(0);
  // Return an image masking out cloudy areas.

// A function to mask out cloudy pixels.
var clouds = function(image) {
  // Select the QA band.
  var QA = image.select(['pixel_qa']);
  // Get the internal_cloud_algorithm_flag bit.
  return getQABits(QA, 5,5, 'Cloud').eq(0);
  // Return an image masking out cloudy areas.

var maskClouds = function(image) {
  var cs = cloud_shadows(image);
  var c = clouds(image);
  image = image.updateMask(cs);
  return image.updateMask(c);

var mosaic_free = maskClouds(mosaic);

var visParams = {bands: ['B4', 'B3', 'B2'],min: [0,0,0],max: [2000, 2000, 2000]};

Map.addLayer(mosaic, visParams, 'With clouds'); 
Map.addLayer(mosaic_free, visParams, 'Cloud free'); 

Could anybody help me to figure out what's wrong with that?

1 Answer 1


First, what does mosaic?

Composites all the images in a collection, using the mask.

Returns: Image

Since mosaic returns an image object, use this function at the end of the process, because if you removes clouds for a single image, you can't fill gaps. Apply mosaic.map(maskClouds) (as ImageCollection) instead maskClouds(mosaic) (as Image).

But, is mosaic what you need for a nice composite? With mosaic:

enter image description here

With median reducer:

enter image description here

CODE: https://code.earthengine.google.com/432c55d13f4651bc4e46d9cab4ab2f9c

  • Thanks! I did not realize the sequence makes a big difference! Commented Jun 24, 2019 at 22:15
  • And yes, I used median as well, but it does not meet my need. What I wanted to do is, for the same area, we have multiple Landsat 8 images taken on different dates during a period of time. I want to overlap all of them, then calculate the NDWI and some other indexes and then, for every single point, we have a collection of the corresponding single pixels on different images. I want to summarize its value distribution, have a majority vote to determine the land cover at that point (water body, vegetation area, etc.) So I guess this method would help. Thank you so much! Commented Jun 24, 2019 at 22:23
  • You're welcome! So you wanna do a time-series analysis. Check this: code.earthengine.google.com/a4f2f8a7092d34c8bd7f53ba26115928. The same code without mosaic and with the calculation of two indices. If you want to do an MTA, don't mosaic your images, work with image collection instead
    – aldo_tapia
    Commented Jun 24, 2019 at 22:34
  • Hi Aldo, thank you so much. Your hint is really close to what I want. However as I tried it on another case, it doesn't work. To be frank I'm not really understand what does your function(img) means, particularly the return value on the 58th line. If it is convenient to you, could you take a look at another thread that I posted just now? Thank you so much gis.stackexchange.com/questions/327232/… Commented Jun 28, 2019 at 14:26

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.