x
represents points, each point is assigned to a grid cell. Points that fall on a border between cells are placed in the cell to the right and/or in the cell below. The value of a grid cell is determined by the values associated with the points and function fun
.## S3 method for class 'matrix,Raster':
rasterize(x, y, field, fun='last', background=NA,
mask=FALSE, update=FALSE, updateValue='all', filename="", na.rm=TRUE, ...)
## S3 method for class 'SpatialPoints,Raster':
rasterize(x, y, field, fun='last', background=NA,
mask=FALSE, update=FALSE, updateValue='all', filename="", na.rm=TRUE, ...)
## S3 method for class 'SpatialLines,Raster':
rasterize(x, y, field, fun='last', background=NA,
mask=FALSE, update=FALSE, updateValue='all', filename="", ...)
## S3 method for class 'SpatialPolygons,Raster':
rasterize(x, y, field, fun='last', background=NA,
mask=FALSE, update=FALSE, updateValue='all', filename="",
getCover=FALSE, silent=FALSE, ...)
x
is a Spatial*DataFrame, this can be the colummin, max
, or mean
, or one of the following character values: 'first'
, x
. Default is NA
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
. DefaulTRUE
, 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
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 TRUE
, NA
values are removed if fun
honors the na.rm
argumentTRUE
, 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 1TRUE
, feedback on the polygon count is suppressed. Default is FALSE
writeRaster
extract
###############################
# rasterize points
###############################
r <- raster(ncols=36, nrows=18)
n <- 1000
x <- runif(n) * 360 - 180
y <- runif(n) * 180 - 90
xy <- cbind(x, y)
# get the (last) indices
r0 <- rasterize(xy, r)
# prensence/absensce (NA) (is there a point or not?)
r1 <- rasterize(xy, r, field=1)
# how many points?
r2 <- rasterize(xy, r, fun=function(x,...)length(x))
vals <- runif(n)
# sum of the values associated with the points
r3 <- rasterize(xy, r, vals, fun=sum)
# with a SpatialPointsDataFrame
vals <- 1:n
p <- data.frame(xy, name=vals)
coordinates(p) <- ~x+y
r <- rasterize(p, r, 'name', fun=min)
#r2 <- rasterize(p, r, 'name', fun=max)
#plot(r, r2, cex=0.5)
###############################
# 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))
p1 <- Polygons(list(Polygon(p1), Polygon(hole, hole=TRUE)), 1)
p2 <- Polygons(list(Polygon(rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0)))), 2)
p3 <- Polygons(list(Polygon(rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0)))), 3)
pols <- SpatialPolygons( list( p1 , p2, p3) )
r <- raster(ncol=90, nrow=45)
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