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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jan 21 15:35:31 CET 2009


Author: rhijmans
Date: 2009-01-21 15:35:30 +0100 (Wed, 21 Jan 2009)
New Revision: 168

Added:
   pkg/raster/man/setValues.Rd
Modified:
   pkg/raster/R/Merge.R
   pkg/raster/R/bounding.box.R
   pkg/raster/R/set.R
   pkg/raster/R/set.values.R
   pkg/raster/R/stack.create.R
   pkg/raster/man/bbox.Rd
   pkg/raster/man/set.Rd
Log:


Modified: pkg/raster/R/Merge.R
===================================================================
--- pkg/raster/R/Merge.R	2009-01-21 11:47:01 UTC (rev 167)
+++ pkg/raster/R/Merge.R	2009-01-21 14:35:30 UTC (rev 168)
@@ -45,7 +45,7 @@
 	bb <- .outerBox(rasters)
 	outraster <- setRaster(rasters[[1]], filename)
 #	bndbox <- newBbox(bb[1,1], bb[1,2], bb[2,1], bb[2,2])
-	outraster <- setBbox(outraster, bb, keepres=TRUE)
+	outraster <- setBbox(outraster, bb, keepres=TRUE, snap=FALSE)
 
 	rowcol <- matrix(0, ncol=3, nrow=length(rasters))
 	for (i in 1:length(rasters)) {

Modified: pkg/raster/R/bounding.box.R
===================================================================
--- pkg/raster/R/bounding.box.R	2009-01-21 11:47:01 UTC (rev 167)
+++ pkg/raster/R/bounding.box.R	2009-01-21 14:35:30 UTC (rev 168)
@@ -86,10 +86,36 @@
 )
 
 
-setBbox <- function(object, bndbox, keepres=FALSE) {
+setBbox <- function(object, bndbox, keepres=FALSE, snap=FALSE) {
 	xrs <- xres(object)
 	yrs <- yres(object)
-	object at bbox <- getBbox(bndbox)
+	oldbb <- getBbox(object)
+	bb <- getBbox(bndbox)
+	
+	if (snap) {
+		bb at xmin <- max(bb at xmin, oldbb at xmin)
+		bb at xmax <- min(bb at xmax, oldbb at xmax)
+		bb at ymin <- max(bb at ymin, oldbb at ymin)
+		bb at ymax <- min(bb at ymax, oldbb at ymax)
+		col <- colFromX(object, bb at xmin)
+		mn <- xFromCol(object, col) - 0.5 * xres(object)
+		mx <- xFromCol(object, col) + 0.5 * xres(object)
+		if (abs(bb at xmin - mn) > abs(bb at xmin - mx)) { bb at xmin <- mx } else { bb at xmin <- mn }
+		col <- colFromX(object, bb at xmax)
+		mn <- xFromCol(object, col) - 0.5 * xres(object)
+		mx <- xFromCol(object, col) + 0.5 * xres(object)
+		if (abs(bb at xmax - mn) > abs(bb at xmax - mx)) { bb at xmax <- mx } else { bb at xmax <- mn }
+		row <- rowFromY(object, bb at ymin)
+		mn <- yFromRow(object, row) - 0.5 * yres(object)
+		mx <- yFromRow(object, row) + 0.5 * yres(object)
+		if (abs(bb at ymin - mn) > abs(bb at ymin - mx)) { bb at ymin <- mx } else { bb at ymin <- mn }
+		row <- rowFromY(object, bb at ymax)
+		mn <- yFromRow(object, row) - 0.5 * yres(object)
+		mx <- yFromRow(object, row) + 0.5 * yres(object)
+		if (abs(bb at ymax - mn) > abs(bb at ymax - mx)) { bb at ymax <- mx } else { bb at ymax <- mn }
+	}
+	
+	object at bbox <- bb
 	if (keepres) {
 		nc <- as.integer(round( (xmax(object) - xmin(object)) / xrs ))
 		if (nc < 1) { stop( "xmin and xmax are less than one cell apart" ) 

Modified: pkg/raster/R/set.R
===================================================================
--- pkg/raster/R/set.R	2009-01-21 11:47:01 UTC (rev 167)
+++ pkg/raster/R/set.R	2009-01-21 14:35:30 UTC (rev 168)
@@ -17,6 +17,18 @@
 	return(raster)
 }
 
+setRes <- function(object, xres, yres=xres) {
+	object <- clearValues(object)
+	bb <- getBbox(object)
+	nc <- (bb at xmax - bb at xmin) / xres
+	nr <- (bb at ymax - bb at ymin) / yres
+	bb at xmax <- bb at xmin + nc * xres
+	bb at ymin <- bb at ymax - nr * yres
+	object <- setBbox(object, bb, snap=FALSE)
+	object <- setRowCol(object, nr, nc)
+	return(object)
+}
+
 setRaster <- function(object, filename="", values=NA) {
 	if (class(object) == 'RasterStack') { object <- asRasterLayer(object, 1) }
 	if (class(object) == 'RasterBrick') { object <- asRasterLayer(object, 1) }

Modified: pkg/raster/R/set.values.R
===================================================================
--- pkg/raster/R/set.values.R	2009-01-21 11:47:01 UTC (rev 167)
+++ pkg/raster/R/set.values.R	2009-01-21 14:35:30 UTC (rev 168)
@@ -51,6 +51,11 @@
 	} else {
 		object at data@values <- matrix(NA,0,0)
 	}
+	if (class(object) == 'RasterLayer') {
+		object at data@min <- NA
+		object at data@max <- NA
+		object at data@haveminmax <- FALSE
+	}
 	return(object)
 }
 

Modified: pkg/raster/R/stack.create.R
===================================================================
--- pkg/raster/R/stack.create.R	2009-01-21 11:47:01 UTC (rev 167)
+++ pkg/raster/R/stack.create.R	2009-01-21 14:35:30 UTC (rev 168)
@@ -82,14 +82,17 @@
 		stop("rstack should be a RasterStack object") 
 	}
 	for (i in 1 : length(rasters)) { 
-		if (length(rasters) == 1) { raster <- rasters 
-		} else { raster <- rasters[[i]] }
+		if (length(rasters) == 1) { 
+			raster <- rasters 
+		} else { 
+			raster <- rasters[[i]] 
+		}
 		
 		addraster <- TRUE
 		i <- nlayers(rstack) + 1
 		if (i == 1) {
 			rstack <- setRowCol(rstack, nrow(raster), ncol(raster))
-			rstack <- setBbox(rstack, raster)
+			rstack <- setBbox(rstack, raster, snap=FALSE)
 			rstack <- setProjection(rstack, projection(raster))
 		} else {
 			if (!compare(c(rstack, raster))) { 

Modified: pkg/raster/man/bbox.Rd
===================================================================
--- pkg/raster/man/bbox.Rd	2009-01-21 11:47:01 UTC (rev 167)
+++ pkg/raster/man/bbox.Rd	2009-01-21 14:35:30 UTC (rev 168)
@@ -25,7 +25,7 @@
 \usage{
 getBbox(object)
 newBbox(xmn, xmx, ymn, ymx)
-setBbox(object, bndbox, keepres=FALSE)
+setBbox(object, bndbox, keepres=FALSE, snap=FALSE)
 changeBbox(object, xmn=xmin(object), xmx=xmax(object), ymn=ymin(object), ymx = ymax(object), keepres=FALSE) 
 }
 
@@ -37,6 +37,7 @@
   \item{object}{A Raster* object}
   \item{bndbox}{An object of class BoundingBox (which you can create with newBbox() )}  
   \item{keepres}{logical. If \code{TRUE}, the resolution of the cells will stay the same after adjusting the bounding box (by adjusting the number of rows and columns). if \code{FALSE}, the number of rows and columns will stay the same, and the resolution will be adjusted}
+  \item{snap}{Adjust the bounding box so that the new raster is aligned with the old raster }
 }
   
 \author{Robert J. Hijmans  \email{r.hijmans at gmail.com}}

Modified: pkg/raster/man/set.Rd
===================================================================
--- pkg/raster/man/set.Rd	2009-01-21 11:47:01 UTC (rev 167)
+++ pkg/raster/man/set.Rd	2009-01-21 14:35:30 UTC (rev 168)
@@ -4,50 +4,36 @@
 \alias{setProjection}
 \alias{setDatatype}
 \alias{setRowCol}
-\alias{setValues}
-\alias{setValuesBlock}
-\alias{setValuesSparse}
-\alias{clearValues}
-\alias{makeSparse}
+\alias{setRes}
 \alias{setMinMax}
 
 \title{ Set properties of a RasterLayer object }
 
 \description{
 Use these functions to set name and datatype properties of a RasterLayer object. While you can access the slots of the object directly, you would do that at your own peril because the value of one raster property can affect values in other properties, and changing slot values may thus break the integrity of the objects. Using slots directly may also lead to code to break in the future if underlying class definitions change. 
-  
 }
 
 \usage{
 setProjection(object, projstring)
 setFilename(object, filename)
-clearValues(object)
 setRaster(object, filename="", values=NA)
 setDatatype(raster, datatype, datasize)
 setRowCol(raster, nrows=nrow(raster), ncols=ncol(raster)) 
+setRes(object, xres, yres=xres)
 setMinMax(raster, readfromdisk=FALSE)
-setValues(raster, values, rownr=-1) 
-setValuesBlock(raster, blockvalues, firstcell, lastcell) 
-setValuesSparse(raster, sparsevalues, cellnumbers) 
-
-makeSparse(raster)
 }
 
 \arguments{
   \item{object}{A Raster* object }
   \item{raster}{ A \code{RasterLayer} object }
+  \item{values}{ Data values to associate with the RasterLayer object}
   \item{filename}{ filename, can be blank }
   \item{datatype}{ the type of data ('numeric' or 'integer' that will be written to disk }
   \item{datasize}{ The size, in number of bytes, that wil be written to disk. typically 4 for integer, and 8 for numeric datatype }
-  \item{values}{ Data values to associate with the RasterLayer object. There should be values for all cells or for a single row}
-  \item{rownr}{ row number, between 1 and nrow(raster) }
-  \item{blockvalues}{ Data values to associate with the RasterLayer object for a block of data, i.e. a rectangular area within the entire raster }
-  \item{firstcell}{ first cell (upper left corner) of the blockdata }
-  \item{lastcell}{ last cell (upper left corner) of the blockdata }
-  \item{sparsevalues}{ not implemented yet -- sparse data values. I.e. values for a number of raster cells }
-  \item{cellnumbers}{ the cell numbers corresponding to the sparse data }
   \item{nrows}{ the new number of rows for the raster }
   \item{ncols}{ the new number of cols for the raster }  
+  \item{xres}{ the new x resolution}
+  \item{yres}{ the new y resolution}  
   \item{projstring}{a character string describing a projection and datum in PROJ4 format }
   \item{readfromdisk}{Logical. If \code{TRUE} then data are read from disk (if available) to determine the min and max value}
 }
@@ -63,18 +49,13 @@
 \author{ Robert J. Hijmans \email{r.hijmans at gmail.com} }
 
 \examples{ 
-	rs <- newRaster()
-	rs <- setFilename(rs, "c:/test.grd")
-	rs <- setRowCol(rs, nrows=18, ncols=36) 
-	rs <- setProjection(rs, "+proj=longlat +datum=WGS84")
-	rs <- setDatatype(rs, datatype="integer", datasize=4)
-	rs <- setDatatype(rs, datatype="numeric", datasize=8)
-	vals <- 1:ncell(rs)
-	rs <- setValues(rs, vals)
-	rs <- clearValues(rs)
-	rowvals <- 1:ncol(rs)
-	rs <- setValues(rs, rowvals, 3)
-	
+	r <- newRaster()
+	r <- setFilename(r, "c:/test.grd")
+	r <- setRowCol(r, nrows=18, ncols=36) 
+	r <- setRes(r, xres=1/120) 
+	r <- setProjection(r, "+proj=longlat +datum=WGS84")
+	r <- setDatatype(r, datatype="integer", datasize=4)
+	r <- setDatatype(r, datatype="numeric", datasize=8)
 }
 
 \keyword{ spatial }

Added: pkg/raster/man/setValues.Rd
===================================================================
--- pkg/raster/man/setValues.Rd	                        (rev 0)
+++ pkg/raster/man/setValues.Rd	2009-01-21 14:35:30 UTC (rev 168)
@@ -0,0 +1,54 @@
+\name{setValues}
+\alias{setValues}
+\alias{setValuesBlock}
+\alias{setValuesSparse}
+\alias{clearValues}
+\alias{makeSparse}
+
+\title{ Set values of a RasterLayer object }
+
+\description{
+Use these functions to assign values ot a RasterLayer object. While you can access the slots of the object directly, you would do that at your own peril because the value of one raster property can affect values in other properties, and changing slot values may thus break the integrity of the objects. Using slots directly may also lead to code to break in the future if underlying class definitions change. 
+  
+}
+
+\usage{
+setValues(raster, values, rownr=-1) 
+setValuesBlock(raster, blockvalues, firstcell, lastcell) 
+setValuesSparse(raster, sparsevalues, cellnumbers) 
+clearValues(object)
+
+makeSparse(raster)
+}
+
+\arguments{
+  \item{raster}{ A \code{RasterLayer} object }
+  \item{object}{ A \code{Raster*} object }
+  \item{values}{ Data values to associate with the RasterLayer object. There should be values for all cells or for a single row}
+  \item{rownr}{ row number, between 1 and nrow(raster) }
+  \item{blockvalues}{ Data values to associate with the RasterLayer object for a block of data, i.e. a rectangular area within the entire raster }
+  \item{firstcell}{ first cell (upper left corner) of the blockdata }
+  \item{lastcell}{ last cell (upper left corner) of the blockdata }
+  \item{sparsevalues}{ not implemented yet -- sparse data values. I.e. values for a number of raster cells }
+  \item{cellnumbers}{ the cell numbers corresponding to the sparse data }
+}
+
+\details{
+}
+
+\value{
+	a Raster* object
+}
+\author{ Robert J. Hijmans \email{r.hijmans at gmail.com} }
+
+\examples{ 
+	rs <- newRaster()
+	vals <- 1:ncell(rs)
+	rs <- setValues(rs, vals)
+	rs <- clearValues(rs)
+	rowvals <- 1:ncol(rs)
+	rs <- setValues(rs, rowvals, 3)
+}
+
+\keyword{ spatial }
+



More information about the Raster-commits mailing list