# Calculating mean NDVI of two different locations

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 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) {
}

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')
}

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);

//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)
``````

Result I want is like this for both shapefiles.

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

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) {
}
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')
}

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);
``````