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:

* 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)

  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))

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,

  return image.set('count',ClippedCount);


//Here I get null in print
var max = ClippedCount.reduceColumns(ee.Reducer.max(),  ["count"]);
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


Yes - you are right. It is because you have multiple bands. That means that the metadata property "count" is not a number but a dictionary. However you have a typo in your code as well. You are calling the function ClippedCount from within itself. It the return portion of that function I think you meant:

return image.set('count',countpixels);

When you correct this, the code will no longer return 'null' but will throw an error because of the dictionary "count". You need to disaggregate this:

var max = ClippedCount.reduceColumns(ee.Reducer.max().disaggregate(), ["count"]);

This will now give you the max pixel count of any single band across all the images, ie not the total of all the maxima of five bands (in your case), but the biggest single band.

If however, you want the absolute possible maximum for the whole image (max by band combined for an image of 5 bands), you could do something like this (instead of the line above):

var max_list = ClippedCount.reduceColumns(ee.Reducer.toList(), ["count"]);
var arrays = ee.List(max_list.get('list')).map(function(dict){
  return ee.Dictionary(dict).toArray().toList();
var maxPerBand =  ee.Array(arrays).reduce(ee.Reducer.max(), [0]);
var grandTotal = ee.List(maxPerBand.toList().get(0)).reduce(ee.Reducer.sum());
print("GRAND TOTAL:", grandTotal);

This gets all the counts per band per image and then assembles them into an array (where each column in the array is a band and each row is an image in your collection). It then finds the max value for each band and finally sums the resultant list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.