0

I'm new at using GIS with R and I'm trying to open an ENVI file containing hyperspectral data following the suggestions from this post https://stackoverflow.com/questions/57474186/r-how-to-read-envi-hdr-file, but I don't seem to be able to do so. I tried three different approaches but all of them failed. I also can't seem to find any other posts where my problem is described.

# install.packages("rgdal")
# install.packages("raster")
# install.packages("caTools")
library("rgdal")
library("raster")
library("caTools")

dirname <- "S:/LAB-cavender/4_Project_Folders/oakWilt/oak_wilt_image_analyses/R_input/6.15.2021 - Revisions/ENVI export/AISA/Resampled_flights"
filename <- file.path(dirname, "AISA_Flight_4_resampled")
file.exists(filename)
  1. The first option that I tried was using file name only
    x <- read.ENVI(filename)

But I got the following error message:

#Error in read.ENVI(filename) :
#  read.ENVI: Could not open input file: S:/LAB-cavender/4_Project_Folders/oakWilt/oak_wilt_image_analyses/R_input/6.15.2021 - Revisions/ENVI export/AISA/Resampled_flights/AISA_Flight_4_resampled
#In addition: Warning message:
#  In nRow * nCol * nBand : NAs produced by integer overflow
  1. I tried then the second option which is using file name + header file name read using file.path
    headerfile <- file.path(dirname, "AISA_Flight_4_resampled")
    x <- read.ENVI(filename = filename,headerfile = headerfile)

Again, I got an error message that says:

#Error in read.ENVI(filename = filename, headerfile = headerfile) :
#  read.ENVI: Could not open input header file: S:/LAB-cavender/4_Project_Folders/oakWilt/oak_wilt_image_analyses/R_input/6.15.2021 - Revisions/ENVI export/AISA/Resampled_flights/AISA_Flight_4_resampled
  1. Finally, I tried the third option by using file name + header file name read using readLines
hdr_file <- readLines(con = "S:/LAB-cavender/4_Project_Folders/oakWilt/oak_wilt_image_analyses/R_input/6.15.2021 - Revisions/ENVI export/AISA/Resampled_flights/AISA_Flight_4_resampled.hdr")
x <- read.ENVI(filename = filename,headerfile = hdr_file)

But I got the error message:

#Error in read.ENVI(filename = filename, headerfile = hdr_file) :
#  read.ENVI: Could not open input header file: ENVIdescription = {  Spectrally Resampled File. Input number of bands: 63, output number of bands: 115. [Fri Jun 25 16:57:21 2021]}samples = 5187lines   = 6111bands   = 115header offset = 0file type = ENVI Standarddata type = 4interleave = bilsensor type = Unknownbyte order = 0map info = {UTM, 1.000, 1.000, 482828.358, 5029367.353, 7.5000000000e-001, 7.5000000000e-001, 15, North, WGS-84, units=Meters}coordinate system string = {PROJCS["UTM_Zone_15N",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-93.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]}default bands = {46,31,16}wavelength units = Nanometersdata ignore value = -9999.00000000e+000band names = { Resampled
#    In addition: Warning message:
#      In if (!file.exists(headerfile)) stop("read.ENVI: Could not open input header file: ",  :
#                                              the condition has length > 1 and only the first element will be used
0

1 Answer 1

1

To read an ENVI file with read.ENVI, you give the data file as the first parameter, and optionally the header file as the second parameter.

For example, I have two files, ice (with NO extension) and ice.hdr.

enter image description here

I can then do either of these two:

> re = read.ENVI("/tmp/test/data/ice")
> re = read.ENVI("/tmp/test/data/ice", headerfile="/tmp/test/data/ice.hdr")

Make sure for you both these parts exist. Try using file.info:

> file.info("/tmp/test/data/ice")
                     size isdir mode               mtime               ctime
/tmp/test/data/ice 136192 FALSE  644 2021-06-29 12:02:31 2021-06-29 12:06:07
                                 atime   uid  gid    uname   grname
/tmp/test/data/ice 2021-06-29 12:06:38 12818 1001 rowlings rowlings
> file.info("/tmp/test/data/ice.hdr")
                       size isdir mode               mtime               ctime
/tmp/test/data/ice.hdr  230 FALSE  644 2021-06-29 12:02:36 2021-06-29 12:02:36
                                     atime   uid  gid    uname   grname
/tmp/test/data/ice.hdr 2021-06-29 12:02:50 12818 1001 rowlings rowlings

Alternatively, try the raster function from the raster package, and feed it the path to the data file (not the header file) but the header file must be the name of the data file with .hdr on:

> rr = raster("/tmp/test/data/ice")
> 

If none of this helps then I think we need your data files - if you can put them on a sharing site (Dropbox, OneDrive, etc etc) and make them available then we have more chance of helping.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.