1

I have tried to select only shrubs and grass on NDVI but on plotting chart it shows this error. Here is the code.

var  feat =ee.Geometry.Point([33.15865673674562, -5.173302054347385]);
    var s2a = ee.ImageCollection('COPERNICUS/S2_SR')
                        
                        .filter(ee.Filter.date('2019-01-01', '2020-12-30'))
                        
                  .select('B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B11','B12')
                        .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
                        .map(function(s2a){return s2a.clip(feat)});
       Map.setCenter (6.3690,34.8888,5);

        var s2a_median = s2a.median()
                          .clip(feat);
        var nir = s2a_median.select('B8');
        var red = s2a_median.select('B4');
        var ndvi = nir.subtract(red).divide(nir.add(red)).rename('NDVI');
              // Display the result.
        var ndvi2 =ndvi
                  .where(ndvi.gt(0.18).and(ndvi.lte(0.22)), 2)
                  .where(ndvi.gt(0.22).and(ndvi.lte(0.27)), 3);
        
        
    // var wrd=ndvi2.clip(feat)
        
    var subndvi=ndvi2.clip(feat)
    // var NDVI = s2a.map(
    // function(img) {
    //   // var ndvi2 =ndvi
    //   //             .where(ndvi.gt(0.18).and(ndvi.lte(0.22)), 2)
    //   //             .where(ndvi.gt(0.22).and(ndvi.lte(0.27)), 3);
    //     return img.normalizedDifference(['B8','B4'])
    //               .rename('NDVI')
    //               .copyProperties(img, ['system:time_start']);
    // });
    Map.addLayer(subndvi,{min: -1000, max: 5000, palette: ['00FF00','000000']},'Sub County',true)
    

       // Create a NDVI chart.
        var ndviChart2 = ui.Chart.image.series(subndvi, feat, ee.Reducer.mean(), 1000);
        ndviChart2.setOptions({
      title: 'Mean NDVI of area',
      vAxis: {title: 'NDVI', maxValue: 1},
      hAxis: {title: 'date', format: 'MM-yy', gridlines: {count: 7}},
      maxPixels:90e9,
  });

Error is! The

Error generating chart: No features contain non-null values of "system:time_start".

1 Answer 1

1

There are some issues in your code. Apparently, you are only interested in NDVI values in one single point represented by feat. So, you need one value for each image in Image Collection. You have only one image to do that so, subndvi is only one image; not a time series. For visualization purpose you need an area; not a point. In this case, you also need to use NDVI values assigned to shrubs and grass for obtaining the chart; not class values. Following code fixes your main issues.

var geometry = ee.Geometry.Polygon(
        [[[33.072139402761245, -5.103420275598498],
          [33.072139402761245, -5.260703195158846],
          [33.278133055104995, -5.260703195158846],
          [33.278133055104995, -5.103420275598498]]], null, false);

var feat = ee.Geometry.Point([33.15865673674562, -5.173302054347385]);

Map.centerObject(feat, 12);
Map.addLayer(feat);

var s2a = ee.ImageCollection('COPERNICUS/S2_SR')
                  .filter(ee.Filter.date('2019-01-01', '2020-12-30'))
                  .select('B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B11','B12')
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
                  .filterBounds(feat);

print(s2a);

//ndvi calculation

var subndvi = s2a.map(function (image) {
  
  var date = image.get('system:time_start');
  
  var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
  
  var ndvi2 = ndvi.gt(0.18).and(ndvi.lte(0.27))
                           .multiply(ndvi);
  
  return ndvi2.set('system:time_start', date);
  
});

var test = subndvi.median()
                  .clip(geometry);

var imageVisParam = {"opacity":1,
                     "bands":["NDVI"],
                     "min":0,"max":0.27,
                     "palette":["ffffff","f2ff33","ffa41b","ff5c0e","2dff34","1d58ff"]};

Map.addLayer(test, imageVisParam, 'test');

 // Create a NDVI chart.
var ndviChart2 = ui.Chart.image.series(subndvi, feat, ee.Reducer.mean(), 1000);
      ndviChart2.setOptions({
      title: 'Mean NDVI of area',
      vAxis: {title: 'NDVI', maxValue: 1},
      hAxis: {title: 'date', format: 'MM-yy', gridlines: {count: 7}},
      maxPixels:90e9,
  });
  
print(ndviChart2);

After running above code in GEE code editor, it can be observed in following picture that shrubs and grass appear in specific intervals in your date range.

enter image description here

1
  • Thanks for the help it worked. I had another question. can I add another line of dummy data to the same chart of this Ndvi please. So that we have two lines( one for ndvi and one for dummy data) Apr 26 at 17:41

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.