Google Earth Engine For Image Analysis
Creating NDVI Layer and calculating its moving average
//Sentinel-2 TOA reflectance Image
var S2 = ee.ImageCollection('COPERNICUS/S2');
// Function to mask clouds using the Sentinel-2 QA band.
function maskS2clouds(image) {
// Get QA bands with 60 meters resolution
var qa = image.select('QA60');
var cloudBitMask = ee.Number(2).pow(10).int();
var cirrusBitMask = ee.Number(2).pow(11).int();
// Create mask using the Bit-Wise 'AND' Operator
var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(
qa.bitwiseAnd(cirrusBitMask).eq(0));
// Return the masked and scaled image
return image.updateMask(mask).divide(10000)
.copyProperties(image, ["system:time_start"]);
}
// Filter and pre-process
var S2_preProcessed = S2.filterDate('2017-01-01', '2017-12-31')
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 3))
.map(maskS2clouds)
.median() // Return the Median Value
//Bands with 10 meters resolution
var bands = ['B2', 'B3', 'B4', 'B8'];
// Select bands and location of the study area
var image = S2_preProcessed.select(bands)
.clip(study_area);
// Visualize False-Color Composite
Map.addLayer(image,
{bands: ['B8', 'B4', 'B3'],
max: 0.4}, 'Sentinel 2');
// NDVI Layer
var ndvi_layer = image.normalizedDifference(['B8', 'B4'])
// Visualize NDVI
Map.addLayer(ndvi_layer,
{min: -1, max: 1,
palette: ['red', 'yellow', 'green']},
'NDVI');
// Run NDVI on image with no cloud mask
var ndvi_report = S2.filterDate('2017-01-01', '2017-12-31')
.map(addNDVI)
// plot the average ndvi value
print(ui.Chart.image.series({
imageCollection: ndvi_report.select('nd'),
region: study_area,
reducer: ee.Reducer.mean()
}))
Sanjay Gandhi National Park