Google Earth Engine – Analyzing Multi-Trends in Google Earth Engine

google-earth-engineprecipitationtime series

I've wrote below code for visualize precipitation changes over a year with ui.chart command in two different provinces.

var persiann = ee.ImageCollection('NOAA/PERSIANN-CDR')
.filterDate('2017-01-01', '2017-01-30');

var province1 = table2;

var province2 = table3;

var regions = ee.FeatureCollection([
  ee.Feature(province1,{label : 'province1'}),
  ee.Feature(province2,{label : 'province2'})]
  );

var chart = ui.Chart.image.seriesByRegion(
  persiann,regions,ee.Reducer.mean(),'precipitation',1000,
  'system:time_start','label'
  );

print(chart);

however it shows 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.

Best Answer

The problem is that your 'table2' and 'table3' are probably feature collections. But you should give access to your 'table2' and 'table3'. Then you are making feature collection within a feature collection, and that's why the error is thrown. 'table2' and 'table3' should be geometries (e.g. a point or polygon) in the way your code is written now. Below is an example which works in the way you wrote this code:

// get the image collection
var persiann = ee.ImageCollection('NOAA/PERSIANN-CDR')
    .filterDate('2017-01-01', '2017-01-30');

// get the geometry of the feature collections (with only 1 feature)
var province1 = table2.first().geometry();
var province2 = table3.first().geometry();
// rebuild a feature collection with custom labels
var regions = ee.FeatureCollection([
  ee.Feature(province1,{label : 'province1'}),
  ee.Feature(province2,{label : 'province2'})]
  );
// make a chart
var chart = ui.Chart.image.seriesByRegion(
    persiann,regions,ee.Reducer.mean(),'precipitation',1000,
    'system:time_start','label');
print(chart);

// or merge the import collecitons
var provinces = table2.merge(table3);
print(provinces);
// make a chart
var chart = ui.Chart.image.seriesByRegion(
  persiann,provinces,ee.Reducer.mean(),'precipitation',1000,
 'system:time_start','NAM');
print(chart);

// add the featurecollection to the map
Map.centerObject(table)
Map.addLayer(provinces.draw('red'))

Full code

If you don't know how to correctly get the geometries from 'table2' and 'table3' give access to your 'table2' and 'table3' and it's probably easily solved. - edit: access was given and the answer is updated

Related Question