0

I am trying to use Google Earth Engine (GEE) to remove the cloud and replaced it with other sentinel2 images. The codes below start from loading the images, mosaicing them, and applying cloud masking to generate the new result

"s2large2016" is the original image, and "s2large2016c" is the one that removes the cloud, where "largearea" is a shapefile I imported.

However, the result shows a totally black map of my selected area "largearea", and it says "Cannot read properties of undefined (reading 'setVisible')" once I clicked the layer "s2large2016c", though the first part works successfully.

Does anyone know what's wrong with my code?

var s2large2017 = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterDate('2017-05-01','2017-08-31')
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 2)) 
  .filterBounds(largearea)
  .select(['B2', 'B3', 'B4', 'B5', 'B7', 'B8', 'B8A'])
  .mosaic()
  .clip(largearea);

Map.addLayer(s2large2017, imageVisParam, '2017clouds')
Map.centerObject(s2large2017,10)

// 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 the masked and scaled data, without the QA bands.
  return image.updateMask(mask).divide(10000)
      .select("B.*")
      .copyProperties(s2large2017, ["system:time_start"]);
}

// Map the function over one year of data and take the median.
// Load Sentinel-2 TOA reflectance data.
var s2large2017c = ee.ImageCollection('COPERNICUS/S2_SR')
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 2)) 
  .filterDate('2017-01-01','2017-12-31')
  .filterBounds(largearea)
  .select(['B2', 'B3', 'B4', 'B5', 'B7', 'B8', 'B8A','QA60'])
  .map(maskS2clouds)
  .mosaic()
  .clip(largearea);

Map.addLayer(s2large2017c, imageVisParam, '2017noclouds');
Map.centerObject(largearea, 10) 

//To solve this problem, I also chose to use another way to remove the clouds, which is using s2ClearSky I saw from //https://gis.stackexchange.com/questions/445556/different-methods-for-masking-clouds-of-sentinel-2-images-in-gee

function = s2ClearSky(image) {
      var scl = image.select('SCL');
      var clear_sky_pixels = scl.eq(4).or(scl.eq(5)).or(scl.eq(6)).or(scl.eq(11));
      return image.updateMask(clear_sky_pixels).divide(10000);
};

//so the code would be

var s2large2017 = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterDate('2017-05-01','2017-08-31')
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 2)) 
  .filterBounds(largearea)
  .select(['B2', 'B3', 'B4', 'B5', 'B7', 'B8', 'B8A'])
  .mosaic()
  .clip(largearea);

Map.addLayer(s2large2017, imageVisParam, '2017clouds')
Map.centerObject(s2large2017,10)

function s2ClearSky(image) {
      var scl = image.select('SCL');
      var clear_sky_pixels = scl.eq(4).or(scl.eq(5)).or(scl.eq(6)).or(scl.eq(11));
      return image.updateMask(clear_sky_pixels).divide(10000);
}

// Map the function over one year of data and take the median.
// Load Sentinel-2 TOA reflectance data.
var s2large2017c = ee.ImageCollection('COPERNICUS/S2_SR')
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 2)) 
  .filterDate('2017-01-01','2017-12-31')
  .filterBounds(largearea)
  .select(['B2', 'B3', 'B4', 'B5', 'B7', 'B8', 'B8A','QA60'])
  .map(s2ClearSky)
  .mosaic()
  .clip(largearea);

Map.addLayer(s2large2017c, imageVisParam, '2017noclouds');
Map.centerObject(largearea, 10)```

and the result is the same.
0

1 Answer 1

0

After trying a lot of ways, I finally found the problem, which came from ''Map.centerObject'', ''mosaic()'' at the second part. Instead of mosaic, I replaced it by ''median'', though I still don't know if this function also merge the imagecollection together. Besides, I removed the ''Map.centerObject'', so the final code would be like this:

  • largearea, is the geometry shp. I uploaded
  • imageVisParam, is the RGB band combination
  • s2large2016, is original imagery with clouds
  • s2large2016c, is the final result without clouds
var s2large2016 = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterDate('2016-05-01','2016-08-31')
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 2)) 
  .filterBounds(largearea)
  .select(['B2', 'B3', 'B4', 'B5', 'B7', 'B8', 'B8A'])
  .mosaic()
  .clip(largearea);

Map.addLayer(s2large2016, imageVisParam, '2016clouds')
Map.centerObject(s2large2016,10)

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 the masked and scaled data, without the QA bands.
  return image.updateMask(mask).divide(10000)
      .select("B.*")
      .copyProperties(image, ["system:time_start"])
}

// Map the function over one year of data and take the median.
// Load Sentinel-2 TOA reflectance data.
var s2large2016c = ee.ImageCollection('COPERNICUS/S2')
    .filterDate('2016-01-01', '2016-12-31')
    // Pre-filter to get less cloudy granules.
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
    .map(maskS2clouds)
    .filterBounds(largearea)
    .median()
    .clip(largearea)
    

// Display the results.
Map.addLayer(s2large2016c, {bands: ['B4', 'B3', 'B2'], min: 0, max: 0.3}, '201');

Your Answer

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

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