[Raster-commits] r309 - in pkg/raster: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Mar 4 08:50:14 CET 2009


Author: rhijmans
Date: 2009-03-04 08:50:14 +0100 (Wed, 04 Mar 2009)
New Revision: 309

Added:
   pkg/raster/man/cover-methods.Rd
Removed:
   pkg/raster/man/cover.Rd
Modified:
   pkg/raster/R/aggregate.R
   pkg/raster/R/calc.R
   pkg/raster/R/cover.R
   pkg/raster/R/mCalc.R
   pkg/raster/R/overlay.R
   pkg/raster/R/summary.R
   pkg/raster/man/aggregate-methods.Rd
Log:


Modified: pkg/raster/R/aggregate.R
===================================================================
--- pkg/raster/R/aggregate.R	2009-03-04 05:10:15 UTC (rev 308)
+++ pkg/raster/R/aggregate.R	2009-03-04 07:50:14 UTC (rev 309)
@@ -59,7 +59,7 @@
 		}
 
 	} else if ( dataSource(x) == 'disk') { 
-		if (!.CanProcessInMemory(x, 1) && filename == '') {
+		if (!.CanProcessInMemory(x, 2) && filename == '') {
 			filename <- tempfile()
 			outraster <- setFilename(outraster, filename )
 			if (options('verbose')[[1]]) { cat('values were written to:', filename(raster))	}						

Modified: pkg/raster/R/calc.R
===================================================================
--- pkg/raster/R/calc.R	2009-03-04 05:10:15 UTC (rev 308)
+++ pkg/raster/R/calc.R	2009-03-04 07:50:14 UTC (rev 309)
@@ -33,7 +33,7 @@
 			outraster <- writeRaster(outraster, overwrite=overwrite, filetype=filetype)
 		}
 	} else if (dataSource(x) == 'disk') {
-		if (!.CanProcessInMemory(x, 1) & filename == '') {
+		if (!.CanProcessInMemory(x, 2) & filename == '') {
 			filename <- tempfile()
 			outraster <- setFilename(outraster, filename )
 		}

Modified: pkg/raster/R/cover.R
===================================================================
--- pkg/raster/R/cover.R	2009-03-04 05:10:15 UTC (rev 308)
+++ pkg/raster/R/cover.R	2009-03-04 07:50:14 UTC (rev 309)
@@ -5,20 +5,27 @@
 # Licence GPL v3
 
 
-cover <- function(x, y, filename="", overwrite=TRUE) {
-	if (class(x) != 'RasterLayer' | class(y) != 'RasterLayer') {
-		stop('first two arguments should be objects of class "RasterLayer"')
-	}
-	if (!compare(c(x, y))) { 
-		stop('rasters do not match') 
-	}
+if (!isGeneric("cover")) {
+	setGeneric("cover", function(x,y,...)
+		standardGeneric("cover"))
+}	
+
+setMethod('cover', signature(x='RasterLayer', y='RasterLayer'), 
+	function(x, y, filename="", overwrite=TRUE, filetype='raster', datatype=dataType(x), track=-1) {
+	
+	compare(c(x, y))
+	
 	outRaster <- setRaster(x, filename)
+	outRaster <- setDatatype(outRaster, datatype)
+	
+	# need to check the datatype. if x = INT and y = FLT, result should be FLT?
+	
 	if ( dataContent(x) == 'all' & dataContent(y) == 'all') {
 		x at data@values[is.na(x at data@values)] <- values(y)[is.na(x at data@values)]
 		rm(y)
 		outRaster <- setValues(outRaster, values(x))
 		if (filename(outRaster) != "") { 
-			outraster <- writeRaster(outRaster, overwrite=overwrite) 
+			outraster <- writeRaster(outRaster, filetype=filetype, overwrite=overwrite) 
 		}
 	} else {
 		if (dataContent(x) == 'nodata'  &  dataSource(x) == 'ram' ) {
@@ -27,6 +34,13 @@
 		if (dataContent(y) == 'nodata'  &  dataSource(y) == 'ram' ) {
 			stop('values for y are not available')
 		}
+		
+		if (!.CanProcessInMemory(x, 2) && filename == '') {
+			filename <- tempfile()
+			outraster <- setFilename(outraster, filename )
+			if (options('verbose')[[1]]) { cat('writing values to:', filename(raster))	}						
+		}
+		
 		v <- vector(length=0)
 		for (r in 1:nrow(outRaster)) {
 			x <- readRow(x, r)
@@ -37,7 +51,7 @@
 				v <- c(v, vals)
 			} else {
 				outRaster <- setValues(outRaster, vals, r)
-				outRaster <- writeRaster(outRaster, overwrite=overwrite)
+				outRaster <- writeRaster(outRaster, filetype=filetype, overwrite=overwrite)
 			}
 		}
 		if (filename(outRaster) == "") {
@@ -46,4 +60,4 @@
 	}
 	return(outRaster)
 }
-
+)

Modified: pkg/raster/R/mCalc.R
===================================================================
--- pkg/raster/R/mCalc.R	2009-03-04 05:10:15 UTC (rev 308)
+++ pkg/raster/R/mCalc.R	2009-03-04 07:50:14 UTC (rev 309)
@@ -22,7 +22,7 @@
 		}
 	} else {
 		starttime <- proc.time()
-		if (!.CanProcessInMemory(x, 1) & filename == '') {
+		if (!.CanProcessInMemory(x, 2) & filename == '') {
 			filename=tempfile()
 			outraster <- setFilename(outraster, filename )
 		}

Modified: pkg/raster/R/overlay.R
===================================================================
--- pkg/raster/R/overlay.R	2009-03-04 05:10:15 UTC (rev 308)
+++ pkg/raster/R/overlay.R	2009-03-04 07:50:14 UTC (rev 309)
@@ -68,7 +68,7 @@
 		
 	} else {
 		if (filename(outraster) == "") {
-			if (!.CanProcessInMemory(outraster, 1)) {
+			if (!.CanProcessInMemory(outraster, 2)) {
 				filename <- tempfile()
 				outraster <- setFilename(outraster, filename )
 			} else {

Modified: pkg/raster/R/summary.R
===================================================================
--- pkg/raster/R/summary.R	2009-03-04 05:10:15 UTC (rev 308)
+++ pkg/raster/R/summary.R	2009-03-04 07:50:14 UTC (rev 309)
@@ -17,6 +17,14 @@
 	}
 
 	m <- matrix(NA, nrow=ncol(rasters[[1]]), ncol=length(rasters))
+	
+	for (i in 2:length(rasters)) {
+		if (extends(class(rasters[[i]]), "Raster")) {
+			compare(c(rasters[[1]], rasters[[i]]))
+		}
+	}
+
+	
 	for (r in 1:nrow(rasters[[1]])) {
 		m[] <- NA
 		for (i in 1:length(rasters)) {

Modified: pkg/raster/man/aggregate-methods.Rd
===================================================================
--- pkg/raster/man/aggregate-methods.Rd	2009-03-04 05:10:15 UTC (rev 308)
+++ pkg/raster/man/aggregate-methods.Rd	2009-03-04 07:50:14 UTC (rev 309)
@@ -1,7 +1,10 @@
 \name{aggregate}
+
 \docType{methods}
+
 \alias{aggregate}
 \alias{aggregate,RasterLayer-method}
+
 \title{Aggregate}
 
 \description{

Added: pkg/raster/man/cover-methods.Rd
===================================================================
--- pkg/raster/man/cover-methods.Rd	                        (rev 0)
+++ pkg/raster/man/cover-methods.Rd	2009-03-04 07:50:14 UTC (rev 309)
@@ -0,0 +1,54 @@
+\name{cover}
+\docType{methods}
+
+\alias{cover}
+\alias{cover,RasterLayer,RasterLayer-method}
+
+\title{ cover }
+\description{
+ Replace \code{NA} values in the first RasterLayer with the values of the second RasterLayer
+ 
+ 
+}
+\section{Methods}{
+\describe{
+\code{cover(x, y, filename="", overwrite=TRUE) }
+
+  \item{\code{x}}{ a RasterLayer object }
+  \item{\code{y}}{ a RasterLayer object }
+  \item{\code{filename}}{ filename for the output raster. A valid filename must be provided when the data of the input rasters are on disk }
+  \item{\code{overwrite}}{logical. If \code{TRUE}, existing files will be overwritten}
+
+
+\item{x = "RasterLayer", y = "RasterLayer"}{ ~~describe this method here }
+}}
+
+\details{
+The function returns a RasterLayer with the values of the second RasterLayer, \code{y}, 
+where the values of the first RasterLayer, \code{x}, are NA and the values of the first RasterLayer elsewhere.
+
+If the input RasterLayer objects have 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 no filename is specified, and the resulting RasterLayer is too large to hold in memory, it is saved to a temporary file.  
+}
+
+\value{
+A new RasterLayer object, and in some cases the side effect of a new file on disk.
+}
+
+\author{Robert J. Hijmans}
+
+\examples{
+r1 <- raster(ncols=36, nrows=18)
+r1[] <- 1:ncell(r1)
+r2 <- setValues(r1, runif(ncell(r1)))
+r2 <- calc(r2, fun=function(x){ x[x<0.5] <- NA; return(x)} )
+#equivalent to: 
+#r2[r2<0.5] <- NA
+
+r3 <- cover(r2, r1)
+}	
+
+\keyword{methods}
+\keyword{spatial}

Deleted: pkg/raster/man/cover.Rd
===================================================================
--- pkg/raster/man/cover.Rd	2009-03-04 05:10:15 UTC (rev 308)
+++ pkg/raster/man/cover.Rd	2009-03-04 07:50:14 UTC (rev 309)
@@ -1,41 +0,0 @@
-\name{cover}
-\alias{cover}
-
-\title{ Raster cover calculation }
-\description{
-  Replace NA values in a raster with the values of another raster 
-}
-
-\usage{
-cover(x, y, filename="", overwrite=TRUE) 
-}
-
-\arguments{
-  \item{x}{ a RasterLayer object }
-  \item{y}{ a RasterLayer object }
-  \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}
-}
-
-\details{
-The function returns a RasterLayer with the values of the second RasterLayer where the values of the first RasterLayer are NA and the values of the first RasterLayer elsewhere.
-
-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{
-r1 <- raster()
-r1[] <- 1:ncell(r1)
-r2 <- setValues(r1, runif(ncell(r1)))
-fun <- function(x) { x[x<0.5] <- NA; return(x) }
-r2 <- calc(r2, fun)
-r3 <- cover(r2, r1)
-}	
-
-\keyword{ spatial }



More information about the Raster-commits mailing list