raster (version 1.0.4)

polygonsToRaster: Polygons to raster conversion

Description

Polygon to raster conversion. A polygon value is transferred to a raster-cell if it covers the center of the cell. Either values associated with each polygon, or a polygon ID is transferred. Holes in polygons are recognized if they are correctly specified.

Usage

polygonsToRaster(p, raster, field=0, overlap='last', mask=FALSE, updateRaster=FALSE, updateValue="NA", getCover=FALSE, filename="", silent=FALSE, ...)

Arguments

p
SpatialPolygons or a SpatialPolygonsDataFrame object
raster
Raster* object
field
The index (or colname) of the column in the SpatialPolygonsDataFrame to be transfered to the RasterLayer (see Details)
overlap
Character. Determines what values to assign to cells with overlapping polygons. Choose from 'first', 'last', 'sum', 'min', or 'max'
filename
Character. Output filename
mask
Logical. If TRUE the values of the input RasterLayer are 'masked' by the polygons. I.e. cells overlapping with the polygons retain their values, the other cells become NA
updateRaster
Logical. If TRUE, raster must be a RasterLayer. Its values are updated where the polygons overlap cells
updateValue
Character. Select cells to be updated (if updateRaster == TRUE) by their current values. Either 'all', 'NA', '!NA', or 'zero'
getCover
Logical. If TRUE, the fraction of each grid cell that is covered by the polygons is returned (and the values of field, overlap, mask, and updateRaster are ignored. The fraction covered is estimated by dividing each c
silent
Logical. If TRUE, feedback on the polygon count is suppressed
...
Additional arguments. See Details.

Details

For SpatialPolygons, the field argument is ignored and the polygon index is used (i.e. numbers from 1 to the number of polygons). This is also done for SpatialPolygonDataFrame objects when field==0. If field < 0, all polygons get the value 1. The following additional arguments can be passed, to replace default values for this function rll{ overwrite Logical. If TRUE, "filename" will be overwritten if it exists format Character. Output file type. See writeRaster datatype Character. Output data type. See dataType progress Character. Valid values are "text", "tcltk", "windows" (on that platform only) and "" }

See Also

linesToRaster, pointsToRaster

Examples

Run this code
p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
p2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))
p3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0))
pols <- SpatialPolygons( list(  Polygons(list(Polygon(p1), Polygon(hole)), 1), Polygons(list(Polygon(p2)), 2), Polygons(list(Polygon(p3)), 3)))
pols@polygons[[1]]@Polygons[[2]]@hole <- TRUE

r <- raster()
r <- polygonsToRaster(pols, r, overlap='sum')
#plot(r)
#plot(pols, add=T)

## add a polygon
#p5 <- rbind(c(-180,10), c(0,90), c(40,90), c(145,-10),  c(-25, -15), c(-180,0), c(-180,10))
#addpoly <- SpatialPolygons(list(Polygons(list(Polygon(p5)), 1)))
#addpoly <- as(addpoly, "SpatialPolygonsDataFrame")
#addpoly@data[1,1] <- 10
#r2 <- polygonsToRaster(addpoly, r, field=1, updateRaster=TRUE, updateValue="NA")
#plot(r2)
#plot(pols, border="blue", lwd=2, add=TRUE)
#plot(addpoly, add=TRUE, border="red", lwd=2)

## get the percentage cover of polygons in a cell
#r3 <- raster(ncol=36, nrow=18)
#r3 <- polygonsToRaster(pols, r3, getCover=TRUE)

Run the code above in your browser using DataCamp Workspace