执行使用空间点提取时间序列值的函数时出现错误消息

Error message while executing a function for extracting time-series values using spatial points

我有一个函数可以提取点路径的时间序列栅格值。到现在为止一切正常;但是,我今天在尝试使用它时不断收到错误消息。我收到的错误是:

Error in UseMethod("extract_") : no applicable method for 'extract_' applied to an object of class "c('RasterLayer', 'Raster', 'BasicRaster')"

你能帮忙吗?我已经尝试了很多,但我无法解决这个问题。

library(raster)
#--------------------------------------start myfunction-----------------------------

# extract raster values for each single point in each path and put it a dataframe
lst <- function (rstack.lst, points, df.name, IDstr){
  df.name <- list()
  ii <- 1
  #number of layers in a list of raster stacks
  n = Reduce(`+`, lapply(rstack.lst, nlayers))

  for (j in 1:length(rstack.lst)){
    df.name[[j]] <- as.data.frame(matrix(0, ncol = nlayers(rstack.lst[[j]])+3, nrow = nrow(points)))
    names(df.name[[j]]) <- append(c("coords.x","coords.y","ID"), substr(names(rstack.lst[[j]]),5,14), after = 3)
    #calculating x and y coordinates
    df.name[[j]][1:2] <- coordinates(points) 
    # setting up unique IDs 
    df.name[[j]]$ID <- paste0(IDstr, ".",seq(1:(nrow(df.name[[j]])))) 

    for (i in 1:nlayers(rstack.lst[[j]])){
      df.name[[j]][i+3] <- extract(rstack.lst[[j]][[i]], points)
      colnames(df.name[[j]][i+3]) <- substr(names(rstack.lst[[j]][[i]]),5,14)
      cat(paste0(round((ii/(n))*100), '% completed'))
      ii <- ii+1
      Sys.sleep(.05)
      if (ii == n) cat(': Done')
      else cat('4')
    }

  }
  return(df.name)
}
#--------------------------------------end start myfunction-----------------------------

lsdata[[1]] <- lst(rstack.lst=r.lst, points=Pnt.shp, IDstr="P1")

我可能错了,但我认为 extract 函数没有出现故障,但您可能正在加载其他一些具有完全相同功能的包(我的意思是具有相同的名称)。有许多软件包可能具有 同名 的函数。例如,extract 也是 tidyr 中的函数。为避免此类问题,我建议您在指定功能时也添加包名称。在这里你可以做:raster::extract.

为了确定,只需执行 extract 并仔细检查它包含的内容。不管它包含什么,可能不是下面这样:

function (x, y, ...) 
standardGeneric("extract")
<bytecode: 0x000000001873c4d0>
<environment: 0x000000001855db68>
Methods may be defined for arguments: x, y
Use  showMethods("extract")  for currently available ones.