Tuesday, 9 January 2018

google earth engine - Filtering Sentinel2 image collection to get least cloudy image for entire ROI


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
Map.addLayer(asset);


//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(
qa.bitwiseAnd(cirrusBitMask).eq(0));
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
//filter.
//print (clipped); //Image Collection- will not print because there are >5000
//elements
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');


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...