我尝试通过这种方式使用程序包“并行化”功能:rasterize
R
parallel
- 将SpatialPolygonsDataFrame对象分成几
n
部分
rasterize
每个部分分别
- 将所有零件合并为一个栅格
在我的计算机中,并行化rasterize
功能比无并行化功能少2.75倍rasterize
。
注意:以下代码从网上下载了一个多边形shapefile(〜26.2 MB)。您可以使用任何SpatialPolygonDataFrame对象。这仅仅是一个例子。
加载库和示例数据:
# Load libraries
library('raster')
library('rgdal')
# Load a SpatialPolygonsDataFrame example
# Load Brazil administrative level 2 shapefile
BRA_adm2 <- raster::getData(country = "BRA", level = 2)
# Convert NAMES level 2 to factor
BRA_adm2$NAME_2 <- as.factor(BRA_adm2$NAME_2)
# Plot BRA_adm2
plot(BRA_adm2)
box()
# Define RasterLayer object
r.raster <- raster()
# Define raster extent
extent(r.raster) <- extent(BRA_adm2)
# Define pixel size
res(r.raster) <- 0.1
图1:巴西SpatialPolygonsDataFrame图
简单线程示例
# Simple thread -----------------------------------------------------------
# Rasterize
system.time(BRA_adm2.r <- rasterize(BRA_adm2, r.raster, 'NAME_2'))
我的笔记本电脑上的时间:
# Output:
# user system elapsed
# 23.883 0.010 23.891
多线程线程示例
# Multithread -------------------------------------------------------------
# Load 'parallel' package for support Parallel computation in R
library('parallel')
# Calculate the number of cores
no_cores <- detectCores() - 1
# Number of polygons features in SPDF
features <- 1:nrow(BRA_adm2[,])
# Split features in n parts
n <- 50
parts <- split(features, cut(features, n))
# Initiate cluster (after loading all the necessary object to R environment: BRA_adm2, parts, r.raster, n)
cl <- makeCluster(no_cores, type = "FORK")
print(cl)
# Parallelize rasterize function
system.time(rParts <- parLapply(cl = cl, X = 1:n, fun = function(x) rasterize(BRA_adm2[parts[[x]],], r.raster, 'NAME_2')))
# Finish
stopCluster(cl)
# Merge all raster parts
rMerge <- do.call(merge, rParts)
# Plot raster
plot(rMerge)
图2:巴西栅格图
我的笔记本电脑上的时间:
# Output:
# user system elapsed
# 0.203 0.033 8.688
有关R中并行化的更多信息: