6

I am trying to filter an ImageCollection by multiple time intervals (rain seasons) that change from year to year. Hence, I can't just use the ee.Filter.calendarRange.

I tried to merge the dates together and filter the collection but it does not work. What do I have to do?

var ROI = /* color: #0b4a8b */ee.Geometry.Polygon([
      [36.5048217773437, 0.560293804172086],
      [37.9302978515625, 0.560293804172086],
      [37.9302978515625, 1.875580463973453],
      [36.5048217773437, 1.875580463973453]
  ]);
    Map.centerObject(ROI,8);  

var s2 = ee.ImageCollection('COPERNICUS/S2')
  .filterBounds(ROI);

var longRain2016 = s2.filterDate('2016-04-01','2016-05-31');
var shortRain2016 = s2.filterDate('2016-11-15','2016-12-31');
var longRain2017 = s2.filterDate('2017-04-01','2017-05-31');
var shortRain2017 = s2.filterDate('2017-10-01','2017-12-31');
var longRain2018 = s2.filterDate('2018-03-01','2018-05-31');
var dateFilter = ee.ImageCollection(longRain2016.merge(shortRain2016).merge(longRain2017).merge(shortRain2017).merge(longRain2018));

var s2dateFilter = s2.filter(dateFilter);


var count = s2dateFilter.size();
print('Number of filtered images in the region of interest: ', count);

1 Answer 1

11

Your Code

These lines don't seem to work out:

var dateFilter = ee.ImageCollection(longRain2016.merge(shortRain2016).merge(longRain2017).merge(shortRain2017).merge(longRain2018));

var s2dateFilter = s2.filter(dateFilter);

You are trying to construct another ee.ImageCollection passing existing ImageCollections to it. According to the API, this won't work.

I also think that it is not necessary to achieve what you want because ee.ImageCollection.merge returns an ImageCollection anyway.

This should work:

var allSeasons = longRain2016.merge(shortRain2016).merge(longRain2017).merge(shortRain2017).merge(longRain2018);

Now, since you already should have a merged ImageCollection of all the seasons in allSeasons, you don't need the second step. (Actually this wouldn't work because your dateFilter object is not a Filter and filter() only accepts a Filter.)

You can simply proceed with var count = allSeasons.size();

Alternative way ("The Multiple-Filter-Method")

You could first create the Filters and then Filter.or()-join them together. Next you could apply this joint Filter to the ImageCollection:

//Create Filters
var longRain2016Filter = ee.Filter.date('2016-04-01','2016-05-31');
var shortRain2016Filter = ee.Filter.date('2016-11-15','2016-12-31');
var longRain2017Filter = ee.Filter.date('2017-04-01','2016-05-31');
var shortRain2017Filter = ee.Filter.date('2017-10-01','2016-12-31');
var longRain2018Filter = ee.Filter.date('2018-03-01','2018-05-31');

var allSeasonsFilter = ee.Filter.or(longRain2016Filter, shortRain2016Filter, longRain2017Filter, shortRain2017Filter, longRain2018Filter); // Create joint Filter

var allSeasons = s2.filter(allSeasonsFilter); //apply Filter to ImageCollection
1
  • 2
    Thank you so much! That alternative way seems to be a lot smoother.
    – loloj0
    Commented Jun 25, 2018 at 22:10

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.