Learn R Programming

raster (version 1.6-10)

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=NULL, background=NA, na.rm=TRUE, filename="", ...)

Arguments

raster
Raster* 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. You can also supply a matrix if its number of rows matches the
fun
Function to be applied, on a cell by cell basis, to the (values of) the points that fall in a cell. If no function is specified, the number of points per cell is returned. Typical examples of functions that can be used are mean, sum
background
Numeric. The value to be applied to the cells in which no points fall (typically NA or 0)
na.rm
Logical. Remove NA values? This argument might be ignored by fun
filename
Character. Output filename
...
Additional arguments. See Details

Value

  • RasterLayer

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 to know the number 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. "text", "window", or "" (the default, no progress bar) }

See Also

linesToRaster, polygonsToRaster, rasterFromXYZ

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