Learn R Programming

tmaptools (version 2.0)

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. Note that this function supports sf objects, but still uses sp-based methods (see details).

Usage

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

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

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

Value

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

Details

This function is a wrapper around rasterize.

This function supports sf objects, but still uses sp-based methods, from the packages sp, rgeos, and/or rgdal.

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

Run the code above in your browser using DataLab