-3
var region = ee.FeatureCollection ('users/basjangoosen/Province_New_SANeighbours')
              .filter(ee.Filter.eq('PROVINCE', 'Eastern Cape'));
print('The region is:', region)
Map.addLayer(region)



var rawData = ee.ImageCollection('COPERNICUS/S2_SR')
                .filterDate('2021-01-01', '2021-12-31')
                .select(0,1,2,3,4,5,6,7,8,9,10,11)//.median()
                .filter(ee.Filter.bounds(region));
                
                

                
//CLip function

var clippedSentinel = function(image){
  return image.clip(region);
}

var clipped = rawData.map(clippedSentinel);
print('Clipped image information', clipped);
Map.addLayer(clipped);



var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
  return image.addBands(ndvi);
};

// Test the addNDVI function on a single image.

var band_ndvi = rawData.map(addNDVI).select('NDVI');
print('NDVI, infromation: ', band_ndvi)


//NBI Function
var addNBI = function(image) {
  var nbi = image.normalizedDifference(['B8', 'B12']).rename('NBI');
  return image.addBands(nbi);
}
  
var band_nbi = rawData.map(addNBI).select('NBI')
print('NBI, infromation: ', band_nbi)

var band1 = band_nbi;
var band2 = band_ndvi;


var chart = ui.Chart.image.series({
  imageCollection: [band1, band2],
  region: rawData,
  reducer: ee.Reducer.median(),
  scale: 1000,
  xProperty: 'system:time_start'
}).setSeriesNames(['NBI', 'NDVI'])
  .setOptions({
        lineWidth: 5,
        colors: ['e37d05', '1d6b99'],
        curveType: 'function',
        title: 'Average NBI & NDVI Values for The Western Cape',
        interpolateNulls: true,
        vAxis: {title: 'Vegetation Index Values', titleTextStyle: {italic: false, bold: true}},
        hAxis: {title: 'Date', format: 'YYYY-MMM', titleTextStyle: {italic: false, bold: true}}
      });
print(chart);
1
  • Welcome to GIS SE. As a new user, please take the Tour. This is an example of a "Wall of code" post. They are downvoted and closed because the Question contains no description of the environment, task, and problem, without which others are unlikely to find the Question (and therefore the Answer). It's often best to write the entire Question, the go back and craft a descriptive sentence fragment the summarizes the content as a title. Please Edit the Question.
    – Vince
    Mar 6, 2022 at 13:43

1 Answer 1

2

A bunch of things were wrong when plotting your timeseries of NDVI and NBI so I edited the code:

var chart = ui.Chart.image.series({
  imageCollection: band_nbi.select(['NBI', 'NDVI']).limit(25),
  region: region,
  reducer: ee.Reducer.median(),
  scale: 1000,
  xProperty: 'system:time_start'
})

Also the feature collection wasn't shared so I used a geometry to show the result. Be aware the plotting is not going to work for the entire timeseries you plan, that's why I use .limit(25) for the moment.

https://code.earthengine.google.com/9c04a69de6643c3faef5af66f3ac92ed

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