使用 Google 地球引擎中图像集合中每个单独图像的波段值填充要素集合



在谷歌地球引擎中,我以包含几个多边形的JSON的形式加载了一个特征集合。我想向此要素集合添加列,它为我提供了每个面的两个波段以及影像集合中包含的多个图像中每个波段的平均值。

这是我到目前为止的代码。

//Polygons
var polygons = ee.FeatureCollection('ft:1_z8-9NMZnJie34pXG6l-3StxlcwSKSTJFfVbrdBA');
Map.addLayer(polygons);
//Date of interest
var start = ee.Date('2008-01-01');
var finish = ee.Date('2010-12-31');
//IMPORT Landsat IMAGEs
var Landsat = ee.ImageCollection('LANDSAT/LT05/C01/T1') //Landsat images
.filterBounds(polygons)
.filterDate(start,finish)
.select('B4','B3');
//Add ImageCollection to Map
Map.addLayer(Landsat);
//Map the function over the collection and display the result
print(Landsat);
// Empty Collection to fill
var ft = ee.FeatureCollection(ee.List([]))
var fill = function(img, ini) {
// type cast
var inift = ee.FeatureCollection(ini)
// gets the values for the points in the current img
var mean = img.reduceRegions({
collection:polygons,
reducer: ee.Reducer.mean(),
});
// Print the first feature, to illustrate the result.
print(ee.Feature(mean.first()).select(img.bandNames()));
// writes the mean in each feature
var ft2 = polygons.map(function(f){return f.set("mean", mean)})
// merges the FeatureCollections
return inift.merge(ft2)
// gets the date of the img
var date = img.date().format()
// writes the date in each feature
var ft3 = polygons.map(function(f){return f.set("date", date)})
// merges the FeatureCollections
return inift.merge(ft3)
}
// Iterates over the ImageCollection
var newft = ee.FeatureCollection(Landsat.iterate(fill, ft))
// Export
Export.table.toDrive(newft,
"anyDescription",
"anyFolder",
"test")

在控制台中,我收到一条错误消息

元素(错误) 无法解码 JSON。 错误:对象"{"类型"的字段"值"缺失或为空。 对象:{"类型":"ArgumentRef","value":null}.

在生成的csv文件中,我得到了一个名为mean的新列,但它填充了并且没有实际值。

这里没有理由使用iterate()。您可以做的是一个嵌套map()。在多边形上,然后在图像上。您可以平展生成的列表列表,将其转换为单个列表,如下所示:

// compute mean band values by mapping over polygons and then over images
var results = polygons.map(function(f) {
return images.map(function(i) {
var mean = i.reduceRegion({
geometry: f.geometry(),
reducer: ee.Reducer.mean(),
});
return f.setMulti(mean).set({date: i.date()})
})
})
// flatten
results = results.flatten()

脚本:https://code.earthengine.google.com/b65a731c78f78a6f9e08300dcf552dff

同样的方法也可以用于reduceRegions(),映射图像,然后映射区域。但是,您必须映射生成的要素以设置日期。

如果您的要素覆盖的区域较大,则可能还可以添加images.filterBounds(f)

PS:您的桌子没有共享

最新更新