ExploreR
ExploreR

Reputation: 343

R how to read ENVI .hdr-file?

To extract specific information of a ENVI .hdr-file I want to read it into R, using caTools::read.ENVI(). Unfortunately R just throws ERROR messages and I do not have a clue how to solve this problem.

What I have tried so far is following:

# 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

Does anybody know how to solve this problem? Thanks a lot for your help in advance!

Upvotes: 1

Views: 3379

Answers (3)

Robert Hijmans
Robert Hijmans

Reputation: 47491

This should work

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

In your code, you should separate creating the file name from using it. You made a mistake in the creating of a filename. Create it first, assign it to a variable, and test if it exists with file.exists. Also, to create filenames it is better to use file.path than paste.

library("caTools")

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

x <- read.ENVI(filename)

Or simply

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

Upvotes: 1

ExploreR
ExploreR

Reputation: 343

base::readLines() does it somehow!

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

Upvotes: 0

gacero
gacero

Reputation: 128

The filename is dataset or dataset.hdr? It seems like you are writing the extension ".hdr" twice.

It would be:

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

Upvotes: 0

Related Questions