Learn R Programming

tmaptools (version 1.2)

poly_to_raster: Convert spatial polygons to a raster

Description

Convert spatial polygons to a raster. The value of each raster cell will be the polygon ID number. Alternatively, if copy.data, the polygon data is appended to each raster cell.

Usage

poly_to_raster(shp, r = NULL, nrow = NA, ncol = NA, N = 250000, use.cover = FALSE, copy.data = FALSE, to.Raster = !copy.data, ...)

Arguments

shp
shape object. A SpatialPoints(DataFrame), a SpatialGrid(DataFrame), or an sf object that can be coerced as such.
r
Raster object. If not specified, it will be created from the bounding box of shp and the arguments N, nrow, and ncol.
nrow
number of raster rows. If NA, it is automatically determined by N and the aspect ratio of shp.
ncol
number of raster columns. If NA, it is automatically determined by N and the aspect ratio of shp.
N
preferred number of raster cells.
use.cover
logical; should the cover method be used? This method determines per raster cell which polygon has the highest cover fraction. This method is better, but very slow, since N times the number of polygons combinations are processed (using the getCover argument of rasterize). By default, when a raster cell is covered by multiple polygons, the last polygon is taken (see fun argment of rasterize))
copy.data
should the polygon data be appended to the raster? Only recommended when N is small.
to.Raster
logical; should the output be a Raster object (TRUE), or a SpatialGridDataFrame (FALSE). If TRUE, a RasterBrick is returned when by is specified, and a RasterLayer when by is unspecified. For very large rasters, use TRUE, since large Raster objects are accessed from disk.
...
arguments passed on to rasterize

Value

A SpatialGridDataFrame, or a Raster object when (to.Raster=TRUE)

See Also

points_to_raster

Examples

Run this code
## Not run: 
# if (require(tmap)) {
#     data(NLD_muni)
# 
#     # choropleth of 65+ population percentages
#     qtm(NLD_muni, fill="pop_65plus", format="NLD")
# 
#     # rasterized version
#     NLD_rst <- poly_to_raster(NLD_muni, copy.data = TRUE)
#     qtm(NLD_rst, raster="pop_65plus", format="NLD")
# }
# ## End(Not run)

Run the code above in your browser using DataLab