Following a previous post I now have some code which sets null values in an image collection to zero.

I have used this code on several variables, however, I'd like to know which variables have NULL values in, opposed to the value just being 0.

Is there a way of flagging or highlighting an image collection that has null values in it, and maybe counting them per region? I've tried to set the value of the band to the metadata which I can then filter for, but it doesn't seem to work. My code is below:

var currentSpeedNulls = currentSpeed.map(function(image) {
  var Nulls = image.unmask(-999);
  return image.set("current", Nulls.get("current"))});

var currentSpeedNulls1 = currentSpeedNulls
.filterMetadata("current", "equals", -999);

var currentSpeedNulls_mean = currentSpeedNulls1.mean();

//count nulls per region
 var currentSpeedNulls_region = currentSpeedNulls_mean.reduceRegions({
  reducer: ee.Reducer.count(),
  collection: reefs_500,
  scale: 30});
  • @Rodrigo E. Principe Can you please provide an example over a collection, not an image? Thank you very much!
    – S AB
    Commented May 31, 2021 at 11:46

1 Answer 1


The reduction is fine, but you gotta adapt the input in order to get what you want. I leave you an example:

var i = ee.Image.random()
var scale = 1e5

// mask out some pixels
i = i.updateMask(i.lte(0.5))

// add it reprojected to see a bigger size pixels and
// make a better interpretation
Map.addLayer(i.reproject(i.projection().atScale(scale)), {}, 'masked image')
Map.centerObject(geometry, 6)

// get an image in which the only valid pixels are over the mask
// This is the key of the process, the proper input to count over
var mask = i.mask().not().selfMask()

// Inspect the mask layer
Map.addLayer(mask.reproject(i.projection().atScale(scale)), {}, 'mask pixels')

// use a reducer for counting the masked pixels
var count = mask.reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: geometry,
      scale: scale,
      maxPixels: 1e13


  • Hi Rodrigo, so the line var mask = i.mask().not().selfMask(), selects the mask, assigns 0 to non-null values and 1 to null values, then adds the actual value where the value is not zero, is this right? If I've assigned -999, I can apply this to an image collection and this should filter out all anything but values of -999 which I can then count per region? Commented Jun 29, 2020 at 7:17
  • yes, you could wrap this into a function to map over a collection. In that case the mask to count over would be mask = i.eq(-999).selfMask(). At the end you could add count as an image property and then filter the collection according to that property. Commented Jun 29, 2020 at 9:53
  • Ah great yes! thank you. Commented Jun 29, 2020 at 12:57

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.