0

When I run this code, this error appears: Line 20: january.clipToCollection is not a function.

How can I transform it to appear as a map on Google Earth Engine?

var mg = ee.FeatureCollection('users/isaacvasconcelos/shapefile_matas_de_minas'); //put any shape You want right here

var january  = ee.List.sequence(0, 29).map(function(n) {
  var start = ee.Date('1990-01-01').advance(n, 'year');
  var end = start.advance(1, 'month');
  return ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY').filterDate(start, end)
  .select('precipitation').sum().reduceRegion({
                    reducer: ee.Reducer.mean(),
                    geometry: mg,
                    scale: 1000
  });
});

print(january);

var clip1 = january.clipToCollection(mg);

var vis = {opacity: 1,
            bands: ['precipitation'],
            min: 0.00,
            max: 200.00,
            palette: ['FF0000', 'FF4000','FF8000', 'FFBF00', 'FFFF00', 'BFFF00', '80FF00', '40FF00', '00FF00', '00FF80', '00FFBF', '00FFFF', '0080FF', '0000FF']
};

Map.addLayer(clip1, vis, 'Jan', 1);

1 Answer 1

2

You are running into this error, because you are trying to use .clipToCollection(collection) on a feature collection, while this method works only on images.

In this piece of code you are using .reduceRegion and it expects a geometry, however you are passing in mg (a feature collection):

ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY').filterDate(start, end)
  .select('precipitation').sum().reduceRegion({
                    reducer: ee.Reducer.mean(),
                    geometry: mg,
                    scale: 1000
  });

So, better way to do this - use .reduceRegions() instead of .reduceRegion:

ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY').filterDate(start, end)
  .select('precipitation').sum().reduceRegions({
                    reducer: ee.Reducer.mean(),
                    collection: mg,
                    scale: 1000
  });

It's difficult to guess what exactly what's your ultimate goal is (also, you feature collection is not shared), you will need to give more context.

One more tip, when mapping over the list with dates, it may be useful to add a date to each feature, so you will know it for your further analysis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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