【问题标题】:stack and brick function error despite all of the rasters have been尽管所有栅格都存在堆栈和砖块功能错误
【发布时间】:2019-04-13 17:39:58
【问题描述】:

大家好..

我有 13 个生物气候变量(.tiff 格式),我将使用 dismo 包来执行 sdm。 我遵循了 Robert J. Hijmans 和 Jane Elith 编写的教程。 但是,当我尝试堆叠所有变量时,出现以下错误

.local(.Object, ...) 中的错误:

.rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", 中的错误: 无法从此文件创建 RasterLayer 对象。

我的文件的所有坐标系、范围和像元大小都已调整,因此它们都相同.. 当我尝试使用替代砖块功能时,出现以下错误:

.rasterObjectFromFile(x, objecttype = "RasterBrick", ...) 中的错误: 无法从此文件创建 RasterLayer 对象。 另外:有12条警告(使用warnings()查看)

我使用了 warning() 消息,但它是空的..

你们中的任何人对可能导致此类错误的原因有任何提示吗? 我试过谷歌它,但不幸的是,没有答案可以解决它。 提前谢谢你..

这里显示的是成绩单的一部分

#setting the workspace
 setwd("D:/Riset/MaxentSelaginella/newpaperproject_part2/MakalahVI/Workspace_R")

#Loading Libraries
 library("sp")
 library("raster")
 library("maptools")
 library("rgdal")
 library("dismo")
 library("rJava")

 #open the csv file
 obs.data <- read.csv(file = "data3/Selaginella_plana.csv", sep = ",")

 #open Environmental Data
 files <- list.files(path = "data3/tif/", pattern = ".tif", full.names=TRUE)

 #stacking all the files
  predictors <- brick(files)

【问题讨论】:

  • 嗨,欢迎来到 SO。给定消息,首先要检查的是您的“路径”是否正确。如果你这样做file.exists(files),你会得到什么?
  • 您好,亲爱的先生@Ibusett,在我运行file.exist(files) 后,我得到以下信息:[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE对对对[21]对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对对 结果
  • 好的,所以这不是问题所在。我现在注意到您使用的是brick。请参阅下文了解可能的解决方案。

标签: r raster r-raster sp rgdal


【解决方案1】:

我猜你需要使用stack 而不是brick。按照brick的帮助,其实:

RasterBrick 是一个多层光栅对象。它们通常由 多层(带)文件;但它们也可以完全存在于内存中。 它们类似于 RasterStack(可以使用堆栈创建),但处理 使用 RasterBrick 时,时间应该更短。 但它们的灵活性较差,因为它们只能指向单个文件。

所以,如果我们尝试“堆叠”多个文件:

library(raster)
r <- raster(ncols = 100, nrows = 100, vals = 1:10000)
rfile1 <- tempfile(fileext = ".tif")
writeRaster(r, filename = rfile1)
rfile2 <- tempfile(fileext = ".tif")
writeRaster(r, filename = rfile2)

files_to_stack <- c(rfile1, rfile2)

这失败了:

brick(files_to_stack)
#> Warning in if (x == "" | x == ".") {: the condition has length > 1 and only
#> the first element will be used
#> Warning in if (!start %in% c("htt", "ftp")) {: the condition has length > 1
#> and only the first element will be used
#> Warning in if (fileext %in% c(".GRD", ".GRI")) {: the condition has length
#> > 1 and only the first element will be used
#> Warning in if (!file.exists(x)) {: the condition has length > 1 and only
#> the first element will be used
.....
#> Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...): Cannot create a RasterLayer object from this file.

虽然这有效:

stack(files_to_stack)
#> class       : RasterStack 
#> dimensions  : 100, 100, 10000, 2  (nrow, ncol, ncell, nlayers)
#> resolution  : 3.6, 1.8  (x, y)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
#> names       : file46e41bcd78e3, file46e43ea75bad 
#> min values  :                1,                1 
#> max values  :            10000,            10000

如果您想拥有brick 以进一步提高“效率” 处理,你可以将不同的“层”保存为多波段 tiff,然后使用砖打开:

rfile_multi <- tempfile(fileext = ".tif")
writeRaster(stack(files_to_stack), filename = rfile_multi)
brick(rfile_multi)

#> class       : RasterBrick 
#> dimensions  : 100, 100, 10000, 2  (nrow, ncol, ncell, nlayers)
#> resolution  : 3.6, 1.8  (x, y)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
#> data source : D:\RTemp\RtmpacXztJ\file4808784f268c.tif 
#> names       : file4808784f268c.1, file4808784f268c.2 
#> min values  :                  1,                  1 
#> max values  :              10000,              10000

reprex package (v0.2.1) 于 2018 年 11 月 10 日创建

【讨论】:

  • 哦..我现在明白了。它真的很有效.. 非常感谢您,先生,它解决了所有问题..
猜你喜欢
  • 1970-01-01
  • 2017-11-01
  • 2018-06-09
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
  • 1970-01-01
相关资源
最近更新 更多