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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Mar 19 13:21:01 CET 2009


Author: rhijmans
Date: 2009-03-19 13:21:01 +0100 (Thu, 19 Mar 2009)
New Revision: 374

Added:
   pkg/raster/R/valuesRow.R
   pkg/raster/man/extremeValues.Rd
   pkg/raster/man/fileFunctions.Rd
Removed:
   pkg/raster/man/misc.Rd
Modified:
   pkg/raster/R/copyRaster.R
   pkg/raster/R/distance.R
   pkg/raster/R/export.R
   pkg/raster/R/filenames.R
   pkg/raster/R/setFilename.R
   pkg/raster/R/values.R
   pkg/raster/R/writeRaster.R
   pkg/raster/man/values.Rd
Log:


Modified: pkg/raster/R/copyRaster.R
===================================================================
--- pkg/raster/R/copyRaster.R	2009-03-19 08:54:11 UTC (rev 373)
+++ pkg/raster/R/copyRaster.R	2009-03-19 12:21:01 UTC (rev 374)
@@ -37,7 +37,8 @@
 	fileext <- toupper(fileExtension(f)) 
 	if (fileext == ".GRD") {
 		file.remove(f)
-		file.remove(setFileExtension(f, '.gri'))
+		fileExtension(f) <- '.gri'
+		file.remove(f)
 	}
 	return(r)
 }
@@ -47,8 +48,10 @@
 	f <- trim(filename(raster))
 	fileext <- toupper(fileExtension(f)) 
 	if (fileext == ".GRD") {
-		fgrd <- setFileExtension(filename, '.grd')
-		fgri <- setFileExtension(filename, '.gri')
+		fgrd <- filename
+		fileExtension(fgrd) <- '.grd'
+		fgri <- filename
+		fileExtension(fgri) <- '.gri'
 		if ( file.exists(fgrd) & !overwrite ) {
 			stop('file exists & overwrite = FALSE')
 		}
@@ -57,7 +60,7 @@
 		}
 		res <- file.copy(f, fgrd, overwrite)
 		if (!res) { stop('could not copy grd file') } 
-		f <- setFileExtension(f, '.gri')
+		fileExtension(f) <- ".gri"
 		res <- file.copy(f, fgri, overwrite)
 		if (!res) { stop('could not copy gri file') } 
 		filename(raster) <- fgrd

Modified: pkg/raster/R/distance.R
===================================================================
--- pkg/raster/R/distance.R	2009-03-19 08:54:11 UTC (rev 373)
+++ pkg/raster/R/distance.R	2009-03-19 12:21:01 UTC (rev 374)
@@ -59,7 +59,8 @@
 			outRaster <- raster(object, filename)
 			for(r in 1:nrows)
 			{
-				rowValues <- valuesRow(readRow(object, rownr = r), rownr = r)
+				object <- readRow(object, rownr = r)
+				rowValues <- values(object)
 				outRowValues <- rep(Inf,times=ncols)
 				outRowValues[is.na(rowValues)] <- 0
 				outRaster <- setValues(outRaster, outRowValues, r)
@@ -70,9 +71,13 @@
 				while(remainingCells){
 					remainingCells <- FALSE
 					oldRowValues <- integer(0)
-					rowWindow <- valuesRow(readRow(outRaster, rownr=1), rownr = 1)
+					outRaster <- readRow(outRaster, rownr=1)
+					rowWindow <- values(outRaster)
 					for(r in 1:nrows){
-						if(r<nrows-1){rowWindow <- c(rowWindow,valuesRow(readRow(outRaster, rownr=r+1), rownr=r+1))}
+						if(r < nrows-1) {
+							outRaster <- readRow(outRaster, rownr=r+1)
+							rowWindow <- c(rowWindow, values(outRaster))
+						}
 						adj <- adjacency(fromCells=(((max(1,r-1))*ncols)+1):(min(nrows,(r+2)*ncols)), toCells=((r-1)*ncols+1):(r*ncols),directions=8)
 						coord <- cbind(xyFromCell(object,adj[,1]),xyFromCell(object,adj[,2]))
 						distance <- apply(coord,1,function(x){pointDistance(x[1:2],x[3:4], type='GreatCircle')})
@@ -98,9 +103,13 @@
 				while(remainingCells){
 					remainingCells <- FALSE
 					oldRowValues <- integer(0)
-					rowWindow <- valuesRow(readRow(outRaster, rownr = 1), rownr = 1)
+					outRaster <- readRow(outRaster, rownr = 1)
+					rowWindow <- values(outRaster)
 					for(r in 1:nrows){
-						if(r<nrows-1){rowWindow <- c(rowWindow,valuesRow(readRow(outRaster, rownr=r+1), rownr=r+1))}
+						if(r<nrows-1){
+							outRaster <- readRow(outRaster, rownr=r+1)
+							rowWindow <- c(rowWindow, values(outRaster))
+						}
 						fromCells <- (((max(1,r-1))*ncols)+1):(min(nrows,(r+2)*ncols))
 						toCells <- ((r-1)*ncols+1):(r*ncols)
 						adj1 <- adjacency(object,fromCells=fromCells,toCells=toCells,directions=4)

Modified: pkg/raster/R/export.R
===================================================================
--- pkg/raster/R/export.R	2009-03-19 08:54:11 UTC (rev 373)
+++ pkg/raster/R/export.R	2009-03-19 12:21:01 UTC (rev 374)
@@ -18,7 +18,8 @@
  
 .writeStx <- function(raster) {
 	if (raster at data@haveminmax) {
-		stxfile <- setFileExtension(filename(raster), ".stx")
+		stxfile <- filename(raster)
+		fileExtension(stxfile) <- ".stx"
 		thefile <- file(stxfile, "w")  # open an txt file connectionis
 		cat(1, " ", minValue(raster), " ", maxValue(raster), "\n", file = thefile)
 		close(thefile)
@@ -27,7 +28,8 @@
  
  
 .writeBilHdr <- function(raster) {
-	hdrfile <- setFileExtension(filename(raster), ".hdr")
+	hdrfile <- filename(raster)
+	fileExtension(hdrfile) <- ".hdr"
 	thefile <- file(hdrfile, "w")  # open an txt file connectionis
 	cat("NROWS ",  nrow(raster), "\n", file = thefile)
 	cat("NCOLS ",  ncol(raster), "\n", file = thefile)
@@ -77,7 +79,8 @@
 
 
 .writeErdasRawHdr <- function(raster) {
-	hdrfile <- setFileExtension(filename(raster), ".raw")
+	hdrfile <- filename(raster)
+	fileExtension(hdrfile) <- ".raw"
 	thefile <- file(hdrfile, "w")  # open an txt file connectionis
 	cat("IMAGINE_RAW_FILE\n", file = thefile)
 	cat("PIXEL_FILES ", .setFileExtensionValues(filename(raster)), "\n", file = thefile)
@@ -122,7 +125,8 @@
  
 
 worldFile <- function(raster, extension=".wld") {
-	hdrfile <- setFileExtension(filename(raster), extension)
+	hdrfile <- filename(raster)
+	fileExtension(hdrfile) <- extension
 	thefile <- file(hdrfile, "w")  
 	cat(xres(raster), "\n", file = thefile)
 	cat("0\n", file = thefile)
@@ -137,7 +141,8 @@
  
 
 .writeENVIHdr <- function(raster) {
-	hdrfile <- setFileExtension(filename(raster), ".hdr")
+	hdrfile <- filename(raster)
+	fileExtension(hdrfile) <- ".hdr"
 	thefile <- file(hdrfile, "w") 
 	cat("ENVI\n", file = thefile)
 	cat("description = {", raster at file@shortname, "}", "\n", file = thefile)

Modified: pkg/raster/R/filenames.R
===================================================================
--- pkg/raster/R/filenames.R	2009-03-19 08:54:11 UTC (rev 373)
+++ pkg/raster/R/filenames.R	2009-03-19 12:21:01 UTC (rev 374)
@@ -49,11 +49,11 @@
 }   
 
 
-setFileExtension <- function(filename, newextension="") {
+'fileExtension<-' <- function(filename, value) {
 	lfn <- nchar(filename)
-	newextension <- trim(newextension)
-	if (newextension != "" & substr(newextension, 1, 1) != ".") {
-		newextension <- paste(".", newextension, sep="") 
+	value <- trim(value)
+	if (value != "" & substr(value, 1, 1) != ".") {
+		value <- paste(".", value, sep="") 
 	}
 	extstart <- -1
 	for (i in lfn : 2) {
@@ -63,9 +63,9 @@
 		}
 	}
     if (extstart > 0) {
-	   fname <- paste(substr(filename, 1, extstart-1), newextension, sep="")
+	   fname <- paste(substr(filename, 1, extstart-1), value, sep="")
 	   }
-	else { fname <- paste(filename, newextension, sep="")   
+	else { fname <- paste(filename, value, sep="")   
 	}
   return(fname)  
 }   

Modified: pkg/raster/R/setFilename.R
===================================================================
--- pkg/raster/R/setFilename.R	2009-03-19 08:54:11 UTC (rev 373)
+++ pkg/raster/R/setFilename.R	2009-03-19 12:21:01 UTC (rev 374)
@@ -13,19 +13,17 @@
 }
 
 'filename<-' <- function(x, value) {
-
 	filename <- trim(value)
 	if (is.na(filename) || is.null(filename)) {
 		filename <- ""
 	}
 	if (class(x)=='RasterStack') {
-		x at filename <- setFileExtension(filename, ".stk")
+		fileExtension(filename) <- ".stk"
+		x at filename <- filename
 	} else {
 		x at file@name <- filename
-	}	
-	if (class(x)=='RasterLayer') {
 		shortname <- shortFileName(filename)
-		shortname <- setFileExtension(shortname, "")
+		fileExtension(shortname) <- ""
 		shortname <- gsub(" ", "_", shortname)
 		if (nbands(x) > 1) { 
 			shortname <- paste(shortname, "_", band(x)) 

Modified: pkg/raster/R/values.R
===================================================================
--- pkg/raster/R/values.R	2009-03-19 08:54:11 UTC (rev 373)
+++ pkg/raster/R/values.R	2009-03-19 12:21:01 UTC (rev 374)
@@ -1,7 +1,7 @@
 # Author: Robert J. Hijmans, r.hijmans at gmail.com
 # International Rice Research Institute
 # Date :  June 2008
-# Version 0,1
+# Version 0.8
 # Licence GPL v3
 
 
@@ -54,81 +54,6 @@
 
 
 
-
-
-valuesRow <- function(raster, rownr) {
-	if (dataContent(raster) == 'nodata') {
-		stop('no values in memory. First read or set values')
-	}
-	if (rownr < 0) {
-		if (dataContent(raster) == 'all') {
-			return(raster)
-		} else {
-			stop('cannot get these values')
-		}
-	}
-	if (!(validRow(raster, rownr))) {
-		stop(paste(rownr,'is not a valid rownumber')) 
-	}
-	if (dataContent(raster) == 'sparse') {
-		return (.valuesRow.sparse(raster, rownr)) 
-	} else if (dataContent(raster) == 'row') {
-		startcell <- cellFromRowCol(raster, rownr, 1)
-		endcell <- startcell+ncol(raster)-1
-		if (dataIndices(raster) == c(startcell, endcell)) {
-			return(values(raster))
-		} else {
-			stop('this row is not in memory. First use readRow() or readAll')		
-		}
-	} else if (dataContent(raster) == 'block') {
-		firstcol <- colFromCell(raster, dataIndices(raster)[1])
-		lastcol <- colFromCell(raster, dataIndices(raster)[2])
-		if (firstcol != 1 | lastcol != ncol(raster)) {
-			stop('the block data in this raster does not have complete rows')
-		}
-		firstrow <- rowFromCell(raster, dataIndices(raster)[1])
-		lastrow <- rowFromCell(raster, dataIndices(raster)[2])
-		if (rownr < firstrow | rownr > lastrow) {
-			stop('this row is not in memory. First use readRow() or readAll')		
-		}
-		startcell <- ((rownr - firstrow) * ncol(raster) + 1) 
-		endcell <- startcell + ncol(raster) - 1
-		if (class(raster) == 'RasterStack') {
-			return(values(raster)[startcell:endcell,])
-		} else {	
-			return(values(raster)[startcell:endcell])
-		}
-	} else if (dataContent(raster) == 'all'){
-		startcell <- cellFromRowCol(raster, rownr, 1)
-		endcell <- startcell+ncol(raster)-1
-		if (class(raster) == 'RasterStack') {
-			return(values(raster)[startcell:endcell,])
-		} else {	
-			return(values(raster)[startcell:endcell])
-		}
-	} else {
-		stop('something is wrong with the RasterLayer dataContent')
-	}
-}
-
-
-.valuesRow.sparse <- function(raster, rownr, explode=TRUE) {
-	if (dataContent(raster) != 'sparse') {stop('cannot do. Need sparse')}
-	startcell <- cellFromRowCol(raster, rownr, 1)
-	endcell <- startcell+ncol(raster)-1
-	d <- cbind(dataIndices(raster), values(raster))
-	d <- d[d[,1] >= startcell & d[,1] <= endcell, ] 
-	if (explode) { 
-		cells <- startcell:endcell
-		cells[] <- NA
-		cells[d[,1]] <- d[,2]	
-		return(cells)
-	} else {
-		return(d)
-	}	
-}
-
-
 .values.as.dataframe <- function(raster) {
 	m <- as.data.frame(.values.as.matrix(raster, FALSE))
 	if (isTRUE(length(raster at data@colnames) == dim(m)[2])) { 

Added: pkg/raster/R/valuesRow.R
===================================================================
--- pkg/raster/R/valuesRow.R	                        (rev 0)
+++ pkg/raster/R/valuesRow.R	2009-03-19 12:21:01 UTC (rev 374)
@@ -0,0 +1,78 @@
+# Author: Robert J. Hijmans, r.hijmans at gmail.com
+# International Rice Research Institute
+# Date :  June 2008
+# Version 0.8
+# Licence GPL v3
+
+valuesRow <- function(object, rownr) {
+	if (dataContent(object) == 'nodata') {
+		stop('no values in memory. First read or set values')
+	}
+	if (rownr < 0) {
+		if (dataContent(object) == 'all') {
+			return(object)
+		} else {
+			stop('cannot get these values')
+		}
+	}
+	if (!(validRow(object, rownr))) {
+		stop(paste(rownr,'is not a valid rownumber')) 
+	}
+	if (dataContent(object) == 'sparse') {
+		return (.valuesRow.sparse(object, rownr)) 
+	} else if (dataContent(object) == 'row') {
+		startcell <- cellFromRowCol(object, rownr, 1)
+		endcell <- startcell+ncol(object)-1
+		if (dataIndices(object) == c(startcell, endcell)) {
+			return(values(object))
+		} else {
+			stop('this row is not in memory. First use readRow() or readAll')		
+		}
+	} else if (dataContent(object) == 'block') {
+		firstcol <- colFromCell(object, dataIndices(object)[1])
+		lastcol <- colFromCell(object, dataIndices(object)[2])
+		if (firstcol != 1 | lastcol != ncol(object)) {
+			stop('the block data in this object does not have complete rows')
+		}
+		firstrow <- rowFromCell(object, dataIndices(object)[1])
+		lastrow <- rowFromCell(object, dataIndices(object)[2])
+		if (rownr < firstrow | rownr > lastrow) {
+			stop('this row is not in memory. First use readRow() or readAll')		
+		}
+		startcell <- ((rownr - firstrow) * ncol(object) + 1) 
+		endcell <- startcell + ncol(object) - 1
+		if (class(object) == 'objectStack') {
+			return(values(object)[startcell:endcell,])
+		} else {	
+			return(values(object)[startcell:endcell])
+		}
+	} else if (dataContent(object) == 'all'){
+		startcell <- cellFromRowCol(object, rownr, 1)
+		endcell <- startcell+ncol(object)-1
+		if (class(object) == 'objectStack') {
+			return(values(object)[startcell:endcell,])
+		} else {	
+			return(values(object)[startcell:endcell])
+		}
+	} else {
+		stop('something is wrong with the objectLayer dataContent')
+	}
+}
+
+
+.valuesRow.sparse <- function(raster, rownr, explode=TRUE) {
+	if (dataContent(raster) != 'sparse') {stop('cannot do. Need sparse')}
+	startcell <- cellFromRowCol(raster, rownr, 1)
+	endcell <- startcell+ncol(raster)-1
+	d <- cbind(dataIndices(raster), values(raster))
+	d <- d[d[,1] >= startcell & d[,1] <= endcell, ] 
+	if (explode) { 
+		cells <- startcell:endcell
+		cells[] <- NA
+		cells[d[,1]] <- d[,2]	
+		return(cells)
+	} else {
+		return(d)
+	}	
+}
+

Modified: pkg/raster/R/writeRaster.R
===================================================================
--- pkg/raster/R/writeRaster.R	2009-03-19 08:54:11 UTC (rev 373)
+++ pkg/raster/R/writeRaster.R	2009-03-19 12:21:01 UTC (rev 374)
@@ -6,12 +6,12 @@
 
 
 .setFileExtensionValues <- function(fname) {
-	fname <- setFileExtension(fname, ".gri")
+	fileExtension(fname) <- ".gri"
 	return(fname)
 }
  
 .setFileExtensionHeader <- function(fname) {
-	fname <- setFileExtension(fname, ".grd")
+	fileExtension(fname) <- ".grd"
 	return(fname)
 }
  

Added: pkg/raster/man/extremeValues.Rd
===================================================================
--- pkg/raster/man/extremeValues.Rd	                        (rev 0)
+++ pkg/raster/man/extremeValues.Rd	2009-03-19 12:21:01 UTC (rev 374)
@@ -0,0 +1,63 @@
+\name{extremeValues}
+\alias{maxValue}
+\alias{minValue}
+  
+  
+\title{Get raster properties}
+\description{
+  values returns cell values for a Raster* object (typically after read* has been used).
+ 
+  valuesRow returns values for a row of values in the case that all values are in memory 
+  (if they are on disk, use \code{readRow} and \code{values})
+}
+
+\usage{
+minValue(object, layer=1)
+maxValue(object, layer=1)
+}
+
+\arguments{
+  \item{object}{RasterLayer or RasterStack object}
+  \item{layer}{layer number (for RasterStack objects}
+}
+  
+
+\note{
+	RasterLayer values are returned as a vector unless the option format='matrix' is used. 
+	In that case, a matrix is returned, optionally with the row and col numbers (names=TRUE). 
+	Some of these functions simply take the values from the data slots in the Raster or RasterStack object.   
+}
+
+\value{
+raster cell value(s)
+}
+
+\author{Robert J. Hijmans }
+\examples{
+#using a new default raster (1 degree global)
+r <- raster()
+r <- setValues(r, 1:ncell(r))
+minValue(r)
+maxValue(r)
+r <- setValues(r, round(100 * runif(ncell(r)) + 0.5))
+minValue(r)
+maxValue(r)
+
+r <- raster(system.file("external/test.ag", package="sp"))
+# this raster is from an 'external' format, so min and maxValue are not known
+minValue(r)
+maxValue(r)
+
+r1 <- setMinMax(r)
+# now they are known:
+minValue(r1)
+maxValue(r1)
+
+r2 <- readAll(r)
+# now they are known too:
+minValue(r2)
+maxValue(r2)
+
+
+}
+\keyword{spatial}

Added: pkg/raster/man/fileFunctions.Rd
===================================================================
--- pkg/raster/man/fileFunctions.Rd	                        (rev 0)
+++ pkg/raster/man/fileFunctions.Rd	2009-03-19 12:21:01 UTC (rev 374)
@@ -0,0 +1,48 @@
+\name{fileFunctions}
+
+\alias{fileExtension}
+\alias{fileExtension<-}
+\alias{shortFileName}
+\alias{filePath}
+\alias{trim}
+
+\title{filename functions}
+
+\description{
+  Low level functions for file name handling. 
+  Get or change file extension or extract the filename or path from a full name (path + filename). 
+  use \code{trim} to remove the whitespace before or after a string of characters.
+}
+
+\usage{
+fileExtension(filename)
+fileExtension(filename) <- value
+shortFileName(filename)
+filePath(filename)
+trim(x)
+}
+
+\arguments{
+  \item{filename}{A filename, with or without the path}
+  \item{value}{A file extension including the dot, e.g., ".txt" }
+  \item{x}{A character string (filename)}
+}
+
+\value{
+A file extension, filename or path.  
+}
+
+\author{Robert J. Hijmans }
+
+\examples{
+	fn <- "  c:/temp folder/filename.extension  "
+	fn <- trim(fn)
+	fileExtension(fn)
+	fileExtension(fn) <- ".txt"
+	fileExtension(fn)
+	shortFileName(fn)
+	filePath(fn)
+}
+
+\keyword{file}
+

Deleted: pkg/raster/man/misc.Rd
===================================================================
--- pkg/raster/man/misc.Rd	2009-03-19 08:54:11 UTC (rev 373)
+++ pkg/raster/man/misc.Rd	2009-03-19 12:21:01 UTC (rev 374)
@@ -1,47 +0,0 @@
-\name{fileFunctions}
-
-\alias{setFileExtension}
-\alias{fileExtension}
-\alias{shortFileName}
-\alias{filePath}
-\alias{trim}
-
-\title{filename functions}
-
-\description{
-  Low level functions for file name handling. 
-  Get or change file extension or extract the filename or path from a full name (path + filename). 
-  use \code{trim} to remove the whitespace before or after a string of characters.
-}
-
-\usage{
-setFileExtension(filename, newextension)
-fileExtension(filename)
-shortFileName(filename)
-filePath(filename)
-trim(x)
-}
-
-\arguments{
-  \item{filename}{A filename, with or without the path}
-  \item{newextension}{A file extension, e.g., ".txt" }
-  \item{x}{A character string (filename)}
-}
-
-\value{
-A file extension, filename or path.  
-}
-
-\author{Robert J. Hijmans }
-
-\examples{
-	fn <- "  c:/temp folder/filename.extension  "
-	fn <- trim(fn)
-	fileExtension(fn)
-	setFileExtension(fn, ".txt")
-	shortFileName(fn)
-	filePath(fn)
-}
-
-\keyword{file}
-

Modified: pkg/raster/man/values.Rd
===================================================================
--- pkg/raster/man/values.Rd	2009-03-19 08:54:11 UTC (rev 373)
+++ pkg/raster/man/values.Rd	2009-03-19 12:21:01 UTC (rev 374)
@@ -1,31 +1,27 @@
-\name{Values}
+\name{values}
 \alias{values}
 \alias{valuesRow}
-\alias{maxValue}
-\alias{minValue}
   
   
-\title{Get raster properties}
+\title{Get raster values}
+
 \description{
-  values returns cell values for a Raster* object (typically after read* has been used). 
-  valuesRow returns values for a row of values in the case that all values are in memory (if they are on disk, use readRow() and values())
-  minValue and maxValue return the extreme data values for a RasterLayer (or layer in a RasterStack)
+  values returns cell values for a Raster* object (typically after read* has been used).
+ 
+  valuesRow returns values for a row of values in the case that all values are in memory 
+  (if they are on disk, use \code{readRow} and \code{values})
 }
 
 \usage{
 values(object, format='', names=FALSE)
-valuesRow(raster, rownr)
-minValue(object, layer=1)
-maxValue(object, layer=1)
+valuesRow(object, rownr)
 }
 
 \arguments{
-  \item{raster}{RasterLayer or RasterStack object}
   \item{object}{RasterLayer or RasterStack object}
   \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