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:
https://code.earthengine.google.com/4ad5be8fd16607a589bd54858af0edd6
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)])
.connectedPixelCount({
maxSize: 128,
eightConnected: false
})
var connected_area = ee.Image.pixelArea()
.addBands(connected_size)
.lte(25000000)
.eq(1)
.selfMask()
.select('labels')
var fill = test_image.focalMax( 1,'square','pixels',10)
.updateMask(connected_area)
.selfMask()
var filled = test_image.addBands(fill)
.reduce(ee.Reducer.max()).regexpRename('max','constant')
Map.addLayer(test_image.randomVisualizer())
Map.addLayer(filled.randomVisualizer())
This is the expected result.