monthlyRainfall.filter(...).first 不是一个函数

问题描述

https://code.earthengine.google.com/18914d58d2f193bf206e108774902ce5

var months = ee.List.sequence(1,12)

...

var monthlyRainfall = months.map(function(month) {
    var filtered = monthlyCol.filter(ee.Filter.eq('month',month))
    var monthlyMean = filtered.mean()
    return monthlyMean.set('month',month)
})

...

var deviation = months.map(function(month) {
  var longTermMean = ee.Image(monthlyRainfall
    .filter(ee.Filter.eq('month',month)).first())
  var monthlyObserved = ee.Image(observedRainfall
    .filter(ee.Filter.eq('month',month)).first())
  var deviation = (monthlyObserved.subtract(longTermMean)
    .divide(longTermMean)).multiply(100)
    .set('month',month)
  return deviation
})

我收到错误或行号。 88 在我上面提到的链接中。

Line 88: monthlyRainfall.filter(...).first is not a function

我为这个问题找到的解决方案是使用 ee.Image。但即使在使用它之后,我也面临同样的错误。请帮助我如何度过难关。

解决方法

monthlyRainfallee.List,而不是 ee.ImageCollection。它们在某些方面很相似,但 ee.List 没有 .first() 操作(而是您可以编写 .get(0))。

过滤器在列表和集合上的工作方式也不同。在这种情况下,为了使其按您想要的方式工作,您应该将您的 List 转换为 ImageCollection。改变

var monthlyRainfall = months.map(function(month) {
    var filtered = monthlyCol.filter(ee.Filter.eq('month',month))
    var monthlyMean = filtered.mean()
    return monthlyMean.set('month',month)
})

应用ee.ImageCollection(...),它将图像列表转换为一个ImageCollection:

var monthlyRainfall = ee.ImageCollection(months.map(function(month) {
    var filtered = monthlyCol.filter(ee.Filter.eq('month',month));
    var monthlyMean = filtered.mean();
    return monthlyMean.set('month',month);
}));