1

I need the closest image of a collection to a date. Let's say the image that is closest to 1st of August 2019.

The help says that

Creates a unary or binary filter that passes if the left operand, a date range, contains the right operand, a date.

which seems to be what I need. I expect to use it like this

ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
  .filter(
    ee.Filter.dateRangeContains(ee.dateRange('2019-07-24','2019-08-09'),'2019-08-01')
  )

but the problem is that dateRangeContains has the following arguments:

ee.Filter.dateRangeContains(leftField, rightValue, rightField, leftValue)
leftField (String, default: null):

A selector for the left operand. Should not be specified if leftValue is specified.

rightValue (Object, default: null):

The value of the right operand. Should not be specified if rightField is specified.

rightField (String, default: null):

A selector for the right operand. Should not be specified if rightValue is specified.

leftValue (Object, default: null):

The value of the left operand. Should not be specified if leftField is specified.

Considering the definition, I did left the first 2 arguments aside, and used only the last 2, which are named in the definition, i.e. the left operand, a date range, contains the right operand, a date:

ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
  .filter(
    ee.Filter.dateRangeContains(undefined,undefined,ee.dateRange('2019-07-24','2019-08-09'),'2019-08-01')
  )

Needless to say, it didn't work, although there is an image with the date on 2019-07-25

1

I think there is some confusion here regarding what dateRangeContains does. It DOES NOT give you the image closest to the date you want. It just checks a DateRange object in the metadata of an image and gives you images which contains the date you supplied within the range listed in its metadata. The string field is actually supposed to be the name of the DateRange field and the object field is supposed to be the date that you want. for example,

var ic = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
  .filterDate('2018-07-24','2018-11-09');

print(ic.size());

// since ls8 images only have start date, i am just advancing the
// date by 1 month for demonstration purpose
ic = ic.map(function(image){
  return image.set('daterange',ee.DateRange(ee.Date(image.get('system:time_start')),ee.Date(image.get('system:time_start')).advance(1,'month')))
});

ic = ic.filter(
    ee.Filter.dateRangeContains('daterange',ee.Date('2018-08-01'))
  )
print(ic.size(), ic.first());

So, it does not do what you want. However, you can do it by using the system:time_start property of the images and compute how far they are from the date you want. Once you sort it using that you are good to go.

// narrow down images to some smaller window to look within smaller dataset 
var ic = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
  .filterDate('2019-07-24','2019-09-09');

// convert required date to millis
var requiredDate = ee.Date('2019-08-01').millis();

// calculate difference between image date and required  date. store the absolute value
ic = ic.map(function(image){
  return image.set('dateDist', ee.Number(image.get("system:time_start")).subtract(requiredDate).abs());
})

// sort the image using that absolute difference and take the first one 
var closestImage = ic.sort('dateDist').first();

// check the date of closest available image
print(ee.Date(closestImage.get("system:time_start")));

Although, if you want multiple scenes, you might need to take a set of images than just first.

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.