R 如何读取 ENVI .hdr 文件?

R how to read ENVI .hdr-file?

为了提取 ENVI .hdr 文件的特定信息,我想使用 caTools::read.ENVI() 将其读入 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

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

文件名是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")

这应该有效

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)