I am trying to use the functions proposed at Creating Sentinel-2 cloud free, cloud-shadow free composite or scene on Google Earth Engine (GEE)? to mask out clouds in a collection and display the results.
However, when I display the images, clouds are not masked.
What am I doing wrong?
Here is my code:
var aoi = <br />
/* color: #d6cfcd */<br />
/* shown: false */<br />
/* displayProperties: [<br />
{<br />
"type": "rectangle"<br />
}<br />
] */<br />
ee.Geometry.Polygon(
[[[19.11805207896173, 68.89159582156138],
[19.11805207896173, 68.53463738722539],
[20.46937043833673, 68.53463738722539],
[20.46937043833673, 68.89159582156138]]], null, false),<br />
s2H = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED");
//Cloud masking function using QA image<br />
// Adapted form http://www.gisandbeers.com/generar-imagenes-satelite-sin-nubes/<br />
function mask_sentinel2(image) {<br />
var qa = image.select('QA60');<br />
//exclude opaque and cirrus clouds from image<br />
var opaque = 1 << 10;<br />
var cirrus = 1 << 11;<br />
var mask = qa.bitwiseAnd(opaque).eq(0)<br />
.and(qa.bitwiseAnd(cirrus).eq(0));<br />
return image.updateMask(mask);<br />
}<br />
<br />
//Center on the study area<br />
Map.setCenter(19.84, 68.68, 8); <br />
<br />
// Select all images within the aoi with specified cloud % (per scene), year and month range<br />
var sdata = s2H.filterBounds(aoi)<br />
.filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 20)<br />
.filterDate('2017-07-01','2022-09-30')<br />
.filter(ee.Filter.calendarRange(7,9,'month'));<br />
<br />
// Print collection to the Console to check number of images, dates etc<br />
print(sdata); <br />
//Mask clouds in the data and print masked collection to the console <br />
var sdata_masked = sdata.map(mask_sentinel2);<br />
print(sdata_masked);<br />
//The following displays all images in true colour, it can take a while<br />
function addImage(image) { // display each image in collection in true colour<br />
var id = image.id;<br />
var image = ee.Image(image.id)<br />
Map.addLayer(image.clip (aoi), {bands: ['B4','B3','B2'],min:0, max:12000, gamma:2.4 });<br />
}<br />
sdata_masked.evaluate(function(sdata_masked) { // use map on client-side<br />
sdata_masked.features.map(addImage);<br />
});<br />