我采取了不同的方法,但最终得到了我想要的。我没有使用parlappy,而是使用foreach循环遍历光栅列表,并行执行密度函数。
这个博客真的很有帮助:
http://www.gis-blog.com/increasing-the-speed-of-raster-processing-with-r-part-23-parallelisation/
library(doParallel)
library(foreach)
#Density function, 1km circular radius
Density_Function_1000 <- function (raster_layer){
raster_name <- names(raster_layer)
short_name <- substr(raster_name,1,4)
weight <- focalWeight(raster_layer,1000,type = "circle")
half_output <- "X:/Path"
full_output <- paste0(half_output,short_name,"_1km.tif")
focal(raster_layer, weight, fun=sum, full_output, na.rm=TRUE, pad=TRUE, NAonly=FALSE, overwrite=TRUE)
}
#Define how many cores you want to use
UseCores <- detectCores() -1
#Register CoreCluster
cl <- makeCluster(UseCores)
registerDoParallel(cl)
#Create my list of rasters
raster_list <- list(roads_raster, cuts_raster, wells_raster, seis_raster, pipes_raster, fires_raster)
#Use foreach loop and %dopar% command to execute my density function in parallel
foreach(i = raster_list) %dopar% {
library(raster)
Density_Function_1000(i)
}
#end cluster
stopCluster(cl)