Reputation: 563
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?
Upvotes: 11
Views: 19875
Reputation: 408
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)
Upvotes: 4
Reputation: 181
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)
Upvotes: 8
Reputation: 563
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.
Upvotes: 12
Reputation: 563
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?
Upvotes: 0