1

I am trying to calculate mean NDVI of two location over a chart. I combined the two shapefiles successfully. Then I try to generate time series chart for both locations but only I obtained for one location. What i am trying to do both locations separate mean Ndvi visualization in chart?

Code:

var extensiv = ee.FeatureCollection("users/Muhammad_Waqas/Location1");
var intensiv = ee.FeatureCollection("users/Muhammad_Waqas/Location2");

var classes = new ee.FeatureCollection([extensiv,intensiv]);
var classes_flatten = classes.flatten();
var dataset = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
    .filterDate('2013-05-01', '2021-12-31').filterBounds(extensiv);

// Applies scaling factors.
function applyScaleFactors(image) {
  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);
  return image.addBands(opticalBands, null, true)
              .addBands(thermalBands, null, true);
}

dataset = dataset.map(applyScaleFactors);
var table_bounds = function(image) {
  // Crop by table extension
  return image.clip(extensiv);
};
var NDVIClipped = dataset.map(table_bounds);

 var ndvi_func = function (i) {
      var ndvi = i.normalizedDifference (['SR_B5', 'SR_B4']).rename ('NDVI')
      return i.addBands(ndvi);
    }

    var image_ndvi = NDVIClipped.map(ndvi_func);

    //Calculating year wise NDVI
    var year = ee.List.sequence(2013,2021);
    var year_func = function(y){
      var range = ee.Filter.calendarRange (y, y, 'year');
      return image_ndvi.select('NDVI').filter(range).mean().set ('Year', y)
    };
    var yearwise_ndvi = ee.ImageCollection(year.map(year_func));
    print (yearwise_ndvi);
    Map.addLayer (yearwise_ndvi)


//Creating time-series chart:
var chart = ui.Chart.image.seriesByRegion ({
  imageCollection: yearwise_ndvi,
  regions: extensiv,
  reducer: ee.Reducer.mean(),
  scale: 30,
  xProperty: 'Year'
}).setOptions ({title: "NDVI over time",
  hAxis: {title: 'Time of the year', format: 'year'}
});
print(chart)

Code Link. enter link description here

Result I want is like this for both shapefiles.

Result I want

1
  • The link you shared appears to be broken, could you fix it?
    – M. Nicolas
    Commented Apr 12, 2022 at 21:56

1 Answer 1

0

I solve this problem using this code.

var provinces= table.merge(table2);
print(provinces);
var dataset = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
    .filterDate('2021-05-01', '2021-12-31').filterBounds(provinces);
// Applies scaling factors.
function applyScaleFactors(image) {
  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);
  return image.addBands(opticalBands, null, true)
              .addBands(thermalBands, null, true);
}
dataset = dataset.map(applyScaleFactors);
var table_bounds = function(image) {
  // Crop by table extension
  return image.clip(provinces);
};
var NDVIClipped = dataset.map(table_bounds);

 var ndvi_func = function (i) {
      var ndvi = i.normalizedDifference (['SR_B5', 'SR_B4']).rename ('NDVI')
      return i.addBands(ndvi).copyProperties(i,['system:time_start','system:time_end']);
    }

    var image_ndvi = NDVIClipped.map(ndvi_func);

var NDVIchart = 
    ui.Chart.image
        .seriesByRegion({
          imageCollection: image_ndvi,
          // these are two geometries
          regions: provinces,
          reducer: ee.Reducer.mean(),
          scale: 300,
          //seriesProperty: 'label',
          xProperty: 'system:time_start'
        }).setSeriesNames(['Truman', 'Heffan'])
        .setOptions({
          title: 'Average NDVI Value by Date',
          hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true}},
          VAxis: {
            title: 'NDVI (x1e4)',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 5,
          colors: ['f0af07','0f8755','76b349'],
        })
print(NDVIchart);
1
  • Please add details how it solved your problem and accept this answer as well.
    – Padmanabha
    Commented Jun 30, 2023 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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