I have about 50.000 small glacier rasters (rectangular shape) that need to be clipped to the glacier's extent. I have the glacier polygons stored in a large SpatialPolygonsDataFrame.

I want to use parallel computing or else it would take forever, I think. For previous tasks I've successfully used the mcmapplyfunction, but I am open for other approaches.

My (admittedly rudimentary) code so far is:

filenames <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=F)
filelocations <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=T)
glaciers <- readOGR("/.../13_rgi60_CentralAsia.shp",verbose=TRUE)

fun_clip <- function(filelocations, filenames, glaciers){
  r <- raster(filelocations)
  r <- crop(r,glaciers) # here I need to clarify the corresponding shp in the SPDF
  writeRaster(r, paste0("/.../RGI60-13_crop/",filenames))

mcmapply(fun_proj, filelocations, filenames, mc.cores = 50)

How can I give the crop-function the right iterative arguments? filelocationis of the same length as glaciers, so in a for-loopI would use something like r <- crop(r,glaciers[i]), but how do I pass the iteration in my kind of function? What would be the way to introduce the i, so to speak?

1 Answer 1


"filelocation is of the same length as glaciers" and in the same order? In which case:

for(i in 1:length(filelocations)){
  r = raster(filelocations[i]) # get the ith file name
  g = glaciers[i,] # get the i'th row of `glaciers`.
  r = crop(r, g) # crop
  writeRaster(r, file.path(outputdest, filenames[i])) # save in outputdest
  • Thanks! Something like that was my first approach, too, but it was taking forever, so I was hoping for a solution that would allow for computing on different cores (using for example rslurm or parallel)... Nov 26, 2019 at 7:40
  • You can modify the above to use foreach and %dopar% - cran.r-project.org/web/packages/doParallel/vignettes/…
    – Spacedman
    Nov 26, 2019 at 8:45
  • I just found out that we are required to use the rslurm package for our cluster, so I will open a new question directly about that. Nov 26, 2019 at 14:00

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.