I want to see how the new Landsat 7 Collection 2 (C2, 'LANDSAT/LE07/C02/T1_L2') differs from Collection 1 (C1). I have created an adequate Landsat 7 C1 composite since GEE provides a cloud mask function but does not for L7 C2.

Have I understood the bitwise concepts correctly for my own cloud mask? The resulting image is patchy, is this because C2 has better cloud identification so more clouds, haze and shadows are removed?

My image is super dark, I've played around with min max and gamma, but still looks dark.

Shareable link (hopefully sharing configured correctly): https://code.earthengine.google.com/5b109a17b9027d14ad5ed8aba364859c

Alternatively, code below:

// script aim:
// create cloud free composite with L7 Collection 2

// GEE supplied cloud mask for L7 Collection 1
var cloudMaskL457 = function(image) {
  var qa = image.select('pixel_qa');
  // If the cloud bit (5) is set and the cloud confidence (7) is high
  // or the cloud shadow bit is set (3), then it's a bad pixel.
  var cloud = qa.bitwiseAnd(1 << 5)
          .and(qa.bitwiseAnd(1 << 7))
          .or(qa.bitwiseAnd(1 << 3))
  // Remove edge pixels that don't occur in all bands
  var mask2 = image.mask().reduce(ee.Reducer.min());
  return image.updateMask(cloud.not()).updateMask(mask2);

// cloud mask I am trying to write for L7 Collection 2
var cloudMaskC2L7 = function(image) {
  var dilatedCloud = (1 << 1)
  var cloud = (1 << 3)
  var cloudShadow = (1 << 4)
  var qa = image.select('QA_PIXEL');
  var mask = qa.bitwiseAnd(dilatedCloud)
  // var mask = qa.bitwiseAnd(dilatedCloud).eq(0).and(
  //   qa.bitwiseAnd(cloud).eq(0)).and(qa.bitwiseAnd(cloudShadow).eq(0))
  var mask2 = image.mask().reduce(ee.Reducer.min());
  return image.updateMask(mask.not()).updateMask(mask2);

var landsat7C1 = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
  .filterDate('1999-01-01', '2000-12-31')

var landsat7C2 = ee.ImageCollection('LANDSAT/LE07/C02/T1_L2')
  .filterDate('1999-01-01', '2000-12-31')

var L7C1params = {
  bands: ['B3', 'B2', 'B1'],
  min: 0,
  max: 3000,
  gamma: 1.4

var L7C2params = {
  bands: ['SR_B3', 'SR_B2', 'SR_B1'],
  min: 5000,
  max: 50000,
  gamma: 2

Map.addLayer(landsat7C2, L7C2params, 'landsat 7 Collection 2 1999 - 2000 cloud masked')
Map.addLayer(landsat7C1, L7C1params, 'landsat 7 Collection 1 1999 - 2000 cloud masked')

3 Answers 3


Bits 1, 3, and 4 look correct, according to the User's Guide, but you haven't scaled the data appropriately. The scaling factors are different for C2.


  • Fantastic, thanks Noel - This is good to know that I've applied the bitmask correctly. Hopefully this will help other newcomers to bitmasks. I've applied the scaling factor for both collections and both composites look good.
    – Matt Payne
    Commented Aug 4, 2021 at 16:03

Thank you so much for posting this! I used this code to mask L8 and L9 collection 2 as well.

One thing, Guilherme Oliveira is correct that the OR opertator is better. See screenshots below: enter image description here

Compared to: enter image description here

  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 7, 2022 at 15:28

It seems that like your logic for producing the mask is wrong because you are considering dilated cloud AND cloud. It should be the OR operator, no?

 var mask = qa.bitwiseAnd(dilatedCloud)

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.