Use this R Code to convert and process vector to raster data fast.
Option 1
Option 1
# Load libraries
library('raster')
library('rgdal')
# Load a SpatialPolygonsDataFrame # Load shapefile Data_samplelibrary('parallel')<- system.file("external/lux.shp")
Data_sample2<- shapefile(Data_sample)# Define RasterLayer object r.raster <- raster() # Define raster extent extent(r.raster) <- extent() # Define pixel size res(r.raster) <- 0.1Data_sample2
# Rasterize system.time(.r <- rasterize(Data_sample2, r.raster))Data_sample2
Option 2
# Calculate the number of cores num_cores <- detectCores() - 1 # Number of polygons features features <- 1:nrow([,]) # Split features in n parts n <- 20 parts <- split(features, cut(features, n)) # Initiate cluster (after loading all the necessary object to R environment:Data_sample2, parts, r.raster, n) cl <- makeCluster(Data_sample2_cores, type = "FORK") print(cl) # Parallelize rasterize function system.time(rParts <- parLapply(cl = cl, X = 1:n, fun = function(x) rasterize(numData_sample2[parts[[x]],], r.raster))) # Finish stopCluster(cl) # Merge all raster parts and plot Merge_parts <- do.call(merge, rParts) plot(Merge_parts)


0 comments:
Post a Comment