1

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!

ExploreR
  • 313
  • 4
  • 15

3 Answers3

1

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)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
0

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"
gacero
  • 118
  • 9
  • thanks for your answer! yes, i have noticed the same, but unfortunately i does not work, i just get the error message `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` instead... it is really annoying, it seems like something is messed up when i want to point to the dataset... i have already studied the help page of the function, but this does not help as well. – ExploreR Aug 13 '19 at 09:45
0

base::readLines() does it somehow!

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