[R] Polygon to raster with large data set

Vetter, Vanessa Vanessa.Vetter at uni-bayreuth.de
Tue Dec 6 21:00:58 CET 2016


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,
Vanessa



_________________________________________
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