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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Feb 3 06:54:27 CET 2009


Author: rhijmans
Date: 2009-02-03 06:54:27 +0100 (Tue, 03 Feb 2009)
New Revision: 240

Modified:
   pkg/raster/R/bounding.box.R
   pkg/raster/R/coercion.R
   pkg/raster/R/get.R
   pkg/raster/R/properties.R
   pkg/raster/R/replacement.R
   pkg/raster/man/bbox.Rd
   pkg/raster/man/coerce.Rd
   pkg/raster/man/properties.Rd
   pkg/raster/man/set.Rd
Log:


Modified: pkg/raster/R/bounding.box.R
===================================================================
--- pkg/raster/R/bounding.box.R	2009-02-03 04:01:14 UTC (rev 239)
+++ pkg/raster/R/bounding.box.R	2009-02-03 05:54:27 UTC (rev 240)
@@ -176,7 +176,7 @@
 
 changeBbox <- function(object, xmn=xmin(object), xmx=xmax(object), ymn=ymin(object), ymx = ymax(object), keepres=FALSE) {
 	bb <- newBbox(xmn, xmx, ymn, ymx) 
-	if (class(bb) == 'BoundingBox') { 
+	if (class(object) == 'BoundingBox') { 
 		return(bb)
 	}
 	object <- setBbox(object, bb, keepres=keepres) 

Modified: pkg/raster/R/coercion.R
===================================================================
--- pkg/raster/R/coercion.R	2009-02-03 04:01:14 UTC (rev 239)
+++ pkg/raster/R/coercion.R	2009-02-03 05:54:27 UTC (rev 240)
@@ -109,7 +109,7 @@
 
 
 setMethod('asRasterLayer', signature(object='RasterStack'), 
-	function(object, index=1){
+	function(object, index){
 		if (nlayers(object) > 0) {
 			dindex <- max(1, min(nlayers(object), index))
 			if (dindex != index) { warning(paste("index was changed to", dindex))}
@@ -129,7 +129,7 @@
 
 
 setMethod('asRasterLayer', signature(object='SpatialPixelsDataFrame'), 
-	function(object, index=1){
+	function(object, index){
 		r <- raster()
 		r <- setBbox(r, getBbox(object))
 		r <- setProjection(r, object at proj4string)
@@ -155,7 +155,7 @@
 
 
 setMethod('asRasterLayer', signature(object='SpatialGridDataFrame'), 
-	function(object, index=1){
+	function(object, index){
 		r <- raster()
 		r <- setBbox(r, getBbox(object))
 		r <- setProjection(r, object at proj4string)

Modified: pkg/raster/R/get.R
===================================================================
--- pkg/raster/R/get.R	2009-02-03 04:01:14 UTC (rev 239)
+++ pkg/raster/R/get.R	2009-02-03 05:54:27 UTC (rev 240)
@@ -121,7 +121,7 @@
 cxyFromBbox <- function(object, bbox) {
 	if (.isSPgrid(object)) { object <- asRasterLayer(object, FALSE) }
 	bbox <- getBbox(bbox)
-	cells <- cellFromBbox(object, bbox)
+	cells <- cellsFromBbox(object, bbox)
 	cxy <- cbind(cells, xyFromCell(object, cells))
 	colnames(cxy) <- c("cell", "x", "y")
 	return(cxy)

Modified: pkg/raster/R/properties.R
===================================================================
--- pkg/raster/R/properties.R	2009-02-03 04:01:14 UTC (rev 239)
+++ pkg/raster/R/properties.R	2009-02-03 05:54:27 UTC (rev 240)
@@ -80,6 +80,7 @@
 }
 
 ncell <- function(object) {
+# return numeric to avoid integer overflow
 	return(return( as.numeric(nrow(object)) * ncol(object )))
 }
 
@@ -113,9 +114,7 @@
 }
 
 resolution <- function(object) {
-	x <- xres(object)
-	y <- yres(object)
-	return(c(x, y))
+	return(c(xres(object), yres(object)))
 }
 
 
@@ -142,9 +141,18 @@
     }
 )
 
+setMethod('nlayers', signature(object='Spatial'), 
+	function(object){
+		if ( class(object)=='SpatialPixelsDataFrame' |  class(object)=='SpatialGridDataFrame' ) { 
+			return( dim(object at data)[2] ) 
+		} else {
+			return( 0 )
+		}
+    }
+)
 
 
-layers <- function(object) {
+layerNames <- function(object) {
 	if (class(object) == "RasterLayer") {
 		return(filename(object))
 	} else if (class(object) == "RasterStack") {
@@ -166,7 +174,11 @@
 }
 
 nbands <- function(object) {
-	return(object at file@nbands)
+	if (class(object) == "RasterLayer") {
+		return(object at file@nbands)
+	} else {
+		stop(paste("not implemented for:", class(object), "objects"))
+	}	
 }
 
 projection <- function(object, asText=TRUE) {

Modified: pkg/raster/R/replacement.R
===================================================================
--- pkg/raster/R/replacement.R	2009-02-03 04:01:14 UTC (rev 239)
+++ pkg/raster/R/replacement.R	2009-02-03 05:54:27 UTC (rev 240)
@@ -20,9 +20,31 @@
 	return( setRowCol(x, nrows=value) )
 }	
 
+'resolution<-' <- function(x, value) {
+	if (length(value) == 1) {
+		return( setRes(x, xres=value, yres=value) )
+	} else {
+		return( setRes(x, xres=value[1], yres=value[2]) )
+	}
+}
 
+'xmin<-' <- function(x, value) {
+	return(changeBbox(x, xmn=value))
+}
 
+'xmax<-' <- function(x, value) {
+	return(changeBbox(x, xmx=value))
+}
 
+'ymin<-' <- function(x, value) {
+	return(changeBbox(x, ymn=value))
+}
+
+'ymax<-' <- function(x, value) {
+	return(changeBbox(x, ymx=value))
+}
+
+
 .getColValues <- function(r, colnr) {
 	firstcol <- 1:nrow(r) * ncol(r) - ncol(r) 
 	cells <- colnr + firstcol 

Modified: pkg/raster/man/bbox.Rd
===================================================================
--- pkg/raster/man/bbox.Rd	2009-02-03 04:01:14 UTC (rev 239)
+++ pkg/raster/man/bbox.Rd	2009-02-03 05:54:27 UTC (rev 240)
@@ -11,6 +11,11 @@
 \alias{setBbox}
 \alias{changeBbox}
 \alias{cellsFromBbox}
+
+\alias{xmin<-}
+\alias{xmax<-}
+\alias{ymin<-}
+\alias{ymax<-}
   
 \title{Bounding box functions}
 \description{
@@ -47,6 +52,9 @@
 bb <- newBbox(-10, 10, -20, 20)
 r <- setBbox(r, bb, keepres=TRUE)
 r <- changeBbox(r, xmn=xmin(r)+1, xmx=xmax(r)+1)
+# change BoundingBox coordinates one by one
+xmin(r) <- -30
+ymax(r) <- 60
 }
 
 \keyword{spatial}

Modified: pkg/raster/man/coerce.Rd
===================================================================
--- pkg/raster/man/coerce.Rd	2009-02-03 04:01:14 UTC (rev 239)
+++ pkg/raster/man/coerce.Rd	2009-02-03 05:54:27 UTC (rev 240)
@@ -13,12 +13,12 @@
 }
 
 \usage{
-asRasterLayer(object, index=1) 
+asRasterLayer(object, index) 
 }
 
 \arguments{
   \item{object}{ a Raster* type object (SpatialPixel, SpatialPixelDataFrame , SpatialGrid, or SpatialGridDataFrame }
-  \item{index}{integer >= 1 indicating the layer in the RasterStack, or the column in the sp object dataframe to take the values from}
+  \item{index}{integer (between 1 and nlayers(object) indicating the layer in the RasterStack, or the column in the sp object dataframe to take the values from}
 }
 
 \details{

Modified: pkg/raster/man/properties.Rd
===================================================================
--- pkg/raster/man/properties.Rd	2009-02-03 04:01:14 UTC (rev 239)
+++ pkg/raster/man/properties.Rd	2009-02-03 05:54:27 UTC (rev 240)
@@ -3,11 +3,8 @@
 \alias{filename<-}
 \alias{ncol}
 \alias{nrow}
-\alias{ncol<-}
-\alias{nrow<-}
 \alias{nrow,BasicRaster-method}
 \alias{ncol,BasicRaster-method}
-\alias{ncells}
 \alias{ncell}
 \alias{xres}
 \alias{yres}
@@ -27,7 +24,8 @@
 \alias{nlayers,BasicRaster-method}
 \alias{nlayers,Raster-method}
 \alias{nlayers,RasterStack-method}
-\alias{layers}
+\alias{nlayers,Spatial-method}
+\alias{layerNames}
 \alias{band}
 \alias{nbands}
 \alias{dataType} 
@@ -59,7 +57,7 @@
 dataSize(object)
 dataType(object)
 nlayers(object)
-layers(object)
+layerNames(object)
 band(object)
 nbands(object)
 }
@@ -69,7 +67,6 @@
   \item{x}{Raster* object}
   \item{asText}{logical. If \code{TRUE}, the projection is returned as text. Otherwise a CRS object is returned}
 }
-  
 
 \note{
   Some of these functions simply take the values from the data slots in the RasterLayer or RasterStack object. It is advisable to use these and other 'getter' functions rather than dirctly accessing the slots of an object. This will ensure that code will not break in the future, even if the underlying class definitions change. 
@@ -84,21 +81,21 @@
 \author{Robert J. Hijmans }
 \examples{
 #using a new default raster (1 degree global)
-rs <- raster()
-ncell(rs)
-ncol(rs)
-nrow(rs)
-ncell(rs)
-xres(rs)
-yres(rs)
-resolution(rs)
-xmin(rs)
-xmax(rs)
-ymin(rs)
-ymax(rs)
-projection(rs)
-origin(rs)
-nbands(rs)
-band(rs)
+r <- raster()
+ncell(r)
+ncol(r)
+nrow(r)
+ncell(r)
+xres(r)
+yres(r)
+resolution(r)
+xmin(r)
+xmax(r)
+ymin(r)
+ymax(r)
+projection(r)
+origin(r)
+nbands(r)
+band(r)
 }
 \keyword{spatial}

Modified: pkg/raster/man/set.Rd
===================================================================
--- pkg/raster/man/set.Rd	2009-02-03 04:01:14 UTC (rev 239)
+++ pkg/raster/man/set.Rd	2009-02-03 05:54:27 UTC (rev 240)
@@ -3,7 +3,10 @@
 \alias{setFilename}
 \alias{setProjection}
 \alias{setRowCol}
+\alias{ncol<-}
+\alias{nrow<-}
 \alias{setRes}
+\alias{resolution<-}
 \alias{setMinMax}
 
 \title{ Set properties of a RasterLayer object }
@@ -38,6 +41,7 @@
 setRaster copies the parameters of a RasterLayer object to a new object, but does not copy the filename nor the data associated with the original raster. 
 Use it when creating new RasterLayer objects from existing objects.
 
+
 While you could 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. 
 }
 
@@ -47,11 +51,19 @@
 \author{ Robert J. Hijmans }
 
 \examples{ 
-	r <- raster()
-	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 <- raster()
+r <- setRowCol(r, nrows=18, ncols=36) 
+# equivalent to
+nrow(r) <- 18
+ncol(r) <- 36
+r <- setRes(r, xres=1/120) 
+# equivalent to
+resolution(r) <- 1/120
+# set yres diffrently
+resolution(r) <- c(1/120, 1/60)
+r <- setProjection(r, "+proj=longlat +datum=WGS84")
+r <- setFilename(r, "c:/test.grd")
+	
 }
 
 \keyword{ spatial }



More information about the Raster-commits mailing list