7

I am exploring Sentinel-2 time-series NDVI with Google Earth Engine. In another post (here) I calculated and exported NDVI values from Sentinel-2. Now I would like to create a NDVI time series chart, but when I run the code (see below) I get

Computation timed out.

error in the console tab. I have no idea why.

Here is the code:

//Import GEE Feature Collection (Somaliland kml)
    var geometry = ee.FeatureCollection('ft:1bzF2pepsJHoGIXn-ojYYOkOBHWtfhqzS9QLuz_l3');

// Create image collection of S-2 imagery for the perdiod 2016-2018
var S2 = ee.ImageCollection('COPERNICUS/S2')

//filter start and end date
.filterDate('2018-09-01', '2018-10-31')

//filter according to drawn boundary
.filterBounds(geometry);

// Function to mask cloud from built-in quality band
// information on cloud
var maskcloud1 = function(image) {
var QA60 = image.select(['QA60']);
return image.updateMask(QA60.lt(1));
};

// Function to calculate and add an NDVI band
var addNDVI = function(image) {
return image.addBands(image.normalizedDifference(['B8', 'B4']));
};

// Add NDVI band to image collection
var S2 = S2.map(addNDVI);

// Extract NDVI band and create NDVI median composite image
var NDVI = S2.select(['nd']);
var NDVI = NDVI.median();

// Create palettes for display of NDVI
var ndvi_pal = ['#d73027', '#f46d43', '#fdae61', '#fee08b', '#d9ef8b',
'#a6d96a'];

// Create a time series chart.
var plotNDVI = ui.Chart.image.seriesByRegion(S2, geometry,ee.Reducer.mean(),'nd',500,'system:time_start', 'system:index')
              .setChartType('LineChart').setOptions({
                title: 'NDVI short-term time series',
                hAxis: {title: 'Date'},
                vAxis: {title: 'NDVI'}
});

// Display.
print(plotNDVI);

// Display NDVI results on map
Map.addLayer(NDVI.clip(geometry), {min:-0.5, max:0.9, palette: ndvi_pal}, 'NDVI');
13

You are using the variable NDVI to compute the chart, but this variable is a single image, because you applied median(). If you want a time series plot you have to compute the chart using the NDVI collection before calculating median.

Therefore, I just change the name of the median

//Import GEE Feature Collection (Somaliland kml)
    var geometry = ee.FeatureCollection('ft:1bzF2pepsJHoGIXn-ojYYOkOBHWtfhqzS9QLuz_l3');

// Create image collection of S-2 imagery for the perdiod 2016-2018
var S2 = ee.ImageCollection('COPERNICUS/S2')

//filter start and end date
.filterDate('2018-09-01', '2018-10-31')

//filter according to drawn boundary
.filterBounds(geometry);

// Function to mask cloud from built-in quality band
// information on cloud
var maskcloud1 = function(image) {
var QA60 = image.select(['QA60']);
return image.updateMask(QA60.lt(1));
};

// Function to calculate and add an NDVI band
var addNDVI = function(image) {
return image.addBands(image.normalizedDifference(['B8', 'B4']));
};

// Add NDVI band to image collection
var S2 = S2.map(addNDVI);
// Extract NDVI band and create NDVI median composite image
var NDVI = S2.select(['nd']);
var NDVImed = NDVI.median(); //I just changed the name of this variable ;)

// Create palettes for display of NDVI
var ndvi_pal = ['#d73027', '#f46d43', '#fdae61', '#fee08b', '#d9ef8b',
'#a6d96a'];

// Create a time series chart.
var plotNDVI = ui.Chart.image.seriesByRegion(S2, geometry,ee.Reducer.mean(),
'nd',500,'system:time_start', 'system:index')
              .setChartType('LineChart').setOptions({
                title: 'NDVI short-term time series',
                hAxis: {title: 'Date'},
                vAxis: {title: 'NDVI'}
});

// Display.
print(plotNDVI);

// Display NDVI results on map
Map.addLayer(NDVImed.clip(geometry), {min:-0.5, max:0.9, palette: ndvi_pal}, 'NDVI');

And I got the following:

enter image description here

4
  • maskcloud1 function is not being applied in this code, is it? May 9 '20 at 18:18
  • 1
    You are right. As it was not applied in the code provided by Michele Bolognesi, I did no t apply it myself. To apply the mask you just have to type var S2 = S2.map(maskcloud1);
    – HMSP
    May 11 '20 at 6:34
  • Can we code to export the table to a drive instead of producing the chart? May 11 '20 at 21:53
  • 1
    Absolutely, check this out to see the different options available: developers.google.com/earth-engine/exporting
    – HMSP
    May 12 '20 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.