BIG陽
2021-08-20 18:43:59
我創(chuàng)建了一個 Sentinel 2 圖像列表。我還從 DATATAKE_IDENTIFIER 字段中提取了日期值,并將其粘貼回我列表中每個圖像的 ee.Date 類型的名為“DATE”的屬性。現(xiàn)在,我正在嘗試檢索按時間順序最接近用戶指定的感興趣日期的圖像。例如,如果我的日期為:5 月 5 日、5 月 10 日、5 月 15 日、5 月 20 日,用戶選擇 14 日-我想在 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 filtersvar 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 imagevar 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 conversionvar subList = interestImageCollection.toList(interestImageCollection.size());//get the image with the chronologically closest date to my date of interestvar dateOfInterest = ee.Date('2017-07-10');對于編程語言,我會使用一個循環(huán),在每次迭代時檢查我想要的日期和檢索到的日期之間的差異是否低于任何以前的值并更新一些時間變量。但我認(rèn)為地球引擎有一種更自動化的方式來做到這一點。我還嘗試將 sort 函數(shù)與自定義比較器一起使用,但效果不佳。還有一些常規(guī)的 javascript(如最接近的)函數(shù)似乎在地球引擎中不起作用。
1 回答

瀟湘沐
TA貢獻(xiàn)1816條經(jīng)驗 獲得超6個贊
我認(rèn)為有一個更短更簡單的解決方案。每個圖像都應(yīng)該有一個名為system:time_start的屬性,我們可以利用它來發(fā)揮我們的優(yōu)勢。由于我們知道我們的日期,我們可以向我們的圖像添加一個新屬性,以毫秒為單位表示到我們所需日期的距離,因為我們可以將其視為數(shù)字運算,通過 YMD 格式附帶的所有日月注意事項。
ic = ic.map(function(image){
return image.set(
'dateDist',
ee.Number(image.get('system:time_start')).subtract(dateOfInterest.millis()).abs()
);
});
這個新字段“dateDist”現(xiàn)在可用于對圖像集合進(jìn)行排序。
ic = ic.sort('dateDist');
默認(rèn)的排序方式是升序,所以這應(yīng)該是好的。您可以通過拍攝第一張圖像來獲得最近的場景。需要考慮的一件事是,如果您的 ROI 矩形覆蓋了足夠的區(qū)域來查看多行/路徑,那么可能有多個場景的日期最近。在這種情況下,檢查同一日期范圍內(nèi)有多少場景可能是個好主意。
添加回答
舉報
0/150
提交
取消