raster (version 1.1.7)

pointsToRaster: Point to raster conversion

Description

Use points to set values of a RasterLayer object. For example to count the number of points, or points representing unique classes

Usage

pointsToRaster(raster, xy, values=1, fun=length, background=NA, filename="", ...)

Arguments

raster
RasterLayer object
xy
Matrix of x and y coordinates (in the same map reference system as the raster) or a SpatialPoints* object
values
Vector of length of the number of points (i.e., dim(xy)[1] if xy is a matrix) with the values to be used; alternatively you can supply a single value which is then assigned to each point
fun
Function to be applied, on a cell by cell basis, to the points a cell; the default function is length, i.e. it returns the number of points in a cell. Typical examples of other functions that can be used are mean, sum, max and function(
background
Numeric. The value to be applied to the cells in which no points fall (typically NA or 0)
filename
Character. Output filename
...
Additional arguments. See Details

Value

  • RasterLayer object

Details

Each point is assinged to a grid cell. The value of a grid cell is determined by the values associated with the points and function fun. If you want the count of points in each grid cell, use the (default) length function. I.e. for each cell it computes the length of the vector of points. For the sum of the values, use sum, for a yes/no result, you can use fun=function(x){length(x)>0} For the number of unique values use fun=function(x){length(unique(x))} 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, polygonsToRaster

Examples

Run this code
r <- raster(ncols=36, nrows=18)
n <- 1000
x <- runif(n)* 360 - 180
y <- runif(n)* 180 - 90
xy <- cbind(x, y)
vals <- rep(1, n)
r <- pointsToRaster(r, xy, vals)

# now with an sp SpatialPointsDataFrame object
p <- as.data.frame(cbind(xy, vals))
coordinates(p) <- ~x+y
r <- pointsToRaster(r, p, p@data[,'vals'])

Run the code above in your browser using DataCamp Workspace