I have a code that calculates the max number of pixels among all the rasters it retrieves and then creates new imagecollection with only rasters that contains rasters in the size I want. For some reason, , today I have tried to do this on different dataset- sentinel 2, but it didn't work and I get null as number of pixels.
I suspect that it's because on the prexious dataset I have used dataset that had only one band image- NDVI, and now I have more, but I don't really know and without this number I can't run the whole script.
This is the code I have:
Map.centerObject(geometry,10);
/**
* Function to mask clouds using the Sentinel-2 QA band
* @param {ee.Image} image Sentinel-2 image
* @return {ee.Image} cloud masked Sentinel-2 image
*/
function maskS2clouds(image) {
var qa = image.select('QA60');
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
// 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).divide(10000)
.copyProperties(image, ['system:time_start']);
}
// Map the function over one year of data and take the median.
// Load Sentinel-2 TOA reflectance data.
var dataset = ee.ImageCollection('COPERNICUS/S2')
.filterDate('2019-10-14', '2019-12-08')
// Pre-filter to get less cloudy granules.
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
.select('B2','B3','B4','B8','QA60')
.filterBounds(geometry)
.map(maskS2clouds);
var rgbVis = {
min: 0.0,
max: 0.3,
bands: ['B4', 'B3', 'B2'],
};
var clippedCol=dataset.map(function(im){
return im.clip(geometry);
});
// Get the number of images.
var count = clippedCol.size();
print('Count: ',count);
// //Filter according to number of pixels
var ClippedCount = clippedCol.map(function(image){
var countpixels = ee.Number(image.reduceRegion({
reducer: ee.Reducer.count(),
geometry: geometry,
crs: 'EPSG:4326',
scale: 30,
//maxPixels:21176024,
}));
return image.set('count',ClippedCount);
});
print('Count:',ClippedCount);
//Here I get null in print
var max = ClippedCount.reduceColumns(ee.Reducer.max(), ["count"]);
print(max.get('max'));
var max_number=max.getNumber('max');
I need to filter the images in the end according to this number, I am not sure how to solve it .
*I can't upload the original geometry but it didn't work on any geometry I have tried so you can try it on whatever polygon you draw