2

I want to generate a time series chart of precipitation data using CHIRPS in Google Earth Engine. I have some twenty points over which I want the data individually, i.e, I want twenty charts.

So far, I have managed to get a single chart over all the points altogether. How can I modify the following code as per my requirements?

Link to the code: https://code.earthengine.google.com/f38eca09db9e303f69d5a69e9d863a0a

//Filter image collection
var precip = chirps.filterDate('2000-01-01','2008-12-31');

//Create and print rainfall chart
print(ui.Chart.image.series(precip.limit(5000),tunga,ee.Reducer.mean(),1000).setOptions({

title: 'gp_1: PPT time series',
hAxis: {title: 'Day'},
vAxis:{title:'rainfall (mm/day)'}
}));
0

In order to generate multiple charts, you have to do this “on the client side”, in the JavaScript code itself, rather than sending a single request to Earth Engine. In order to do this specifically for all points in your FeatureCollection, you use the evaluate operation to download enough information to do that:

tunga.select([]).evaluate(function (featureCollection) {
  featureCollection.features.forEach(function (feature) {
    // Do something with each feature here.
  });
});
  1. tunga.select([]) discards all the properties of the features. This is because we only need the ID and the geometry, which will always be present. (If your features had a property which you wanted to use to label the charts, you would put its name in the brackets here.)
  2. .evaluate(...) tells Earth Engine to compute the result — i.e. the collection without any properties — and download it for use, in GeoJSON format. It takes a callback function because the computation may take a while to complete.
  3. featureCollection is not an ee.FeatureCollection — it is a plain data structure, which we can access using JavaScript functionality. In particular, we'll write featureCollection.features.forEach(...) to loop over each of the features in the collection.
  4. Within the inner function/loop, feature is the individual feature. Again, this is a GeoJSON feature structure, not an Earth Engine object, so we can access the contents directly. In particular, it has feature.geometry (which we can use to generate the chart) and feature.id (which we can use to label which chart this is).

Filling in the middle with your chart code, we get this (https://code.earthengine.google.com/7a58159ebd13fbcd32f136b7213e43f9):

tunga.select([]).evaluate(function (featureCollection) {
  featureCollection.features.forEach(function (feature) {
    print(
      ui.Chart.image.series(
        precip.limit(5000),
        ee.Geometry(feature.geometry),
        ee.Reducer.mean(),
        1000)
      .setOptions({
        title: 'gp_1: PPT time series ' + feature.id,
        hAxis: {title: 'Day'},
        vAxis:{title:'rainfall (mm/day)'}
      }));
  });
});

Note the ee.Geometry(...) cast to convert the GeoJSON geometry back into an ee.Geometry object.

If we wanted to do more complex processing with each feature, it would be wise to, instead of re-uploading each part of the feature that we need to use, look up the feature by ID in the collection (using, perhaps, ee.FeatureCollection.filter().first()) but that is not necessary for this case.

  • Thanks a bunch; this saved me. :) – Jyoti Dhwanikar Oct 24 '19 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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