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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jan 27 03:29:16 CET 2009


Author: rhijmans
Date: 2009-01-27 03:29:16 +0100 (Tue, 27 Jan 2009)
New Revision: 186

Added:
   pkg/raster/R/stack.R
Removed:
   pkg/raster/R/stack.create.R
Modified:
   pkg/raster/DESCRIPTION
   pkg/raster/R/map.R
   pkg/raster/man/create.stack.Rd
   pkg/raster/man/project.Rd
Log:


Modified: pkg/raster/DESCRIPTION
===================================================================
--- pkg/raster/DESCRIPTION	2009-01-27 00:27:09 UTC (rev 185)
+++ pkg/raster/DESCRIPTION	2009-01-27 02:29:16 UTC (rev 186)
@@ -2,7 +2,7 @@
 Type: Package
 Title: Raster data handling for geographic data analysis and modeling
 Version: 0.8.7-2
-Date: 26-Jan-2009
+Date: 27-Jan-2009
 Depends: methods, sp, rgdal (>= 0.5-33), R (>= 2.8.0)
 Author: Robert J. Hijmans & Jacob van Etten
 Maintainer: Robert J. Hijmans <r.hijmans at gmail.com> 

Modified: pkg/raster/R/map.R
===================================================================
--- pkg/raster/R/map.R	2009-01-27 00:27:09 UTC (rev 185)
+++ pkg/raster/R/map.R	2009-01-27 02:29:16 UTC (rev 186)
@@ -15,7 +15,7 @@
 	if ( class(object) != 'RasterLayer' ) { 
 		index <- round(index)
 		i <- min(max(1, index), nlayers(object))
-		if (i != index) { stop("index should be >= 1 and <=", nlayers(object), " =nlayers(stack)") }
+		if (i != index) { stop("index should be >= 1 and <=", nlayers(object), " =nlayers(object)") }
 		raster2 <- asRasterLayer(object, i)
 		if (dataContent(object) == 'all') {
 			raster2 <- setValues(raster2, values(object)[i,])

Added: pkg/raster/R/stack.R
===================================================================
--- pkg/raster/R/stack.R	                        (rev 0)
+++ pkg/raster/R/stack.R	2009-01-27 02:29:16 UTC (rev 186)
@@ -0,0 +1,155 @@
+# Author: Robert J. Hijmans, r.hijmans at gmail.com
+# International Rice Research Institute
+# Date : June 2008
+# Version 0.8
+# Licence GPL v3
+
+stackOpen <- function(stackfile) {
+	st <- read.table(stackfile, as.is=FALSE, strip.white=TRUE)
+	rst <- stackFromFiles(st[,1], st[,2])
+	rst <- setFilename(rst, stackfile)
+	return(rst)
+}
+
+stackSave <- function(rstack) {
+	stackfile <- trim(rstack at filename)
+	if (stackfile == "") { stop('RasterStack does not have a filename.') }
+	thefile <- file(stackfile, "w")
+	for (i in 1:length(rstack at layers)) {
+		fname <- trim(filename(rstack at layers[[i]]))
+		if (trim(fname) == "") {
+			stop("cannot save a RasterStack that has Layers without filenames. Use writeStack instead.")
+		}	
+		cat(fname, "\t", band(rstack at layers[[i]]),"\n", file=thefile)
+	}
+	close(thefile)
+	return(rstack)
+}
+
+
+
+stackFromFiles <- function(rasterfiles, bands= rep(1, length(rasterfiles))) {
+#	stop("this function is depracated. Use makeStack() instead.")
+	rstack <- new("RasterStack") 
+	return(addFiles(rstack, rasterfiles, bands))
+}
+
+
+
+if (!isGeneric("makeStack")) {
+	setGeneric("makeStack", function(x, ...)
+		standardGeneric("makeStack"))
+}	
+
+
+setMethod("makeStack", signature(x='RasterLayer'), 
+function(x, ...) {
+	rlist <- c(x, list(...))
+	for (i in 1:length(rlist)) {
+		if (is.character(rlist[[i]])) {
+			rlist[i] <- rasterFromFile(rlist[[i]])
+		} else {
+			if (class(rlist[[i]]) != "RasterLayer") {
+				stop("Arguments should be RasterLayer objects or filenames")
+			}
+		}	
+	}
+	return(addRasters(new("RasterStack"), rlist))
+} )
+
+
+addFiles <- function(rstack, rasterfiles, bands= rep(1, length(rasterfiles))) {
+	rasters <- list()
+	for (i in 1:length(rasterfiles)) { 
+		rasters <- c(rasters, rasterFromFile(rasterfiles[[i]], FALSE, band=bands[[i]]))
+	}	
+	rstack <- addRasters(rstack, rasters) 
+	return(rstack)
+}
+
+
+
+addRasters <- function(rstack, rasters) {
+#rasters is a list of raster objects
+	if (class(rstack) != "RasterStack") { 
+		stop("rstack should be a RasterStack object") 
+	}
+	if (length(rasters) == 1 & class(rasters) == 'RasterLayer') {
+		rasters <- list(rasters)
+	}
+
+	for (i in 1 : length(rasters)) { 
+		raster <- rasters[[i]] 
+		if (dataContent(raster) != 'all' & dataSource(raster) == 'ram') {
+			stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
+		}
+		nl <- rstack at data@nlayers + 1
+		rstack at data@nlayers <- as.integer(nl)
+		if (nlayers(rstack) == 1) {
+			rstack <- setRowCol(rstack, nrow(raster), ncol(raster))
+			rstack <- setBbox(rstack, raster, snap=FALSE)
+			rstack <- setProjection(rstack, projection(raster))
+			if (trim(raster at file@shortname) != "") {
+				cname <- trim(raster at file@shortname)
+			} else {
+				cname <- "layer1"
+			}
+			rstack at data@colnames[1] <- cname
+			if (dataContent(raster) == 'all') {
+				rstack at data@values <- matrix(nrow=ncell(rstack), ncol=length(rasters))
+				rstack at data@values[,1] <- values(raster)
+				rstack at data@content <- 'all'
+				raster <- clearValues(raster)
+			} else {
+				if (dataSource(raster) == 'ram' & dataContent(raster) != "all") {
+					stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
+				}
+			}
+		} else {
+			if (!compare(c(rstack, raster))) { 
+				stop(paste("could not add raster:", filename(raster))) 
+			}
+			count <- 1
+			cname <- trim(raster at file@shortname)
+			if (cname == "") {
+				cname <- paste("layer", nl, sep="")
+			}
+			cn <- cname
+			for (j in 1:(nl-1)) {
+				if ( cn == rstack at data@colnames[j] ) { 
+					count <- count + 1 
+					cn <- paste(cname, "_", count, sep="")
+				}
+			}	
+			rstack at data@colnames[nl] <- cn
+			if (dataContent(rstack)=='all') {
+				if (dataContent(raster) != 'all') {
+					raster <- readAll(raster)
+				} 
+				rstack at data@values[,nl] <- values(raster)
+				raster <- clearValues(raster)
+			} else {
+				if (dataSource(raster)=='disk') {
+					raster <- clearValues(raster)
+				}
+			}
+		}	
+		rstack at layers[nl] <- raster 
+		rstack at data@min[nl] <- raster at data@min
+		rstack at data@max[nl] <- raster at data@max		
+	}	
+	return(rstack)
+}	
+
+
+stackRemove <- function(rstack, indices) {
+	indices <- sort(indices, decreasing=TRUE)
+	for (i in 1:length(indices)) {
+		index <- -1 * indices[i]
+		rstack at layers <- rstack at layers[index]
+		rstack at data@nlayers <- as.integer(rstack at data@nlayers - 1)
+	}	
+	return(rstack)
+}
+
+

Deleted: pkg/raster/R/stack.create.R
===================================================================
--- pkg/raster/R/stack.create.R	2009-01-27 00:27:09 UTC (rev 185)
+++ pkg/raster/R/stack.create.R	2009-01-27 02:29:16 UTC (rev 186)
@@ -1,155 +0,0 @@
-# Author: Robert J. Hijmans, r.hijmans at gmail.com
-# International Rice Research Institute
-# Date : June 2008
-# Version 0.8
-# Licence GPL v3
-
-stackOpen <- function(stackfile) {
-	st <- read.table(stackfile, as.is=FALSE, strip.white=TRUE)
-	rst <- stackFromFiles(st[,1], st[,2])
-	rst <- setFilename(rst, stackfile)
-	return(rst)
-}
-
-stackSave <- function(rstack) {
-	stackfile <- trim(rstack at filename)
-	if (stackfile == "") { stop('RasterStack does not have a filename.') }
-	thefile <- file(stackfile, "w")
-	for (i in 1:length(rstack at layers)) {
-		fname <- trim(filename(rstack at layers[[i]]))
-		if (trim(fname) == "") {
-			stop("cannot save a Stack that has Layers without filenames. Use writeStack instead.")
-		}	
-		cat(fname, "\t", band(rstack at layers[[i]]),"\n", file=thefile)
-	}
-	close(thefile)
-	return(rstack)
-}
-
-
-
-stackFromFiles <- function(rasterfiles, bands= rep(1, length(rasterfiles))) {
-#	stop("this function is depracated. Use makeStack() instead.")
-	rstack <- new("RasterStack") 
-	return(addFiles(rstack, rasterfiles, bands))
-}
-
-
-
-if (!isGeneric("stack")) {
-	setGeneric("stack", function(x, ...)
-		standardGeneric("stack"))
-}	
-
-
-setMethod("stack", signature(x='RasterLayer'), 
-function(x, ...) {
-	rlist <- c(x, list(...))
-	for (i in 1:length(rlist)) {
-		if (is.character(rlist[[i]])) {
-			rlist[i] <- rasterFromFile(rlist[[i]])
-		} else {
-			if (class(rlist[[i]]) != "RasterLayer") {
-				stop("Arguments should be RasterLayer objects or filenames")
-			}
-		}	
-	}
-	return(addRasters(new("RasterStack"), rlist))
-} )
-
-
-addFiles <- function(rstack, rasterfiles, bands= rep(1, length(rasterfiles))) {
-	rasters <- list()
-	for (i in 1:length(rasterfiles)) { 
-		rasters <- c(rasters, rasterFromFile(rasterfiles[[i]], FALSE, band=bands[[i]]))
-	}	
-	rstack <- addRasters(rstack, rasters) 
-	return(rstack)
-}
-
-
-
-addRasters <- function(rstack, rasters) {
-#rasters is a list of raster objects
-	if (class(rstack) != "RasterStack") { 
-		stop("rstack should be a RasterStack object") 
-	}
-	if (length(rasters) == 1 & class(rasters) == 'RasterLayer') {
-		rasters <- list(rasters)
-	}
-
-	for (i in 1 : length(rasters)) { 
-		raster <- rasters[[i]] 
-		if (dataContent(raster) != 'all' & dataSource(raster) == 'ram') {
-			stop("Cannot add a memory based raster without values to a stack")
-		}
-		nl <- rstack at data@nlayers + 1
-		rstack at data@nlayers <- as.integer(nl)
-		if (nlayers(rstack) == 1) {
-			rstack <- setRowCol(rstack, nrow(raster), ncol(raster))
-			rstack <- setBbox(rstack, raster, snap=FALSE)
-			rstack <- setProjection(rstack, projection(raster))
-			if (trim(raster at file@shortname) != "") {
-				cname <- trim(raster at file@shortname)
-			} else {
-				cname <- "layer1"
-			}
-			rstack at data@colnames[1] <- cname
-			if (dataContent(raster) == 'all') {
-				rstack at data@values <- matrix(nrow=ncell(rstack), ncol=length(rasters))
-				rstack at data@values[,1] <- values(raster)
-				rstack at data@content <- 'all'
-				raster <- clearValues(raster)
-			} else {
-				if (dataSource(raster) == 'ram' & dataContent(raster) != "all") {
-					stop("adding memory based raster to stack, withouth adding values")
-				}
-			}
-		} else {
-			if (!compare(c(rstack, raster))) { 
-				stop(paste("could not add raster:", filename(raster))) 
-			}
-			count <- 1
-			cname <- trim(raster at file@shortname)
-			if (cname == "") {
-				cname <- paste("layer", nl, sep="")
-			}
-			cn <- cname
-			for (j in 1:(nl-1)) {
-				if ( cn == rstack at data@colnames[j] ) { 
-					count <- count + 1 
-					cn <- paste(cname, "_", count, sep="")
-				}
-			}	
-			rstack at data@colnames[nl] <- cn
-			if (dataContent(rstack)=='all') {
-				if (dataContent(raster) != 'all') {
-					raster <- readAll(raster)
-				} 
-				rstack at data@values[,nl] <- values(raster)
-				raster <- clearValues(raster)
-			} else {
-				if (dataSource(raster)=='disk') {
-					raster <- clearValues(raster)
-				}
-			}
-		}	
-		rstack at layers[nl] <- raster 
-		rstack at data@min[nl] <- raster at data@min
-		rstack at data@max[nl] <- raster at data@max		
-	}	
-	return(rstack)
-}	
-
-
-stackRemove <- function(rstack, indices) {
-	indices <- sort(indices, decreasing=TRUE)
-	for (i in 1:length(indices)) {
-		index <- -1 * indices[i]
-		rstack at layers <- rstack at layers[index]
-		rstack at data@nlayers <- as.integer(rstack at data@nlayers - 1)
-	}	
-	return(rstack)
-}
-
-

Modified: pkg/raster/man/create.stack.Rd
===================================================================
--- pkg/raster/man/create.stack.Rd	2009-01-27 00:27:09 UTC (rev 185)
+++ pkg/raster/man/create.stack.Rd	2009-01-27 02:29:16 UTC (rev 186)
@@ -1,6 +1,6 @@
 \name{rasterstack.create}
-\alias{stack}
-\alias{stack,RasterLayer-method}
+\alias{makeStack}
+\alias{makeStack,RasterLayer-method}
 \alias{stackFromFiles}
 \alias{stackFromRasters}
 \alias{addFiles}
@@ -18,7 +18,7 @@
 \usage{
 stackOpen(stackfile)
 stackSave(rstack)
-stack(x, ...)
+makeStack(x, ...)
 stackFromFiles(rasterfiles, bands= rep(1, length(rasterfiles)))
 addFiles(rstack, rasterfiles, bands= rep(1, length(rasterfiles))) 
 addRasters(rstack, rasters) 

Modified: pkg/raster/man/project.Rd
===================================================================
--- pkg/raster/man/project.Rd	2009-01-27 00:27:09 UTC (rev 185)
+++ pkg/raster/man/project.Rd	2009-01-27 02:29:16 UTC (rev 186)
@@ -30,12 +30,20 @@
 \author{Robert J. Hijmans \email{r.hijmans at gmail.com}}
 
 \examples{
-r <- newRaster(-120, -80, 30, 60, ncols=40, nrows=30)
+# create a new (not projected) RasterLayer with cellnumbers as values
+r <- newRaster(-110, -90, 40, 60, ncols=40, nrows=30)
 r <- setValues(r, 1:ncell(r))
+# proj.4 projection description
 newproj <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100"
-bb <- projectBbox(r, newproj)
-bb <- setRes(bb, 100000)
-rp <- projectRaster(r, bb)
+# create a new RasterLayer with a BoundingBox with the new projection
+projras <- projectBbox(r, newproj)
+# Adjust the cell size 
+projras <- setRes(projras, 100000)
+# project the values of RasterLayer 'r' to the new RasterLayer 'projras'
+projras <- projectRaster(r, projras)
+# inverse projection, back to the properties of 'r'
+inverse <- projectRaster(projras, r)
+
 }
 
 \keyword{spatial}



More information about the Raster-commits mailing list