My research area needs multiple landsat images(About 15 images), so I want to mosaic images where they are in the same path first (they have the same acquisition date). Howerver, I only know the mosaic operation for the specified date


So how do I get the date of each image in the image collection? Or is there any better iterative way to mosaic the same date images?

1 Answer 1


I typically iterate over the dates you need your mosaic for:

var startDate = '2022-05-01'
var endDate = '2022-06-01'

var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
var numberOfDays = ee.Date(endDate).difference(ee.Date(startDate), 'days')
var dayOffsets = ee.List.sequence(0, numberOfDays.subtract(1))
var dailyMosaics = ee.ImageCollection(dayOffsets
  .map(function (dayOffset) {
    var date = ee.Date(startDate).advance(ee.Number(dayOffset), 'days')
    var images = collection.filterDate(date.getRange('day'))
    return ee.ImageCollection(images)
      .set('system:time_start', date.millis())

Map.addLayer(dailyMosaics.first(), {bands: 'SR_B4,SR_B3,SR_B2', min: 0, max: 3000, gamma: 1.5})

function preProcess(image) {
  var qa = image.select('QA_PIXEL')
  var cloudShadow = bitwiseExtract(qa, 4)
  var snow = bitwiseExtract(qa, 5)
  var cloud = bitwiseExtract(qa, 6).not()
  return image

function bitwiseExtract(value, fromBit, toBit) {
  if (toBit === undefined)
    toBit = fromBit
  var maskSize = ee.Number(1).add(toBit).subtract(fromBit)
  var mask = ee.Number(1).leftShift(maskSize).subtract(1)
  return value.rightShift(fromBit).bitwiseAnd(mask)


Alternatively, you could use a join:

var dates = ee.FeatureCollection(dayOffsets
  .map(function (dayOffset) {
      var date = ee.Date(startDate).advance(ee.Number(dayOffset), 'days')
        .advance(12, 'hours')
      return ee.Feature(null, {'system:time_start': date.millis()})
var twelveHoursInMillis = 12 * 60 * 60 * 1000
var dailyMosaics = ee.ImageCollection(
    primary: dates,
    secondary: collection,
    condition: ee.Filter.maxDifference({
      difference: twelveHoursInMillis,
      leftField: 'system:time_start',
      rightField: 'system:time_start'
).map(function (feature) {
  return ee.ImageCollection(ee.List(feature.get('images')))
    .set('system:time_start', feature.get('system:time_start'))


  • Thanks for your help, this is exactly what I want
    – yiboliu
    Jun 9, 2022 at 7:47

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.