Canada2015
Canada2015

Reputation: 65

How to convert Tiff to IMG (ERDAS) format using R programming?

I have hundreds of satellite images in Tiff format in a working directory, that firstly, must be multiplied by 2 , then converted to IMG (ERDAS) format, but in the same name as input file.

For example:

input:

MOD11A1_2009-04-01.LST_Day_1km.tiff

output:

MOD11A1_2009-04-01.LST_Day_1km.img

So, the first step is to multiply by 2 Then convert from the image from tiff to IMG format.

I wold be very grateful if an R Programming Expert could help me to do so.

Thanks in advance.

Upvotes: 1

Views: 3932

Answers (2)

Robert Hijmans
Robert Hijmans

Reputation: 47146

And here is dickoa's solution within a loop:

library(raster)
input <- list.files(pattern='tif$')
output <- gsub("tif", "img", input)

for (i in seq(along=input)) {
   r <- raster(input[i])
   # using "2 byte unsigned integer" data type
   r <- calc(r, fun = function(x) x * 2, datatype='INT2U', filename = output[i])
}

Upvotes: 2

dickoa
dickoa

Reputation: 18437

You can use the raster package

library(raster)
input <- "MOD11A1_2009-04-01.LST_Day_1km.tif"
output <- gsub("tif", "img", input)

r <- raster(input)
r <- r * 2
writeRaster(r, output, format = "HFA")

You can also use a loop to process all the hundred files

EDIT

As pointed out by @RobertH, we can simplify this code using the filename param of the calc function.

library(raster)
input <- "MOD11A1_2009-04-01.LST_Day_1km.tif"
output <- gsub("tif", "img", input)
r <- raster(input)
r <- calc(r, fun = function(x) x * 2, filename = output)

Thanks a lot @RobertH (for the raster package too)

Upvotes: 4

Related Questions