0

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 />

1 Answer 1

0

I would suggest using this tutorial to filter clouds for Sentinel 2 imagery: https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2_CLOUD_PROBABILITY

The QA band of Sentinel 2 masks out of lot of clear imagery as cirrus and misses many obvious clouds.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 26, 2022 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.