[GIS] Monitoring Progress of projectRaster in R

coordinate systemrraster

I am new to R and I am having a few issues with this piece of code. The goal of it is to take set of daily temperature rasters of the United States, reproject them, and clip them. At the moment, I am doing this by creating a raster stack, projecting it, and then clipping it. When I run this block of code, I don't get any errors, but it gets hung up on the projectRaster step (at the moment I am trying to reproject a stack of 365 days).

library(raster)
library(rgdal)
#import area of interest file
boundary <- readOGR(dsn = 'D:/Scratch/DataConvert/Boundary' , layer = 
'AreaOfInterest' )

#define spatial reference
sr <- ('+proj=lcc +lat_1=45 +lat_2=49 +lat_0=44.25 +lon_0=-109.5 
+x_0=600000.0000000001 +y_0=0 +ellps=GRS80 +datum=NAD83 
+to_meter=0.3048006096012192 +no_defs ')

#project boundary
pr <- spTransform(boundary, crs(sr))

#Create 'images' list that is composed of daily temps for the entire US
images <- list.files(path = 'D:/Scratch/DataConvert/Daily/', pattern = 
'*.tif' , full.names = TRUE, recursive = FALSE)

#Create a raster stack of the images list 
s <- stack(images)

#Project the raster stack
s.proj <- projectRaster(s, crs = sr)

#Crop the raster stack to the area of interest
s.crop <- crop(s.proj, extent(pr))

#Save a clipped, projected image for each day
writeRaster(s.crop, filename = 'D:/Scratch/DataConvert/JAF_Clipped/clip', 
        bylayer =TRUE, progress='text' , suffix=names(s), format='GTiff' , 
        overwrite=TRUE)

It takes an extremely long time (>24hours) for this segment to run and I want to be able to monitor its progress. I read through this article that describes setting up a progress bar but I can't seem to make it work. This was one of the many ways I tried to implement the progress bar.

total <- 100
pb <- txtProgressBar(min = 0, max = total, style = 3)
s.proj <- projectRaster(s, crs = sr)
for(i in 1:total){
   Sys.sleep(0.1)
   # update progress bar
   setTxtProgressBar(pb, i)
}
close(pb)

Does anyone have any insight into incorporating a progress bar with the projectRaster function? Or does anyone know of a more efficient way to complete this task?

Best Answer

A progress bar like that can only show the progress of the loop that it is updated in. Your code creates a progress bar object, then does the raster projection, then it does a loop. So as it stands, the code will hit the projectRaster and stop there, with no progress. If the projectRaster completes, you'd then see a progress bar update by 100 0.1 second intervals.

To get progress within a single R function requires the author of that function to implement progress monitoring within it. projectRaster does not seem to have that functionality in it already, so you'd have to ask the author to do it.

If you want to know how long your projection step is going to take, try it on fewer layers, and at lower resolutions, and then scale that up to get an estimate.