[Raster-commits] r310 - in pkg/raster: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Mar 4 17:12:51 CET 2009
Author: rhijmans
Date: 2009-03-04 17:12:51 +0100 (Wed, 04 Mar 2009)
New Revision: 310
Added:
pkg/raster/man/changeStack.Rd
pkg/raster/man/stack-methods.Rd
Removed:
pkg/raster/man/create.stack.Rd
Modified:
pkg/raster/R/addToStack.R
pkg/raster/R/aggregate.R
pkg/raster/R/cover.R
pkg/raster/R/expand.R
pkg/raster/R/stack.R
pkg/raster/R/unstack.R
pkg/raster/man/compare.Rd
pkg/raster/man/dimensions.Rd
pkg/raster/man/expand.Rd
pkg/raster/man/extent.Rd
Log:
Modified: pkg/raster/R/addToStack.R
===================================================================
--- pkg/raster/R/addToStack.R 2009-03-04 07:50:14 UTC (rev 309)
+++ pkg/raster/R/addToStack.R 2009-03-04 16:12:51 UTC (rev 310)
@@ -41,63 +41,90 @@
}
for (i in 1 : length(rasters)) {
- raster <- rasters[[i]]
+ raster <- rasters[[i]]
+
if (dataContent(raster) != 'all' & dataSource(raster) == 'ram') {
stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
}
- nl <- rstack at data@nlayers + 1
- rstack at data@nlayers <- as.integer(nl)
- if (nlayers(rstack) == 1) {
- rstack <- setRowCol(rstack, nrow(raster), ncol(raster))
- rstack <- setBbox(rstack, raster, snap=FALSE)
- rstack <- setProjection(rstack, projection(raster))
- if (trim(raster at file@shortname) != "") {
- cname <- trim(raster at file@shortname)
+
+ if (nlayers(rstack) == 0) {
+ if (class(raster) == 'RasterStack') {
+ rstack <- raster
} else {
- cname <- "layer1"
- }
- rstack at data@colnames[1] <- cname
- if (dataContent(raster) == 'all') {
- rstack at data@values <- as.matrix(values(raster))
- rstack at data@content <- 'all'
- raster <- clearValues(raster)
- } else {
- if (dataSource(raster) == 'ram' & dataContent(raster) != "all") {
- stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
+ rstack <- setRowCol(rstack, nrow(raster), ncol(raster))
+ rstack <- setBbox(rstack, raster, snap=FALSE)
+ rstack <- setProjection(rstack, projection(raster))
+
+ nl <- rstack at data@nlayers + nlayers(raster)
+ rstack at data@nlayers <- as.integer(nl)
+ rstack at layers[nl] <- raster
+ rstack at data@min[nl] <- raster at data@min
+ rstack at data@max[nl] <- raster at data@max
+
+ if (trim(raster at file@shortname) != "") {
+ cname <- trim(raster at file@shortname)
+ } else {
+ cname <- "layer1"
}
+ rstack at data@colnames[1] <- cname
+ if (dataContent(raster) == 'all') {
+ rstack at data@values <- as.matrix(values(raster))
+ rstack at data@content <- 'all'
+ raster <- clearValues(raster)
+ } else {
+ if (dataSource(raster) == 'ram' & dataContent(raster) != "all") {
+ stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
+ }
+ }
+
}
+
} else {
- if (!compare(c(rstack, raster))) {
- stop(paste("could not add raster:", filename(raster)))
+
+ if (class(raster) == 'RasterStack') {
+ rasterlist <- unstack(raster)
+ } else {
+ rasterlist <- list(raster)
}
- count <- 1
- cname <- trim(raster at file@shortname)
- if (cname == "") {
- cname <- paste("layer", nl, sep="")
- }
- cn <- cname
- for (j in 1:(nl-1)) {
- if ( cn == rstack at data@colnames[j] ) {
- count <- count + 1
- cn <- paste(cname, "_", count, sep="")
+
+ for (k in 1:length(rasterlist)) {
+ nl <- as.integer( rstack at data@nlayers + nlayers(raster) )
+ rstack at data@nlayers <- nl
+ rstack at layers[nl] <- raster
+ rstack at data@min[nl] <- raster at data@min
+ rstack at data@max[nl] <- raster at data@max
+
+ raster <- rasterlist[[k]]
+ if (!compare(c(rstack, raster))) {
+ stop(paste("could not add raster:", filename(raster)))
}
- }
- rstack at data@colnames[nl] <- cn
- if (dataContent(rstack)=='all') {
- if (dataContent(raster) != 'all') {
- raster <- readAll(raster)
- }
- rstack at data@values <- cbind(rstack at data@values, values(raster))
- raster <- clearValues(raster)
- } else {
- if (dataSource(raster)=='disk') {
+ count <- 1
+ cname <- trim(raster at file@shortname)
+ if (cname == "") {
+ cname <- paste("layer", nl, sep="")
+ }
+ cn <- cname
+ for (j in 1:(nl-1)) {
+ if ( cn == rstack at data@colnames[j] ) {
+ count <- count + 1
+ cn <- paste(cname, "_", count, sep="")
+ }
+ }
+ rstack at data@colnames[nl] <- cn
+ if (dataContent(rstack)=='all') {
+ if (dataContent(raster) != 'all') {
+ raster <- readAll(raster)
+ }
+ rstack at data@values <- cbind(rstack at data@values, values(raster))
raster <- clearValues(raster)
+ } else {
+ if (dataSource(raster)=='disk') {
+ raster <- clearValues(raster)
+ }
}
- }
- }
- rstack at layers[nl] <- raster
- rstack at data@min[nl] <- raster at data@min
- rstack at data@max[nl] <- raster at data@max
+
+ }
+ }
}
return(rstack)
}
Modified: pkg/raster/R/aggregate.R
===================================================================
--- pkg/raster/R/aggregate.R 2009-03-04 07:50:14 UTC (rev 309)
+++ pkg/raster/R/aggregate.R 2009-03-04 16:12:51 UTC (rev 310)
@@ -62,10 +62,10 @@
if (!.CanProcessInMemory(x, 2) && filename == '') {
filename <- tempfile()
outraster <- setFilename(outraster, filename )
- if (options('verbose')[[1]]) { cat('values were written to:', filename(raster)) }
+ if (options('verbose')[[1]]) { cat('writing raster to:', filename(raster)) }
}
+ starttime <- proc.time()
- starttime <- proc.time()
cols <- rep(rep(1:csteps,each=xfact)[1:ncol(x)], times=yfact)
rows <- rep(1, each=(ncol(x) * yfact))
v <- vector(length=0)
Modified: pkg/raster/R/cover.R
===================================================================
--- pkg/raster/R/cover.R 2009-03-04 07:50:14 UTC (rev 309)
+++ pkg/raster/R/cover.R 2009-03-04 16:12:51 UTC (rev 310)
@@ -38,8 +38,9 @@
if (!.CanProcessInMemory(x, 2) && filename == '') {
filename <- tempfile()
outraster <- setFilename(outraster, filename )
- if (options('verbose')[[1]]) { cat('writing values to:', filename(raster)) }
+ if (options('verbose')[[1]]) { cat('writing raster to:', filename(raster)) }
}
+ starttime <- proc.time()
v <- vector(length=0)
for (r in 1:nrow(outRaster)) {
@@ -53,6 +54,14 @@
outRaster <- setValues(outRaster, vals, r)
outRaster <- writeRaster(outRaster, filetype=filetype, overwrite=overwrite)
}
+
+ if (r %in% track) {
+ elapsed <- (proc.time() - starttime)[3]
+ tpr <- elapsed /r
+ ttg <- round(tpr/60 * (nrow(raster) - r), digits=1)
+ cat('row', r, '-', ttg, 'minutes to go\n')
+ }
+
}
if (filename(outRaster) == "") {
outRaster <- setValues(outRaster, v)
Modified: pkg/raster/R/expand.R
===================================================================
--- pkg/raster/R/expand.R 2009-03-04 07:50:14 UTC (rev 309)
+++ pkg/raster/R/expand.R 2009-03-04 16:12:51 UTC (rev 310)
@@ -7,7 +7,9 @@
# Licence GPL v3
-expand <- function(raster, bndbox, filename="", overwrite=FALSE) {
+expand <- function(raster, bndbox, filename=NULL, filetype='raster', overwrite=FALSE, track=-1) {
+ if (is.null(filename)) { filename <- "" }
+
bndbox <- getBbox(bndbox)
res <- resolution(raster)
# snap points to pixel boundaries
@@ -38,14 +40,21 @@
startcell <- (r + startrow -2) * ncol(outraster) + startcol
d[startcell:(startcell+ncol(raster)-1)] <- vals
outraster <- setValues(outraster, d)
- if (filename(outraster) != "") {writeRaster(outraster, overwrite=overwrite)}
+ if (filename(outraster) != "") {writeRaster(outraster, filetype=filetype, overwrite=overwrite)}
}
} else if ( dataSource(raster) == 'disk' ) {
-
+ if (!.CanProcessInMemory(x, 2) && filename == '') {
+ filename <- tempfile()
+ outraster <- setFilename(outraster, filename )
+ if (options('verbose')[[1]]) { cat('writing raster to:', filename(raster)) }
+ }
+ starttime <- proc.time()
+
v <- vector(length=0)
d <- vector(length=ncol(outraster))
for (r in 1:nrow(raster)) {
+
raster <- readRow(raster, r)
vals <- values(raster)
d[] <- NA
@@ -54,12 +63,22 @@
if (filename(outraster) != '') {
outraster <- setValues(outraster, d, r)
- outraster <- writeRaster(outraster, overwrite=overwrite)
+ outraster <- writeRaster(outraster, filetype=filetype, overwrite=overwrite)
} else {
v <- c(v, d)
}
+
+ if (r %in% track) {
+ elapsed <- (proc.time() - starttime)[3]
+ tpr <- elapsed /r
+ ttg <- round(tpr/60 * (nrow(raster) - r), digits=1)
+ cat('row', r, '-', ttg, 'minutes to go\n')
+ }
+
}
- if (filename(outraster) == '') { outraster <- setValues(outraster, v) }
+ if (filename(outraster) == '') {
+ outraster <- setValues(outraster, v)
+ }
}
return(outraster)
}
Modified: pkg/raster/R/stack.R
===================================================================
--- pkg/raster/R/stack.R 2009-03-04 07:50:14 UTC (rev 309)
+++ pkg/raster/R/stack.R 2009-03-04 16:12:51 UTC (rev 310)
@@ -19,32 +19,49 @@
standardGeneric("stack"))
}
-setMethod("stack", signature(x='RasterLayer'),
-function(x, ...) {
+setMethod("stack", signature(x='Raster'),
+function(x, ..., bands=NULL) {
rlist <- c(x, list(...))
- return(stack(rlist))
+ return(stack(rlist, bands))
} )
+
setMethod("stack", signature(x='character'),
-function(x, ...) {
+function(x, ..., bands=NULL) {
rlist <- c(x, list(...))
- return(stack(rlist))
+ return(stack(rlist, bands))
} )
setMethod("stack", signature(x='list'),
-function(x) {
+function(x, bands=NULL) {
+ j <- 0
+ r <- list()
for (i in 1:length(x)) {
+ j <- j + 1
if (is.character(x[[i]])) {
- x[i] <- rasterFromFile(x[[i]])
+ if (is.null(bands)) {
+ r[j] <- rasterFromFile(x[[i]])
+ } else {
+ if (bands[[i]] < 1) {
+ r[j] <- rasterFromFile(x[[i]], 1)
+ bds <- nbands(r)
+ if (bds > 1) {
+ for (b in 2:bds) {
+ j <- j + 1
+ r[j] <- rasterFromFile(x[[i]], b)
+ }
+ }
+ }
+ }
+ } else if (extends(class(x[[i]]), "Raster")) {
+ r[j] <- x[[i]]
} else {
- if (class(x[[i]]) != "RasterLayer") {
- stop("Arguments should be RasterLayer objects or filenames")
- }
+ stop("Arguments should be Raster* objects or filenames")
}
}
- return(addRasters(new("RasterStack"), x))
+ return(addRasters(new("RasterStack"), r))
} )
Modified: pkg/raster/R/unstack.R
===================================================================
--- pkg/raster/R/unstack.R 2009-03-04 07:50:14 UTC (rev 309)
+++ pkg/raster/R/unstack.R 2009-03-04 16:12:51 UTC (rev 310)
@@ -4,13 +4,12 @@
# Version 0.8
# Licence GPL v3
-
-
if (!isGeneric("unstack")) {
setGeneric("unstack", function(x, ...)
standardGeneric("unstack"))
}
+
setMethod("unstack", signature(x='RasterStack'),
function(x) {
rlist <- list()
Added: pkg/raster/man/changeStack.Rd
===================================================================
--- pkg/raster/man/changeStack.Rd (rev 0)
+++ pkg/raster/man/changeStack.Rd 2009-03-04 16:12:51 UTC (rev 310)
@@ -0,0 +1,50 @@
+\name{changestack}
+
+\alias{stackFromFiles}
+
+\alias{addFiles}
+\alias{addRasters}
+\alias{dropLayer}
+
+\title{ Change a RasterStack }
+
+\description{
+ A RasterStack is a collection of rasters with the same spatial extent and resolution.
+ With these functions, you can add raster to or remove raster from a rasterstack.
+}
+
+\usage{
+addFiles(rstack, rasterfiles, bands= rep(1, length(rasterfiles)))
+addRasters(rstack, rasters)
+dropLayer(rstack, indices)
+}
+
+\arguments{
+ \item{rasterfiles}{ Filename(s) of (a) raster dataset(s) }
+ \item{rasters}{ a list of RasterLayer objects }
+ \item{bands}{ a vector or list of bands of raster data files (default values = 1)}
+ \item{rstack}{ a RasterStack object }
+ \item{indices}{ a vector of the indices of the raster(s) to remove from a RasterStack }
+}
+
+
+\value{
+ a RasterStack object
+}
+\author{Robert J. Hijmans}
+
+\seealso{ \code{\link[raster]{raster}}, \code{\link[raster]{rasterFromFile}}}
+
+\examples{
+ rasterfile <- system.file("external/test.ag", package="sp")
+ st <- stackFromFiles(rasterfile)
+# now adding the same file multiple times, in most cases you would add different files
+# this generates warnings, but it is not an error
+ st <- addFiles(st, c(rasterfile, rasterfile))
+ rs <- rasterFromFile(rasterfile)
+ st <- addRasters(st, c(rs, rs))
+ st
+ st <- dropLayer(st, c(3, 5))
+ nlayers(st)
+}
+\keyword{ spatial }
Modified: pkg/raster/man/compare.Rd
===================================================================
--- pkg/raster/man/compare.Rd 2009-03-04 07:50:14 UTC (rev 309)
+++ pkg/raster/man/compare.Rd 2009-03-04 16:12:51 UTC (rev 310)
@@ -1,10 +1,11 @@
-\name{utilities}
+\name{compare}
+
\alias{compare}
+
+\title{ Compare RasterLayer objects }
-
-\title{ Compare rasters }
\description{
- compare evaluates whether a list of rasters has the same origin, resolution, number of rows and columns, and projection (or a subset thereof).
+ compare evaluates whether a list of rasters has the same origin, resolution, number of rows and columns, and projection (or a subset of these comparisons).
}
\usage{
@@ -24,6 +25,7 @@
}
\author{Robert J. Hijmans}
+
\examples{
r1 <- raster()
r2 <- r1
@@ -33,5 +35,6 @@
compare(c(r1, r3), stopiffalse=FALSE)
compare(c(r1, r3), rowcol=FALSE)
}
+
\keyword{ spatial }
Deleted: pkg/raster/man/create.stack.Rd
===================================================================
--- pkg/raster/man/create.stack.Rd 2009-03-04 07:50:14 UTC (rev 309)
+++ pkg/raster/man/create.stack.Rd 2009-03-04 16:12:51 UTC (rev 310)
@@ -1,60 +0,0 @@
-\name{stack}
-\alias{stack}
-\alias{stack,RasterLayer-method}
-\alias{stack,character-method}
-\alias{stack,list-method}
-\alias{stackFromFiles}
-\alias{stackFromRasters}
-\alias{addFiles}
-\alias{addRasters}
-\alias{dropLayer}
-
-\title{ Create or change a RasterStack }
-
-\description{
- A RasterStack is a collection of rasters with the same spatial extent and resolution. They can be crated from RasterLayer objects, or from raster files.
- You can add raster to or remove raster from a rasterstack.
-}
-
-\usage{
-stack(x, ...)
-stackFromFiles(rasterfiles, bands= rep(1, length(rasterfiles)))
-addFiles(rstack, rasterfiles, bands= rep(1, length(rasterfiles)))
-addRasters(rstack, rasters)
-dropLayer(rstack, indices)
-}
-
-\arguments{
- \item{x}{a RasterLayer, character (raster filename), or list (of RasterLayers and/or filenames) object}
- \item{rasterfiles}{ Filename(s) of (a) raster dataset(s) }
- \item{bands}{ A vector or list of bands of raster data files (default values = 1)}
- \item{rstack}{ a RasterStack object }
- \item{...}{additional RasterLayer objects (or filenames) }
- \item{rasters}{ RasterLayer object(s) }
- \item{indices}{ the indices of the raster(s) to remove from a RasterStack }
-}
-\details{
- When a RasterStack is saved to a file, only pointers (filenames) to raster datasets are saved, not the data. If the name or location of a raster file changes, the RasterStack becomes invalid.
- In stackFromFiles, use index=-1 to add all bands of a single file to the RasterStack
-
-}
-\value{
- a RasterStack object
-}
-\author{Robert J. Hijmans}
-
-\seealso{ \code{\link[raster]{raster}}, \code{\link[raster]{rasterFromFile}}}
-
-\examples{
- rasterfile <- system.file("external/test.ag", package="sp")
- st <- stackFromFiles(rasterfile)
-# now adding the same file multiple times, in most cases you would add different files
-# this generates warnings, but it is not an error
- st <- addFiles(st, c(rasterfile, rasterfile))
- rs <- rasterFromFile(rasterfile)
- st <- addRasters(st, c(rs, rs))
- st
- st <- dropLayer(st, c(3, 5))
- nlayers(st)
-}
-\keyword{ spatial }
Modified: pkg/raster/man/dimensions.Rd
===================================================================
--- pkg/raster/man/dimensions.Rd 2009-03-04 07:50:14 UTC (rev 309)
+++ pkg/raster/man/dimensions.Rd 2009-03-04 16:12:51 UTC (rev 310)
@@ -11,7 +11,6 @@
\alias{xres}
\alias{yres}
\alias{resolution}
-\alias{origin}
\alias{nlayers}
\alias{nlayers,BasicRaster-method}
\alias{nlayers,Raster-method}
@@ -22,8 +21,9 @@
\alias{nbands}
\title{Dimensions}
+
\description{
- Get dimensions of a Raster* object
+ Get dimension properties of a Raster* object
}
\usage{
@@ -33,7 +33,6 @@
xres(object)
yres(object)
resolution(object)
-origin(object)
nlayers(object)
layerNames(object)
band(object)
Modified: pkg/raster/man/expand.Rd
===================================================================
--- pkg/raster/man/expand.Rd 2009-03-04 07:50:14 UTC (rev 309)
+++ pkg/raster/man/expand.Rd 2009-03-04 16:12:51 UTC (rev 310)
@@ -1,19 +1,26 @@
\name{expand}
+
\alias{expand}
-\title{Expand the extent of a RasterLayer}
+\title{expand}
+
\description{
-expand returns an elarged RasterLayer. The new RasterLayer has the extreme minimum and maximum coordinates of the input RasterLayer and BoundingBox object. Thus, all of the cells of the original raster are inlcluded.
+Expand returns an RasterLayer with a larger spatial extent.
+The output RasterLayer has the extreme minimum and maximum coordinates of the input RasterLayer and input BoundingBox arguments.
+Thus, all of the cells of the original raster are included.
}
+
\usage{
-expand(raster, bndbox, filename="", overwrite=FALSE)
+expand(raster, bndbox, filename=NULL, filetype='raster', overwrite=FALSE, track=-1)
}
\arguments{
\item{raster}{a RasterLayer object}
\item{bndbox}{BoundingBox object, or any object that is or has a bounding box (see below)}
- \item{filename}{character. Output filename for a new raster}
- \item{overwrite}{if \code{TRUE}, the file will be overwritten if it exists}
+ \item{filename}{filename for the output RasterLayer}
+ \item{overwrite}{if TRUE, "filename" will be overwritten if it exists}
+ \item{filetype}{output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{writeRaster}}}
+ \item{track}{vector of row numbers for which the function will report that they have been processed}
}
\details{
@@ -22,11 +29,12 @@
}
\value{
-A new RasterLayer object (in the R environment), and in some cases the side effect of a new file on disk.
+A new RasterLayer object, and in some cases the side effect of a new file on disk.
}
+
\author{Robert J. Hijmans}
-\seealso{\code{\link[raster]{crop}}, \code{\link[raster]{Merge}}}
+\seealso{\code{\link[raster]{crop}}, \code{\link[raster]{merge}}}
\examples{
Modified: pkg/raster/man/extent.Rd
===================================================================
--- pkg/raster/man/extent.Rd 2009-03-04 07:50:14 UTC (rev 309)
+++ pkg/raster/man/extent.Rd 2009-03-04 16:12:51 UTC (rev 310)
@@ -4,6 +4,8 @@
\alias{xmax}
\alias{ymin}
\alias{ymax}
+\alias{origin}
+
\alias{projection}
\alias{isLatLon}
\alias{isLatLon,Raster-method}
@@ -22,6 +24,7 @@
xmax(object)
ymin(object)
ymax(object)
+origin(object)
projection(object, asText=TRUE)
isLatLon(object)
}
Added: pkg/raster/man/stack-methods.Rd
===================================================================
--- pkg/raster/man/stack-methods.Rd (rev 0)
+++ pkg/raster/man/stack-methods.Rd 2009-03-04 16:12:51 UTC (rev 310)
@@ -0,0 +1,55 @@
+\name{stack}
+
+\docType{methods}
+
+\alias{stack}
+
+\alias{stack,character-method}
+\alias{stack,Raster-method}
+\alias{stack,list-method}
+
+\title{ stack }
+
+\description{
+A RasterStack is a collection of RasterLayer objects with the same spatial extent and resolution.
+A RasterStack can be created from RasterLayer objects, or from raster files, or both.
+}
+
+\section{Methods}{
+\describe{
+\code{stack(x, ...)}
+
+
+ \item{\code{x}}{ filename (character), Raster* object, or list (of filenames and/or Raster* objects) }
+ \item{\code{...}}{additional RasterLayer objects (or filenames) }
+ \item{bands}{ optional argument. a vector or list of bands of raster data files}
+
+\code{x} can be a filename from which a RasterLayer can be created (with \code{rasterFromFile}, a Raster* object,
+or a list of filenames and/or Raster* objects.
+
+The argument \code{bands} can be used to pass a vector of band numbers to indicated which band of a file (in a multiband file) should be used.
+A band numbers < 1 will lead to inclusion of all bands into the RasterStack.
+
+If \code{x} is a \code{list}, no additional arguments \code{...} are accepted, except for the optional \code{bands} argument
+
+}}
+
+\value{
+ a RasterStack object
+}
+
+
+\author{Robert J. Hijmans}
+
+\seealso{ \code{\link[raster]{changestack}}, \code{\link[raster]{rasterFromFile}}}
+
+\examples{
+ fn <- system.file("external/test.ag", package="sp")
+ s <- stack(fn, fn)
+ r <- rasterFromFile(fn)
+ s <- stack(r, fn)
+}
+
+
+\keyword{methods}
+\keyword{spatial}
More information about the Raster-commits
mailing list