I'm interested in transitions that occur on a date, such as bushfires. I want to get the closest (in time) cloud-free landsat image possible using as many images as necessary in the collection up until that date. It's easy to make a cloud-free collection of images using the map function but then I want the masked pixels of the image closest to the date to the filled (where possible) with the previous image and if that is also masked, the next previous etc. I thought of using imagecollection.qualityMosaic with a layer of the deltadays from the final date to control mosaicing process but I cannot manipulate the image date in the imageCollection.map function as it is external. There also may be a much simpler way to do this. I'm new to Python and GEE (but not programming) which is certainly not helping. Any ideas?


3 Answers 3


After reading carefully your question, I've realized that what you need is a function I have incorporated in geetools

from geetools import tools

masked_collection = ee.ImageCollection('a masked col')
filled_collection = tools.imagecollection.fill_with_last(masked_collection)

The hardcoded version is:

def fill_with_last(collection):
    """ Fill masked values of each image pixel with the last available
    :param collection: the collection that holds the images that will be filled
    :type collection: ee.ImageCollection
    :rtype: ee.ImageCollection

    new = collection.sort('system:time_start', True)
    collist = new.toList(new.size())
    first = ee.Image(collist.get(0)).unmask()
    rest = collist.slice(1)

    def wrap(img, ini):
        ini = ee.List(ini)
        img = ee.Image(img)
        last = ee.Image(ini.get(-1))
        mask = img.mask().Not()
        last_masked = last.updateMask(mask)
        last2add = last_masked.unmask()
        img2add = img.unmask()
        added = img2add.add(last2add) \
            .set('system:index', ee.String(img.id()))

        props = img.propertyNames()
        condition = props.contains('system:time_start')

        final = ee.Image(ee.Algorithms.If(condition,

        return ini.add(final.copyProperties(img))

    newcol = ee.List(rest.iterate(wrap, ee.List([first])))
    return ee.ImageCollection.fromImages(newcol)
  • Rodrigo, thanks again, I'll definitely be using this straight away until I get your geebap working in Python 3.7. Then I have all sorts of options. Sep 12, 2018 at 2:01

Can't you just filter images by time and try to create a mosaic with these images? So, first filter:

temporalFiltered = spatialFiltered.filterDate('2016-01-01','2016-12-28')

and then do the mosaic? And if this does not work you'll just have to increase the window of dates!


I've been pulled off this by other dramas but someone posted (thanks for that) a very helpful post on 'Best Available Pixel' that definitely had useful info in it. It's taking me a while to digest.



Cheers P

Oh there you are in a comment above. Thanks Rodrigo E. Principe

  • You are welcome =) There is a jupyter notebook in the repository as an example on how to use it. Sep 7, 2018 at 1:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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