Miscellaneous¶
Session options¶
There is a number of session options that influence reading and writing
files. These can be set in a session, with rasterOptions
, and saved
to make them persistent in between sessions. But you probably should not
change the default values unless you have pressing need to do so. You
can, for example, set the directory where temporary files are written,
and set your preferred default file format and data type. Some of these
settings can be overwritten by arguments to functions where they apply
(with arguments like filename, datatype, format). Except for generic
functions like mean, ‘+’, and sqrt. These functions may write a file
when the result is too large to hold in memory and then these options
can only be set through the session options. The options chunksize and
maxmemory determine the maximum size (in number of cells) of a single
chunk of values that is read/written in chunk-by-chunk processing of
very large files.
library(raster)
rasterOptions()
## format : raster
## datatype : FLT4S
## overwrite : FALSE
## progress : none
## timer : FALSE
## chunksize : 1e+08
## maxmemory : 5e+09
## memfrac : 0.6
## tmpdir : c:\temp\Rtmpkjsx6n/raster/
## tmptime : 168
## setfileext : TRUE
## tolerance : 0.1
## standardnames : TRUE
## warn depracat.: TRUE
## header : none
Coercion to objects of other classes¶
Although the raster package defines its own set of classes, it is easy
to coerce objects of these classes to objects of the ‘spatial’ family
defined in the sp package. This allows for using functions defined by sp
(e.g. spplot) and for using other packages that expect spatial*
objects. To create a Raster object from variable n in a SpatialGrid* x
use raster(x, n)
or stack(x)
or brick(x)
. Vice versa use
as( , )
You can also convert objects of class “im” (spatstat) and “asc”
(adehabitat) to a RasterLayer
and “kasc” (adehabitat) to a
RasterStack
or Brick using the raster(x)
, stack(x)
or
brick(x)
function.
r1 <- raster(ncol=36, nrow=18)
r2 <- r1
values(r1) <- runif(ncell(r1))
values(r2) <- runif(ncell(r1))
s <- stack(r1, r2)
sgdf <- as(s, 'SpatialGridDataFrame')
newr2 <- raster(sgdf, 2)
news <- stack(sgdf)
Extending raster objects¶
It is straightforward to build on the Raster* objects using the S4
inheritance mechanism. Say you need objects that behave like a
RasterLayer
, but have some additional properties that you need to
use in your own functions (S4 methods). See Chambers (2009) and the help
pages of the Methods package for more info. Below is an example:
setClass ('myRaster',
contains = 'RasterLayer',
representation (
important = 'data.frame',
essential = 'character'
) ,
prototype (
important = data.frame(),
essential = ''
)
)
r <- raster(nrow=10, ncol=10)
m <- as(r, 'myRaster')
m@important <- data.frame(id=1:10, value=runif(10))
m@essential <- 'my own slot'
values(m) <- 1:ncell(m)
setMethod ('show' , 'myRaster',
function(object) {
callNextMethod(object) # call show(RasterLayer)
cat('essential:', object@essential, '\n')
cat('important information:\n')
print( object@important)
})
m
## class : myRaster
## dimensions : 10, 10, 100 (nrow, ncol, ncell)
## resolution : 36, 18 (x, y)
## extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## source : memory
## names : layer
## values : 1, 100 (min, max)
##
## essential: my own slot
## important information:
## id value
## 1 1 0.9608613
## 2 2 0.3111691
## 3 3 0.8612748
## 4 4 0.8352472
## 5 5 0.8221431
## 6 6 0.5390177
## 7 7 0.6969546
## 8 8 0.3095380
## 9 9 0.1058503
## 10 10 0.6639418