Learn R Programming

raster (version 1.7-8)

rasterize: Rasterize points, lines, or polygons

Description

Transfer values associate with 'vector' type spatial data (points, lines, polygons) to the spatially overlapping raster cells. This function has replaced the obsolete functions pointsToRaster, linesToRaster, and polygonsToRaster.

Usage

rasterize(x, y, ...)

Arguments

x
points (a SpatialPoints* object, or a two-column matrix), SpatialLines*, SpatialPolygons*, or an Extent object
y
Raster* object
...
Additional arguments, see under Details

Value

  • RasterLayer or RasterBrick

Details

= all cases = rll{ field If x is a Spatial*DataFrame, the index (integer), or column name (character) of the variable to be transfered. If field < 0, all features get the attribute value 1, and if field == 0, the attribute index is used (i.e. numbers from 1 to the number of features). If the Spatial object has a data.frame, all values >= 1 will use the attribute index. In all cases you can also provide a vector with the same length as the number of spatial features, or a matrix where the number of rows matches the number of spatial features fun Determine what values to assign to cells that are covered by multiple spatial features. You can use functions such as min, max, or mean, or one of the following character values: 'first', 'last', 'sum', 'min', or 'max'. The default value is 'last' background Value to put in the cells that are not covered by any of the features of x. Default is NA mask Logical. If TRUE the values of the input Raster object are 'masked' by the spatial features of x. That is, cells that spatially overlap with the spatial features retain their values, the other cells become NA. Default is FALSE. Cannot be used when update=TRUE update Logical. If TRUE, the values of the Raster* object are updated for the cells that overlap the spatial features of x. Default is FALSE. Cannot be used when mask=TRUE updateValue Numeric (normally an integer), or character. Only relevant when update=TRUE. Select, by their values, the cells to be updated with the values of the spatial features. Valid character values are 'all', 'NA', and '!NA'. Default is 'all' filename Character. Output filename (optional) 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) } = points = If x represents points, 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))} Note that the function must take an na.rm argument, either explicitly or through 'dots'. This means that fun=lenght fails, but fun=function(x,...)lenght(x) works (but ignores the na.rm argument. To use the na.rm argument you can use a function like this fun=function(x,na.rm){if (na.rm) length(na.omit(x)) else (length(x)}, or use a function that removes NA values in all cases, like this function to compute the number of unique values "richness": fun=function(x, ...){length(unique(na.omit(x)))}. You can also pass multiple functions using a statement like fun=function(x)c(lenght(x),mean(x)), in which case the returned object is a RasterBrick (multiple layers). There is one additional argument: rll{ na.rm . Remove NA values? Default is TRUE } = polygons and Extent = A polygon value is transferred to a raster-cell if it covers the center of the cell. Either values associated with each polygon, or a polygon ID is transferred. Holes in polygons are recognized if they are correctly specified. The following additional arguments are available: rll{ getCover Logical. If TRUE, the fraction of each grid cell that is covered by the polygons is returned (and the values of field, fun, mask, and update are ignored. The fraction covered is estimated by dividing each cell into 100 subcells and determining presence/absence of the polygon in the center of each subcell silent Logical. If TRUE, feedback on the polygon count is suppressed. Default is FALSE }

See Also

extract

Examples

Run this code
###############################
# rasterize points
###############################
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 <- rasterize(xy, r, vals)

# now with an sp SpatialPointsDataFrame object
p <- as.data.frame(cbind(xy, name=vals))
coordinates(p) <- ~x+y
r <- rasterize(p, r, 'name')


###############################
# rasterize lines
###############################
cds1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60))
cds2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55))
cds3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45))

lines <- SpatialLines(list(Lines(list(Line(cds1)), "1"), Lines(list(Line(cds2)), "2"), Lines(list(Line(cds3)), "3") ))

r <- raster(ncols=90, nrows=45)
r <- rasterize(lines, r)

plot(r)
plot(lines, add=TRUE)

r <- rasterize(lines, r, fun='count')
plot(r)

r[] <- 1:ncell(r)
r <- rasterize(lines, r, mask=TRUE)
plot(r)

r[] <- 1
r[lines] <- 10
plot(r)

###############################
# rasterize polygons
###############################

p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
p2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))
p3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0))
pols <- SpatialPolygons( list(  Polygons(list(Polygon(p1), Polygon(hole)), 1), Polygons(list(Polygon(p2)), 2), Polygons(list(Polygon(p3)), 3)))
pols@polygons[[1]]@Polygons[[2]]@hole <- TRUE

r <- raster(ncol=180, nrow=90)
r <- rasterize(pols, r, fun='sum')
plot(r)
plot(pols, add=T)

# add a polygon
p5 <- rbind(c(-180,10), c(0,90), c(40,90), c(145,-10),  c(-25, -15), c(-180,0), c(-180,10))
addpoly <- SpatialPolygons(list(Polygons(list(Polygon(p5)), 1)))
addpoly <- as(addpoly, "SpatialPolygonsDataFrame")
addpoly@data[1,1] <- 10
r2 <- rasterize(addpoly, r, field=1, update=TRUE, updateValue="NA")
plot(r2)
plot(pols, border="blue", lwd=2, add=TRUE)
plot(addpoly, add=TRUE, border="red", lwd=2)

# get the percentage cover of polygons in a cell
r3 <- raster(ncol=36, nrow=18)
r3 <- rasterize(pols, r3, getCover=TRUE)

Run the code above in your browser using DataLab