Learn R Programming

tmaptools (version 1.2)

points_to_raster: Bin spatial points to a raster

Description

Bin spatial points to a raster. For each raster cell, the number of points are counted. Optionally, a factor variable can be specified by which the points are counts are split.

Usage

points_to_raster(shp, nrow = NA, ncol = NA, N = 250000, by = NULL, to.Raster = FALSE)

Arguments

shp
shape object. a SpatialPoints(DataFrame), a SpatialGrid(DataFrame), or an sf object that can be coerced as such.
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.
by
name of a data variable which should be a factor. The points are split and counted according to the levels of this factor.
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.

Value

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

Details

This function is a wrapper around rasterize.

See Also

poly_to_raster

Examples

Run this code
## Not run: 
# if (require(tmap)) {
#     data(NLD_muni, NLD_prov)
# 
#     # sample points (each point represents 1000 people)
#     NLD_muni_points <- sample_dots(NLD_muni, vars = "population",
#         w=1000, convert2density = TRUE)
# 
#     # dot map
#     tm_shape(NLD_muni_points) + tm_dots()
# 
#     # convert points to raster
#     NLD_rst <- points_to_raster(NLD_muni_points, N = 1e4)
# 
#     # plot raster
#     tm_shape(NLD_rst) +
#     	tm_raster() +
#     tm_shape(NLD_prov) +
#     	tm_borders() +
#     tm_format_NLD() + tm_style_grey()
# }
# ## End(Not run)

Run the code above in your browser using DataLab