4

Suppose I want to create a cloud-free composite of Landsat 8 Collection 2 surface reflectance images for summer 2021 of Washington state, USA. How can I do it?

Here is the code to build the collection of interest.

// Washington state boundary.
var roi = ee.FeatureCollection('FAO/GAUL/2015/level1')
  .filter('ADM1_NAME == "Washington"').first().geometry();

// Landsat 8 Collection 2 surface reflectance images of interest.
var col = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
  .filterBounds(roi)
  .filterDate('2021-06-15', '2021-09-15');

// Display the images.
var visParams = {
  bands: ['SR_B6', 'SR_B5', 'SR_B3'],
  min: 0,
  max: 20000
};
Map.setCenter(-120.633, 47.263, 7);
Map.addLayer(col.mosaic().clip(roi), visParams, 'Mosaic');

1 Answer 1

4

The Landsat Collection 2 surface reflectance images include a quality assessment band ("QA_PIXEL") that is derived from the CFMask algorithm. It identifies clouds, cloud shadows, and snow/ice. It is a very good masking system, you can read more about it in section 6.2 of the Landsat 8 Collection 2 (C2) Level 2 Science Product (L2SP) Guide. In the following example, it is used along with the saturation band ("QA_RADSAT") to mask clouds and cloud shadows to generate a median cloud-free composite. Note that the function that masks each image also applies band-specific scaling factors.

// A function that scales and masks Landsat 8 (C2) surface reflectance images.
function prepSrL8(image) {
  // Develop masks for unwanted pixels (fill, cloud, cloud shadow).
  var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
  var saturationMask = image.select('QA_RADSAT').eq(0);

  // Apply the scaling factors to the appropriate bands.
  var getFactorImg = function(factorNames) {
    var factorList = image.toDictionary().select(factorNames).values();
    return ee.Image.constant(factorList);
  };
  var scaleImg = getFactorImg([
    'REFLECTANCE_MULT_BAND_.|TEMPERATURE_MULT_BAND_ST_B10']);
  var offsetImg = getFactorImg([
    'REFLECTANCE_ADD_BAND_.|TEMPERATURE_ADD_BAND_ST_B10']);
  var scaled = image.select('SR_B.|ST_B10').multiply(scaleImg).add(offsetImg);

  // Replace original bands with scaled bands and apply masks.
  return image.addBands(scaled, null, true)
    .updateMask(qaMask).updateMask(saturationMask);
}


// Washington state boundary.
var roi = ee.FeatureCollection('FAO/GAUL/2015/level1')
  .filter('ADM1_NAME == "Washington"').first().geometry();

// Landsat 8 Collection 2 surface reflectance images of interest.
var col = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
  .filterBounds(roi)
  .filterDate('2021-06-15', '2021-09-15')
  .map(prepSrL8)
  .select('SR.*')
  .median();

// Display the cloud-free median composite.
var visParams = {
  bands: ['SR_B6', 'SR_B5', 'SR_B3'],
  min: 0,
  max: 0.4
};
Map.setCenter(-120.633, 47.263, 7);
Map.addLayer(col.clip(roi), visParams, 'Cloud-free mosaic');

A similar function could be used to prepare Landsat 5 and 7 data as well.

6
  • Hi Justin, can you clarify what work the scaling (scaleImg and offsetImg functions) is doing in this block? I am under the impression that these factors have already been applied to Level 2 Landsat data (LC08/C02/T1_L2).
    – J Kelly
    Commented Oct 12, 2022 at 22:59
  • 1
    Scale factor and offset need to be applied. The data processed and provided by USGS is made to fit in to uint16 datatype to reduce storage and transfer burden. You need to apply the scaling and offset to get the data back into the appropriate floating point range ~[0,1]. This post might help a little regarding why: usgs.gov/faqs/… Commented Oct 13, 2022 at 21:05
  • Hi Justin. Thanks for putting together this amazing script. The composites are stunningly beautiful. Do you, by chance, happen to have code for Landsat 5 and 7 that you'd be willing to share?
    – Tom Dilts
    Commented Nov 4, 2022 at 18:36
  • @TomDilts, you can find Landsat SR cloud masking examples for 5 and 7 that are very similar in the Code Editor example scripts: Examples > Cloud Masking > Landsat457 Surface Reflectance (code.earthengine.google.com/…) Commented Nov 7, 2022 at 22:34
  • Thank you Justin. Looks great.
    – Tom Dilts
    Commented Nov 8, 2022 at 19:42

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.