0

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 zero.

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});
2

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
  })
  
print(count)

https://code.earthengine.google.com/c33fb47153a2399ff65c920fba157bc5

| improve this answer | |
  • 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? – mikejwilliamson Jun 29 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. – Rodrigo E. Principe Jun 29 at 9:53
  • Ah great yes! thank you. – mikejwilliamson Jun 29 at 12:57

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.