I would like to generate a collection of the 3 least cloudy granules for each Sentinel-2 tile within the bounds of a particular shapefile. It seems that this will require a function that iterates on each tile separately rather just using .sort, "Cloudy_Pixel_Percentage", and .mosaic to create a single mosaic of the least cloudy granule for each tile. So, I have created a script that identifies the distinct tiles:
// Load the ROI table
var table =
ee.FeatureCollection("users/theronmb/TTCS_Area_Crop_for_Sentinel-2");
// Find the MGRS_TILES that overlap with the ROI for the time period
// of interest
var collection = ee.ImageCollection("COPERNICUS/S2")
.filter(ee.Filter.calendarRange(3,6,'month'))
.filter(ee.Filter.calendarRange(2017,2017,'year'))
.filterBounds(table)
.distinct('MGRS_TILE')
;
print(collection);
// Get a specific metadata property.
var tiles = ee.List(collection.aggregate_array('MGRS_TILE'));
From there, I was thinking to use a for loop to create the collections I need, iterating on the list of distinct tiles. Unfortunately, this simple loop I am using for testing does not seem to do anything (or even generate an error). Where am I going wrong?
print(tiles.get(0)); // This works
print(tiles.size()); // This works
// Test iterating on the specific metadata property (does not work)
for (var i = 0; i < tiles.size(); i = i + 1) {
print(tiles.get(i));
}