Chapter 13: Data Quality Bitmasks
This chapter provides a workow to explore quality ag bitmasks for imagery in Rocky Mountain National Park, Colorado, United States. The full
GEE code can be found here.
Code for functions was adapted from this Geographic Information Systems Stack Exchange post and this Open Geo Blog post.
Functions
Data Acquisition & Preprocessing
Landsat 8 T1 SR ‘pixel_qa’ band bitmasks:
Bit 0: Fill
Bit 1: Clear
Bit 2: Water
Bit 3: Cloud Shadow
Bit 4: Snow
Bit 5: Cloud
Bits 6-7: Cloud Condence
0: None
1: Low
2: Medium
3: High
Bits 8-9: Cirrus Condence
0: None
1: Low
2: Medium
3: High
Bit 10: Terrain Occlusion
Bitmask value of 1 for single bits means ag is set (i.e. water == 1 –> pixel is water). Bitmask value for multiple bits indicates condition (cirrus
condence of 3).
/**
* Extracts QA values from an image
* @param {ee.Image} qa_band - Single-band image of the QA layer
* @param {Integer} start_bit - Starting bit
* @param {Integer} end_bit - Ending bit
* @param {String} band_name - param_description
* @return {ee.Image} - Image with extracted QA values
*/
function extract_qa_bits(qa_band, start_bit, end_bit, band_name) {
// Initialize QA bit string/pattern to check QA band against
var qa_bits = 0;
// Add each specified QA bit flag value/string/pattern to the QA bits to check/extract
for (var bit = start_bit; bit <= end_bit; bit++) {
qa_bits += Math.pow(2, bit); // Same as qa_bits += (1 << bit);
}
// Return a single band image of the extracted QA bit values
return qa_band
// Rename output band to specified name
.select([0], [band_name])
// Check QA band against specified QA bits to see what QA flag values are set
.bitwiseAnd(qa_bits)
// Get value that matches bitmask documentation
// (0 or 1 for single bit, 0-3 or 0-N for multiple bits)
.rightShift(start_bit);
}