[R] Polygon to raster with large data set

MacQueen, Don macqueen1 at llnl.gov
Wed Dec 7 18:12:36 CET 2016

This sounds like a question better suited for R-sig-geo.

Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550

On 12/6/16, 12:00 PM, "R-help on behalf of Vetter, Vanessa"
<r-help-bounces at r-project.org on behalf of Vanessa.Vetter at uni-bayreuth.de>

>Hi everyone,
>I have a very large shapefile with many polygons (17769 polygons, 104.4
>Mb), which I want to convert to a raster file. A grid cell can be covered
>by either none, 1 or several polygons. I want to assign the percentage
>cover of polygons to the respective grid cell, just like the function
>rasterize (x, y, getCover=TRUE) does. The big problem ist, that the
>rasterize function only notes those polygons covering the center of the
>grid cell and ignores all others. So, I lose more than half of my data
>set when applying this. The second problem is that my data is very large.
>I tried this (see below), but it is very slow and at some point crashes
>down (but works for smaller data sets):
>(r = raster, ln = shapefile with polygons)
>rp <- rasterToPolygons(r)
>covers <- unique(ln at data$PERCENTAGE)
>for(cover in covers){
>  psub <- subset(ln, PERCENTAGE==cover)
>  pu <- gUnaryUnion(psub)
>  gi <- gIntersection(rp, pu, byid = T)
>  ind <- as.numeric(do.call(rbind, strsplit(names(gi), " "))[,1])
>  r[] <- NA
>  r[ind] <- sapply(gi at polygons, function(x) slot(x, 'area'))  # a bit
>faster than gArea(gi, byid = T)
>  writeRaster(r, paste0("cover/", cover, "area.tif"))
>Every polygon contains a cover estimation (percentage cover of an
>invasive plant species) and the column name of that information is called
>PERCENTAGE. There are basically 4 cover classes. At the end of this loope
>I get 4 rasters, which are named correspondingly to the cover class and
>in whose raster cells the information of the percentage cover of polygons
>of every grid cell is stored.
>Has anybody an idea to either trick the rasterize function to consider
>all polygons (not just those covering the cell center of the raster) or
>fasten my code above?
>Any help will be appreciated!
>Thank you very much!
>Best regards,
>PhD student / Doktorandin
>Disturbance Ecology / St�rungs�kologie
>University of Bayreuth / Universit�t Bayreuth
>Phone / Telefon: 0921-552188
>Raum: GEO II, Rm 015.2
>Universit�tsstra�e 30
>D-95447 Bayreuth
>	[[alternative HTML version deleted]]

More information about the R-help mailing list