rral
rral

Reputation: 584

raster images stacked recursively

I have the following problem, please. I need to read recursively raster images, stack and store them in a file with different names (e.g. name1.tiff, name2.tiff, ...)

I tried the following:

 for (i in 10) {
   fn <- system.file ("external / test.grd", package = "raster")
   fn <-stack (fn) # not sure if this idea can work.
   fnSTACK[,, i] <-fn
 }

here expect a result of the form:

dim (fnSTACK)

[1] 115 80 10

or something like that

but it didn't work.

Actually, I have around 300 images that I have to be store with different names. The purpose is to extract time series information (if you know another method or suggestions I would appreciate it)

Any suggestions are welcomed. Thank you in advance for your time.

Upvotes: 1

Views: 1102

Answers (4)

JEquihua
JEquihua

Reputation: 1226

What I would first do is put all your *.tiff in a single folder. Then read all their names into a list. Stack them and then write a multi-layered raster. I'm assuming all the images have the same extent and projection.

    ### Load necessary packages
library(tiff)
library(raster)
library(sp)
library(rgdal)   #I cant recall what packages you might need so this is probably 
library(grid)    # overkill
library(car)

############ function extracts the last n characters from a string  
############ without counting the last m
subs <- function(x, n=1,m=0){
  substr(x, nchar(x)-n-m+1, nchar(x)-m)
  }



setwd("your working directory path") # you set your wd to were all your images are
filez <- list.files() # creates a list with all the files in the wd
no <- length(filez) # amount of files found
imagestack <- stack() # you initialize your raster stack

for (i in 1:no){

  if (subs(filez[i],4)=="tiff"){

  image <- raster(filez[i]) # fill up raster stack with only the tiffs

  imagestack <- addLayer(imagestack,image)
   }
}

writeRaster(imagestack,filename="output path",options="INTERLEAVE=BAND",overwrite=TRUE)
# write stack

I did not try this, but it should work.

Upvotes: 2

rral
rral

Reputation: 584

Another method for stacking

 library(raster)       
 list<-list.files("/PATH/of/DATA/",pattern="NDVI",
            recursive=T,full.names=T)
 data_stack<-stack(list)

Upvotes: 1

Robles
Robles

Reputation: 11

thanks "JEquihua" for your suggestion, just need to add the initial variable before addLayer ie:

for (i in 1:no){

if (subs(filez[i],4)=="tiff"){

image <- raster(filez[i]) # fill up raster stack with only the tiffs

     imagestack <- addLayer(imagestack,image)

}

}

And sorry "RobertH", I'm newbie about R. I will be ask, more sure or exact by next time.

Also, any suggestions for extracting data from time series of MODIS images stacked. Or examples of libraries: "rts ()", "ndvits ()" or "bfast ()"

Greetings to the entire community.

Upvotes: 1

Robert Hijmans
Robert Hijmans

Reputation: 47381

Your question is rather vague and it would have helped if you had provided a full example script such that it could be more easily understood. You say you need to read several (probably not recursively?) raster images (files, presumably) and create a stack. Then you need to store them in files with different names. That sounds like copying the files to new files with a different names, and there are R functions for that, but that is probably not what you intended to ask.

if you have a bunch of files (with full path names or in the working directory), e.g. from list.files()

 f <- system.file ("external/test.grd", package = "raster")
 ff <- rep(f, 10)

you can do

 library(raster)
 s <- stack(ff)

I am assuming that you simply need this stack for operations in R (it is an object, but not a file). You can extract the values in many ways (see the help files and vignette of the raster package). If you want a three dimensional array, you can do

 a <- as.array(s)
 dim(a)
 [1] 115  80  10

Upvotes: 1

Related Questions