矢量的光栅

时间:2018-03-19 17:05:14

标签: r vector raster

我想在R中创建一个栅格矢量,让我解释一下。 我正在处理数据(24个月,jan,jan2等,以及不同年份),并且,对于每个月(jan,jan2,feb,feb2等),我想创建一个栅格。

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

我现在就这样做了(这里我只尝试了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)

我没有收到这些错误消息,如果你可以帮助我,或者你有相同或类似的问题,我会采取任何暗示!

1 个答案:

答案 0 :(得分:0)

在你的循环中你应该使用双括号[[ ]]

但您可以使用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, ""))