I am importing 2 different shapefiles from assets to plot the time series chart of MODIS
but I am unable to make the feature collection out of these shapefiles
how to do that?
var Haryana_state = ee.FeatureCollection('users/abhilashaanu92/HaryanaBoundary');
Map.addLayer(Haryana_state);
var Punjab_state = ee.FeatureCollection('users/abhilashaanu92/punjab_state_boundary');
Map.addLayer(Punjab_state);
// Combine features into a feature collection.
var both_states = ee.FeatureCollection([Haryana_state, Punjab_state]).flatten();
Map.addLayer(both_states);
// Load MODIS vegetation indices data and subset annual images.
var vegIndices = ee.ImageCollection('MODIS/006/MOD13A1')
.filter(ee.Filter.date('2019-01-01', '2020-01-01'))
.select(['NDVI', 'EVI']);
// Define the chart and print it to the console.
var chart =
ui.Chart.image
.seriesByRegion({
imageCollection: vegIndices,
band: 'NDVI',
regions: both_states,
reducer: ee.Reducer.mean(),
scale: 500,
seriesProperty: 'label',
xProperty: 'system:time_start'
})
.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(chart);
if I put the name of a single shapefile variable in the regions then it is working but when I combine the 2 shapefiles into a feature collection then it is showing the error.
How to rectify this?
I want output something like this. (the time series of both the states on the same chart).
Two things need to be done here. First, add .flatten()
after combining the two FeatureCollections.
This way, you make a collection of features (FeatureCollection), which is desired. Otherwise, you end up with a collection of FeatureCollections, which prompts the error.
Second, the seriesProperty
needs to match the label of your FeatureCollection. In this case, 'STATE_NAME'. You can check this by adding print(both_states) to check how your new FeatureCollection looks like. I've updated the code.
var Haryana_state = ee.FeatureCollection('users/abhilashaanu92/HaryanaBoundary');
Map.addLayer(Haryana_state);
var Punjab_state = ee.FeatureCollection('users/abhilashaanu92/punjab_state_boundary');
Map.addLayer(Punjab_state);
// Combine features into a feature collection.
var both_states = ee.FeatureCollection([Haryana_state, Punjab_state]).flatten();
print('Check the properties; this will tell you what seriesProperty to use', both_states)
Map.addLayer(both_states);
// Load MODIS vegetation indices data and subset annual images.
var vegIndices = ee.ImageCollection('MODIS/006/MOD13A1')
.filter(ee.Filter.date('2019-01-01', '2020-01-01'))
.select(['NDVI', 'EVI']);
// Define the chart and print it to the console.
var chart =
ui.Chart.image
.seriesByRegion({
imageCollection: vegIndices,
band: 'NDVI',
regions: both_states,
reducer: ee.Reducer.mean(),
scale: 500,
seriesProperty: 'STATE_NAME',
xProperty: 'system:time_start'
})
.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(chart);