Load an img in R

Hello all,

I am doing some processing on S1 SLC images and would like to load the resulting images in an R script. But I get the error “rasterio.errors.RasterioIOError: ‘xxx.img’ not recognized as a supported file format”
Do you know if there is an R library enabling me to load these images ? Do I have to change something to my process to produce something else than “.img” files ? Are “.img” files just disguised tif’s ?

Thanks for any help

Sebastien Effinier

the rasters files inside the data folder of BEAM DIMAP products processed by SNAP are of ENVI format, consisting of a img file (the actual raster) and a hdr file (metadata) with the same name.

You can load them with the gdal module

import gdal

data = gdal.Open(raster.img)        
b = data.GetRasterBand(1)
array = b.ReadAsArray()

Thanks a lot for the quick response
We are going to test this

Euh… this looks like python script and not R…

oh, sorry - I somehow overlooked this - yes…

R is even easier I guess:
https://www.rdocumentation.org/packages/caTools/versions/1.17.1/topics/read.ENVI%20%26%20write.ENVI

Thanks again :slight_smile:

This code works:
img <- read.ENVI(imgfname, hdrfname)
rasterImg <- raster(img)
writeRaster(rasterImg, tiffname, format=“GTiff”)

we can then call gdalwarp on the converted file to reproject (which is much quicker than using the R libraries on the rasterImg directly)

Thanks again ABraun for the assistance

1 Like

You should be able to read ENVI files directly in gdalwarp.

that is true. I also often work with the .img files directly using the gdal libraries. Maybe seffinier has some additional processing steps which are only offered in r.

It is a bit late, but with the terra package we can also load dimap files into R.

For example, say we have a dimap file DEM_TC, which has a band elevation_HH, that we want to load into R (as a SpatRaster object).
We can read elevation_HH like this:

library(terra)
elevation <- rast("parent_folder/DEM_TC.data/elevation_HH.img")

We need to supply the img file inside the **.data folder of the dimap file. And, it is also possible to read multiple bands into the same spatRaster by supplying the paths as a vector of paths.

For example, for a dimap file “my_file”

library(terra)
elevation <- rast( c("parent_folder/my_file.data/band_1.img", "parent_folder/my_file.data/band_2.img") )