Learn R Programming

raster (version 1.0.0-1)

pointsToRaster: Point to raster conversion

Description

Convert points to a RasterLayer object

Usage

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

Arguments

raster
a RasterLayer object
xy
a matrix of x and y coordinates (in the same map reference system as the raster) or a SpatialPoints* object
values
a 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
the 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 funct
background
the value to be applied to the cells in which no points fall (typically NA or 0)
filename
output filename
...
additional arguments. See Details.

Value

  • a 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){if(lenght(x)>0 {r<-1} else {r<-0}; return(r)} 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 DataLab