使用 google earth engine 在列表中查找最接近感兴趣日期的日期
Find closest date in list to date of interest with google earth engine
我已经创建了一个哨兵 2 图像列表。我还从 DATATAKE_IDENTIFIER 字段中提取了日期值,并将其作为我列表中每个图像的名为 "DATE" 的 ee.Date 类型的 属性 粘贴回去。现在我正在尝试检索按时间顺序最接近 user.For 示例指定的感兴趣日期的图像,如果我有以下日期:5 月 5 日、5 月 10 日、5 月 15 日、5 月 20 日和用户选择 5 月 14 日,我想 return 5 月 15 日。有人可以帮忙吗?
这是我的代码:
var startDate = ee.Date('2017-07-01');
var finishDate = ee.Date('2017-07-31');
//Create imageCollection with the sentinel2 data and the date filters
var interestImageCollection = ee.ImageCollection(sentinel2_1C_ImageCollection)
.filterBounds(interestRectangle) //changed here your geometrical shape
.filterDate(startDate, finishDate)
.sort('CLOUDY_PIXEL_PERCENTAGE', false);
//function which will extract the date from the 'DATATAKE_IDENTIFIER' field and add it as a new one to every image
var add_date = function(interestImageCollection){
//iterate over the image Collection
//.map must always return something
return interestImageCollection.map(function(image){
//take the image property containing the date
var identifier = ee.String(image.get('DATATAKE_IDENTIFIER'));
//regex
var splitOn = "_|T";
//split
var splitted = identifier.split(splitOn,"");
var date = ee.String(splitted.get(1));
//DATE OPTION
var year = ee.Number.parse(date.slice(0,4));
var month = ee.Number.parse(date.slice(4,6));
var day = ee.Number.parse(date.slice(6,8));
var dateTaken = ee.Date.fromYMD(year, month, day);
return image.set('DATE',dateTaken);
});
};
//list conversion
var subList = interestImageCollection.toList(interestImageCollection.size());
//get the image with the chronologically closest date to my date of interest
var dateOfInterest = ee.Date('2017-07-10');
//failed attempt to use sort for difference in dates calculation and get the closest date
var c = subList.sort(function(a, b){
var distancea = dateOfInterest.difference(a.get['DATE'],'day').round().abs();
var distanceb = dateOfInterest.difference(b.get['DATE'],'day').round().abs();
return distancea - distanceb; // sort a before b when the distance is smaller
}).first();
对于编程语言,我会使用一个循环,在每次迭代时检查我想要的日期和检索到的日期之间的差异是否低于任何先前的值,并更新一些时间变量。但我认为 earth engine 有更自动化的方式来做到这一点。
我还尝试将排序功能与自定义比较器一起使用,但效果不佳。
还有一些常规 javascript(如 closestTo)函数似乎无法在 Earth 引擎中运行。
我认为有一个更短更简单的解决方案。每个图像都应该有一个名为 system:time_start 的 属性,我们可以使用它来发挥我们的优势。因为我们知道我们的日期,所以我们可以在我们的图像中添加一个新的 属性,以毫秒为单位表示到我们所需日期的距离,因为我们可以将其视为数字运算,通过伴随的所有日月考虑因素YMD格式。
ic = ic.map(function(image){
return image.set(
'dateDist',
ee.Number(image.get('system:time_start')).subtract(dateOfInterest.millis()).abs()
);
});
这个新字段 'dateDist' 现在可用于对图像集进行排序。
ic = ic.sort('dateDist');
默认的排序方式是升序,这样应该不错。您可以通过拍摄第一张图像来获得最近的场景。需要考虑的一件事是,如果您的 ROI 矩形覆盖了足够的区域来查看多个 row/path,那么可能会有多个场景的日期最近。在这种情况下,检查有多少场景落在同一日期范围内可能是个好主意。
我已经创建了一个哨兵 2 图像列表。我还从 DATATAKE_IDENTIFIER 字段中提取了日期值,并将其作为我列表中每个图像的名为 "DATE" 的 ee.Date 类型的 属性 粘贴回去。现在我正在尝试检索按时间顺序最接近 user.For 示例指定的感兴趣日期的图像,如果我有以下日期:5 月 5 日、5 月 10 日、5 月 15 日、5 月 20 日和用户选择 5 月 14 日,我想 return 5 月 15 日。有人可以帮忙吗? 这是我的代码:
var startDate = ee.Date('2017-07-01');
var finishDate = ee.Date('2017-07-31');
//Create imageCollection with the sentinel2 data and the date filters
var interestImageCollection = ee.ImageCollection(sentinel2_1C_ImageCollection)
.filterBounds(interestRectangle) //changed here your geometrical shape
.filterDate(startDate, finishDate)
.sort('CLOUDY_PIXEL_PERCENTAGE', false);
//function which will extract the date from the 'DATATAKE_IDENTIFIER' field and add it as a new one to every image
var add_date = function(interestImageCollection){
//iterate over the image Collection
//.map must always return something
return interestImageCollection.map(function(image){
//take the image property containing the date
var identifier = ee.String(image.get('DATATAKE_IDENTIFIER'));
//regex
var splitOn = "_|T";
//split
var splitted = identifier.split(splitOn,"");
var date = ee.String(splitted.get(1));
//DATE OPTION
var year = ee.Number.parse(date.slice(0,4));
var month = ee.Number.parse(date.slice(4,6));
var day = ee.Number.parse(date.slice(6,8));
var dateTaken = ee.Date.fromYMD(year, month, day);
return image.set('DATE',dateTaken);
});
};
//list conversion
var subList = interestImageCollection.toList(interestImageCollection.size());
//get the image with the chronologically closest date to my date of interest
var dateOfInterest = ee.Date('2017-07-10');
//failed attempt to use sort for difference in dates calculation and get the closest date
var c = subList.sort(function(a, b){
var distancea = dateOfInterest.difference(a.get['DATE'],'day').round().abs();
var distanceb = dateOfInterest.difference(b.get['DATE'],'day').round().abs();
return distancea - distanceb; // sort a before b when the distance is smaller
}).first();
对于编程语言,我会使用一个循环,在每次迭代时检查我想要的日期和检索到的日期之间的差异是否低于任何先前的值,并更新一些时间变量。但我认为 earth engine 有更自动化的方式来做到这一点。
我还尝试将排序功能与自定义比较器一起使用,但效果不佳。
还有一些常规 javascript(如 closestTo)函数似乎无法在 Earth 引擎中运行。
我认为有一个更短更简单的解决方案。每个图像都应该有一个名为 system:time_start 的 属性,我们可以使用它来发挥我们的优势。因为我们知道我们的日期,所以我们可以在我们的图像中添加一个新的 属性,以毫秒为单位表示到我们所需日期的距离,因为我们可以将其视为数字运算,通过伴随的所有日月考虑因素YMD格式。
ic = ic.map(function(image){
return image.set(
'dateDist',
ee.Number(image.get('system:time_start')).subtract(dateOfInterest.millis()).abs()
);
});
这个新字段 'dateDist' 现在可用于对图像集进行排序。
ic = ic.sort('dateDist');
默认的排序方式是升序,这样应该不错。您可以通过拍摄第一张图像来获得最近的场景。需要考虑的一件事是,如果您的 ROI 矩形覆盖了足够的区域来查看多个 row/path,那么可能会有多个场景的日期最近。在这种情况下,检查有多少场景落在同一日期范围内可能是个好主意。