2

Does anyone already paralleled mosaic function for rasters in R?

It seems that snow still doesn´t deal with this on an automatic way.

The idea is something like this:

r1<-stack("fit__test23KLR.img")
r2<-stack("fit__test23KKR.img")
r3<-stack("fit__test23KKS.img")
r4<-stack("fit__test23KLS.img")

ff <- function(x1, x2, x3, x4) mosaic(x1, x2, x3, x4, fun="mean") 

beginCluster(35) 
cl <-getCluster() 
m1 <- clusterR(r1, r2, r3, r4, fun = ff, export="mosaic") 
endCluster() 

Each stack r1, r2.... are:

dimensions  : 2516, 412, 1036592, 93  (nrow, ncol, ncell, nlayers)
resolution  : 30, 30  (x, y)
range between: -2.5 to 2.5
  • 1
    How many rasters are you mosaicing, and what are you doing in overlap regions? Because you might be able to write mosaic(r1,r2,r3,r4) as mosaic(mosaic(r1,r2),mosaic(r3,r4)) and you could do the inner mosaics in parallel. – Spacedman Jul 14 '17 at 23:23
1

I found a reasonable solution using the gdalUtils package:

library(gdalUtils)

gdalbuildvrt("fit__test23KLR.img", "r1.vrt", sd=1)
gdalbuildvrt("fit__test23KKR.img", "r2.vrt", sd=1)
gdalbuildvrt("fit__test23KKS.img", "r3.vrt", sd=1)
gdalbuildvrt("fit__test23KLS.img", "r4.vrt", sd=1)

gdalbuildvrt(c("r1.vrt", "r2.vrt", "r3.vrt", "r4.vrt"), "m1.vrt", sd=4)

gdalwarp("m1.vrt", "finalmosaic.tif",
         t_srs="+proj=longlat +datum=WGS84 +no_defs", multi=TRUE)

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.