Learn R Programming

tmaptools (version 2.0-1)

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 = NULL, ...)

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

not used anymore, since the "raster" output is always a RasterLayer as of version 2.0

...

arguments passed on to rasterize

Value

a RasterBrick is returned when by is specified, and a RasterLayer when by is unspecified

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")
}
# }

Run the code above in your browser using DataLab