0

我想从大型 Google 地球引擎图像集合中提取几个点的时间序列数据。因为我想在 R 而不是 GEE 代码编辑器中执行此操作,所以我目前正在使用 rgee 包。我检索了一个图像集合并创建了一个包含 30 个点的特征集合。现在我想使用 rgee::ee_extract() 从 30 个点的图像集合中提取数据。

到目前为止,这是我的代码:

library(rgee)
ee_Initialize("[google_account_name") 

# Retrieve some GEE data
ndvi <- ee$ImageCollection('MODIS/MOD09GA_006_NDVI')

# Define years, months, days of interest
srt_yr = 2001
end_yr = 2016
mon_srt = 1
mon_end = 12
day_srt = 1
day_end = 31
modis = ndvi$
  filter(ee$Filter$calendarRange(srt_yr, end_yr, 'year'))$
  filter(ee$Filter$calendarRange(mon_srt, mon_end, 'month'))$
  filter(ee$Filter$calendarRange(day_srt, day_end, 'day_of_month'))

# Check image coll size
print(c("image collection size: ", modis$size()$getInfo()))    

# Reduce the daily data to monthly means for each year
# Function parameters
years = ee$List$sequence(srt_yr, end_yr)   
months <- ee$List$sequence(1, 12)

# Function
monthly_mean_per_yr <- function(y){
  monthly_sum <- function(m){
    w <- modis$filter(ee$Filter$calendarRange(y, y, 'year'))$
      filter(ee$Filter$calendarRange(m, m, 'month'))$mean()
    
    return(w$set('year', y)$
             set('month', m)$
             set('system:time_start', ee$Date$fromYMD(y, m, 1)))
  }
  return(months$map(ee_utils_pyfunc(monthly_sum)))   #ee_utils_pyfunc wraps R fxns in a Py fxn with the same signature
}

modis_yr_mo_mn <- modis$fromImages(
  years$map(ee_utils_pyfunc(monthly_mean_per_yr ))$flatten())

# Get updated image coll size
print(c("image collection size: ", modis_yr_mo_mn$size()$getInfo()))    
n <- ((end_yr+1) - srt_yr)*12   # check that img size is correct
print(c("correct img coll size: ", n))    # Get image coll size

# Define a region of interest and extract some points from it
roi <- ee$Geometry$Rectangle(c(-110.806027, 40.487787, 
                               -109.167534, 40.003145))

# Generate some random points
points <- ee$FeatureCollection$randomPoints(region=roi, points=30, seed=0, maxError=1)

# Check polygon and point locations
Map$addLayer(roi)+
  Map$addLayer(points)

## Now extract the NDVI data for the specified period from each point
# Return the extracted data as a sf 
points_extract <- ee_extract(x=modis_yr_mo_mn, y=points, scale=500, sf=T, via="getInfo")

使用 ee_extract 对较小的图像集合有效,但是当我将它应用于这个大图像集合时,我收到以下错误:

 Warning in ee_utils_py_to_r(.) :
   restarting interrupted promise evaluation
Warning in ee_utils_py_to_r(.) :
  restarting interrupted promise evaluation
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  EEException: Computation timed out.

按照rgee 'Best Practices' page上的建议,我尝试使用以下命令将数据直接导出到我的 Google Drive:

points_extract_to_drive <- ee_extract(x=modis_yr_mo_mn, y=points, scale=500, sf=T, via="drive")

这可行,但速度很慢(下载提取的数据需要 39 分钟)。我想自动化此代码以提取多组点特征集合的 NDVI 数据,因此我想使用 ee_extract 的默认方法(即 via="getInfo")。

我最近解决这个问题的尝试是将图像集合切成更小的部分(1 年增量,每个 12 个图像),从这些较小的图像集合中提取点数据作为 sf 对象,然后将生成的 sf 对象重新绑定在一起. 我尝试使用 for 循环来做到这一点(我知道这对于 GEE 数据并不理想,但似乎是最直接的路径):

library(dplyr)
library(tidyr)
library(tidyverse)

collection <- modis_yr_mo_mn
listOfImages <- collection$toList(collection$size())  # our obj on which to run the for loop
datalist = list()

for (i in seq_along(listOfImages)) {    

  x <- ee$Image(listOfImages$get(i))                  
  
  dat <- ee_extract(x, points, scale=500, sf=T) 
  dat$i <- i  # maybe you want to keep track of which iteration produced it?
  datalist[[i]] <- dat # add it to your list
  return(datalist)
}

whole_coll <- dplyr::bind_rows(datalist)
head(whole_coll)

在 for 循环运行时,生成的 sf 仅包含 2 个字段的 6 个特征。我预计有 30 个特征(每个点一个)和 193 个字段(“集合”中的每个图像一个加上 for 循环迭代次数)。我觉得这种 for 循环方法可以工作,但我没有得到完全正确的东西。有没有人有任何建议或想法?

4

0 回答 0