#1: create a cloud free composite

A modular land cover system part 1

Step 1: Create a new repository and call it mrc.


Step 2: create a new script and call it importS2_module

Step 3: copy the code below and store it in the script

var inBands = ee.List(['QA60','B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B10','B11','B12']);
var outBands = ee.List(['QA60','cb','blue','green','red','re1','re2','re3','nir','re4','waterVapor','cirrus','swir1','swir2']);

var CloudCoverMax = ??

exports.importData = function(studyArea,startDate,endDate) {

// Get Sentinel-2 data
var s2s = ee.ImageCollection('COPERNICUS/S2')

  function scaleBands(img){
    var prop = img.toDictionary()
    var t = img.select(['B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B10','B11','B12']).divide(10000);
    t = t.addBands(img.select(['QA60'])).set(prop).copyProperties(img,['system:time_start','system:footprint'])
  return ee.Image(t);

s2s = s2s.map(scaleBands);
s2s = s2s.select(inBands,outBands) ;

return s2s;

Step 4: Create a new script and call it main

// import required modules
var importS2 = require("users/../mrc:importS2_module");

// import shapefile data
var cambodia = ee.FeatureCollection("users/.../.../KHM_adm1");
var KampongThum = cambodia.filter(ee.Filter.eq("NAME_1","...")).geometry();

// set the variables
var CloudCoverMax = ...;
var startDate = ...;
var endDate = ...;
var studyArea = ...;

print("getting images");
var s2 = importS2.importData(studyArea,startDate,endDate);
print("found ",s2.size(),"images");
Map.addLayer(ee.Image(s2.first()),{min:0,max:0.6,bands:"swir2,nir,red"},"first image");

Step 5: Run the script



  1. Hi, thank you for this material, I’m learning a lot from that. I’m trying this series #1-#6 but I have a problem on the first part of the script.
    I’m changing the studyArea with mine parameters, I tried with many, and every time the maps displayed are a small ” trapezium” outside the area I choose for analysis, it has the same shape of your study area. What’s causing that?

  2. It shows the first image in your collection. the composite shows the median of all images.. but takes time to calculate. You could export it. you also might want to review the parameters in the cloud and shadow modules..

  3. Ok thank you so much for your fast reply. So I’ll try to export the composite and using it following your classification post! About the cloud parameters, Why did you choose to keep this parameter editable other script and not only in “main”? I mean, I can be wrong and set 3 different values in each script.

Leave a Reply