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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jan 17 10:56:21 CET 2009


Author: rhijmans
Date: 2009-01-17 10:56:21 +0100 (Sat, 17 Jan 2009)
New Revision: 148

Modified:
   pkg/raster/R/Overlay.R
   pkg/raster/R/properties.R
   pkg/raster/man/Arith-methods.Rd
   pkg/raster/man/classes.Rd
   pkg/raster/man/overlay.Rd
Log:


Modified: pkg/raster/R/Overlay.R
===================================================================
--- pkg/raster/R/Overlay.R	2009-01-17 08:15:19 UTC (rev 147)
+++ pkg/raster/R/Overlay.R	2009-01-17 09:56:21 UTC (rev 148)
@@ -4,11 +4,20 @@
 # Version 0,1
 # Licence GPL v3
 
+setMethod('overlay', signature(x='RasterLayer', y='RasterLayer'), 
+	function(x, y, ...){ 
+		return(Overlay(x, y, ...) )
+	}
+)
 
-Overlay <- function(raster1, raster2, ..., fun=function(x,y){return(x+y)}, filename="", overwrite=FALSE) {
-#	if (class(raster1) != 'RasterLayer' | class(raster2) != 'RasterLayer') {
-#		stop('first two arguments should be objects of class "RasterLayer"')}
-	rasters <- c(raster1, raster2)
+
+Overlay <- function(x, y, ..., fun, filename="", overwrite=FALSE){ 
+
+	if (missing(fun)) { stop("you must supply a function 'fun'. E.g., 'fun=function(x,y){return(x+y)}'") }
+	if (missing(filename)) { filename <- "" }
+	if (missing(overwrite)) { overwrite <- FALSE }
+	
+	rasters <- c(x, y)
 	obs <- list(...)
 	if (isTRUE(length(obs) > 0)) {
 		for (i in 1:length(obs)) {
@@ -17,14 +26,14 @@
 			} 
 		}
 	}
-	if (length(rasters) > 6) {stop("sorry, this function cannot take more than 5 RasterLayers at a time")}
+	if (length(rasters) > 6) {stop("sorry, this function cannot take more than 6 RasterLayers at a time")}
 	
 	for (i in 2:length(rasters)) {
-		if (!compare(c(raster1, rasters[i]))) { 
+		if (!compare(c(x, rasters[i]))) { 
 			stop('Extent and/or resolution of rasters do not match') 
 		}	
 	}
-	outraster <- setRaster(raster1)
+	outraster <- setRaster(x)
 	outraster <- setFilename(outraster, filename)
 
 	inram <- TRUE
@@ -33,7 +42,7 @@
 		if (dataContent(rasters[[i]]) != 'all') {inram <- FALSE} 
 		if (dataSource(rasters[[i]]) != 'disk') {ondisk <- FALSE} 		
 	}	
-		
+	
 
 	if ( inram ) {
 	# there has to be a smarter way then this!
@@ -50,9 +59,18 @@
 		}
 		
 		outraster <- setValues(outraster, vals)
-		if (filename(outraster) != "") { writeRaster(outraster, overwrite=overwrite) }
-	} else if ( ondisk ) {
-		v <- vector(length=0)
+		if (filename(outraster) != "") { 
+			writeRaster(outraster, overwrite=overwrite) 
+		}
+		
+	} else {
+		if (filename(outraster) == "") {
+#			v <- vector(length=0)
+			v  <- vector(length=ncell(outraster))
+			endcell <- 0
+			inccol <- ncol(outraster) - 1
+		}	
+		
 		for (r in 1:nrow(outraster)) {
 			for (i in 1:length(rasters)) {
 				if (dataSource(rasters[[i]]) == 'ram') {
@@ -73,7 +91,10 @@
 				vals <- fun( values(rasters[[1]]), values(rasters[[2]]), values(rasters[[3]]), values(rasters[[4]]), values(rasters[[5]]), values(rasters[[6]]) )
 			}
 			if (filename(outraster) == "") {
-				v <- c(v, vals)
+#				v <- c(v, vals)
+				startcell <- endcell + 1
+				endcell <- startcell + inccol
+				v[startcell:endcell] <- vals
 			} else {
 				outraster <- setValues(outraster, vals, r)
 				outraster <- writeRaster(outraster, overwrite=overwrite)

Modified: pkg/raster/R/properties.R
===================================================================
--- pkg/raster/R/properties.R	2009-01-17 08:15:19 UTC (rev 147)
+++ pkg/raster/R/properties.R	2009-01-17 09:56:21 UTC (rev 148)
@@ -11,20 +11,9 @@
 }	
 
 setMethod('isLatLon', signature(object='Raster'), 
-# copied from the SP package (slightly adapted)
-#author:
-# ...
+# from the SP package (slightly adapted)
 	function(object){
-		p4str <- projection(object)
-		if (is.na(p4str) || nchar(p4str) == 0) {
-			return(as.logical(NA))
-		} 
-		res <- grep("longlat", p4str, fixed = TRUE)
-		if (length(res) == 0) {
-			return(FALSE)
-		} else {
-			return(TRUE)
-		}
+		return(	!is.projected(object) )
     }
 )
 
@@ -137,7 +126,13 @@
 
 origin <- function(object) {
 	x <- xmin(object) - xres(object)*(round(xmin(object) / xres(object)))
+	if ( x < 0 & abs(x) < 0.5 * xres(object)) {
+		x <- x + xres(object)
+	}
 	y <- ymax(object) - yres(object)*(round(ymax(object) / yres(object)))
+	if ( y < 0 & abs(y) < 0.5 * yres(object)) {
+		y <- y + yres(object)
+	}
 	return(c(x, y))
 }
 

Modified: pkg/raster/man/Arith-methods.Rd
===================================================================
--- pkg/raster/man/Arith-methods.Rd	2009-01-17 08:15:19 UTC (rev 147)
+++ pkg/raster/man/Arith-methods.Rd	2009-01-17 09:56:21 UTC (rev 148)
@@ -6,7 +6,7 @@
 \alias{Arith,RasterLayer,RasterLayer-method}
 \title{Methods for arithmic function for RasterLayer objects}
 \description{
-  Standard arithmic functions for computations with RasterLayer objects and numeric values
+  Standard arithmic functions for computations with RasterLayer objects and numeric values 
 }
 \section{Note}{
  The RasterLayer objects must have either all data in memory or on disk. If the data do not fit into memory you should use the overlay() function instead.

Modified: pkg/raster/man/classes.Rd
===================================================================
--- pkg/raster/man/classes.Rd	2009-01-17 08:15:19 UTC (rev 147)
+++ pkg/raster/man/classes.Rd	2009-01-17 09:56:21 UTC (rev 148)
@@ -67,7 +67,7 @@
 
 
 \section{Objects from the Class}{
-Objects can be created by calls of the form \code{new("RasterLayer", ...)}.
+Objects can be created by calls of the form \code{new("RasterLayer")}, but you should normally use functions such as newRaster() and rasterFromFile().
 }
 
 \section{Slots}{

Modified: pkg/raster/man/overlay.Rd
===================================================================
--- pkg/raster/man/overlay.Rd	2009-01-17 08:15:19 UTC (rev 147)
+++ pkg/raster/man/overlay.Rd	2009-01-17 09:56:21 UTC (rev 148)
@@ -1,19 +1,20 @@
 \name{overlay}
+\alias{overlay,RasterLayer,RasterLayer-method}
 \alias{Overlay}
 
 \title{ Raster overlay calculation }
 \description{
   Calculate values for a new RasterLayer object, based on two other rasters.  
-  
 }
+
 \usage{
-	Overlay(raster1, raster2, ..., fun=function(x,y){return(x+y)}, filename="", overwrite=FALSE) 
+	Overlay(x, y, ..., fun, filename="", overwrite=FALSE) 
 }
 
 \arguments{
-  \item{raster1}{ a RasterLayer object }
-  \item{raster2}{ a RasterLayer object }
-  \item{...}{ additional RasterLayer objects (up to 3) }
+  \item{x}{ a RasterLayer object }
+  \item{y}{ a RasterLayer object }
+  \item{...}{ additional RasterLayer objects (up to 6 in total, including x and y) }
   \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}
@@ -24,7 +25,7 @@
   multiply: \code{fun=function(x,y){return(x*y)}}
   percentage: \code{fun=function(x,y){return(100 * x / y)}}
   
-  some overlay functions have been implemented as generic functions (*, /, +, -)
+  For smaller raster datasets, overlay functions can have been implemented through normal artimic functions (*, /, +, -) on RasterLayer objects (see examples)
   
 }
 \value{
@@ -36,12 +37,15 @@
 	r <- newRaster()
 	r1 <- init(r)
 	r2 <- init(r)
-	r3 <- Overlay(r1, r2)
+	r3 <- Overlay(r1, r2, fun=function(x,y){return(x+y)})
 
 # long version for multiplication
 	r4 <- Overlay(r1, r2, fun=function(x,y){return(x*y)} )
 # short function doing the same, if values can be loaded into ram memory
 	r5 <- r1 * r2
+
+# multiplication with more than two layers (make sure the number of RasterLayers matches the arguments of 'fun'
+	r6 <- Overlay(r1, r2, r3, r4, fun=function(a,b,c,d){return(a*b+c*d)} )	
 }	
 
 \keyword{ spatial }



More information about the Raster-commits mailing list