I'm reclassifying a DEM asset into 6 categories. However, there are some pixels that don't meet my criteria and are null. I would like to find a way to fill these gaps depending on its area and its neighbor's values.

My current workflow is the following:

  • Get the inverse mask
  • Clump pixels
  • Identify groups that met the area criteria
  • Apply a morphological max filter (10 iterations) within the masked area, and
  • Add bands and reduce by the max category.

Although my code is working and it's doing what I want, it is causing memory issues when working with bigger areas and with a high-res scale. I've been seen that the most memory-consuming step is the morphological filter and the 10 fixed iterations sometimes are not enough to fill all the gaps, is there an alternative to do this?.

A code snippet can be found here:


var test_image = ee.Image("users/dfgm2006/categorical_ranges");
// Fill gaps using the surrounding pixels

var inverse = test_image.unmask().not().eq(1).selfMask()

var connected = inverse.connectedComponents({
  connectedness: ee.Kernel.plus(1),
  maxSize: 128
var connected_size = connected.select([connected.bandNames().get(0)])
    maxSize: 128, 
    eightConnected: false 

var connected_area = ee.Image.pixelArea()
var fill = test_image.focalMax( 1,'square','pixels',10)

var filled = test_image.addBands(fill)


This is the expected result.

enter image description here


1 Answer 1


Have you considered using focalMode instead of Max? Ran into a similar problem today and it worked like a charm.

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.