如何在 R 中加载 .tif 光栅文件 [英] How to load .tif raster files in R
问题描述
我目前正在研究一些光栅模型.我的代码生成了大约 4k 个模型(所有 .tif 文件),但其中大约 10% 无法使用 load(insert path")
打开.我尝试过 readr()
、readRDS()
和 source()
,但都没有奏效.我也重新运行了一些模型,但它没有解决问题.为什么我无法加载 .tif 文件?
load("comparison/mc45bi50/ni1130region33.tif")
<块引用>
加载错误(comparison/mc45bi50/ni1130region33.tif"):坏恢复文件幻数(文件可能已损坏)——没有加载数据另外:警告消息:文件‘ni1130region33.tif’有幻数'II*' 不推荐使用 2 之前的保存版本
source("comparison/mc45bi50/ni1130region33.tif")
<块引用>
源错误(比较/mc45bi50/ni1130region33.tif"):
比较/mc45bi50/ni1130region33.tif:2:0:输入1意外结束:二*
您应该使用 raster
库.它有 raster()
函数,它应该读取您的 .tif 文件.
例如,自己创建一个光栅,并将其保存在磁盘上,然后尝试读取:
库(光栅)#>加载需要的包:sp##创建光栅r <- 光栅(ncols=3,nrows=3)r[] <- 1:9# 保存到磁盘tmp <- paste(tempdir(), "myraster.tif", sep = "/")writeRaster(r, tmp)## 现在读r <- 光栅(tmp)
I am currently working on some raster models. My code has generated about 4k models (all .tif files), but about 10% of these cannot be opened using load("insert path")
. I have tried readr()
, readRDS()
and source()
, none of which worked. I have also reran some models but it didn't fix the problem. Why can't I load my .tif files?
load("comparison/mc45bi50/ni1130region33.tif")
Error in load("comparison/mc45bi50/ni1130region33.tif") : bad restore file magic number (file may be corrupted) -- no data loaded In addition: Warning message: file ‘ni1130region33.tif’ has magic number 'II*' Use of save versions prior to 2 is deprecated
source("comparison/mc45bi50/ni1130region33.tif")
Error in source("comparison/mc45bi50/ni1130region33.tif") :
comparison/mc45bi50/ni1130region33.tif:2:0: unexpected end of input 1: II*
You should use the raster
library. It has the raster()
function, which should read your .tif file.
Example, creating a raster ourselves, and saving it on disk, Then try to read:
library(raster)
#> Loading required package: sp
## create raster
r <- raster(ncols=3, nrows=3)
r[] <- 1:9
# save to disk
tmp <- paste(tempdir(), "myraster.tif", sep = "/")
writeRaster(r, tmp)
## read now
r <- raster(tmp)
这篇关于如何在 R 中加载 .tif 光栅文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!