I am trying to obtain the least cloudy image (or images) across my entire ROI. Though I do not get any errors, the true color composite is not being mapped for me to check if I have indeed sorted and used the least cloudy image in the entire Sentinel 2 image collection in my ROI and within a date range.

/////////////////////////////////////1. Parameters
//Load asset
var asset= ee.FeatureCollection('users/myfolder/asset'); //you can use any asset you have, eg consider a country like US. I cannot make my asset public

//Load Sentinel 2 image collection
var sentinel2 = ee.ImageCollection('COPERNICUS/S2');

//Function for masking clouds
// http://xzsunbest.tk/2018/07/05/MaskingOutCloudInSentinel2WithGEE/

// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = ee.Number(2).pow(10).int();
print (cloudBitMask); //1024
var cirrusBitMask = ee.Number(2).pow(11).int();
print (cirrusBitMask); //2048 

var cloudmask = function (image) {
var qa = image.select('QA60');
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(
return image.updateMask(mask);

// Create clipping function
var clipper = function(image){
return image.clip(asset);

/////////////////////////////////////2. Application
var clipped = sentinel2.map(clipper);
//var filtered = clipped.filterDate('2015-06-01','2018-08-30')
//                  .filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE',2))
//        filtering to images with cloud cover of 2%. I do not want to    
//print (clipped); //Image Collection- will not print because there are >5000   
var count = clipped.size();
print('Count: ', count); //8168695 across Sentinel2 in ROI

var filtered_date = clipped.filterDate('2015-01-01','2015-12-31');
var count_filtered = filtered_date.size();
print('Filtered (by date) Image Count: ', count_filtered);//259629 reduced  
because of date range

var images = ee.Image(filtered_date.sort('CLOUDY_PIXEL_PERCENTAGE').first())
print('Least cloudy image: ', images);
Map.addLayer(images, {bands:['B4','B3','B2']}, 'true color composite');

2 Answers 2


First your actual question; why the image is not showing in the map: That is because you did not filter the images on your area of interest. Somewhere else on the world, your image was added (it gave no error). You can check where using:


What you are doing now is using the full image collection of sentinel 2 in your analysis. That's a huge amount of data, and therefore you cannot print it. The function clip does not filter the image collection on your area of interest. You will need to filter the collection on the area (and date range), using:

// FIRST, reduce the collection spatially and temporally
var filtered = sentinel2.filterBounds(geometry).filterDate('2015-01-01','2015-12-31');
print('Filtered Image Count: ', filtered.size());

Your asset seems to be a feature collection, to clip images on that, use:


Then apply you clip and cloudmask function by mapping over the image collection:

var clipped = filtered.map(cloudmask).map(clipper);

If you will add the image to the map as you did, you will see an image appearing on the map in the area of your study area

// Get a least cloudy image
var leastCloudy = ee.Image(filtered.sort('CLOUDY_PIXEL_PERCENTAGE').first());
Map.addLayer(leastCloudy, {bands:['B4','B3','B2'], min: 0, max: 3500}, 'true color scene');

For your further work, note that sentinel-2 images are cut into 100x100km granules (see the ESA user handbook or GEE dataset catalog). Probably you want to merge them by time or make a composite image for your studyarea and time range. See the link. Also note that cloudmasking for sentinel based on the QA band won't get rid of all the clouds, unfortunately. Link script

  • thank you for the link to your code. However, I am still a little confused. I want the least cloudy image in each S2 tile/grid which is not your var leastcloudy (this is the least cloudy image in all tiles across ROI). How do you suggest I do that?
    – tg110
    Commented Feb 19, 2019 at 19:04
  • You can try to do that, but you will run into other problems, see: code.earthengine.google.com/81ff82bfe7a2cf8667d6508a83e8261e . Make sure you understand how these MGRS Tiles of Sentinel are generated: sentinel.esa.int/web/sentinel/missions/sentinel-2/data-products
    – Kuik
    Commented Feb 20, 2019 at 9:30
  • Again thank you for your response. I have noted the problem of overlap of paths and rows of S2. Looking over your code again, shouldn't it be var clipped = filtered.map(cloudmask); where the filtered (by date and ROI) image collection be masked? And then use this clipped in the rest of the script instead of using var filtered (which has no cloudmask)?
    – tg110
    Commented Feb 20, 2019 at 19:41
  • Yes you are right for the filtered one!
    – Kuik
    Commented Feb 21, 2019 at 22:49

You may collect such data from LandViewer. This tool has a vast database of satellite imagery from numerous satellites including Sentinerl-2 and it is updated on a regular basis.

All you need to do is follow this tutorial:

  • Open LandViewer
  • Click “filters” on the top right
  • Select “Sentinel-2” from the sensors tab below
  • Locate an area you need
  • Set cloudiness level via settings bar above the sensors list

As an example, let’s take a look at this image of New York. As you can see I set cloudliness level at 16% and I can see the image perfectly fine.

Direct link -> https://eos.com/landviewer/?lat=40.86446&lng=-73.01033&z=11&datasets=4&id=LV-UzJC-X3Rp-bGVf-MjAx-OTAy-MTdf-MThU-WExf-MA%3D%3D&b=Red,Green,Blue&anti

Here’s a brief guide to free satellite data that can be found on LandViewer:

  • Landsat 4 - archive 1982-1993
  • Landsat 5 - archive 1984-2013
  • Landsat 7 - archive since 1999
  • MODIS - archive since 2012
  • Landsat 8 - archive since 2013
  • Sentinel-1 - archive since 2014
  • Sentinel-2 - archive since 2015

All the satellite data is free to use and easy to find thanks to the convenient search interface, which reduces time needed to find right image data.

  • 1
    thanks. I found the Landviewer to be useful for preliminary look of scenes. However, it is not free and hence not usable.
    – tg110
    Commented Feb 21, 2019 at 18:36

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.