Google Earth Engine Tutorial: Cloud Masking for Landsat 8 Images
Credit: Youtube Channel “Terra Spatial, Tutorial on applying cloud masking techniques to remove cloud cover from Landsat 8 imagery.”
You can see all the tutorials from here: Techgeo Academy.
Introduction
Cloud masking is a critical step in processing satellite imagery, especially for Landsat 8 datasets, to ensure accurate analysis of surface features. Google Earth Engine (GEE) provides powerful tools to automate this process. This tutorial walks through the steps to apply cloud masking to Landsat 8 images using GEE’s JavaScript API.
Step 1: Loading the Landsat 8 Dataset
Start by loading the Landsat 8 Collection. The dataset includes multiple bands, but the Quality Assessment (QA) band contains information about cloud cover.
// Load Landsat 8 Collection
var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR');
// Filter by date and location
var filtered = dataset.filterDate('2020-01-01', '2020-12-31')
.filterBounds(ee.Geometry.Point([-122.082, 37.422]));
// Select a single image for demonstration
var image = filtered.first();
Step 2: Selecting Relevant Bands
Focus on the visible and near-infrared bands (e.g., bands 2-7) and the QA band for cloud detection. The QA band uses bit values to indicate cloud status.
// Select visible and NIR bands
var bands = ['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7', 'SR_B10'];
var rgbImage = image.select(bands);
// Extract the QA band
var qa = image.select('QA_PIXEL');
Step 3: Applying the Cloud Mask
Use the QA band to create a cloud mask by checking specific bits. For example, bits 3 and 4 indicate cloud cover in Landsat 8.
// Define a function to mask clouds using the QA band
function maskClouds(image) {
// Select the QA band
var qa = image.select('QA_PIXEL');
// Mask clouds: bit 3 (cloud) and bit 4 (cloud shadow)
var cloudMask = qa.bitwiseAnd(1 << 3).eq(0).and(qa.bitwiseAnd(1 << 4).eq(0));// Apply the mask to the image
return image.updateMask(cloudMask);
}
// Apply the cloud mask to the image
var maskedImage = maskClouds(rgbImage);
Step 4: Visualizing the Cloud-Masked Image
Display the cloud-masked image in the GEE Code Editor to verify the results. This ensures that cloud pixels are excluded from further analysis.
// Visualize the masked image
Map.centerObject(ee.Geometry.Point([-122.082, 37.422]), 10);
Map.addLayer(maskedImage, {bands: ['SR_B4', 'SR_B3', 'SR_B2'], max: 0.3}, 'Cloud Masked Image');
FAQ
What is the QA_PIXEL band?
The QA_PIXEL band in Landsat 8 stores metadata about the image quality, including cloud, cloud shadow, and snow presence. Bitwise operations on this band allow precise identification of cloudy areas.
Can I use a different cloud masking method in GEE?
Yes. While the QA_PIXEL method is common, you can also use machine learning models or pre-built cloud cover products like the Landsat Surface Reflectance Cloud Cover Assessment (SR_CLOUD_QA).
How do I handle partially cloudy images?
For partially cloudy images, adjust the mask threshold or use a combination of QA bits. For example, refine the mask to exclude pixels with cloud probabilities above a certain value using additional filters.
Why does the mask not remove all clouds?
Cloud masking may not be 100% accurate due to varying cloud types, sensor limitations, or noise in the QA band. Advanced methods like the Fmask algorithm or external cloud detection tools can improve accuracy.
Is cloud masking required for all Landsat 8 analysis?
Yes. Cloud cover can distort surface reflectance data and skew results. Removing clouds ensures reliable data for tasks like land cover classification, vegetation analysis, or time-series studies.