1

I am importing 2 different shapefiles from assets to plot the time series chart of MODIS

imported assets

but I am unable to make the feature collection out of these shapefiles

how to do that?


// 1st Shapefile
var Haryana_state = ee.FeatureCollection('users/abhilashaanu92/HaryanaBoundary');

Map.addLayer(Haryana_state);


// 2nd Shapefile
var Punjab_state = ee.FeatureCollection('users/abhilashaanu92/punjab_state_boundary');

Map.addLayer(Punjab_state);

// Combine both shapefile features into a single feature collection.
var both_states = ee.FeatureCollection([Haryana_state, Punjab_state]);

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.

Error generating chart: Collection.geometry: Unable to use a collection in an algorithm that requires a feature or image. This may happen when trying to use a collection of collections where a collection of features is expected; use flatten, or map a function to convert inner collections to features. Use clipToCollection (instead of clip) to clip an image to a collection.

How to rectify this?

After adding .flatten() as in here

var both_states = ee.FeatureCollection([Haryana_state, Punjab_state]).flatten();

both the polygons merged into a single layer and the chart drawn was of the Punjab region only, as the values I cross-checked after exporting the CSV of Chart. flattening

but I want a chart something like this, where the time-series is of both Haryana and Punjab on the same chart.

chart required

1 Answer 1

3

I know that you are implementing literally the code for ui.Chart.image.seriesByRegion from this link. For this reason you have two issues in your code. First one: you need to scale images for obtaining true values for NDVI. Second one: your feature collection doesn't have a 'label' property (each feature needs one with name of each state).

So, as I don't have a Haryana_state and a Punjab_state in my assets, I named two of my geometries in my assets with these names. I also added a 'label' property to each feature for testing my approach. My code looks as follows:

function prepImages (img){
  
  var time = img.get('system:time_start');
  
  return img.divide(10000)
    .set('system:time_start', time);
  
}

// 1st Shapefile
var Haryana_state = ee.FeatureCollection("users/joseguerreroa/indice_severidad/chapelco_32719");

Map.addLayer(Haryana_state);

// 2nd Shapefile
var Punjab_state = ee.FeatureCollection("users/joseguerreroa/indice_severidad/huiliches");

Map.addLayer(Punjab_state);

// Combine both shapefile features into a single feature collection.
var both_states = ee.FeatureCollection([ee.Feature(Haryana_state.geometry())
                                        .set('label', 'Haryana_state'), 
                                        ee.Feature(Punjab_state.geometry())
                                        .set('label', 'Punjab_state')]);

print(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'])
                     .map(prepImages);

// 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: 2,
          colors: ['f0af07', '0f8755', '76b349'],
        });
print(chart);

After running above code in GEE code editor, it was printed the chart as expected.

enter image description here

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.