how to efficiently import multiple raster (.tif) files into R
Asked Answered
J

4

11

I am an R novice, especially when it comes to spatial data. I am trying to find a way to efficiently import multiple (~600) single-band raster (.tif) files into R, all stored in the same folder. Not sure if this matters but note that, when viewed in a folder on my Mac and Windows Parallel VM, there are the following five (5) file formats for each .tif = .TIF; .tfw; .TIF.aux.xml; .TIF.ovr; .TIF.xml. At any rate, the following code (and other similar variants I've tried) does not seem to work:

library(sp)
library(rgdal)
library(raster)

#path to where all .tif files are located
setwd("/path/to/workingdirectory")

#my attempt to create a list of my .tif files for lapply
temp = list.files(pattern="*.tif")
temp #returns 'character(0)'

#trying to use the raster function to read all .tif files
myfiles = lapply(temp, raster)
myfiles #returns 'list()'

Is there a way to use some form of loop to import all raster files efficiently?

Julienne answered 10/10, 2018 at 18:50 Comment(1)
What do you mean tif = .TIF? R is case sensitive, so align to extension. Try pattern="*.TIF".Servant
J
12

I found the answer and will post the full code to help other beginner R-users who have this issue. To call a list element, use double square brackets [[]], like this:

#first import all files in a single folder as a list 
rastlist <- list.files(path = "/path/to/wd", pattern='.TIF$', 
all.files=TRUE, full.names=FALSE)

#import all raster files in folder using lapply
allrasters <- lapply(rastlist, raster)

#to check the index numbers of all imported raster list elements
allrasters

#call single raster element
allrasters[[1]]

#to run a function on an individual raster e.g., plot 
plot(allrasters[[1]])

Booyah. Thanks to Parfait for help.

Julienne answered 11/10, 2018 at 21:33 Comment(0)
L
8

if the rasters have the same extent you can simply load them in a stack

#first import all files in a single folder as a list 
rastlist <- list.files(path = "/path/to/wd", pattern='.TIF$', all.files=TRUE, full.names=FALSE)

library(raster)
allrasters <- stack(rastlist)
Lohrman answered 16/10, 2018 at 10:13 Comment(0)
E
4

Try terra:: package, ~3 times faster, see some benckmark:

library(terra)
library(raster)
#first import all files in a single folder as a list 
rastlist <- list.files(path = "/path/to/wd", pattern='.tif$', all.files= T, full.names= T)

library(microbenchmark)
mb <- microbenchmark(
  stk1 <- terra::rast(rastlist),
  stk2 <- raster::stack(rastlist),
  unit = "s",
  times= 3
)
mb

Loading 130 files dimensions: 2017, 2074 (nrow, ncol) I get:

Unit: seconds
                            expr       min        lq      mean    median
   stk1 <- terra::rast(rastlist) 0.6909961 0.7543023 0.7797057 0.8176084
 stk2 <- raster::stack(rastlist) 2.2608693 2.3052319 2.3328234 2.3495944
        uq       max neval cld
 0.8240604 0.8305125     3  a 
 2.3688005 2.3880065     3   b

For newcomers, just the code out from benchmark:

library(terra)
rastlist <- list.files(path = "/path/to/wd", pattern='.tif$', all.files= T, full.names= T)
stk1 <- terra::rast(rastlist)
Euniceeunuch answered 7/9, 2022 at 6:26 Comment(1)
For this test it was loading 130 rasters, with 2017 nrows and 2074 ncols.Euniceeunuch
J
0

Right, OK I think the following code worked:

rastlist <- list.files(path = "/path/to/wd", pattern='.TIF$', all.files=TRUE, 
full.names=FALSE)
lapply(rastlist, raster)

But now how to I access an individual raster file for further analyses?

Julienne answered 10/10, 2018 at 19:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.