[Raster-commits] r215 - pkg/raster/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jan 29 14:28:24 CET 2009


Author: rhijmans
Date: 2009-01-29 14:28:24 +0100 (Thu, 29 Jan 2009)
New Revision: 215

Modified:
   pkg/raster/R/conversion.R
Log:


Modified: pkg/raster/R/conversion.R
===================================================================
--- pkg/raster/R/conversion.R	2009-01-29 12:25:09 UTC (rev 214)
+++ pkg/raster/R/conversion.R	2009-01-29 13:28:24 UTC (rev 215)
@@ -1,18 +1,34 @@
 
-setAs('RasterLayer', 'SpatialGridDataFrame', 
-	function(from){ return(asSpGrid (from)) }
-)
 
-
 setAs('SpatialGridDataFrame', 'RasterStack',
 	function(from){ return(asRasterStack (from)) }
 )
-
+setAs('SpatialPixelsDataFrame', 'RasterStack', 
+	function(from){ return(asRasterStack (from)) }
+)
+	
 setAs('SpatialGridDataFrame', 'RasterLayer', 
 	function(from){ return(asRasterLayer (from)) }
 )
 
+setAs('SpatialGrid', 'RasterLayer', 
+	function(from){ return(asRasterLayer (from)) }
+)
 
+setAs('SpatialPixelsDataFrame', 'RasterLayer', 
+	function(from){ return(asRasterLayer (from)) }
+)
+
+setAs('SpatialPixels', 'RasterLayer', 
+	function(from){ return(asRasterLayer (from)) }
+)
+
+
+
+setAs('RasterLayer', 'SpatialGridDataFrame', 
+	function(from){ return(asSpGrid (from)) }
+)
+
 setAs('RasterStack', 'RasterLayer', 
 	function(from){ return(asRasterLayer (from)) }
 )
@@ -61,11 +77,17 @@
 
 setMethod('asRasterLayer', signature(object='RasterStack', index='numeric'), 
 	function(object, index){
-		dindex <- max(1, min(nlayers(object), index))
-		if (dindex != index) { warning(paste("index was changed to", dindex))}
-		rs <- object at layers[[dindex]]
-		if (dataContent(object) == 'all') {
-			rs <- setValues(rs, values(object)[,dindex])
+		if (nlayers(object) > 0) {
+			dindex <- max(1, min(nlayers(object), index))
+			if (dindex != index) { warning(paste("index was changed to", dindex))}
+			rs <- object at layers[[dindex]]
+			if (dataContent(object) == 'all') {
+				rs <- setValues(rs, values(object)[,dindex])
+			}
+		} else {
+			rs <- new("RasterLayer")
+			rs <- setBbox(rs, getBbox(object))
+			rs <- setRowCol(rs, nrow(object), ncol(object))
 		}
 		return(rs)
 	}
@@ -119,21 +141,28 @@
 	stk <- setBbox(stk, getBbox(spgrid))
 	stk <- setProjection(stk, spgrid at proj4string)
 	stk <- setRowCol(stk, spgrid at grid@cells.dim[2], spgrid at grid@cells.dim[1])
-
+	
 	if (class(spgrid)=='SpatialPixels') {
 		# do noting, there is no data
 		# we could store the indices, but then we would have a sparse raster with no data (or with NAs). That goes against our definition of sparse (all NAs have been removed)
+		stop('cannot make a RasterStack from a SpatialPixels object. Make a RasterLayer instead')		
 	} else if (class(spgrid)=='SpatialPixelsDataFrame') {
-		cells <- spgrid at grid.index
-		if (length(cells)==0) {
-			cells <- cellFromXY(stk, spgrid at coords)
+		spgrid <- as(spgrid, 'SpatialGridDataFrame')
+		rs <- as(stk, 'RasterLayer')
+		stk <- setValues(stk, as.matrix(spgrid at data))
+		for (i in 1:ncol(spgrid at data)) {
+			stk at layers[i] <- rs
 		}
-		vals <- as.matrix(spgrid at data)
-		stk <- setValuesSparse(stk, cells, vals)
 	} else if ( class(spgrid)=='SpatialGrid' ) {
 		# do nothing, there is no data
+		stop('cannot make a RasterStack from a SpatialGrid object. Make a RasterLayer instead')
 	} else if (class(spgrid)=='SpatialGridDataFrame' ) {
 		stk <- setValues(stk, as.matrix(spgrid at data))
+		rs <- as(stk, 'RasterLayer')
+		stk <- setValues(stk, as.matrix(spgrid at data))
+		for (i in 1:ncol(spgrid at data)) {
+			stk at layers[i] <- rs
+		}		
 	}
 	return(stk)
 }



More information about the Raster-commits mailing list