Search code examples
rrasterenvi

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!


Solution

  • 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)