2

My code generates an image collection. In the collection, there are multiple images from the same date (probably from different tiles). Is there a way to filter it and get only one image from a certain date? It's not important which image stays in the collection.

my code:

var image_collection = "COPERNICUS/S2_SR";//image collection.
var first_date       = '2019-06-01'; // First date in image collection
var last_date        = '2019-10-10' ;// Last date in image collection
    
var fc = ee.FeatureCollection(user_account+folder+shpfile_name).sort('Spatial_ID');
var S2 = ee.ImageCollection(image_collection)
        .filterDate(first_date, last_date)
        .filterBounds(fc)
        .filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 18);

//-----------------------------------------------------------------------------------
print(S2);
print(fc)  ; 

1 Answer 1

1

Following script allows having some control of selecting dates in Image Collection. It includes an arbitrary fc geometry in USA.

var fc = ee.Geometry.Polygon(
        [[[-104.60946499778431, 36.501117552980226],
          [-104.60946499778431, 35.86265852547925],
          [-103.45590054465931, 35.86265852547925],
          [-103.45590054465931, 36.501117552980226]]], null, false);

var image_collection = "COPERNICUS/S2_SR";//image collection.
var first_date       = '2019-06-01'; // First date in image collection
var last_date        = '2019-10-10' ;// Last date in image collection
    
//var fc = ee.FeatureCollection(user_account+folder+shpfile_name).sort('Spatial_ID');
var S2 = ee.ImageCollection(image_collection)
        .filterDate(first_date, last_date)
        .filterBounds(fc)
        .filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 18);

//-----------------------------------------------------------------------------------
print(S2);
print(fc);

var allDates = ee.List(S2.aggregate_array('system:time_start'));

var allDatesSimple = allDates.map(function(date){
  return ee.Date(date).format().slice(0,10);
  });

//print all simple dates in collection
print(allDatesSimple);

var allDatesSimple_d = allDates.map(function(date){
  return ee.Date(date).format().slice(0,10);
  }).distinct();

//print all not repeated simple dates in collection not repeated 
print(allDatesSimple_d);

var listOfImages = S2.toList(S2.size());

// fourth image of 2019-07-08: id 34 in allDatesSimple list
print(ee.Date(ee.Image(listOfImages.get(34)).get('system:time_start')).format());

After running it in GEE code editor, it was printed an Image Collection with 124 images. The allDatesSimple function prints all dates (including repeated dates) in that collection. The allDatesSimple_d function considers only distinct dates (list has 40 elements) corroborating there are multiple images for almost all simple dates (one exception is 2019-09-08).

If I want consider fourth image of 2019-07-08, it has an id 34 in allDatesSimple list. It is corroborated in print statement after converting original Image Collection to List:

.
.
.    print(ee.Date(ee.Image(listOfImages.get(34)).get('system:time_start')).format());
.
.
.

For using this image in your code you can define corresponding variable selecting element with id 34 in listOfImages.

Editing Note:

For returning the list of 40 images back, not repeated, you only need to calculate accumulated frequencies in allDatesSimple list. It is necessary to include in former script following code lines:

.
.
.
var countFreq = allDatesSimple_d.map( function freqDates (ele) {
  
  var list = ee.List([]);
  
  var f = allDatesSimple.frequency(ele);
  
  list = list.add(f);
  
  return list;

}).flatten();

var freqAccum = ee.Array(countFreq).accum(0).toList().insert(0, 0).slice(0, 40);

print(freqAccum);

var new_coll = freqAccum.map( function selectNewColl (ele) {
  
  var new_list = listOfImages.get(ele);
  
  return new_list;
  
});

print("As list of Images", new_coll);

new_coll = ee.ImageCollection(new_coll);

print("As Image Collection", new_coll);

Full code here.

2
  • Thank you. That was helpful. Now how do I return the list of 40 images back to an image collection? I want an image collection of just the 40 images list.
    – user166621
    Feb 21, 2021 at 7:13
  • For returning the list of 40 images back, not repeated, you only need to calculate accumulated frequencies in allDatesSimple list. Please, see my Editing Note. By the way, If this answer was helpful consider mark it as accepted.
    – xunilk
    Feb 21, 2021 at 14:34

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.