google-earth-engine – Mosaicking NDVI Image Collection by PATH and ROW in Google Earth Engine

geemapgoogle-earth-engineimage-mosaiclandsat-7

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.

Best Answer

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

Related Question