[Raster-commits] r276 - in pkg/raster: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Feb 12 05:01:44 CET 2009
Author: rhijmans
Date: 2009-02-12 05:01:43 +0100 (Thu, 12 Feb 2009)
New Revision: 276
Added:
pkg/raster/R/sCalc.R
pkg/raster/man/calc-methods.Rd
Removed:
pkg/raster/man/calc.Rd
pkg/raster/man/mCalc.Rd
Modified:
pkg/raster/DESCRIPTION
pkg/raster/R/calc.R
pkg/raster/R/mCalc.R
Log:
Modified: pkg/raster/DESCRIPTION
===================================================================
--- pkg/raster/DESCRIPTION 2009-02-11 16:42:26 UTC (rev 275)
+++ pkg/raster/DESCRIPTION 2009-02-12 04:01:43 UTC (rev 276)
@@ -1,8 +1,8 @@
Package: raster
Type: Package
Title: Raster data handling for geographic data analysis and modeling
-Version: 0.8.8-4
-Date: 11-Feb-2009
+Version: 0.8.8-5
+Date: 12-Feb-2009
Depends: methods, sp, rgdal (>= 0.5-33), R (>= 2.8.0)
Author: Robert J. Hijmans & Jacob van Etten
Maintainer: Robert J. Hijmans <r.hijmans at gmail.com>
Modified: pkg/raster/R/calc.R
===================================================================
--- pkg/raster/R/calc.R 2009-02-11 16:42:26 UTC (rev 275)
+++ pkg/raster/R/calc.R 2009-02-12 04:01:43 UTC (rev 276)
@@ -6,44 +6,56 @@
+if (!isGeneric("calc")) {
+ setGeneric("calc", function(x, fun, ...)
+ standardGeneric("calc"))
+}
-calc <- function(raster, fun=sqrt, filename="", overwrite=FALSE, filetype='raster', datatype='FLT4S') {
+setMethod('calc', signature(x='RasterLayer', fun='function'),
+function(x, fun, filename="", overwrite=FALSE, filetype='raster', datatype='FLT4S', track=-1) {
if (length(fun(5)) > 1) {
stop("function 'fun' returns more than one value")
}
filename <- trim(filename)
- outraster <- setRaster(raster, filename)
-
+ outraster <- setRaster(x, filename)
outraster <- setDatatype(outraster, datatype)
- if (!(dataContent(raster) == 'all' | dataContent(raster) == 'sparse' | dataSource(raster) == 'disk')) {
- stop('raster has no data on disk, nor a complete set of raster values in memory')
+ if (!(dataContent(x) == 'all' | dataContent(x) == 'sparse' | dataSource(x) == 'disk')) {
+ stop('RasterLayer has no data on disk, nor a complete set of values in memory')
}
- if ( dataContent(raster) == 'all') {
- outraster <- setValues(outraster, fun(values(raster)))
+ if ( dataContent(x) == 'all') {
+ outraster <- setValues(outraster, fun(values(x)))
if (filename(outraster)!="") {
outraster <- writeRaster(outraster, overwrite=overwrite, filetype=filetype)
}
- } else if ( dataContent(raster) == 'sparse') {
- outraster <- setValuesSparse(outraster, fun(values(raster)), dataIndices(raster))
+ } else if ( dataContent(x) == 'sparse') {
+ outraster <- setValuesSparse(outraster, fun(values(x)), dataIndices(x))
if (filename(outraster) != "") {
outraster <- writeRaster(outraster, overwrite=overwrite, filetype=filetype)
}
- } else if (dataSource(raster) == 'disk') {
+ } else if (dataSource(x) == 'disk') {
v <- vector(length=0)
- for (r in 1:nrow(raster)) {
- raster <- readRow(raster, r)
+ starttime <- proc.time()
+ for (r in 1:nrow(x)) {
+ x <- readRow(x, r)
if (filename(outraster)=="") {
- v <- c(v, fun(values(raster)))
+ v <- c(v, fun(values(x)))
} else {
- outraster <- setValues(outraster, fun(values(raster)), r)
+ outraster <- setValues(outraster, fun(values(x)), r)
outraster <- writeRaster(outraster, overwrite=overwrite, filetype=filetype)
}
+
+ if (r %in% track) {
+ elapsed <- (proc.time() - starttime)[3]
+ tpr <- elapsed /r
+ ttg <- round(tpr/60 * (nrow(x) - r), digits=1)
+ cat('row', r, '-', ttg, 'minutes to go\n')
+ }
+
}
if (filename(outraster) == "") { outraster <- setValues(outraster, v) }
}
return(outraster)
}
-
-
+)
Modified: pkg/raster/R/mCalc.R
===================================================================
--- pkg/raster/R/mCalc.R 2009-02-11 16:42:26 UTC (rev 275)
+++ pkg/raster/R/mCalc.R 2009-02-12 04:01:43 UTC (rev 276)
@@ -5,29 +5,39 @@
# Licence GPL v3
-mCalc <- function(object, fun=sum, filename="", overwrite=FALSE, filetype='raster', datatype='FLT4S', track=-1) {
+mCalc <- function(...) {
+ stop('mCalc has been replaced by generic function "calc"')
+}
+
+setMethod('calc', signature(x='RasterStack', fun='function'),
+function(x, fun, filename="", overwrite=FALSE, filetype='raster', datatype='FLT4S', track=-1) {
if (length(fun(seq(1:5))) > 1) {
stop("function 'fun' returns more than one value")
}
filename <- trim(filename)
- outraster <- setRaster(object at layers[[1]], filename)
+ outraster <- setRaster(x, filename)
outraster <- setDatatype(outraster, datatype)
- if (dataContent(object) == "all") {
- outraster <- setValues(outraster, apply(values(object), 1, fun))
+ if (dataContent(x) == "all") {
+ outraster <- setValues(outraster, apply(values(x), 1, fun))
if (filename != "") {
outRaster <- writeRaster(outraster, filetype=filetype, overwrite=overwrite)
}
} else {
- for (r in 1:nrow(object)) {
- object <- readRow(object, r)
- vals <- apply(values(object), 1, fun)
+ starttime <- proc.time()
+ for (r in 1:nrow(x)) {
+ x <- readRow(x, r)
+ vals <- apply(values(x), 1, fun)
outraster <- setValues(outraster, vals, r)
outraster <- writeRaster(outraster, filetype=filetype, overwrite=overwrite)
if (r %in% track) {
- cat('row', r)
+ elapsed <- (proc.time() - starttime)[3]
+ tpr <- elapsed /r
+ ttg <- round(tpr/60 * (nrow(x) - r), digits=1)
+ cat('row', r, '-', ttg, 'minutes to go\n')
}
}
}
return(outraster)
}
+)
Added: pkg/raster/R/sCalc.R
===================================================================
--- pkg/raster/R/sCalc.R (rev 0)
+++ pkg/raster/R/sCalc.R 2009-02-12 04:01:43 UTC (rev 276)
@@ -0,0 +1,8 @@
+
+setMethod('calc', signature(x='RasterStack', fun='list'),
+function(x, fun, filename="", overwrite=FALSE, filetype='raster', datatype='FLT4S', track=-1) {
+
+ warning('not implemented yet')
+
+}
+)
Added: pkg/raster/man/calc-methods.Rd
===================================================================
--- pkg/raster/man/calc-methods.Rd (rev 0)
+++ pkg/raster/man/calc-methods.Rd 2009-02-12 04:01:43 UTC (rev 276)
@@ -0,0 +1,72 @@
+\name{calc-methods}
+\docType{methods}
+\alias{calc}
+\alias{calc,RasterLayer,function-method}
+\alias{calc,RasterStack,function-method}
+\alias{calc,RasterStack,list-method}
+
+\title{ Calculate }
+
+\description{
+ Calculate values for a new RasterLayer, from a single RasterLayer or RasterStack
+}
+
+
+\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}
+}
+
+
+\item{x = "RasterStack", fun = "function"}{
+ as above
+}
+
+
+\item{x = "RasterStack", fun = "list"}{
+ coming soon...
+}
+
+}}
+
+\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.
+}
+
+
+\value{
+ a new RasterLayer
+}
+\author{ Robert J. Hijmans}
+
+\examples{
+r <- raster(ncols=36, nrows=18)
+r[] <- 1:ncell(r)
+
+# multiply values with 10
+fun <- function(x) { x * 10 }
+rc <- calc(r, fun)
+
+# set values below 100 to NA.
+fun <- function(x) { x[x<100] <- NA; return(x) }
+rc <- calc(r, fun)
+
+# set NA values to -9999
+fun <- function(x) { x[is.na(x)] <- -9999; return(x)}
+rc <- calc(rc, fun)
+}
+
+\keyword{methods}
+\keyword{spatial}
+
+
+
Deleted: pkg/raster/man/calc.Rd
===================================================================
--- pkg/raster/man/calc.Rd 2009-02-11 16:42:26 UTC (rev 275)
+++ pkg/raster/man/calc.Rd 2009-02-12 04:01:43 UTC (rev 276)
@@ -1,54 +0,0 @@
-\name{calculate}
-
-\alias{calc}
-
-\title{RasterLayer calculate}
-\description{
- Calculate values for a new raster, from scratch or based on a single existing raster
-}
-\usage{
-calc(raster, fun=sqrt, filename='', overwrite=FALSE, filetype='raster', datatype='FLT4S')
-}
-
-\arguments{
- \item{raster}{A RasterLayer object}
- \item{fun}{The function to be applied}
- \item{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{overwrite}{Logical to indicate whether an existing output file should be overwritten}
- \item{filetype}{output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{writeRaster}}}
- \item{datatype}{output data type; see \code{\link[raster]{setDatatype}}}
-}
-
-\details{
-Use \code{calc} to calculate values for a new raster using a function (\code{fun}) that takes a single number as an argument (and retruns a single number). E.g. to add, multiply or divide a number (cell value) by a constant.
-
-If the input RasterLayer object has all values in memory (e.g. after readAll(raster)), the function will also return the new values in memory. If a filename is provided, the values will also be saved to that file.
-If the values are not in memory the new values will be written to file.
-}
-
-\value{
-A new RasterLayer object (in the R environment), and in some cases the side effect of a new file on disk.
-}
-\author{Robert J. Hijmans}
-
-\examples{
-
-r <- raster(ncols=36, nrows=18)
-r[] <- 1:ncell(r)
-
-
-# multiply values with 10
-fun <- function(x) { x * 10 }
-rc <- calc(r, fun)
-
-# set values below 100 to NA.
-fun <- function(x) { x[x<100] <- NA; return(x) }
-rc <- calc(r, fun)
-
-# set NA values to -9999
-fun <- function(x) { x[is.na(x)] <- -9999; return(x)}
-rc <- calc(rc, fun)
-
-}
-
-\keyword{spatial}
Deleted: pkg/raster/man/mCalc.Rd
===================================================================
--- pkg/raster/man/mCalc.Rd 2009-02-11 16:42:26 UTC (rev 275)
+++ pkg/raster/man/mCalc.Rd 2009-02-12 04:01:43 UTC (rev 276)
@@ -1,36 +0,0 @@
-\name{mCalc}
-\alias{mCalc}
-
-\title{ multiple layers calculate }
-\description{
- Calculate values for a new RasterLayer from the layers in a RasterStack
-}
-
-\usage{
-mCalc(object, fun=sum, filename="", overwrite=FALSE, filetype='raster', datatype='FLT4S', track=-1)
-}
-
-\arguments{
- \item{object}{ a RasterStack object }
- \item{fun}{ the function to be applied }
- \item{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{overwrite}{ logical to indicate whether an existing output file should be overwritten }
- \item{filetype}{output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{writeRaster}}}
- \item{datatype}{output data type; see \code{\link[raster]{setDatatype}}}
- \item{track}{vector of row numbers for which the function will report that they have been processed. }
-}
-
-\details{
- \code{fun} should be a function that takes a one dimensional vector of value as input, and returns a single value.
-}
-
-\value{
- a new RasterLayer
-}
-\author{ Robert J. Hijmans}
-
-\examples{
-
-
-}
-\keyword{ spatial }
More information about the Raster-commits
mailing list