【问题标题】:vector OF raster光栅矢量
【发布时间】:2025-12-29 22:00:06
【问题描述】:

我正在尝试在 R 中创建光栅矢量,让我解释一下。 我正在处理数据(24 个月、1 月、1 月 2 日等,以及不同的年份),并且每个月(1 月、1 月 2 日、2 月、2 月 2 日等)我想创建一个栅格。

我是手工完成的,但它真的很长,我正在尝试优化一下这段代码。我正在使用一个函数,以及该函数内的一个循环来创建我的栅格。但正如你想象的那样,它不起作用。我想按月自动创建我的栅格。

我现在这样做了(这里我只尝试了 5 个 .tif 文件):

library(sp)
library(raster)
library(stringr)
setwd(dir = "E:/perso/NEON-DS-Field-Site-Spatial-Data/SJER")
files_vec=list.files(path = "E:/perso/NEON-DS-Field-Site-Spatial-Data/SJER",full.names=FALSE, recursive=FALSE)
files_vec

month<-c("jan","jan2","fev","fev2","mar")

vec_of_raster<-c("raster",length(files_vec))

raster_vec<-function(vec_of_raster,files_vec,month,year_str)
{
  for (i in seq(1,length(files_vec)))
  {
     vec_of_raster[i]<-(month[i]<-raster(files_vec[i]))
  }
}

raster_vec(vec_of_raster,files_vec,month,"1995")

恐怕我想使用两个对象(光栅和矢量),它们要么以这种方式接近工作,要么无法关联。

这是我得到的错误(和警告):

Error in .local(.Object, ...) :
In addition: Warning messages:
1: In month[i] <- raster(files_vec[i]) :
number of items to replace is not a multiple of replacement length
2: In vec_of_raster[i] <- (month[i] <- raster(files_vec[i])) :
  number of items to replace is not a multiple of replacement length
3: In month[i] <- raster(files_vec[i]) :
  number of items to replace is not a multiple of replacement length
4: In vec_of_raster[i] <- (month[i] <- raster(files_vec[i])) :
  number of items to replace is not a multiple of replacement length
5: In month[i] <- raster(files_vec[i]) :
  number of items to replace is not a multiple of replacement length
6: In vec_of_raster[i] <- (month[i] <- raster(files_vec[i])) :
  number of items to replace is not a multiple of replacement length
7: In month[i] <- raster(files_vec[i]) :
  number of items to replace is not a multiple of replacement length
8: In vec_of_raster[i] <- (month[i] <- raster(files_vec[i])) :
  number of items to replace is not a multiple of replacement length
9: In month[i] <- raster(files_vec[i]) :
  number of items to replace is not a multiple of replacement length
10: In vec_of_raster[i] <- (month[i] <- raster(files_vec[i])) :
  number of items to replace is not a multiple of replacement length


Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  : 
  Cannot create a RasterLayer object from this file. (file does not exist)

我没有收到这些错误消息,如果你能帮助我,或者你有相同或类似的问题,我正在接受任何提示!

【问题讨论】:

    标签: r vector raster


    【解决方案1】:

    在您的循环中,您可能应该使用双括号[[ ]]

    但是您可以使用stack(如果栅格可以堆叠——它们需要具有相同的范围和分辨率),或者lapply。如有必要,处理之后的名称。

    library(raster)
    
    # example file for reproducibility
    f <- system.file("external/test.grd", package="raster")
    ff <- rep(f, 3)
    
    s <- stack(ff)
    s
    #class       : RasterStack 
    #dimensions  : 115, 80, 9200, 3  (nrow, ncol, ncell, nlayers)
    #resolution  : 40, 40  (x, y)
    #extent      : 178400, 181600, 329400, 334000  (xmin, xmax, ymin, ymax)
    #coord. ref. : +init=epsg:28992 +towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 +proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs 
    #names       :  test.1,  test.2,  test.3 
    #min values  : 128.434, 128.434, 128.434 
    #max values  : 1805.78, 1805.78, 1805.78 
    

    如果你真的想要一个列表(或者不能制作一个 RasterStack)

    x <- lapply(ff, raster)
    names(x) <- basename(extension(ff, ""))
    

    【讨论】:

    • 所以,如果我理解你的意思,你是在告诉我应该堆叠我的所有文件并在使用我的堆栈创建栅格之后? (抱歉,我不明白 f 是什么,system.file 为您提供了包中文件的全名,但我不明白我们为什么要使用它,我知道在您复制相同文件之后文件只是为了测试,而不是system.file,你能给我解释一下吗?)我绝对不需要一个列表,我只是想用这个代码完成事情^^如果可能的话,以一种更紧凑的方式,所以任何可以工作的东西!
    • 问题和答案应该是可重复的;这就是为什么我使用 R 附带的文件的原因。在您的情况下,您可以使用list.files。如果您在这个级别遇到问题,我建议您多学习 R(和 R 空间)。这可能会有所帮助:rspatial.org