Google earth engine:calculating the NDVI from Sentinel 2

I’m new and I want to extract the NDVI from Sentinel 2 pictures. Could you give me the script?

@syrine Please install SNAP ToolBox for Sentinel 2 if you haven’t. There is a module in SNAP to calculate NDVI. I also found a link to the tutorial Calculate Normalized Difference Vegetation Index (NDVI)

Here is a technical refrence for NDVI on this page : Sentinel 2 NDVI. As per this, the Band 8 of Sentinel 2 corresponds to NIR and Band 4 corresponds to the Red Band.

Hope this helps.

@syrine in case you haven’t figured it out yet:

// S2 image
var image = ee.Image('COPERNICUS/S2/20160701T184340_20160701T221909_T11SKA');

// Visualization parameters 
var visParams = {bands: ['B8', 'B4', 'B3'], max: 3048, gamma: 1};
var visParams_ndvi = {min: -0.2, max: 0.8, palette: 'FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,' +
    '3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301'};

// Calculate NDVI
var image_ndvi = image.normalizedDifference(['B8','B4']);


// Map results
Map.centerObject(image,9)
Map.addLayer(image,visParams,'Sentinel-2 False Color Infrared')
Map.addLayer(image_ndvi,visParams_ndvi,'Sentinel-2 NDVI')

HTH,

Val

3 Likes

@Val did this
var plot = ee.Geometry.Polygon([
// The placemark variable has an array called shape that contains the locations of the corners of the polygon
<#list placemark.shape as coord>[${coord.longitude}, ${coord.latitude}],</#list>]);

// HASTA AQUI ---------------

// Load the Sentinel 2 - This is only available from 2015 onwards. Select the NDVI band. Resolution of the pixels is 10 meters.

var sentinel = ee.ImageCollection(‘COPERNICUS/S2’).
filterDate(startTime, endTime);
// filter(ee.Filter.lte(‘CLOUDY_PIXEL_PERCENTAGE’, 100));

//Function to add NDVI band to Sentinel 2

function addNDVI(image) {
var ndvi = image.normalizedDifference([‘B8’, ‘B4’]);
var newImage = image.addBands(ndvi);
return newImage;
}

//Filter Sentinel data according to the area of interest and print NDVI chart

var filt_sentinel = sentinel.filterDate(startTime, endTime);
var plot_sentinel = filt_sentinel.filterBounds(plot);
var mappedNDVI = plot_sentinel.map(addNDVI);
var SentinelNDVI = Chart.image.series(mappedNDVI.select(‘nd’), plot)
.setOptions({
title: ‘Sentinel 2 NDVI’,
hAxis: {title: ‘Date’ },
vAxis: { title: ‘NDVI’,viewWindowMode: ‘explicit’,viewWindow: {max: 1,min: 0,},gridlines: {count: 5,}}
});

print(SentinelNDVI);
but i don’t know how i should right the place mark

I’m a bit confused by your code. What’s your ultimate goal? Do you want to map the NDVI or just extract and plot NDVI values?

And the placemark, should it be a point location or a polygon?

@Val
i want to plot NDVI values and the location is a polygon.

I suppose you’re getting the polygon coordinates from somewhere outside the GEE playground?
If you have a specific area, you need to define the area properly with ee.Geometry.Polygon or if it’s a rectangle with ee.Geometry.Rectangle … if the area definition is not strict, you could consider drawing a polygon, which would be the easiest and fastest option.

 var aoi = geometry; //hand drawn polygon
 // var aoi = ee.Geometry.Rectangle(xMin, yMin, xMax, yMax) // Rectangle - insert coordinates
 // var aoi = ee.Geometry.Polygon(aLng, aLat, bLng, bLat, ..., aLng, aLat) // Polygon - insert coordinates

Otherwise the code is working … just one thing:

You use filterDate two times, so you can get rid of one.

Best,

Valentin

thank you it’s work you save my life

@Val can you please help me in finding tasseled cap indices for sentinel 2 data using earth engine code.

Could you please the dedicated google earth engine forums to discuss gee issues!
Thanks.