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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 11 15:30:57 CET 2009


Author: rhijmans
Date: 2009-02-11 15:30:57 +0100 (Wed, 11 Feb 2009)
New Revision: 273

Modified:
   pkg/raster/R/values.R
   pkg/raster/man/LinesToRaster.Rd
   pkg/raster/man/values.Rd
Log:


Modified: pkg/raster/R/values.R
===================================================================
--- pkg/raster/R/values.R	2009-02-11 09:33:47 UTC (rev 272)
+++ pkg/raster/R/values.R	2009-02-11 14:30:57 UTC (rev 273)
@@ -5,27 +5,53 @@
 # Licence GPL v3
 
 
+values <- function(object, format='', names=FALSE) {
+	format <- trim(format)
+	if (class(object) == 'RasterLayer') {
+		if (format=='') {format <- 'vector'}
+		return(.rasterValues(object, format, names=names))
+	} else {
+		if (format=='') {format <- 'matrix'}
+		return(.stackValues(object, format, names=names))	
+	}
+}
 
-#if (!isGeneric("values")) {
-#	setGeneric("values", function(object, ...)
-#		standardGeneric("values"))
-#}	
 
-#setMethod('values', signature(object='Raster'), 
-values <- function(object, format='vector', names=FALSE) {
-	if (dataContent(object)=="nodata") {stop("first read some data (e.g., readAll()") }
+.rasterValues <- function(x, format='vector', names=FALSE) {
+	if (dataContent(x)=="nodata") {
+		stop("first read some data (e.g., readAll()") 
+	}
 	if (format=='matrix') { 
-		return(.values.as.matrix(object, names)) 
+		return(.values.as.matrix(x, names)) 
 	} else if (format=='dataframe') { 
-		return(.values.as.dataframe(object)) 
+		return(.values.as.dataframe(x)) 
 	} else {
-		return(object at data@values) 
+		return(x at data@values) 
 	}
 }
-#)
 
 
+.stackValues <- function(x, format='matrix', names=FALSE) {
+		if (dataContent(x)=="nodata") {
+			stop("first read some data (e.g., readAll()") 
+		}
+		vals <- x at data@values
+		if (names) {
+			colnames(vals) <- x at data@colnames
+		} 
+		if (format == 'dataframe') {
+			vals <- as.data.frame(vals)
+		}
+		if (format == 'vector') {
+			vals <- as.vector(vals)
+		}
+		return(vals)
+}
 
+
+
+
+
 valuesRow <- function(raster, rownr) {
 	if (dataContent(raster) == 'nodata') {
 		stop('no values in memory. First read or set values')

Modified: pkg/raster/man/LinesToRaster.Rd
===================================================================
--- pkg/raster/man/LinesToRaster.Rd	2009-02-11 09:33:47 UTC (rev 272)
+++ pkg/raster/man/LinesToRaster.Rd	2009-02-11 14:30:57 UTC (rev 273)
@@ -38,8 +38,8 @@
 
 r <- raster(ncols=90, nrows=45)
 r <- linesToRaster(lines, r)
-plot(r)
-plot(lines, add=TRUE)
+#plot(r)
+#plot(lines, add=TRUE)
 }
 
 \keyword{ spatial }

Modified: pkg/raster/man/values.Rd
===================================================================
--- pkg/raster/man/values.Rd	2009-02-11 09:33:47 UTC (rev 272)
+++ pkg/raster/man/values.Rd	2009-02-11 14:30:57 UTC (rev 273)
@@ -13,7 +13,7 @@
 }
 
 \usage{
-values(object, format='vector', names=FALSE)
+values(object, format='', names=FALSE)
 valuesRow(raster, rownr)
 minValue(object, layer=1)
 maxValue(object, layer=1)
@@ -22,7 +22,7 @@
 \arguments{
   \item{raster}{RasterLayer or RasterStack object}
   \item{object}{RasterLayer or RasterStack object}
-  \item{format}{use \code{vector} to get the data as a single vector or \code{matrix} to get them as a matrix}
+  \item{format}{specifiy the output format. Either '', 'vector', 'matrix' or 'dataframe'. If '' is used, a matrix is returned for a RasterStack, and a vector for a RasterLayer}
   \item{names}{If TRUE, row and col names are provided}
   \item{rownr}{The rownumber for which values are returned}
   \item{layer}{layer number (for RasterStack objects}



More information about the Raster-commits mailing list