当我们遇到下面问题时,我们就是可能嵌套类型太多了,所以我们需要去掉原有的括号,也就是括号嵌套太多了,所以我们用在打印list时我们就需要脱掉外面的一层括号,这样才能正确展示,这里我们首先看看错误提示
错误:
ImageCollection (Error)
ImageCollection.fromImages, argument 'images': Invalid type.
Expected type: List<Image<unknown bands>>.
Actual type: List<List<Image<[LST_Day_1km]>>>. Actual value: [[<Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>], [<Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>], [<Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>], [<Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>], [<Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>, <Image<[LST_Day_1km]>>]]
原始代码:
var ROI = ee.FeatureCollection("projects/geemijan/assets/Bangladesh"); var modis = ee.ImageCollection("MODIS/006/MOD11A2") .select('LST_Day_1km') .filterDate('2012-01-01','2016-12-31') .filterBounds(ROI) Map.setOptions('SATELLITE') Map.addLayer(ROI) Map.centerObject(ROI,5) var LST_Res = function(image){ var LST_temp = image.toFloat().multiply(0.02).subtract(273.15) image = image.addBands(LST_temp); return image; } var LST_new = modis.map(LST_Res) var modis = LST_new.select('LST_Day_1km') .map(function(img){ var d = ee.Date(ee.Number(img.get('system:time_start'))); var m = ee.Number(d.get('month')); var y = ee.Number(d.get('year')); return img.set({'month':m, 'year':y}) }) print(modis) var months = ee.List.sequence(1,12) var years = ([2012, 2013, 2014, 2015, 2016]) var byYearMonth = ee.ImageCollection.fromImages( years.map(function(y){ return months.map(function(m){ return modis.filterMetadata('year','equals',y) .filterMetadata('month','equals',m) .select('LST_Day_1km').mean() .set('year',y) .set('month',m) .set('date', ee.Date.fromYMD(y,m,1)) }); }) ) print( byYearMonth )
此处的需要一个函数:
flatten()
Flattens collections of collections.
Arguments:
this:collection (FeatureCollection):
The input collection of collections.
Returns: FeatureCollection
flatten()
Flattens any sublists into a single list.
Arguments:
this:list (List)
Returns: List
修改后的代码:
var modis = ee.ImageCollection("MODIS/006/MOD11A2") .select('LST_Day_1km') .filterDate('2012-01-01','2016-12-31') .filterBounds(ROI) Map.setOptions('SATELLITE') Map.addLayer(ROI) Map.centerObject(ROI,5) var LST_Res = function(image){ var LST_temp = image.toFloat().multiply(0.02).subtract(273.15) image = image.addBands(LST_temp); return image; } var LST_new = modis.map(LST_Res) var modis = LST_new.select('LST_Day_1km') .map(function(img){ var d = ee.Date(ee.Number(img.get('system:time_start'))); var m = ee.Number(d.get('month')); var y = ee.Number(d.get('year')); return img.set({'month':m, 'year':y}) }) print(modis) var months = ee.List.sequence(1,12) // var years = ([2012, 2013, 2014, 2015, 2016] var years = ee.List.sequence(2012,2016) var byYearMonth = ee.ImageCollection.fromImages( years.map(function(y){ return months.map(function(m){ return modis.filterMetadata('year','equals',y) .filterMetadata('month','equals',m) .select('LST_Day_1km').mean() .set('year',y) .set('month',m) .set('date', ee.Date.fromYMD(y,m,1)) }); }).flatten() ) print( byYearMonth )