[Raster-commits] r362 - in pkg/raster: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Mar 14 14:26:34 CET 2009
Author: rhijmans
Date: 2009-03-14 14:26:33 +0100 (Sat, 14 Mar 2009)
New Revision: 362
Added:
pkg/raster/R/overlayList.R
Modified:
pkg/raster/R/overlay.R
pkg/raster/R/overlayStack.R
pkg/raster/man/Math-methods.Rd
pkg/raster/man/aggregate.Rd
pkg/raster/man/calc.Rd
pkg/raster/man/overlay.Rd
Log:
Modified: pkg/raster/R/overlay.R
===================================================================
--- pkg/raster/R/overlay.R 2009-03-14 10:39:40 UTC (rev 361)
+++ pkg/raster/R/overlay.R 2009-03-14 13:26:33 UTC (rev 362)
@@ -28,84 +28,8 @@
}
}
}
- return(overlay(rasters, fun=fun, filename=filename, overwrite=overwrite, filetype=filetype, datatype=datatype, track=track))
+ return(.overlayList(rasters, fun=fun, filename=filename, overwrite=overwrite, filetype=filetype, datatype=datatype, track=track))
}
)
-
-setMethod('overlay', signature(x='list', y='missing'),
-function(x, y, fun=sum, filename="", overwrite=FALSE, filetype='raster', datatype='FLT4S', track=-1){
-
- compare(x)
-
- outraster <- raster(x[[1]], filename)
- outraster <- setDatatype(outraster, datatype)
-
- inram <- TRUE
- for (i in 1:length(x)) {
- if (dataContent(x[[i]]) != 'all') {
- inram <- FALSE
- }
- }
-
- vallist <- list()
-
- if ( inram ) {
- for (i in 1:length(x)) {
- vallist[[i]] <- values(x[[i]])
- clearValues(x[[i]])
- }
- vals <- do.call(fun, vallist)
-
- outraster <- setValues(outraster, vals)
- if (filename(outraster) != "") {
- outraster <- writeRaster(outraster, overwrite=overwrite, filetype=filetype)
- }
-
- } else {
- if (filename(outraster) == "") {
- if (!canProcessInMemory(outraster, 4)) {
- filename <- tempfile()
- outraster <- setFilename(outraster, filename )
- } else {
- v <- vector(length=ncell(outraster))
- startcells <- cellFromCol(outraster, 1)
- endcells <- cellFromCol(outraster, ncol(outraster))
- }
- }
- starttime <- proc.time()
-
- for (r in 1:nrow(outraster)) {
- for (i in 1:length(x)) {
- if (dataSource(x[[i]]) == 'ram') {
- x[i] <- valuesRow(x[[i]], r)
- } else {
- x[i] <- readRow(x[[i]], r)
- }
- }
-
- for (i in 1:length(x)) {
- vallist[[i]] <- values(x[[i]])
- # clearValues(rasters[[i]])
- }
- vals <- do.call(fun, vallist)
-
- if (filename(outraster) == "") {
-# v <- c(v, vals)
- v[startcells[r]:endcells[r]] <- vals
- } else {
- outraster <- setValues(outraster, vals, r)
- outraster <- writeRaster(outraster, filetype=filetype, overwrite=overwrite)
- }
-
- if (r %in% track) { .showTrack(r, track, starttime) }
-
- }
- if (filename(outraster) == "") {
- outraster <- setValues(outraster, v)
- }
- }
- return(outraster)
-}
-)
Added: pkg/raster/R/overlayList.R
===================================================================
--- pkg/raster/R/overlayList.R (rev 0)
+++ pkg/raster/R/overlayList.R 2009-03-14 13:26:33 UTC (rev 362)
@@ -0,0 +1,80 @@
+# Author: Robert J. Hijmans and Reinhard Krug
+# International Rice Research Institute
+# Date : June 2008
+# Version 0.8
+# Licence GPL v3
+
+
+.overlayList <- function(x, fun=sum, filename="", overwrite=FALSE, filetype='raster', datatype='FLT4S', track=-1){
+
+ compare(x)
+
+ outraster <- raster(x[[1]], filename)
+ outraster <- setDatatype(outraster, datatype)
+
+ inram <- TRUE
+ for (i in 1:length(x)) {
+ if (dataContent(x[[i]]) != 'all') {
+ inram <- FALSE
+ }
+ }
+
+ vallist <- list()
+
+ if ( inram ) {
+ for (i in 1:length(x)) {
+ vallist[[i]] <- values(x[[i]])
+ clearValues(x[[i]])
+ }
+ vals <- do.call(fun, vallist)
+
+ outraster <- setValues(outraster, vals)
+ if (filename(outraster) != "") {
+ outraster <- writeRaster(outraster, overwrite=overwrite, filetype=filetype)
+ }
+
+ } else {
+ if (filename(outraster) == "") {
+ if (!canProcessInMemory(outraster, 4)) {
+ filename <- tempfile()
+ outraster <- setFilename(outraster, filename )
+ } else {
+ v <- vector(length=ncell(outraster))
+ startcells <- cellFromCol(outraster, 1)
+ endcells <- cellFromCol(outraster, ncol(outraster))
+ }
+ }
+ starttime <- proc.time()
+
+ for (r in 1:nrow(outraster)) {
+ for (i in 1:length(x)) {
+ if (dataSource(x[[i]]) == 'ram') {
+ x[i] <- valuesRow(x[[i]], r)
+ } else {
+ x[i] <- readRow(x[[i]], r)
+ }
+ }
+
+ for (i in 1:length(x)) {
+ vallist[[i]] <- values(x[[i]])
+ # clearValues(rasters[[i]])
+ }
+ vals <- do.call(fun, vallist)
+
+ if (filename(outraster) == "") {
+# v <- c(v, vals)
+ v[startcells[r]:endcells[r]] <- vals
+ } else {
+ outraster <- setValues(outraster, vals, r)
+ outraster <- writeRaster(outraster, filetype=filetype, overwrite=overwrite)
+ }
+
+ if (r %in% track) { .showTrack(r, track, starttime) }
+
+ }
+ if (filename(outraster) == "") {
+ outraster <- setValues(outraster, v)
+ }
+ }
+ return(outraster)
+}
Modified: pkg/raster/R/overlayStack.R
===================================================================
--- pkg/raster/R/overlayStack.R 2009-03-14 10:39:40 UTC (rev 361)
+++ pkg/raster/R/overlayStack.R 2009-03-14 13:26:33 UTC (rev 362)
@@ -23,7 +23,9 @@
}
if (length(fun) == 1) {
- return(overlay(rasters, fun=fun, overwrite=overwrite, filetype=filetype, datatype=datatype, track=track))
+
+ return(.overlayList(rasters, fun=fun, overwrite=overwrite, filetype=filetype, datatype=datatype, track=track))
+
} else {
if (filename != "" && (length(filename) != length(fun)) ) {
stop('you must provide a filename for each function if you provide multiple functions')
@@ -32,7 +34,7 @@
# the idea is to optimize this, by reading all (row) data only once....
res <- list()
for (i in 1:length(fun)) {
- res[i] <- (overlay(rasters, fun=fun, overwrite=overwrite, filetype=filetype, datatype=datatype, track=track))
+ res[i] <- ( .overlayList(rasters, fun=fun, overwrite=overwrite, filetype=filetype, datatype=datatype, track=track))
}
return(res)
}
Modified: pkg/raster/man/Math-methods.Rd
===================================================================
--- pkg/raster/man/Math-methods.Rd 2009-03-14 10:39:40 UTC (rev 361)
+++ pkg/raster/man/Math-methods.Rd 2009-03-14 13:26:33 UTC (rev 362)
@@ -1,13 +1,18 @@
\name{Math-methods}
+
\docType{methods}
+
\alias{Math-methods}
\alias{Math,RasterLayer-method}
-\title{ Mathematical function for RasterLayer objects }
+\title{mathematical functions}
+
\description{
- Standard mathematical functions that can be used with a RasterLayer object as argument:
- \code{"abs", "sign", "sqrt", "ceiling", "floor", "trunc", "log", "log10", "log2", "log1p", "acos", "acosh", "asin", "asinh", "atan", "atanh", "exp", "expm1", "cos", "cosh", "sin", "sinh", "tan", "tanh"}.
+ Standard mathematical functions that can be used with a RasterLayer object as argument:
+ \code{"abs", "sign", "sqrt", "ceiling", "floor", "trunc", "log", "log10", "log2", "log1p", "acos", "acosh", "asin",
+ "asinh", "atan", "atanh", "exp", "expm1", "cos", "cosh", "sin", "sinh", "tan", "tanh"}.
}
+
\section{Note}{
If the values of the output RasterLayer cannot be held in memory, they will be saved to a temporary file.
If you want to set the filename (and perhaps filetype and datatype), use \code{\link[raster]{calc}} instead of the Math-methods.
Modified: pkg/raster/man/aggregate.Rd
===================================================================
--- pkg/raster/man/aggregate.Rd 2009-03-14 10:39:40 UTC (rev 361)
+++ pkg/raster/man/aggregate.Rd 2009-03-14 13:26:33 UTC (rev 362)
@@ -5,10 +5,11 @@
\alias{aggregate}
\alias{aggregate,RasterLayer-method}
-\title{Aggregate}
+\title{aggregate}
\description{
-Aggregate a RasterLayer to create a new raster with a lower resolution (larger cells). Aggregation groups rectangular areas of RasterLayer cells to create a new RasterLayer with larger cells.
+Aggregate a RasterLayer to create a new RasterLayer with a lower resolution (larger cells).
+Aggregation groups rectangular areas of RasterLayer cells to create a new RasterLayer with larger cells.
A new value is computed for the resulting cells according to a user specified function (default value = \code{mean}).
}
@@ -24,22 +25,26 @@
\section{Methods}{
\describe{
-A full call to the aggregate method is:
+A full call to the aggregate method for a RasterLayer is:
\code{aggregate(x, fact=2, fun=mean, expand=TRUE, na.rm=TRUE, filename=NULL, filetype='raster', datatype='FLT4S', overwrite=FALSE, track=-1) }
- \item{\code{x}}{a RasterLayer object}
- \item{\code{fun}}{function used to aggregate values}
- \item{\code{fact}}{integer. Aggregation factor expressed as number of cells in each direction (horizontally and vertically). Or two integers (horizontal and vertial aggregation fcator). See details}
- \item{\code{expand}}{logical. If \code{TRUE} the output RasterLayer will get larger if a division of the number of columns or rows with \code{factor} does not return an integer}
- \item{\code{na.rm}}{logical. If \code{TRUE}, remove NA cells from calculations}
- \item{\code{filename}}{character. output filename}
- \item{\code{overwrite}}{logical. If \code{TRUE}, "filename" will be overwritten if it exists}
- \item{\code{filetype}}{character. output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{rasterFormats}}}
- \item{\code{datatype}}{character. output data type; see \code{\link[raster]{setDatatype}}}
- \item{\code{track}}{vector of row numbers for which the function will report that they have been processed}
+
+\tabular{rll}{
+\tab \code{x} \tab a RasterLayer object \cr
+\tab \code{fun} \tab function used to aggregate values \cr
+\tab \code{fact} \tab integer. Aggregation factor expressed as number of cells in each direction (horizontally and vertically). Or two integers (horizontal and vertial aggregation fcator). See details \cr
+\tab \code{expand} \tab logical. If \code{TRUE} the output RasterLayer will get larger if a division of the number of columns or rows with \code{factor} does not return an integer \cr
+\tab \code{na.rm} \tab logical. If \code{TRUE}, remove NA cells from calculations \cr
+\tab \code{filename} \tab character. output filename \cr
+\tab \code{overwrite} \tab logical. If \code{TRUE}, "filename" will be overwritten if it exists \cr
+\tab \code{filetype} \tab character. output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{rasterFormats}} \cr
+\tab \code{datatype} \tab character. output data type; see \code{\link[raster]{setDatatype}} \cr
+\tab \code{track} \tab vector of row numbers for which the function will report that they have been processed \cr
}
+
}
+}
\details{
Aggregation will result in a RasterLayer with \code{fact*fact} fewer cells; sometimes adjusted according to the values of \code{expand}.
Modified: pkg/raster/man/calc.Rd
===================================================================
--- pkg/raster/man/calc.Rd 2009-03-14 10:39:40 UTC (rev 361)
+++ pkg/raster/man/calc.Rd 2009-03-14 13:26:33 UTC (rev 362)
@@ -1,54 +1,80 @@
-\name{calc-methods}
+\name{calc}
+
\docType{methods}
+
\alias{calc}
\alias{calc,RasterLayer,function-method}
\alias{calc,RasterStack,function-method}
\alias{calc,RasterStack,list-method}
-\title{ Calculate }
+\title{ calculate }
\description{
- Calculate values for a new RasterLayer, from a single RasterLayer or RasterStack
+ Calculate values for a new RasterLayer, from a single RasterLayer or from a RasterStack
}
+\usage{
+calc(x, fun, ...)
+}
+
+\arguments{
+ \item{x}{a Raster* object}
+ \item{fun}{a function}
+ \item{...}{Additional arguments. See below, under Methods}
+}
+
\section{Methods}{
\describe{
-\item{x = "RasterLayer", fun = "function"}{
- \code{calc(x, fun, filename='', overwrite=FALSE, filetype='raster', datatype='FLT4S')}
- \item{\code{x}}{A RasterLayer object}
- \item{\code{fun}}{The function to be applied}
- \item{\code{filename}}{Output filename for a new raster; if NA the result is not written to a file but returned with the RasterLayer object, in the data slot}
- \item{\code{overwrite}}{Logical to indicate whether an existing output file should be overwritten}
- \item{\code{filetype}}{output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{writeRaster}}}
- \item{\code{datatype}}{output data type; see \code{\link[raster]{setDatatype}}}
- \item{\code{track}}{vector of row numbers for which the function will report that they have been processed}
+
+\bold{1) \code{x} is a RasterLayer object; \code{fun} is a function}
+
+\code{calc(x, fun, filename='', overwrite=FALSE, filetype='raster', datatype='FLT4S')}
+
+\tabular{rll}{
+ \tab \code{x} \tab a RasterLayer object \cr
+ \tab \code{fun} \tab The function to be applied\cr
+ \tab \code{filename} \tab Output filename for a new raster; if NA the result is not written to a file but returned with the RasterLayer object, in the data slot \cr
+ \tab \code{overwrite} \tab Logical to indicate whether an existing output file should be overwritten \cr
+ \tab \code{filetype} \tab output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{writeRaster}} \cr
+ \tab \code{datatype} \tab output data type; see \code{\link[raster]{setDatatype}} \cr
+ \tab \code{track} \tab vector of row numbers for which the function will report that they have been processed \cr
}
-\item{x = "RasterStack", fun = "function"}{
- as above
+\bold{2) \code{x} is a RasterLayer object; \code{fun} is a function}
+
+\code{calc(x, fun, filename='', overwrite=FALSE, filetype='raster', datatype='FLT4S')}
+
+\tabular{rll}{
+ \tab \code{x} \tab a RasterStack object \cr
}
+Other items as above
+\bold{3) \code{x} is a RasterLayer object; \code{fun} is a list of functions}
-\item{x = "RasterStack", fun = "list"}{
- coming soon...
+\code{calc(x, fun, filename='', overwrite=FALSE, filetype='raster', datatype='FLT4S')}
+
+\tabular{rll}{
+ \tab \code{x} \tab a RasterStack object \cr
+ \tab \code{fun} \tab a list of the functions to be applied \cr
}
+Other items as above
}}
\details{
if \code{x = "RasterLayer"}, \code{fun} should be a function that takes a single value as input, and returns a single value.
-if \code{x = "RasterStack"}, \code{fun} should be a function that takes a one dimensional vector of value as input, and returns a single value.
+if \code{x = "RasterStack"}, \code{fun} should be a function that takes a vector of value as input, and returns a single value.
}
\value{
- a new RasterLayer
+a RasterLayer object
}
-\seealso{ \code{\link[raster]{Math-methods}} }
+\seealso{ \code{ \link[raster]{overlay}} , \link[raster]{Arith-methods}, \link[raster]{Math-methods}}
\author{ Robert J. Hijmans}
@@ -72,6 +98,3 @@
\keyword{methods}
\keyword{spatial}
-
-
-
Modified: pkg/raster/man/overlay.Rd
===================================================================
--- pkg/raster/man/overlay.Rd 2009-03-14 10:39:40 UTC (rev 361)
+++ pkg/raster/man/overlay.Rd 2009-03-14 13:26:33 UTC (rev 362)
@@ -5,10 +5,10 @@
\alias{overlay}
\alias{overlay,RasterLayer,RasterLayer-method}
\alias{overlay,RasterLayer,missing-method}
-\alias{overlay,list,missing-method}
\alias{overlay,RasterStack,missing-method}
+\alias{overlay,list,missing-method}
-\title{Overlay}
+\title{overlay}
\description{
Calculate values for a new RasterLayer object, based on two or more existing and RasterLayers of the same extent and resolution.
@@ -25,47 +25,40 @@
\code{overlay(x, y, ...)}
-A full call to the overlay method with RasterLayer objects:
+\bold{1) x and y are Raster* objects}
-1) x and y are Raster* objects
-
\code{overlay(x, y, ..., fun=sum, overwrite=FALSE, filetype='raster', datatype='FLT4S', track=-1)}
- \item{x}{a RasterLayer object}
- \item{y}{a RasterLayer object}
- \item{...}{additional RasterLayer objects}
- \item{fun}{the function to be appliepd. This should be a function that takes two numbers as an argument }
- \item{filename}{ filename for the output raster. A valid filename must be provided when the data of the input rasters are on disk }
- \item{overwrite}{logical. If \code{TRUE}, existing files will be overwritten}
- \item{filetype}{output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{writeRaster}}}
+\tabular{rll}{
+\tab \code{x} \tab a RasterLayer object \cr
+\tab \code{y} \tab a RasterLayer object \cr
+\tab \code{...} \tab additional RasterLayer objects \cr
+\tab \code{fun} \tab the function to be applied. This should be a function that takes two numbers as an argument \cr
+\tab \code{filename} \tab filename for the output raster. A valid filename must be provided when the data of the input rasters are on disk \cr
+\tab \code{overwrite}\tab logical. If \code{TRUE}, existing files will be overwritten \cr
+\tab \code{filetype} \tab output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{writeRaster}} \cr
+}
-
-2) x is a RasterLayer object, y is missing (equivalent to the \code{\link[raster]{calc}})
+\bold{2) x is a Raster* object, y is missing (this is equivalent to \code{\link[raster]{calc}})}
- \item{x}{a RasterLayer object}
- \item{fun}{the function to be appliepd. This should be a function that takes two numbers as an argument }
- \item{filename}{ filename for the output raster. A valid filename must be provided when the data of the input rasters are on disk }
- \item{overwrite}{logical. If \code{TRUE}, existing files will be overwritten}
- \item{filetype}{output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{writeRaster}}}
+\code{overlay(x, fun=sum, overwrite=FALSE, filetype='raster', datatype='FLT4S', track=-1)}
+\tabular{rll}{
+ \tab \code{x} \tab a RasterLayer object \cr
+ \tab \code{fun} \tab the function to be appliepd. This should be a function that takes two numbers as an argument \cr
+ \tab \code{filename} \tab filename for the output raster. A valid filename must be provided when the data of the input rasters are on disk \cr
+ \tab \code{overwrite} \tab logical. If \code{TRUE}, existing files will be overwritten \cr
+ \tab \code{filetype} \tab output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{writeRaster}} \cr
+}
-3) x is RasterStack object, y is missing (equivalent to the \code{\link[raster]{calc}})
- \item{x}{a RasterStack object}
-other items as above
-
-
-4) x is a list of Raster* objects, y is missing
-
- \item{x}{a list of Raster* objects}
-other items as above
-
}
}
\details{
In stead of the overlay function you can also use normal artimic functions such as *, /, +, - with RasterLayer objects (see examples),
-but then you cannot specifiy an output filename, filetype or datatype.
+but then you cannot specifiy an output filename, filetype or datatype. Overlay should be more efficient when using large data files that
+cannot be loaded into memory.
}
\value{
More information about the Raster-commits
mailing list