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.