6

I think that must be a "simple" way to import several Geotiff files into R instead of copy and paste n times Rfd<-readGDAL() and then save them as individual objects.

I found the following code in a post regarding how to import multiple shapefiles ...

get all files with the .shp extension from working directory setwd("D:/GIS_DataBase/GIS_Tirol/Tirol_Verbreitungskarten/Verbreitungs_Daten") shps <- dir(getwd(), ".shp") the assign function will take the string representing shp and turn it into a variable which holds the spatial points data for (shp in shps) assign(shp, readShapePoints(shp)) plot(get(shp[1])) # i.e. ...done*

But it does not translate to raster files with readGDAL {rgdal} function.

Other post suggested the use of for or/and function, when importing several *.csv files into R. However, I have been equally unsuccessful into make it work.

  • Try library(raster); r <- stack(list.files(pattern="tif$", full.names=FALSE)) ## you can convert to sp/rgdal format if need be – mdsumner Feb 21 '15 at 0:52
6

A simple for loop will suffice. You can use readGDAL in the rgdal package but I would recommend raster in the raster package. You have to be a bit tricky and use strsplit in the assign function to strip off the ".tif" file extension.

setwd("C:/rasters")
rlist=list.files(getwd(), pattern="tif$", full.names=FALSE)
for(i in rlist) { assign(unlist(strsplit(i, "[.]"))[1], raster(i)) } 

If your data aligns (same resolution, origin coordinates, extent, number of rows and columns) then it is is as simple as passing the rlist object to the raster stack or brick function. This results in a single object containing all of the rasters. Even if your rasters are not aligned, you can use the "quick=TRUE" argument, which will skip consistency checks. The resulting stack object will contain all of the designated rasters and you can slit them into individual raster class objects.

Both the stack and brick functions have internal looping and will accept a list of rasters and the rasters can be single or multiple band.

  • hello, thank you for all the advise. It resolved the problems – Silvia Mar 4 '15 at 23:25
2

This code will create a raster brick which needs all the rasters to be of the same extent. You could tweak it for creating a raster stack. I'm assuming your files in the working directory and are named Band*.tif

library(raster) # load the raster package
brk <- do.call(brick, lapply(list.files(path = "./", pattern = "Band*.*tif"), raster))
-1

It really depends on how your files are structured, namely if the have multiple bands. Assuming that you have only one band in each file you can use the following code to make a rasterstack. Using the 'raster' library makes it easier.

library(raster)
datafiles <- Sys.glob("*.tif") #Or whatever identifies your files
resultingStack <- stack()
for(i in 1:NROW(datafiles)){
  tempraster <- raster(datafiles[i])
  resultingStack <- stack(resultingStack,tempraster)
}

This will iterate over all the rasters and stack them into one big rasterStack that can be used for processing.

  • The for loop is not necessary and multiband rasters are irrelevant. If aligned, you can just pass "datafile" straight to the "stack" function (ie., r <- stack(datafiles) ). Even if not aligned you could use the quick=TRUE argument, which skips consistency checks, then split out the rasters in the stack into individual objects. Also, since datafiles is a character vector, there are no rows associated. Where "NROW" will work, it is more suited to arrays. The "length" function would be more appropriate for a vector object. The lowercase instance "nrow" will return NULL. – Jeffrey Evans Feb 21 '15 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.