Re: [R] Polygon to raster with large data set

2016-12-07 Thread MacQueen, Don
This sounds like a question better suited for R-sig-geo.
-Don

-- 
Don MacQueen

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





On 12/6/16, 12:00 PM, "R-help on behalf of Vetter, Vanessa"

wrote:

>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@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@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]]
>

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

[R] Polygon to raster with large data set

2016-12-06 Thread Vetter, Vanessa
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@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@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]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.