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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jan 9 04:21:29 CET 2009


Author: rhijmans
Date: 2009-01-09 04:21:29 +0100 (Fri, 09 Jan 2009)
New Revision: 108

Modified:
   pkg/raster/R/standard.generic.functions.R
   pkg/raster/man/class.raster.Rd
Log:


Modified: pkg/raster/R/standard.generic.functions.R
===================================================================
--- pkg/raster/R/standard.generic.functions.R	2009-01-09 02:19:58 UTC (rev 107)
+++ pkg/raster/R/standard.generic.functions.R	2009-01-09 03:21:29 UTC (rev 108)
@@ -65,13 +65,17 @@
 
 setMethod("Logic", signature(e1='RasterLayer', e2='RasterLayer'),
     function(e1, e2){ 
-		return(setRaster(e1, values=callGeneric(.getLogicalValues(e1), .getLogicalValues(e2))))
+		if ( compare(c(e1, e2)) ) {
+			return(setRaster(e1, values=callGeneric(.getLogicalValues(e1), .getLogicalValues(e2))))
+		}
 	}
 )
 
 setMethod("Arith", signature(e1='RasterLayer', e2='RasterLayer'),
     function(e1, e2){ 
-		return(setRaster(e1, values=callGeneric(.getValues(e1), .getValues(e2))))
+		if (compare(c(e1, e2))) {
+			return(setRaster(e1, values=callGeneric(.getValues(e1), .getValues(e2))))
+		}	
 	}
 )
 
@@ -96,7 +100,7 @@
 		} else {
 			v <- .getValues(x)
 			for (i in 1:length(obs)) {
-				if (class(obs[[1]]) == 'RasterLayer') {
+				if (class(obs[[1]]) == 'RasterLayer' & compare(c(x, obs[[1]]))) {
 					v <- pmax(v, .getValues(obs[[i]]), na.rm=na.rm)
 				} else if (is.atomic(obs[[1]])) {
 					v <- pmax(v, rep(obs[[1]], ncells(x)), na.rm=na.rm)
@@ -120,7 +124,7 @@
 		} else {
 			v <- .getValues(x)
 			for (i in 1:length(obs)) {
-				if (class(obs[[1]]) == 'RasterLayer') {
+				if (class(obs[[1]]) == 'RasterLayer' & compare(c(x, obs[[1]]))) {
 					v <- pmin(v, .getValues(obs[[i]]), na.rm=na.rm)
 				} else if (is.atomic(obs[[1]])) {
 					v <- pmin(v, rep(obs[[1]], ncells(x)), na.rm=na.rm)
@@ -136,30 +140,61 @@
 )
 
 
+.getSum <- function(obs, x, ..., na.rm=FALSE) {
+	v <- .getValues(x)
+	if (!(is.null(dim(v)))) {
+		v <- rowSums(.getValues(x), na.rm=na.rm)
+	}
+	for (i in 1:length(obs)) {
+		if ( (class(obs[[1]]) == 'RasterLayer' | class(obs[[1]]) == 'RasterStack' | class(obs[[1]]) == 'RasterBrick') & compare(c(x, obs[[1]])) ) {			
+			v <- rowSums(cbind(v, .getValues(obs[[i]]), na.rm=na.rm))
+		} else if (is.atomic(obs[[1]])) {
+			v <- rowSums(cbind(v, rep(obs[[1]], ncells(x)), na.rm=na.rm))
+		} else if (length(obs[[1]])==ncells(x)) {
+			v <- rowSums(cbind(v, obs[[1]], na.rm=na.rm))
+		} else {
+			stop(paste("I do not understand this argument:",obs[1])) 
+		}	
+	}
+	return(setRaster(x, values=v))
+}
 
+
 setMethod("sum", signature(x='RasterLayer'),
 	function(x, ..., na.rm=FALSE){
 		obs <- list(...)
 		if (length(obs) == 0) {
 			return(x)
 		} else {
-			v <- .getValues(x)
-			for (i in 1:length(obs)) {
-				if (class(obs[[1]]) == 'RasterLayer') {
-					v <- rowSums(cbind(v, .getValues(obs[[i]]), na.rm=na.rm))
-				} else if (is.atomic(obs[[1]])) {
-					v <- rowSums(cbind(v, rep(obs[[1]], ncells(x)), na.rm=na.rm))
-				} else if (length(obs[[1]])==ncells(x)) {
-					v <- rowSums(cbind(v, obs[[1]], na.rm=na.rm))
-				} else {
-					stop(paste("I do not understand this argument:",obs[1])) 
-				}	
-			}
-			return(setRaster(x, values=v))
+			return(.getSum(obs, x, ..., na.rm))
 		}
 	}
 )
 
+
+setMethod("sum", signature(x='RasterStack'),
+	function(x, ..., na.rm=FALSE){
+		obs <- list(...)
+		if (length(obs) == 0) {
+			return(setRaster(x, values=rowSums(.getValues(x), na.rm)))
+		} else {
+			return(.getSum(obs, x, ..., na.rm))		
+		}
+	}
+)
+
+
+setMethod("sum", signature(x='RasterBrick'),
+	function(x, ..., na.rm=FALSE){
+		obs <- list(...)
+		if (length(obs) == 0) {
+			return(setRaster(x, values=rowSums(.getValues(x), na.rm)))
+		} else {
+			return(.getSum(obs, x, ..., na.rm))		
+		}
+	}
+)
+
 #todo "any", "all" 
 
 	
@@ -176,6 +211,10 @@
 )	
 	
 	
+	
+	
+	
+	
 setMethod('dim', signature(x='AbstractRaster'), 
 	function(x){ return(c(nrow(x), ncol(x)))}
 )

Modified: pkg/raster/man/class.raster.Rd
===================================================================
--- pkg/raster/man/class.raster.Rd	2009-01-09 02:19:58 UTC (rev 107)
+++ pkg/raster/man/class.raster.Rd	2009-01-09 03:21:29 UTC (rev 108)
@@ -15,10 +15,13 @@
 \alias{==,AbstractRaster,AbstractRaster-method}
 \alias{!=,AbstractRaster,AbstractRaster-method}
 \alias{is.na,RasterLayer-method}
+
+\alias{sum,RasterLayer-method}
+\alias{sum,RasterStack-method}
+\alias{sum,RasterBrick-method}
 \alias{min,RasterLayer-method}
 \alias{max,RasterLayer-method}
 \alias{range,RasterLayer-method}
-\alias{sum,RasterLayer-method}
 \alias{[,RasterLayer-method}
 
 \title{Class "RasterLayer" }



More information about the Raster-commits mailing list