3

I have 1000 multi-layer rasters and have to export each separate layer to a tif.

Some friends recommended R to do the job. I used the following code:

library(raster)

namesVector<- c("layer1", "layer2", etc) #contain layer names

stack <- stack("multiLayerRaster.tif") #20 layers, total 2 GB

s <- unstack(stack)

for(i in seq_along(s)){writeRaster(s[[i]], file=namesVector[i])}

The code works fine, but it is very slow, it takes about 20 min to export layers from one multiLayerRaster.

Is there anyway to make the process faster?

Any advice is highly welcomed!

4
  • 1
    I don't think there's any need to unstack, just use s <- stack("multiLayerRaster.tif"). Don't call your object "stack". Loop on seq_len(nlayers(s)) and you'll have to add .tif to the file name otherwise it will be "native raster" format. There are also options to tile and compress tifs.
    – mdsumner
    Jun 2, 2014 at 3:37
  • 1
    I would recommend gdal_translate for this kind of processing, though. Or, even better, just build vrt's using gdalbuildvrt so that you don not duplicate your images.
    – radouxju
    Jun 2, 2014 at 14:02
  • mdsumner: thanks, I did that already, sorry that I didn't make it clear
    – user22364
    Jun 3, 2014 at 21:06
  • radouxju: I will try that and see if it is faster, thanks for the tip
    – user22364
    Jun 3, 2014 at 21:07

1 Answer 1

1

I dont think there´s a faster way to do that, but what you can do is a list in a table with the name of each multilayerraster with an identifier (column names: id & name, in this order). So you can write this:

#load the table with the name of the image & id
list<-read.table("Table.txt",header=T)

# select "automatic" correlative id 
id<-id+1

#select the name of your image from the table
for(i in 1:length(list[,1])) {if (list[i,1]==id) {image<-list[i,2]} }
image

#set work directory
setwd(your directory)

#load image
stack<-stack(image)

then you can run x times your script!!!

0

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.