Dear All, Thanks a lot for your brilliant replies. It does work (fast) now!
Jerome 2015-07-14 6:02 GMT+02:00 Robert J. Hijmans <[email protected]>: > Jerome, > > rasterize is indeed a bit slow, it is #1 on the list of functions that > need a rewrite for speed. However, I think your assumption is wrong. > You can rasterize all fields in one step, and it is pretty quick for > the example data: > > library(raster) > polyg <- shapefile("polyg.shp") > grain <- 50 > nsc <- 4 > r <- raster(polyg, res=c(grain, grain)) > > x <- rasterize(polyg, r) > # perhaps followed by > y <- deratify(x) > > > So clustering is inefficient here, but I think the below shows how you > would do athat, using much simplified code (whenever you think you > need a construction like "eval(parse(text=paste(", you are wrong). > > library(raster) > polyg <- shapefile("polyg.shp") > > library(doParallel) > cl <- makeCluster(4) > registerDoParallel(cl) > grain <- 50 > nsc <- 4 > r <- raster(polyg, res=c(grain, grain), crs=crs(polyg)) > > rr <- foreach(i= 1:nsc, .packages="raster", .combine=c) %dopar% { > rasterize(polyg, r, field=paste0("sc", i)) > } > > stopCluster(cl) > > > > Best, Robert > > On Mon, Jul 13, 2015 at 3:03 PM, Jérome Mathieu <[email protected]> > wrote: > > Dear all, > > > > I need to rasterize the same shapefile according to different fields > stored > > in the @data of the shapefile. So I need to loop trough the fields (named > > sc1 to sc27) to rasterize upon each field. With a regular sequential loop > > it takes a very long time (much longer than in ArcMap), so I would like > to > > parallelize it, but I didn't succeed so far. > > > > I tried : > > > > # read shapefile > > library(maptools) > > polyg<-readShapePoly("D:\\polyg.shp") > > polyg@proj4string <- CRS("+init=epsg:2154") > > > > library(doParallel) > > cl <- makeCluster(4) > > registerDoParallel(cl) > > > > grain<-50 > > nsc<-4 > > > > foreach(idxsc = 1 : nsc , .packages="raster", polyg=polyg) %dopar% { > > > > > eval(parse(text=paste("RRpolyg_",idxsc,"<-raster(polyg,res=c(grain,grain))",sep=""))) > > # create raster template > > eval(parse(text=paste("projection(RRpolyg_",idxsc,") <- > > proj4string(polyg)",sep=""))) # projection > > > eval(parse(text=paste("RRpolyg_",idxsc,"<-rasterize(polyg,RRpolyg_",idxsc,",field=polyg@data > $sc",idxsc,")",sep=""))) > > # rasterization > > > > } > > > > stopCluster(cl) > > > > and I get the following error : > > > > Error in { : > > task 1 failed - "unable to find an inherited method for function > 'raster' > > for signature '"numeric"'" > > > > > > the data are here > > > > http://we.tl/7qqegfLoBA > > > > > > Any help would be greatly appreciated ! > > > > Jerome > > > > > >> sessionInfo() > > R version 3.1.3 (2015-03-09) > > Platform: x86_64-w64-mingw32/x64 (64-bit) > > Running under: Windows 7 x64 (build 7601) Service Pack 1 > > > > locale: > > [1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 > > LC_MONETARY=French_France.1252 LC_NUMERIC=C > > LC_TIME=French_France.1252 > > > > attached base packages: > > [1] parallel stats graphics grDevices utils datasets methods > > base > > > > other attached packages: > > [1] rgeos_0.3-11 rgdal_0.9-2 maptools_0.8-36 lattice_0.20-31 > > foreign_0.8-63 raster_2.3-40 sp_1.1-0 doParallel_1.0.8 > > iterators_1.0.7 foreach_1.4.2 > > > > loaded via a namespace (and not attached): > > [1] codetools_0.2-11 compiler_3.1.3 grid_3.1.3 tools_3.1.3 > > > > > > > > > > > > > > > > -- > > Jérôme Mathieu > > Université Pierre & Marie Curie > > Institute of Ecology and Environmental Science (Paris) > > > > Bât. A - 7ème Etage, porte 715 > > 7 quai St.-Bernard > > F-75252 Paris Cedex 05 > > > > tel: 01 44 27 34 22 > > > > [[alternative HTML version deleted]] > > > > _______________________________________________ > > R-sig-Geo mailing list > > [email protected] > > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Jérôme Mathieu Université Pierre & Marie Curie Institute of Ecology and Environmental Science (Paris) Bât. A - 7ème Etage, porte 715 7 quai St.-Bernard F-75252 Paris Cedex 05 tel: 01 44 27 34 22 [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [email protected] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
