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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue May 12 10:36:45 CEST 2009


Author: rhijmans
Date: 2009-05-12 10:36:41 +0200 (Tue, 12 May 2009)
New Revision: 453

Modified:
   pkg/raster/R/clump.R
   pkg/raster/man/clump.Rd
Log:


Modified: pkg/raster/R/clump.R
===================================================================
--- pkg/raster/R/clump.R	2009-05-12 07:51:43 UTC (rev 452)
+++ pkg/raster/R/clump.R	2009-05-12 08:36:41 UTC (rev 453)
@@ -6,25 +6,22 @@
 
 clump <- function(raster, filename=NULL, overwrite=FALSE, filetype='raster', datatype='INT4S', track=-1) {
 	cat('WARNING: this function does not return correct results', '\n')
-	x1 <- raster(raster)
-	dataType(x1) <- datatype
 	
+	if (is.null(filename)) { filename <- "" }
 	tmpfile1 <- ""
-	tmpfile2 <- ""
+	x1 <- raster(raster)
+	dataType(x1) <- datatype
 	if (!canProcessInMemory(x1, 3)) {
 		tmpfile1 <- rasterTmpFile()
 		filename(x1) <- tmpfile1
 		if (filename == "") {
 			filename <- rasterTmpFile()
-			filename(x2) <- filename
-			if (getOption('verbose')) { cat('writing raster to:', filename(x2))	}								
+			if (getOption('verbose')) { cat('writing raster to:', filename)	}								
 		}
 	}
 
 	nc <- ncol(x1)
 	v <- vector(length=0)
-	a <- vector(length=nc)
-	a[] <- 0
 	nextclump <- 1
 	c2 <- vector(length=nc)
 	c2[] <- 0
@@ -33,23 +30,14 @@
 		b <- valuesRow(raster, r)
 		c1 <- c2
 		c2[] <- 0
-		if (b[1]==1) {
-			if (a[1] == 1) { c2[1] <- c1[1] 
-			} else if (a[2] == 1) { c2[1] <- c1[2] 
-			} else {
-				c2[1] <- nextclump
-				nextclump <- nextclump + 1
-			}
-		}
-		
-		for (cc in 2:(nc)) {
+		for (cc in 1:nc) {
 			if (b[cc]==1) {
-				if (c2[cc-1] > 0) { c2[cc] <- c2[cc-1] 
-				} else if ( a[cc-1] ==1 ) {
+				if (isTRUE(c2[cc-1] > 0)) { c2[cc] <- c2[cc-1] 
+				} else if ( isTRUE(c1[cc-1] > 0) ) {
 					c2[cc] <- c1[cc-1]
-				} else if (a[cc] == 1) {
+				} else if ( c1[cc] > 0) {
 					c2[cc] <- c1[cc]
-				} else if (isTRUE(a[cc+1] == 1)) {
+				} else if (isTRUE(c1[cc+1] > 0)) {
 					c2[cc] <- c1[cc+1]
 				} else {
 					c2[cc] <- nextclump
@@ -58,22 +46,6 @@
 			}
 		}
 
-		if (b[nc-1]==1) {
-			c2[nc] <- c2[nc-1]
-		} else if (b[nc]==1) {
-			if (a[nc] == 1) { c2[nc] <- c1[nc] 
-			} else if (a[nc-1] == 1) { c2[nc] <- c1[nc-1] 
-			} else {
-				c2[nc] <- nextclump
-				nextclump <- nextclump + 1
-			}
-		}
-		if (tmpfile1 == "") {
-			v <- c(v, c2)
-		} else {
-			x1 <- setValues(x1, c2)
-			x1 <- writeRaster(x1, r)
-		}
 # check for joining clumps:
 		
 		for (cc in 1:nc) {
@@ -89,8 +61,13 @@
 				}
 			}
 		}
-		
-		a <- b
+	
+		if (tmpfile1 == "") {
+			v <- c(v, c2)
+		} else {
+			x1 <- setValues(x1, c2)
+			x1 <- writeRaster(x1, r)
+		}	
 	}
 	if (tmpfile1 == "") {
 		x1 <- setValues(x1, v)

Modified: pkg/raster/man/clump.Rd
===================================================================
--- pkg/raster/man/clump.Rd	2009-05-12 07:51:43 UTC (rev 452)
+++ pkg/raster/man/clump.Rd	2009-05-12 08:36:41 UTC (rev 453)
@@ -32,7 +32,7 @@
 \author{Robert J. Hijmans}
 
 \examples{
-r <- raster(ncols=25, nrows=25)
+r <- raster(ncols=50, nrows=50)
 r[] <- round(runif(ncell(r))*0.75 )
 rc <- clump(r) 
 



More information about the Raster-commits mailing list