I am working on NDVI time series analysis using Sentinal data and also classifying the data it will give some error and not working and how can i get the chart of three different features in single chart

link script

// Function to mask clouds using the Sentinel-2 QA band
/// * @param {ee.Image} image Sentinel-2 image
/// * @return {ee.Image} cloud masked Sentinel-2 image
function maskS2clouds(image) {
  var qa = image.select('QA60');
  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;
  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
  return image.updateMask(mask).divide(10000);
var S2 = ee.ImageCollection('COPERNICUS/S2')

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

//filter according to drawn boundary
 // Pre-filter to get less cloudy granules.
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
// 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();

// Create palettes for display of NDVI
//var ndvi_pal = ['#d73027', '#f46d43', '#fdae61', '#fee08b', '#d9ef8b',
var ndviParams = {min: -1, max: 1, palette: ['blue','white','green' ]};
Map.addLayer(NDVImed, ndviParams, 'NDVI image');

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


// Display.

// Display NDVI results on map

var newfc = wheat.merge(must).merge(beng);
// print(newfc);

var training = NDVI.select(NDVI).sampleRegions({
  collection: newfc, 
  properties: ['id'], 
  scale: 30
// print(training)

var classifier = ee.Classifier.cart().train({
  features: training, 
  classProperty: 'id', 
  inputProperties: B1

var classified = image.select(bands).classify(classifier);
Map.addLayer(classified, {min: 0, max: 2, palette: ['red', 'blue', 'green']});

The first part of your code works when you return this from the cloudmask function:

ee.Image(image.updateMask(mask)).divide(10000).addBands(qa, null, true)
                .copyProperties(image).set("system:time_start", image.get("system:time_start"));

That is because when you use devide or multiply, the image porperties will get lost from your image. So therefore they could not be plotted in the chart.

For the second classification part, you probably want to map over the full collcetion to do the calssification on a per-image basis. However, you should then make sure that your points selected represent the classes over the full study period. Containment by clouds/shadows is very likely to happen.

// bands to use the classification for
var bands = S2.first().bandNames().slice(1,12);
// Think about which image you want to use as input for the classifier: 
// Do you have one 'perfect' image? Or do you want to classify based on every single image?

// map over the image collection, do the classification per-image
var classified = S2.map(function(image){
  var training = image.select(bands).sampleRegions({ collection: newfc, properties: ['id'], scale: 30 }); // print(training)
  var classifier = ee.Classifier.cart().train({ features: training, classProperty: 'id'}); 
  var classImage = image.select(bands).classify(classifier).rename('classified');
  return image.addBands(classImage);


| improve this answer | |
  • In above question i want to classify the image using all NDVI images not from sentinal image – Neeraj Pandey Feb 25 '19 at 7:36

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.