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.