Skip to content

Instantly share code, notes, and snippets.

@kvutien
Last active January 11, 2021 09:11
Show Gist options
  • Save kvutien/97be53cd7694d30ff06ddfaa742a399e to your computer and use it in GitHub Desktop.
Save kvutien/97be53cd7694d30ff06ddfaa742a399e to your computer and use it in GitHub Desktop.
Complete GEE Code Editor app inclusing BestNDVIMosaic core
/*
* @notice fully functional GEE JavaScript for Code Editor
* @notice Generate a mosaic using pixels of the best NDVI of all images of a month
* @notice Do it over all same months of a period of time, for example March (or August, or December)
* @notice ... useful to remove clouds and calculate monthly NDVI stats over several years
* @author Vu Tien Khang, licence MIT, Jan 2021
*/
// prepare the refrence set of images, here it's Sentinel-2 MSI: MultiSpectral Instrument, Level-1C
var myPoint = ee.Geometry.Point(78.2875, 10.4156); // Sendurai, Tamil Nadu, India
Map.centerObject(myPoint, 13); // close zoom around central point, zoom scale 13
var roi = myPoint.buffer(10000); // radius 10 km to limit image Collection
var refSetS2 = ee.ImageCollection('COPERNICUS/S2').filterBounds(roi);
var studyMonth = 4; // month over which the composite mosaic is done
var years = ee.List.sequence(2017, 2020);
var bestNDVICollection = years.map(makeBestImage(studyMonth, refSetS2));
print('bestNDVICollection', bestNDVICollection);
var visuVeg = {bands:['B11', 'B8', 'B4'], min: 500, max: 3000, opacity: 1};
Map.addLayer(ee.Image(bestNDVICollection.get(0)), visuVeg,'test 2017');
Map.addLayer(ee.Image(bestNDVICollection.get(1)), visuVeg,'test 2018');
Map.addLayer(ee.Image(bestNDVICollection.get(2)), visuVeg,'test 2019');
Map.addLayer(ee.Image(bestNDVICollection.get(3)), visuVeg,'test 2020');
//-------------------------
function makeBestImage(studyMonth, refSetS2){
/*
* @notice Function used by map() to make a list of mosaic images using pixels with the best NDVI
* @param {ee.Number} studyMonth: month to generate ee.DateRange (1 to 12)
* @param {ee.imageCollection} refSetS2: full set of images, several years
* @dev {function} addNDVI: function to add NVI channel named 'NDVI' to an image
* @author Vu Tien Khang, licence MIT, Jan 2021
*/
return function(year){
var mosaicStartDate = ee.Date.fromYMD(year, studyMonth, 1);
var range2Mosaic = ee.DateRange(mosaicStartDate, mosaicStartDate.advance(1, 'month'));
var filtered = refSetS2.filterDate (range2Mosaic);
var with_NDVI = filtered.map (addNDVI("B8", "B4"));
return with_NDVI.qualityMosaic ('NDVI')
.set('system:time_start', with_NDVI.first().get('system:time_start'));
};
}
//---------------------------
function addNDVI(NIR, RED){
/*
* @notice Function used by map() to add to "image" the band NDVI (normalized difference NIR and Red)
* @param {ee.String} NIR: name of band NIR in image, depends on the satellite
* @param {ee.String} RED: name of band RED in image, depends on the satellite
* @author Vu Tien Khang, licence MIT, Jan 2021
*/
return function (image) {
var ndvi = image.normalizedDifference ([NIR, RED]).rename ('NDVI');
return image.addBands (ndvi);
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment