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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Apr 20 04:40:22 CEST 2009


Author: rhijmans
Date: 2009-04-20 04:40:21 +0200 (Mon, 20 Apr 2009)
New Revision: 420

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


Modified: pkg/raster/R/reclass.R
===================================================================
--- pkg/raster/R/reclass.R	2009-04-20 02:34:25 UTC (rev 419)
+++ pkg/raster/R/reclass.R	2009-04-20 02:40:21 UTC (rev 420)
@@ -5,8 +5,9 @@
 # Licence GPL v3
 
 reclass <- function(raster, rclmat, filename="", overwrite=FALSE, filetype='raster', datatype='FLT4S', track=-1)  {
+
 	if (class(raster) != 'RasterLayer' ) {
-		stop('first two arguments should be objects of class "RasterLayer"')
+		stop('first argument should be an object of class "RasterLayer"')
 	}
 
 	if ( is.null(dim(rclmat)) ) { 
@@ -47,16 +48,22 @@
 		
 	} else {
 		starttime <- proc.time()
-
+		hasNA <- FALSE
+		for (i in 1:length(rclmat[,1])) {
+			if (is.na(rclmat[i,1]) | is.na(rclmat[i,2])) {
+				namat <- rclmat[i,]
+				rclmat <- rclmat[-i,]
+				hasNA <- TRUE
+			}
+		}
 		for (r in 1:nrow(raster)) {
 			raster <- readRow(raster, r)
 			res <- values(raster)
 			for (i in 1:length(rclmat[,1])) {
-				if (is.na(rclmat[i,1]) | is.na(rclmat[i,2])) {
-					res[ is.na(values(raster)) ] <- rclmat[i, 3] 
-				} else {
-					res[ (values(raster) >= rclmat[i,1]) & (values(raster) <= rclmat[i,2]) ] <- rclmat[i , 3] 
-				}
+				res[ (values(raster) >= rclmat[i,1]) & (values(raster) <= rclmat[i,2]) ] <- rclmat[i , 3] 
+			}
+			if (hasNA) {
+				res[ is.na(values(raster)) ] <- namat[1, 3] 				
 			}	
 			outraster <- setValues(outraster, res, r)
 			outraster <- writeRaster(outraster, overwrite=overwrite, filetype=filetype)



More information about the Raster-commits mailing list