I'm trying to implement a parallel process in order to transform a raster dem into terrain products like aspect, slope, etc. I doing so using future with the following code:
dem = raster("./dem/dem.asc")
output = "./output/"
crs(dem) <- epsg
plan(multiprocess, workers = availableCores()-1, gc = TRUE)
f1 %<-% terrain(dem, filename = paste0(output,"01_slope.asc"), opt = "slope", unit = 'degrees', neighbors = 8)
f2 %<-% terrain(dem, filename = paste0(output,"02_aspect.asc"), opt = "aspect", unit = 'degrees', neighbors = 8)
f1; f2
The processes start in parallel but it makes something weird: it produces both files, at the same time and each with his own name but, both files are exactly the same (in this case, both raster are slope rasters). What I am doing wrong ?
I finally used this function and it worked:
dempath = "./dem/dem.asc"
output = "./output/"
crs(dem) <- epsg
#CREATE DEM PRODUCTS IN PARALLEL
demproductspar <- function(dempath, output, epsg = crs('+init=epsg:25829'), cor = 3){
print ('Leyendo dem...')
dem = raster(dempath)
crs(dem) <- epsg
plan(multiprocess, workers = cor, gc = TRUE)
plan(multiprocess, workers = availableCores(), gc = TRUE)
f1 <- future({ terrain(dem, filename = paste0(output,"01_slope.asc"), opt = "slope", unit = 'degrees', neighbors = 8) })
f2 <- future({
terrain(dem, filename = paste0(output,"02_aspect.asc"), opt = "aspect", unit = 'degrees', neighbors = 8)
aspectreclass(aspectdir = paste0(output,"02_aspect.asc"), output = paste0(output,"02r_aspect.asc")) })
f3 <- future({ terrain(dem, filename = paste0(output,"03_roughness.asc"), opt = "roughness", unit = 'degrees', neighbors = 8) })
f4 <- future({ terrain(dem, filename = paste0(output,"04_tri.asc"), opt = "tri", unit = 'degrees', neighbors = 8) })
f5 <- future({ terrain(dem, filename = paste0(output,"05_tpi.asc"), opt = "tpi", unit = 'degrees', neighbors = 8) })
print ('Procesando dem...')
f1; f2; f3; f4; f5
gc()
}
demproductspar(dempath, output)