我正在尝试并行处理栅格列表并使用parLapply执行焦点功能。我想我误解了一些至关重要的事情。代码运行,但看起来它没有在我的驱动器上正确写出焦点功能。同样,它看起来像是在列表中的第一个栅格上执行了两次密度_Function ....对并行处理世界来说是新的,并且想知道是否有关于如何处理这个问题的建议?只是旁注,当我运行我的Density_Function并使用lapply列表时,它可以工作。我如何并行处理这个?
`# Density function
Density_Function <- function (raster_layer){
weight <- focalWeight(raster_layer,90,type = "circle")
raster_name <- names(raster_layer)
short_name <- substr(raster_name,1,4)
half_output <- "X:/Path"
full_output <- paste0(half_output,short_name,"_90m.tif")
focal(raster_layer, weight, fun=sum, full_output, na.rm=TRUE, pad=TRUE, NAonly=FALSE, overwrite=TRUE)
}
#Bring in raster data and create list
roads_raster <-raster('X:/roads.tif')
pipe_raster <-raster('X:/pipes.tif')
raster_list <- list(roads_raster,pipe_raster) `
#Activate cluster
no_cores <- detectCores() - 1
cl <- makeCluster(no_cores)
#Apply function
parLapply(cl = cl, x = raster_list, fun = Density_Function)
#Close cluster
stopCluster(cl)
答案 0 :(得分:1)
我采取了不同的方法,但最终得到了我的意图。我没有使用parLapply,而是使用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)