如何在R

时间:2018-10-11 00:40:53

标签: r lapply spatial raster tiff

我试图在R中的一堆光栅(.tif)文件上运行相同的重分类。我遵循以下代码中的示例:

wfrastlist <- list.files(path = 
                           "/path/to/directory", pattern='*.TIF$', all.files=TRUE, full.names=FALSE)

#generate a reclassification matrix 
#in this example, values less than 353.2 are assigned a new value of 'NA'
m <- c(-Inf, 353.2, NA)
rclmat <- matrix(m, ncol=3, byrow=TRUE)

#function to reclassify rasters and write a new reclassified tif file for each
batch_reclass <- function(wfrastlist){
  for (i in 1:length(wfrastlist)) {
    #read in raster
    r <- raster(paste0("/path/to/directory", wfrastlist[i]))

    #perform the reclassifcation
    rc <- reclassify(r, rclmat)

    #write each reclass to a new file 
    writeRaster(rc, filename = paste0("/path/to/newdirectory", "rc_", 
                                      wfrastlist[i]), format="GTiff", overwrite=TRUE)
  }
}
#run the function
batch_reclass(wfrastlist)

但是我收到以下错误消息:

Error in .local(.Object, ...) : 

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  : 
Cannot create a RasterLayer object from this file. (file does not exist) 
6. stop("Cannot create a RasterLayer object from this file. (file does not 
exist)") 
5..rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", 
...) 
4..local(x, ...) 
3. raster(paste0("/Users/rachelfield/Documents/UBC/MyNetworks/ES_maps/data/wf_rasters _by_basin", 
wfrastlist[i])) 
2. raster(paste0("/Users/rachelfield/Documents/UBC/MyNetworks/ES_maps/data/wf_rasters_by_basin", 
wfrastlist[i])) 
1. batch_reclass(wfrastlist) 

我在做什么错了?

从此处获得的代码示例: https://rstudio-pubs-static.s3.amazonaws.com/250928_2ab49213bf0047cc9cd02e1745858dd3.html

0 个答案:

没有答案