如何基于一个特定波段的最大值使用栅格堆栈列表创建栅格镶嵌图?

时间:2019-07-25 10:49:09

标签: r raster

我试图通过比较每个像元的输入栅格的NDVI值(cload

下面的代码有效,但是每年大约需要5到10天(我正在比较20年)。

snap 是我的空裁剪输出堆栈,其中ar NA值不在观察区域内,如果值在观察区域内则为0。

merged_raster.list 是一个堆栈列表,其中包含我所有裁剪的陆地景观(波段7是NDVI层)

    for (year in dir(landsat_path)){

      for (snap_row in 1:nrow(snap)){
        for (snap_col in 1:ncol(snap)){
          snap_tmp <- snap[[7]][snap_row, snap_col]
          if(!is.na(snap_tmp)){      #TRUE if current cell is in observed area
            best_scene <- 0
            current_scene <- 0
            for (scene in merged_raster.list) {  #comparing every szene to determine the best cell-value
              ndvi_tmp <- scene[[7]][snap_row, snap_col]
              current_scene = current_scene + 1
              if (!is.na(ndvi_tmp)){
                if (ndvi_tmp>snap_tmp){
                  snap_tmp = ndvi_tmp
                  best_scene = current_scene 
                }
              }
            }
            if(best_scene > 0) {
              for (i in 1:7){
              snap[[i]][snap_row, snap_col] <- merged_raster.list[[best_scene]][[i]][snap_row, snap_col]
             }
            }
          }
        }
      }
    }

有没有办法提高效率?

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样?

fnames <- list.files(landsat_path)
landsat <- stack(fnames)
nscenes <- length(fnames)

# Initialize vectors of max NDVI and best scene numbers
# These are vectors of zeros with the same number of 
# entries as values in the Landsat scene
max.ndvi <- raster(landsat[[1]])
max.ndvi <- values(max.ndvi)*0 
best.scene <- max.ndvi

for (k in 1:nscenes)
{
  current.ndvi <- values(landsat[[k]])
  higher.ind <- which(current.ndvi > max.ndvi)
  max.ndvi[higher.ind] <- current.ndvi[higher.ind]
  best.scene[higher.ind] <- k
}