Google Earth Engine – How to Fix Cloud Masking Function Giving Null Values in Google Earth Engine

geemapgoogle-earth-enginegoogle-earth-engine-javascript-api

I'm trying to do cloud masking using sentinel but As you can observe, there are pixels that are ‘black’ (or dark) especially on the cloud areas. These are potential pixels with ‘NULL’ values (values could be N/A, NULL, or -99999) and I don’t want that because it will mess up the processing later on.

Can you check whether it has null values or not?

To avoid this dilemma, try to increase the threshold of the cloud percentage.

//Import Sentinel-2 Imagery (LEVEL -2A)
var s2 =ee.ImageCollection("COPERNICUS/S2_SR");

//Choose a polygon over study area
var polygon = ee.Geometry.Rectangle([124.776,10.885,125.044,11.199]);
Map.addLayer(polygon);

//filter s2 collection with polygon and dates
var s2spatfilt= s2. filterBounds(polygon);
print('spatfilt',s2spatfilt);

//Sentinel-2 Imagery (Level -2A) collection intersecting a specific point.
var col =ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(ee.Geometry.Rectangle(124.766,10.885,125.044,11.1990));

//Post 2019. Filter the collection by date using date strings.
print('May 01 to June 26 images,2019', col.filterDate('2019-05-01','2019-06-26'))

//Post 2020. Filter the collection by date using date strings.
print('May 06 images,2020', col.filterDate('2020-05-06','2020-05-30'))

//Function to mask clouds using the Sentinel-2 QA band
function maskS2clouds(image) {
var qa = image.select('QA60');

// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;

// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));

return image.updateMask(mask).divide(10000);
}

// Map the function over one month of data and take the median.
// Load Sentinel-2 TOA reflectance data.
var dataset = ee.ImageCollection('COPERNICUS/S2')
.filterBounds(ee.Geometry.Rectangle(124.766,10.885,125.044,11.199))
.filterDate('2020-04-06', '2020-04-10')
.filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 10)
.map(maskS2clouds)
.median();

var rgbVis = {
min: 0.0,
max: 0.3,
bands: ['B4','B3','B2'],
}
Map.centerObject(s2spatfilt,10);
Map.addLayer(dataset, rgbVis, 'RGB');

Best Answer

Try to remove the cloud mask function

//Import Sentinel-2 Imagery (LEVEL -2A)
var s2 =ee.ImageCollection("COPERNICUS/S2_SR");

//Choose a polygon over study area
var polygon = ee.Geometry.Rectangle([124.776,10.885,125.044,11.199]);
Map.addLayer(polygon);

//filter s2 collection with polygon and dates
var s2spatfilt= s2. filterBounds(polygon);
print('spatfilt',s2spatfilt);

//Sentinel-2 Imagery (Level -2A) collection intersecting a specific point.
var col =ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(ee.Geometry.Rectangle(124.766,10.885,125.044,11.1990));

//Post 2019. Filter the collection by date using date strings.
print('May 01 to June 26 images,2019', col.filterDate('2019-05-01','2019-06-26'))

//Post 2020. Filter the collection by date using date strings.
print('May 06 images,2020', col.filterDate('2020-05-06','2020-05-30'))

//Function to mask clouds using the Sentinel-2 QA band
function maskS2clouds(image) {
var qa = image.select('QA60');

// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;

// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));

return image.updateMask(mask).divide(10000);
}

// Map the function over one month of data and take the median.
// Load Sentinel-2 TOA reflectance data.
var dataset = ee.ImageCollection('COPERNICUS/S2')
.filterBounds(ee.Geometry.Rectangle(124.766,10.885,125.044,11.199))
.filterDate('2020-04-06', '2020-04-10')
.filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 10)
// .map(maskS2clouds) Remove this function from here
.median();

var rgbVis = {
min: 0,
max: 3000, // Set Maximum 3000
bands: ['B4','B3','B2'],
}
Map.centerObject(s2spatfilt,10);
Map.addLayer(dataset, rgbVis, 'RGB');
Related Question