[GRASS-SVN] r57008 - grass/trunk/imagery/i.segment

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jul 4 02:13:47 PDT 2013


Author: mmetz
Date: 2013-07-04 02:13:47 -0700 (Thu, 04 Jul 2013)
New Revision: 57008

Modified:
   grass/trunk/imagery/i.segment/create_isegs.c
   grass/trunk/imagery/i.segment/iseg.h
   grass/trunk/imagery/i.segment/open_files.c
Log:
i.segment: fix NULL handling, bounds input, min size

Modified: grass/trunk/imagery/i.segment/create_isegs.c
===================================================================
--- grass/trunk/imagery/i.segment/create_isegs.c	2013-07-04 07:55:17 UTC (rev 57007)
+++ grass/trunk/imagery/i.segment/create_isegs.c	2013-07-04 09:13:47 UTC (rev 57008)
@@ -16,8 +16,14 @@
 
 #define EPSILON 1.0e-8
 
-#define MAX(a,b) ( ((a)>(b)) ? (a) : (b) )
-#define MIN(a,b) ( ((a)<(b)) ? (a) : (b) )
+#ifdef MAX
+#undef MAX
+#endif
+#define MAX(a,b) ( ((a) > (b)) ? (a) : (b) )
+#ifdef MIN
+#undef MIN
+#endif
+#define MIN(a,b) ( ((a) < (b)) ? (a) : (b) )
 
 
 /* internal functions */
@@ -111,7 +117,7 @@
 {
     int row, col;
     int successflag = 1;
-    int have_bound;
+    int have_bound, rid;
     CELL current_bound, bounds_val;
 
     G_debug(1, "segmentation method: %d", globals->method);
@@ -136,25 +142,29 @@
 	    globals->col_max = 0;
 	    for (row = 0; row < globals->nrows; row++) {
 		for (col = 0; col < globals->ncols; col++) {
+		    FLAG_SET(globals->null_flag, row, col);
 		    segment_get(&globals->bounds_seg, &bounds_val,
 				row, col);
 
-		    if (bounds_val == current_bound) {
-			have_bound = 1;
+		    if (!Rast_is_c_null_value(&bounds_val)
+		        && bounds_val == current_bound) {
 
-			FLAG_UNSET(globals->null_flag, row, col);
+			segment_get(&globals->rid_seg, &rid, row, col);
+			if (!Rast_is_c_null_value(&rid)) {
+			    have_bound = 1;
 
-			if (globals->row_min > row)
-			    globals->row_min = row;
-			if (globals->row_max < row)
-			    globals->row_max = row;
-			if (globals->col_min > col)
-			    globals->col_min = col;
-			if (globals->col_max < col)
-			    globals->col_max = col;
+			    FLAG_UNSET(globals->null_flag, row, col);
+
+			    if (globals->row_min > row)
+				globals->row_min = row;
+			    if (globals->row_max < row)
+				globals->row_max = row;
+			    if (globals->col_min > col)
+				globals->col_min = col;
+			    if (globals->col_max < col)
+				globals->col_max = col;
+			}
 		    }
-		    else
-			FLAG_SET(globals->null_flag, row, col);
 		}
 	    }
 	    globals->row_max++;
@@ -163,6 +173,17 @@
 	    if (have_bound)
 		successflag = region_growing(globals);
 	}    /* end outer loop for processing polygons */
+
+	/* restore NULL flag */
+	flag_clear_all(globals->null_flag);
+	for (row = 0; row < globals->nrows; row++) {
+	    for (col = 0; col < globals->ncols; col++) {
+		segment_get(&globals->rid_seg, &rid, row, col);
+		if (Rast_is_c_null_value(&rid))
+		    FLAG_SET(globals->null_flag, row, col);
+	    }
+	}
+
     }
 
     return successflag;
@@ -237,12 +258,14 @@
 	    }
 	}
 
-	G_debug(4, "Starting to process %d candidate cells",
+	G_debug(4, "Starting to process %ld candidate cells",
 		globals->candidate_count);
 
 	/*process candidate cells */
+	G_percent_reset();
 	for (row = globals->row_min; row < globals->row_max; row++) {
-	    G_percent(row, globals->row_max, 4);
+	    G_percent(row - globals->row_min,
+	              globals->row_max - globals->row_min, 4);
 	    for (col = globals->col_min; col < globals->col_max; col++) {
 		if (!(FLAG_GET(globals->candidate_flag, row, col)))
 		    continue;
@@ -301,6 +324,7 @@
 		    if (Ri.count < Rk.count)
 			smaller = Ri.count;
 
+		    /* TODO: better */
 		    adjthresh = pow(alpha2, 1. + (double) smaller / divisor);
 
 		    if (compare_double(Ri_similarity, adjthresh) == -1) {
@@ -322,6 +346,9 @@
 		        (globals->nn >= 8 && Rk.count <= globals->nn))
 		    candidates_only = FALSE;
 		}
+		
+		if (Rk.id == 0)
+		    pathflag = FALSE;
 
 		while (pathflag) {
 		    pathflag = FALSE;
@@ -364,6 +391,9 @@
 			if (Ri_nn == 1 && Rk_nn > 1)
 			    do_merge = 1;
 
+			if (Rk.id == 0)
+			    do_merge = 0;
+
 			/* adjust threshold */
 			if (do_merge) {
 			    int smaller = Rk.count;
@@ -371,6 +401,7 @@
 			    if (Ri.count < Rk.count)
 				smaller = Ri.count;
 
+			    /* TODO: better */
 			    adjthresh = pow(alpha2, 1. + (double) smaller / divisor);
 
 			    do_merge = 0;
@@ -507,6 +538,7 @@
 	flag_clear_all(globals->candidate_flag);
 	
 	n_merges = 0;
+	globals->candidate_count = 0;
 
 	/* Set candidate flag to true/1 for all non-NULL cells */
 	for (row = globals->row_min; row < globals->row_max; row++) {
@@ -519,20 +551,19 @@
 	    }
 	}
 
-	G_debug(4, "Starting to process %d candidate cells",
+	G_debug(4, "Starting to process %ld candidate cells",
 		globals->candidate_count);
 
 	/* process candidate cells */
+	G_percent_reset();
 	for (row = globals->row_min; row < globals->row_max; row++) {
-	    G_percent(row, globals->row_max, 9);
+	    G_percent(row - globals->row_min,
+	              globals->row_max - globals->row_min, 4);
 	    for (col = globals->col_min; col < globals->col_max; col++) {
 		int do_merge = 1;
 		
 		if (!(FLAG_GET(globals->candidate_flag, row, col)))
 		    continue;
-		
-		nbtree_clear(Ri_ngbrs);
-		nbtree_clear(Rk_ngbrs);
 
 		Ri.row = row;
 		Ri.col = col;
@@ -551,8 +582,12 @@
 		memcpy(Ri.mean, Ri_rs.mean, globals->datasize);
 		Ri.count = Ri_rs.count;
 
+		if (Ri.count >= globals->min_segment_size) {
+		    set_candidate_flag(&Ri, FALSE, globals);
+		    do_merge = 0;
+		}
+
 		while (do_merge) {
-
 		    do_merge = 0;
 
 		    /* merge all smaller than min size */
@@ -561,6 +596,8 @@
 
 		    Ri_nn = 0;
 		    Ri_similarity = 2;
+		    
+		    Rk.id = 0;
 
 		    if (do_merge) {
 
@@ -571,10 +608,7 @@
 						   globals);
 		    }
 
-		    if (Ri_nn > 0) {
-
-			nbtree_clear(Ri_ngbrs);
-			
+		    if (Ri_nn > 0 && Rk.id != 0) {
 			/* merge Ri with Rk */
 			/* do not clear candidate flag for Rk */
 			merge_regions(&Ri, &Ri_rs, &Rk, &Rk_rs, 0, globals);
@@ -631,6 +665,13 @@
 
     G_debug(4, "find_best_neighbor()");
 
+    if (Ri->id != Ri_rs->id)
+	G_fatal_error("Ri = %d but Ri_rs = %d", Ri->id, Ri_rs->id);
+    if (Ri->id <= 0)
+	G_fatal_error("Ri is %d", Ri->id);
+    if (Ri_rs->id <= 0)
+	G_fatal_error("Ri_rs is %d", Ri_rs->id);
+
     /* dynamics of the region growing algorithm
      * some regions are growing fast, often surrounded by many small regions
      * not all regions are equally growing, some will only grow at a later stage ? */
@@ -642,17 +683,11 @@
     ngbr_rc.col = Ri->col;
     rbtree_insert(no_check_tree, &ngbr_rc);
 
+    nbtree_clear(Ri_ngbrs);
     n_ngbrs = 0;
     /* TODO: add size of largest region to reg_tree, use this as min */
     Rk->count = globals->ncells;
     Rk->id = Rk_rs->id = 0;
-    
-    if (Ri->id != Ri_rs->id)
-	G_fatal_error("Ri = %d but Ri_rs = %d", Ri->id, Ri_rs->id);
-    if (Ri->id <= 0)
-	G_fatal_error("Ri is %d", Ri->id);
-    if (Ri_rs->id <= 0)
-	G_fatal_error("Ri_rs is %d", Ri_rs->id);
 
     /* go through segment, spreading outwards from head */
     rclist_init(&rilist);
@@ -1031,12 +1066,9 @@
 
 int update_band_vals(int row, int col, struct reg_stats *rs,
                      struct globals *globals) {
-    struct RB_TREE *rc_check_tree;	/* cells already checked */
-    struct rclist rlist;
     struct rc next, ngbr_rc;
     int neighbors[8][2];
     int rid, count, n;
-    int no_check;
     
     /* update band values with sum */
     /* rs->id must be set */
@@ -1053,70 +1085,116 @@
 	G_fatal_error(_("Region ids are different"));
     }
 
-    /* TODO: add fast version for globals->min_reg_size == 3 */
+    if (rs->count == 1) {
+	G_warning(_("Region consists of only one cell, nothing to update"));
+	return rs->count;
+    }
 
-    /* go through region, spreading outwards from head */
-    rclist_init(&rlist);
-
-    rc_check_tree = rbtree_create(compare_rc, sizeof(struct rc));
-    ngbr_rc.row = row;
-    ngbr_rc.col = col;
-    rbtree_insert(rc_check_tree, &ngbr_rc);
+    /* update region stats */
+    segment_put(&globals->bands_seg, (void *)rs->sum, row, col);
     count = 1;
 
-    /* update region stats */
-    segment_put(&globals->bands_seg, (void *)rs->sum,
-		ngbr_rc.row, ngbr_rc.col);
+    /* fast version for rs->count == 2 */
+    if (rs->count == 2) {
+	globals->find_neighbors(row, col, neighbors);
 
-    next.row = row;
-    next.col = col;
-    do {
-	G_debug(5, "find_pixel_neighbors for row: %d , col %d",
-		next.row, next.col);
-
-	globals->find_neighbors(next.row, next.col, neighbors);
-
 	n = globals->nn - 1;
 	do {
 
 	    ngbr_rc.row = neighbors[n][0];
 	    ngbr_rc.col = neighbors[n][1];
 
-	    no_check = (ngbr_rc.row < 0 || ngbr_rc.row >= globals->nrows ||
-		ngbr_rc.col < 0 || ngbr_rc.col >= globals->ncols);
+	    if (ngbr_rc.row < globals->row_min || ngbr_rc.row >= globals->row_max ||
+		ngbr_rc.col < globals->col_min || ngbr_rc.col >= globals->col_max) {
+		continue;
+	    }
 
-	    if (!no_check) {
-		if ((FLAG_GET(globals->null_flag, ngbr_rc.row, ngbr_rc.col)) == 0) {
+	    if ((FLAG_GET(globals->null_flag, ngbr_rc.row, ngbr_rc.col)) == 0) {
+
+		segment_get(&globals->rid_seg, (void *) &rid,
+			    ngbr_rc.row, ngbr_rc.col);
 		
-		    /* already checked ? */
-		    if (!rbtree_find(rc_check_tree, &ngbr_rc)) {
+		if (rid == rs->id) {
 
-			/* not yet checked, don't check it again */
-			rbtree_insert(rc_check_tree, &ngbr_rc);
+		    /* update region stats */
+		    segment_put(&globals->bands_seg,
+				(void *)rs->sum,
+				ngbr_rc.row, ngbr_rc.col);
 
-			segment_get(&globals->rid_seg, (void *) &rid,
-				    ngbr_rc.row, ngbr_rc.col);
-			
-			if (rid == rs->id) {
+		    count++;
 
-			    /* want to check this neighbor's neighbors */
-			    rclist_add(&rlist, ngbr_rc.row, ngbr_rc.col);
+		    /* only one other neighbor can have the same ID
+		     * deactivate for debugging */
+		    break;
+		}
+	    }
+	} while (n--);
+	if (count > 2)
+	    G_fatal_error(_("Region size is larger than 2: %d"), count);
+    }
+    else if (rs->count > 2) {
+	struct RB_TREE *rc_check_tree;	/* cells already checked */
+	struct rclist rlist;
+	int no_check;
 
-			    /* update region stats */
-			    segment_put(&globals->bands_seg,
-					(void *)rs->sum,
+	/* go through region, spreading outwards from head */
+	rclist_init(&rlist);
+
+	rc_check_tree = rbtree_create(compare_rc, sizeof(struct rc));
+	ngbr_rc.row = row;
+	ngbr_rc.col = col;
+	rbtree_insert(rc_check_tree, &ngbr_rc);
+
+	next.row = row;
+	next.col = col;
+	do {
+	    G_debug(5, "find_pixel_neighbors for row: %d , col %d",
+		    next.row, next.col);
+
+	    globals->find_neighbors(next.row, next.col, neighbors);
+
+	    n = globals->nn - 1;
+	    do {
+
+		ngbr_rc.row = neighbors[n][0];
+		ngbr_rc.col = neighbors[n][1];
+
+		no_check = (ngbr_rc.row < 0 || ngbr_rc.row >= globals->nrows ||
+		    ngbr_rc.col < 0 || ngbr_rc.col >= globals->ncols);
+
+		if (!no_check) {
+		    if ((FLAG_GET(globals->null_flag, ngbr_rc.row, ngbr_rc.col)) == 0) {
+		    
+			/* already checked ? */
+			if (!rbtree_find(rc_check_tree, &ngbr_rc)) {
+
+			    /* not yet checked, don't check it again */
+			    rbtree_insert(rc_check_tree, &ngbr_rc);
+
+			    segment_get(&globals->rid_seg, (void *) &rid,
 					ngbr_rc.row, ngbr_rc.col);
-			    count++;
+			    
+			    if (rid == rs->id) {
+
+				/* want to check this neighbor's neighbors */
+				rclist_add(&rlist, ngbr_rc.row, ngbr_rc.col);
+
+				/* update region stats */
+				segment_put(&globals->bands_seg,
+					    (void *)rs->sum,
+					    ngbr_rc.row, ngbr_rc.col);
+				count++;
+			    }
 			}
 		    }
 		}
-	    }
-	} while (n--);
-    } while (rclist_drop(&rlist, &next));
+	    } while (n--);
+	} while (rclist_drop(&rlist, &next));
 
-    /* clean up */
-    rbtree_destroy(rc_check_tree);
-    rclist_destroy(&rlist);
+	/* clean up */
+	rbtree_destroy(rc_check_tree);
+	rclist_destroy(&rlist);
+    }
     
     if (count != rs->count) {
 	G_fatal_error(_("Region size is %d, should be %d"),
@@ -1470,8 +1548,9 @@
 		    /* update region stats */
 		    rs->count++;
 
-		    /* only one other neighbor can have the same ID */
-		    /* break; */
+		    /* only one other neighbor can have the same ID
+		     * deactivate for debugging */
+		    break;
 		}
 	    }
 	} while (n--);

Modified: grass/trunk/imagery/i.segment/iseg.h
===================================================================
--- grass/trunk/imagery/i.segment/iseg.h	2013-07-04 07:55:17 UTC (rev 57007)
+++ grass/trunk/imagery/i.segment/iseg.h	2013-07-04 09:13:47 UTC (rev 57008)
@@ -55,7 +55,7 @@
     /* region info */
     int nrows, ncols;
     int row_min, row_max, col_min, col_max; /* region constraints */
-    int ncells;
+    long ncells, notnullcells;
 
     char *out_name;		/* name of output raster map */
     char *seeds, *bounds_map;	/* optional segment seeds and polygon constraints/boundaries */
@@ -84,7 +84,7 @@
     FLAG *candidate_flag, *null_flag;	/*TODO, need some way to remember MASK/NULL values.  Was using -1, 0, 1 in int array.  Better to use 2 FLAG structures, better readibility? */
 
     /* number of remaining cells to check */
-    int candidate_count;
+    long candidate_count;
 
     /* functions */
 	

Modified: grass/trunk/imagery/i.segment/open_files.c
===================================================================
--- grass/trunk/imagery/i.segment/open_files.c	2013-07-04 07:55:17 UTC (rev 57007)
+++ grass/trunk/imagery/i.segment/open_files.c	2013-07-04 09:13:47 UTC (rev 57008)
@@ -17,7 +17,7 @@
     int *in_fd, bounds_fd, is_null;
     int n, row, col, srows, scols, inlen, outlen, nseg;
     DCELL **inbuf;		/* buffers to store lines from each of the imagery group rasters */
-    CELL *boundsbuf, bounds_null, bounds_val;
+    CELL *boundsbuf, bounds_val;
     int have_bounds = 0;
     CELL s, id;
     struct Range range;	/* min/max values of bounds map */
@@ -29,6 +29,9 @@
     globals->null_flag = flag_create(globals->nrows, globals->ncols);
     globals->candidate_flag = flag_create(globals->nrows, globals->ncols);
 
+    flag_clear_all(globals->null_flag);
+    flag_clear_all(globals->candidate_flag);
+
     G_debug(1, "Checking image group...");
 
     /* ****** open the input rasters ******* */
@@ -92,6 +95,29 @@
     G_debug(1, "data element size, in: %d , out: %d ", inlen, outlen);
     globals->datasize = sizeof(double) * globals->nbands;
 
+    /* count non-null cells */
+    globals->notnullcells = (long)globals->nrows * globals->ncols;
+    for (row = 0; row < globals->nrows; row++) {
+	for (n = 0; n < Ref.nfiles; n++) {
+	    Rast_get_d_row(in_fd[n], inbuf[n], row);
+	}
+	for (col = 0; col < globals->ncols; col++) {
+
+	    is_null = 0;	/*Assume there is data */
+	    for (n = 0; n < Ref.nfiles; n++) {
+		if (Rast_is_d_null_value(&inbuf[n][col])) {
+		    is_null = 1;
+		}
+	    }
+	    if (is_null) {
+		globals->notnullcells--;
+		FLAG_SET(globals->null_flag, row, col);
+	    }
+	}
+    }
+    if (globals->notnullcells < 2)
+	G_fatal_error(_("Insufficient number of non-NULL cells in current region"));
+
     /* segment lib segment size */
     srows = 64;
     scols = 64;
@@ -110,7 +136,10 @@
 	G_fatal_error("Unable to create input temporary files");
 
     /* load input bands to segment structure */
-    G_debug(1, "Reading input rasters into segmentation data files...");
+    if (Ref.nfiles > 1)
+	G_message(_("Loading input bands..."));
+    else
+	G_message(_("Loading input band..."));
 
     globals->bands_val = (double *)G_malloc(inlen);
     globals->second_val = (double *)G_malloc(inlen);
@@ -122,6 +151,7 @@
     globals->col_min = globals->ncols;
     globals->col_max = 0;
     for (row = 0; row < globals->nrows; row++) {
+	G_percent(row, globals->nrows, 4);
 	for (n = 0; n < Ref.nfiles; n++) {
 	    Rast_get_d_row(in_fd[n], inbuf[n], row);
 	}
@@ -129,24 +159,21 @@
 
 	    is_null = 0;	/*Assume there is data */
 	    for (n = 0; n < Ref.nfiles; n++) {
+		globals->bands_val[n] = inbuf[n][col];
 		if (Rast_is_d_null_value(&inbuf[n][col])) {
 		    is_null = 1;
-		    globals->bands_val[n] = inbuf[n][col];
 		}
 		else {
-		    if (globals->weighted == TRUE)
-			globals->bands_val[n] = inbuf[n][col];
-		    else
+		    if (globals->weighted == FALSE)
 		    	/* scaled version */
 			globals->bands_val[n] = (inbuf[n][col] - min[n]) / (max[n] - min[n]);
 		}
 	    }
-	    segment_put(&globals->bands_seg, (void *)globals->bands_val, row, col);
+	    if (segment_put(&globals->bands_seg,
+	                    (void *)globals->bands_val, row, col) != 1)
+		G_fatal_error(_("Unable to write to temporary file"));
 
 	    if (!is_null) {
-
-		FLAG_UNSET(globals->null_flag, row, col);
-		
 		if (!globals->seeds) {
 		    /* sequentially number all cells with a unique segment ID */
 		    id = s;
@@ -154,7 +181,6 @@
 		}
 
 		/* get min/max row/col to narrow the processing window */
-
 		if (globals->row_min > row)
 		    globals->row_min = row;
 		if (globals->row_max < row)
@@ -167,19 +193,24 @@
 	    else {
 		/* all input bands NULL */
 		Rast_set_c_null_value(&id, 1);
-		/*Rast_set_c_null_value(&(globals->rid[row][col]), 1);*/
 		FLAG_SET(globals->null_flag, row, col);
 	    }
-	    if (!globals->seeds)
-		segment_put(&globals->rid_seg, (void *)&id, row, col);
+	    if (!globals->seeds || is_null) {
+		if (segment_put(&globals->rid_seg,
+		                (void *)&id, row, col) != 1)
+		    G_fatal_error(_("Unable to write to temporary file"));
+	    }
 	}
     }
-    G_debug(1, "nrows: %d, min row: %d, max row %d", globals->nrows, globals->row_min, globals->row_max);
-    G_debug(1, "ncols: %d, min col: %d, max col %d", globals->ncols, globals->col_min, globals->col_max);
+    G_debug(1, "nrows: %d, min row: %d, max row %d",
+	       globals->nrows, globals->row_min, globals->row_max);
+    G_debug(1, "ncols: %d, min col: %d, max col %d",
+               globals->ncols, globals->col_min, globals->col_max);
     
     globals->row_max++;
     globals->col_max++;
-    globals->ncells = (globals->row_max - globals->row_min) * (globals->col_max - globals->col_min);
+    globals->ncells = (long)(globals->row_max - globals->row_min) *
+			    (globals->col_max - globals->col_min);
 
     /* bounds/constraints */
 
@@ -205,27 +236,27 @@
 	                  globals->bounds_map);
 	}
 
-	bounds_null = globals->upper_bound = globals->lower_bound + 1;
-
 	bounds_fd = Rast_open_old(globals->bounds_map, globals->bounds_mapset);
 	boundsbuf = Rast_allocate_c_buf();
 
 	for (row = 0; row < globals->nrows; row++) {
 	    Rast_get_c_row(bounds_fd, boundsbuf, row);
 	    for (col = 0; col < globals->ncols; col++) {
+		bounds_val = boundsbuf[col];
 		if (FLAG_GET(globals->null_flag, row, col)) {
 		    Rast_set_c_null_value(&bounds_val, 1);
 		}
 		else {
-		    if (Rast_is_c_null_value(&boundsbuf[col]))
-			boundsbuf[col] = bounds_null;
-		    else {
+		    if (!Rast_is_c_null_value(&bounds_val)) {
 			have_bounds = 1;
-			if (globals->lower_bound > boundsbuf[col])
-			    globals->lower_bound = boundsbuf[col];
+			if (globals->lower_bound > bounds_val)
+			    globals->lower_bound = bounds_val;
+			if (globals->upper_bound < bounds_val)
+			    globals->upper_bound = bounds_val;
 		    }
 		}
-		segment_put(&globals->bounds_seg, &bounds_val, row, col);
+		if (segment_put(&globals->bounds_seg, &bounds_val, row, col) != 1)
+		    G_fatal_error(_("Unable to write to temporary file"));
 	    }
 	}
 	Rast_close(bounds_fd);
@@ -269,7 +300,6 @@
     G_free(fp_range);
     G_free(min);
     G_free(max);
-    /* Need to clean up anything else? */
 
     return TRUE;
 }
@@ -307,10 +337,11 @@
 	    }
 	    else {
 		seeds_val = seeds_buf[col];
-		if (!Rast_is_c_null_value(&seeds_buf[col]))
+		if (!Rast_is_c_null_value(&seeds_val))
 		    have_seeds = 1;
 	    }
-	    segment_put(&seeds_seg, &seeds_val, row, col);
+	    if (segment_put(&seeds_seg, &seeds_val, row, col) != 1)
+		G_fatal_error(_("Unable to write to temporary file"));
 	}
     }
 
@@ -331,12 +362,12 @@
     for (row = 0; row < globals->nrows; row++) {
 	Rast_get_c_row(seeds_fd, seeds_buf, row);
 	for (col = 0; col < globals->ncols; col++) {
-	    if (FLAG_GET(globals->null_flag, row, col)) {
-		segment_put(&globals->rid_seg, &seeds_val, row, col);
-	    }
-	    else if (!(FLAG_GET(globals->candidate_flag, row, col))) {
-		if (Rast_is_c_null_value(&(seeds_buf[col])) || seeds_buf[col] < 1) {
-		    segment_put(&globals->rid_seg, &sneg, row, col);
+	    if (!(FLAG_GET(globals->null_flag, row, col)) && 
+	        !(FLAG_GET(globals->candidate_flag, row, col))) {
+
+		if (Rast_is_c_null_value(&(seeds_buf[col]))) {
+		    if (segment_put(&globals->rid_seg, &sneg, row, col) != 1)
+			G_fatal_error(_("Unable to write to temporary file"));
 		    sneg--;
 		}
 		else {
@@ -374,7 +405,8 @@
     segment_get(seeds_seg, &Ri_id, Ri->row, Ri->col);
     
     /* set new segment id */
-    segment_put(&globals->rid_seg, &new_id, Ri->row, Ri->col);
+    if (segment_put(&globals->rid_seg, &new_id, Ri->row, Ri->col) != 1)
+	G_fatal_error(_("Unable to write to temporary file"));
     /* set candidate flag */
     FLAG_SET(globals->candidate_flag, Ri->row, Ri->col);
 
@@ -428,7 +460,9 @@
 	    }
 
 	    /* set segment id */
-	    segment_put(&globals->rid_seg, &new_id, ngbr_rc.row, ngbr_rc.col);
+	    if (segment_put(&globals->rid_seg,
+	                    &new_id, ngbr_rc.row, ngbr_rc.col) != 1)
+		G_fatal_error(_("Unable to write to temporary file"));
 	    
 	    /* set candidate flag */
 	    FLAG_SET(globals->candidate_flag, ngbr_rc.row, ngbr_rc.col);
@@ -486,16 +520,16 @@
     /* calculate number of region stats that can be kept in memory */
     reg_size_count = (globals->mb - segs_mb) / reg_size_mb;
     globals->min_reg_size = 3;
-    if (reg_size_count < (double) globals->nrows * globals->ncols / globals->min_reg_size) {
-	globals->min_reg_size = (double) globals->nrows * globals->ncols / reg_size_count;
+    if (reg_size_count < (double) globals->notnullcells / globals->min_reg_size) {
+	globals->min_reg_size = (double) globals->notnullcells / reg_size_count;
     }
     else {
-	reg_size_count = (double) globals->nrows * globals->ncols / globals->min_reg_size;
+	reg_size_count = (double) globals->notnullcells / globals->min_reg_size;
 	/* recalculate segs_mb */
 	segs_mb = globals->mb - reg_size_count * reg_size_mb;
     }
 
-    G_verbose_message(_("Stats for regions with at least %d cells are stored in memory"),
+    G_verbose_message(_("Regions with at least %d cells are stored in memory"),
                       globals->min_reg_size);
 
     /* calculate number of segments in memory */



More information about the grass-commit mailing list