I am converting vector to raster in R. However the process was too long. Is there possibility to put the script into multithread or GPU processing in order to do it more faster?
My script to rasterized vector.
r.raster = raster()
extent(r.raster) = extent(setor) #definindo o extent do raster
res(r.raster) = 10 #definindo o tamanho do pixel
setor.r = rasterize(setor, r.raster, 'dens_imov')
r.raster
class : RasterLayer
dimensions : 9636, 11476, 110582736 (nrow, ncol, ncell)
resolution : 10, 10 (x, y)
extent : 505755, 620515, 8555432, 8651792 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
setor
class : SpatialPolygonsDataFrame
features : 5419
extent : 505755, 620515.4, 8555429, 8651792 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=24 +south +ellps=GRS80 +units=m +no_defs
variables : 6
names : ID,CD_GEOCODI, TIPO, dens_imov,area_m,domicilios1
min values : 35464, 290110605000001, RURAL, 0.00000003,100004,1.0000
max values : 58468, 293320820000042, URBANO, 0.54581673,99996, 99.0000
Best Answer
I tried to "parallelize" the function
rasterize
using theR
packageparallel
in this way:n
partsrasterize
every part separatelyIn my computer, the parallelized
rasterize
function took 2.75 times less than the no-parallelizedrasterize
function.Note: the code below download a polygon shapefile (~26.2 MB) from the web. You can use any SpatialPolygonDataFrame object. This is only an example.
Load libraries and example data:
Figure 1: Brazil SpatialPolygonsDataFrame plot
Simple thread example
Time in my laptop:
Multithread thread example
Figure 2: Brazil Raster plot
Time in my laptop:
More info about parallelization in R: