0

I want to create a cloud-free composite for Landsat 5 data and I want the shadows to be masked as well. However, the code only includes the clouds. I am extremely new to this and cannot find any reference codes for Landsat 5 Collection 2 Level 2 data specifically. How do I fix this?

  // AOI
var aoi = table;

// Landsat 5
var landsat5 = l5.filterBounds(aoi)
  .filterDate('2009-11-01', '2009-11-02') 
  .map(cloudMaskLandsat5)
  .median()
  .multiply(0.0000275).add(-0.2)
  .clip(aoi);

Map.addLayer(landsat5, {bands: ['SR_B4', 'SR_B3', 'SR_B2'], min: 0, max: [0.5, 0.3, 0.2]}, 'Landsat 5');

// Landsat 5 cloud mask
function cloudMaskLandsat5(image){
  var qa = image.select('QA_PIXEL');  
  var cloud = 1 << 5;
  var shadow = 1 << 3;  
  
  var mask = qa.bitwiseAnd(cloud).eq(0)
    .and(qa.bitwiseAnd(shadow).eq(0));
    
  return image.updateMask(mask);
}

1 Answer 1

0

You have the bit-mask for QA_PIXEL in the catalog:

Bit 0: Fill
Bit 1: Dilated Cloud
Bit 2: Unused
Bit 3: Cloud
Bit 4: Cloud Shadow
Bit 5: Snow
Bit 6: Clear
    0: Cloud or Dilated Cloud bits are set
    1: Cloud and Dilated Cloud bits are not set
Bit 7: Water
Bits 8-9: Cloud Confidence
    0: None
    1: Low
    2: Medium
    3: High
Bits 10-11: Cloud Shadow Confidence
    0: None
    1: Low
    2: Medium
    3: High
Bits 12-13: Snow/Ice Confidence
    0: None
    1: Low
    2: Medium
    3: High
Bits 14-15: Cirrus Confidence
    0: None
    1: Low
    2: Medium
    3: High

You've used bit 3 and 5 - that's cloud and snow. Include bit 4 for cloud shadow in the same manner.

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.