R 如何读取 ENVI .hdr-file?



要提取 ENVI .hdr 文件的特定信息,我想使用 caTools::read 将其读入 R。环境()。不幸的是,R只是抛出错误消息,我不知道如何解决这个问题。

到目前为止,我尝试的如下:

# install.packages("rgdal")
# install.packages("raster")
# install.packages("caTools")
library("rgdal")
library("raster")
library("caTools")
hdr_dir <- "D:/ExploreR/X_Test/01_data/dataset.hdr"
hdr_file <- read.ENVI(hdr_dir, headerfile = paste(hdr_dir, ".hdr", sep = ""))
# Error in read.ENVI(hdr_dir, headerfile = paste(hdr_dir, ".hdr", sep = "")) : read.ENVI: Could not open input header file: D:/ExploreR/X_Test/01_data/dataset.hdr.hdr

有人知道如何解决这个问题吗?非常感谢您的帮助!

这应该有效

read.ENVI("D:/ExploreR/X_Test/01_data/dataset.hdr")

在代码中,应将创建文件名与使用它分开。您在创建文件名时出错。首先创建它,将其分配给一个变量,然后使用file.exists测试它是否存在。此外,要创建文件名,最好使用file.path而不是paste.

library("caTools")
dirname <- "D:/ExploreR/X_Test/01_data/"
filename <- file.path(dirname, "dataset.hdr")
file.exists(filename)
x <- read.ENVI(filename)

或者干脆

f <- "D:/ExploreR/X_Test/01_data/dataset.hdr"
x <- read.ENVI(f)

文件名是dataset还是dataset.hdr? 似乎您正在两次编写扩展名".hdr"。

它将是:

hdr_dir <- "D:/ExploreR/X_Test/01_data/dataset"

base::readLines() 以某种方式做到了!

hdr_file <- readLines(con = "D:/ExploreR/X_Test/01_data/dataset.hdr")

最新更新