1

I have an image collection of Landsat 7 filtered by a date and PATH and ROW of interest, and I want to know the best way to mosaic all the images (tiles) that have same PATH and ROW into one image.

The collection is created by the following code:

var L7_1 = ee.ImageCollection("LANDSAT/LE07/C02/T1_L2") .filterDate('2000-01-01', '2000-12-31') .filter(ee.Filter.metadata('WRS_PATH', "greater_than", 171)) .filter(ee.Filter.metadata('WRS_PATH', "less_than", 206)) 
.filter(ee.Filter.metadata('WRS_ROW', "greater_than", 27)) 
.filter(ee.Filter.metadata('WRS_ROW', "less_than", 37)) 
print(L7_1, 'L7_1');

I would like to do this iteratively for the whole collection over a broad date range and obtain a new image collection of mosaicked images by single PATH and ROW.

1 Answer 1

1

You can do this with a join.

var collection = ee.ImageCollection("LANDSAT/LE07/C02/T1_L2")
  .filterDate('2000-01-01', '2001-01-01') // End date is exclusive
  .filter(ee.Filter.metadata('WRS_PATH', "greater_than", 171))
  .filter(ee.Filter.metadata('WRS_PATH', "less_than", 206)) 
  .filter(ee.Filter.metadata('WRS_ROW', "greater_than", 27)) 
  .filter(ee.Filter.metadata('WRS_ROW', "less_than", 37))
    
var pathRowCollection = ee.ImageCollection(ee.Join.saveAll('images')
  // Join images with same row/path together
  .apply({
    // Collection with one image for each row/path
    primary: collection
      .distinct(['WRS_PATH', 'WRS_ROW']), 
    secondary: collection, 
    condition: ee.Filter.and(
      ee.Filter.equals({leftField: 'WRS_PATH', rightField: 'WRS_PATH'}),
      ee.Filter.equals({leftField: 'WRS_ROW', rightField: 'WRS_ROW'})
    )
  })
  .map(function (image) {
    // All images for this row/path will be in the `images` property of the image
    return ee.ImageCollection(ee.List(image.get('images')))
      // Remove clouds in every image for this row/path
      .map(maskClouds)
      // Pick some approach to reduce the collection into a single image
      // median(), mean(), mosaic(), qualityMosaic(), reduce(ee.Reducer)
      .median()
      // Rescaling to 0-10000, just out of habit
      .multiply(0.0000275) 
      .subtract(0.2)
      .multiply(10000)
      .int16()
      // Include row/path for the composite
      .copyProperties(image, ['WRS_PATH', 'WRS_PATH'])
  })
)

Map.addLayer(pathRowCollection.mosaic(), {bands: 'SR_B3,SR_B2,SR_B1', min: 0, max: 3000})


function maskClouds(image) {
  var cloudFree = bitwiseExtract(image.select('QA_PIXEL'), 0, 5).eq(0)
  return image
    .updateMask(cloudFree)
}

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

https://code.earthengine.google.com/9f02891f699cab48345e0e99a4960d8d

6
  • Thank you @DanielWiell. The output collection (pathRowCollection) contains single image produced by mosaic of all images of the 2000 year that have the same PATH and ROW?
    – user207655
    Sep 7, 2022 at 7:51
  • No, pathRowCollection contains 271 images, one for each path/row within your constraint. I did make a mistake though - copyProperties() included the path twice. Here's the fixed script with some debug output to show the contents of the resulting collection: code.earthengine.google.com/faf93086755d2655a2c96d65806e57ac. Sep 7, 2022 at 8:52
  • Thank you @DanielWiell. But, for example the first image of pathRowCollection is mosaic of raw images (contains in my collection) that have the same path e row?
    – user207655
    Sep 7, 2022 at 9:06
  • Yes. In this case, a median composite of all images from that path/row Sep 7, 2022 at 10:17
  • Well done @DanielWiell. Thank you.
    – user207655
    Sep 7, 2022 at 10:26

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.