[GRASS-SVN] r43678 - grass-addons/grass7/raster/r.clump2

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 24 12:34:48 EDT 2010


Author: mmetz
Date: 2010-09-24 16:34:47 +0000 (Fri, 24 Sep 2010)
New Revision: 43678

Modified:
   grass-addons/grass7/raster/r.clump2/local_proto.h
   grass-addons/grass7/raster/r.clump2/main.c
   grass-addons/grass7/raster/r.clump2/pq.c
   grass-addons/grass7/raster/r.clump2/ramseg.c
   grass-addons/grass7/raster/r.clump2/ramseg.h
Log:
support for very large maps

Modified: grass-addons/grass7/raster/r.clump2/local_proto.h
===================================================================
--- grass-addons/grass7/raster/r.clump2/local_proto.h	2010-09-24 15:24:55 UTC (rev 43677)
+++ grass-addons/grass7/raster/r.clump2/local_proto.h	2010-09-24 16:34:47 UTC (rev 43678)
@@ -28,7 +28,7 @@
 extern CELL *clump_id;
 
 /* pq.c */
-int init_pq(int);
+int init_pq(long);
 int free_pq(void);
 int add_pnt(long);
 long drop_pnt(void);

Modified: grass-addons/grass7/raster/r.clump2/main.c
===================================================================
--- grass-addons/grass7/raster/r.clump2/main.c	2010-09-24 15:24:55 UTC (rev 43677)
+++ grass-addons/grass7/raster/r.clump2/main.c	2010-09-24 16:34:47 UTC (rev 43678)
@@ -47,7 +47,7 @@
     void *in_ptr, *in_buf;
     int map_type, in_size;
     FLAG *inlist;
-    int ramseg;
+    long ramseg;
     long index, index_nbr;
     int sides, s, r_nbr, c_nbr, have_seeds = 0, *id_map = NULL;
     int nextdr[8] = { 1, -1, 0, 0, -1, 1, 1, -1 };
@@ -118,6 +118,28 @@
 
     nrows = window.rows;
     ncols = window.cols;
+    
+    {
+	long mem;
+	
+	mem = (long) nrows * ncols * sizeof(CELL) + nrows * ((ncols + 7) / 8) * sizeof(unsigned char);
+	
+	if (mem < 1024)
+	    G_verbose_message(_("Will need %ld bytes of memory"), mem);
+	else {
+	    if (mem / 1024 < 1024)
+		G_verbose_message(_("Will need %.2f KB of memory"), (double) mem / 1024);
+	    else {
+		mem /= 1024;
+		if (mem / 1024 < 1024)
+		    G_verbose_message(_("Will need %.2f MB of memory"), (double) mem / 1024);
+		else {
+		    mem /= 1024;
+		    G_verbose_message(_("Will need %.2f GB of memory"), (double) mem / 1024);
+		}
+	    }
+	}
+    }
 
     if ((double)nrows * ncols > LONG_MAX)
 	G_fatal_error(_("Current region is too large, can't process raster map <%s>"),
@@ -134,7 +156,7 @@
 
     inlist = flag_create(nrows, ncols);
 
-    G_message(_("load input map ..."));
+    G_message(_("Loading input map ..."));
     for (r = 0; r < nrows; r++) {
 	Rast_get_row(in_fd, in_buf, r, map_type);
 	in_ptr = in_buf;
@@ -170,7 +192,7 @@
 	struct Cell_head window;
 	
 	/* get start point coordinates */
-	G_message(_("get start point coordinates ..."));
+	G_message(_("Get start point coordinates ..."));
 
 	G_get_window(&window);
 	
@@ -197,6 +219,7 @@
 		clump_id[index] = clump_no++;
 		add_pnt(index);
 		FLAG_SET(inlist, row, col);
+		have_seeds = 1;
 	    }
 	}
 
@@ -204,7 +227,8 @@
 	for (i = 0; i < clump_no; i++)
 	    id_map[i] = i;
 
-	have_seeds = 1;
+	if (!have_seeds)
+	    G_fatal_error(_("All start coordinates fall into NULL (nodata) areas"));
     }
     else {
 	index = SEG_INDEX(ramseg, 0, 0);
@@ -215,12 +239,16 @@
     }
 
     /* determine clumps */
-    G_message(_("determine clumps ..."));
+    G_message(_("Determine clumps ..."));
 
+    int perc = -1, lastperc;
     while (pqsize > 0) {
 	int start_new = 1;
 
-	G_percent(counter++, ncells, 2);
+	lastperc = perc;
+	perc = (double) (counter++ / (double)ncells) * 1000000;
+	if (perc > lastperc && perc < 1000000)
+	    G_percent(perc, 1000000, 2);
 
 	index = drop_pnt();
 	seg_index_rc(ramseg, index, &r, &c);
@@ -278,10 +306,12 @@
 
     if (counter < ncells) {
 	if (!have_seeds)
-	    G_warning("missed some cells!");
+	    G_warning("Missed some cells!");
 	else
-	    G_percent(ncells, ncells, 1);
+	    G_percent(1, 1, 1);
     }
+    else if ((double) ((double)counter++ / ncells) * 1000000 < 1000000)
+	G_percent(1, 1, 1);
     
     if (have_seeds && clump_no > 1) {
 	int i;
@@ -293,7 +323,7 @@
     }
     
     /* write output */
-    G_message(_("write output map ..."));
+    G_message(_("Write output map ..."));
     out_buf = Rast_allocate_buf(CELL_TYPE);
     for (r = 0; r < nrows; r++) {
 	G_percent(r, nrows, 2);

Modified: grass-addons/grass7/raster/r.clump2/pq.c
===================================================================
--- grass-addons/grass7/raster/r.clump2/pq.c	2010-09-24 15:24:55 UTC (rev 43677)
+++ grass-addons/grass7/raster/r.clump2/pq.c	2010-09-24 16:34:47 UTC (rev 43678)
@@ -24,7 +24,7 @@
 #define GET_CHILD(p) (int) (((p) * 3) - 1)
 
 static int heap_alloced = 0;
-static int heap_step;
+static long heap_step;
 static long *heap_index;
 
 int cmp_clump(long cid1, long cid2)
@@ -36,7 +36,7 @@
     return (cid1 < cid2);
 }
 
-int init_pq(int step)
+int init_pq(long step)
 {
     pqsize = 0;
     if (step < 100)

Modified: grass-addons/grass7/raster/r.clump2/ramseg.c
===================================================================
--- grass-addons/grass7/raster/r.clump2/ramseg.c	2010-09-24 15:24:55 UTC (rev 43677)
+++ grass-addons/grass7/raster/r.clump2/ramseg.c	2010-09-24 16:34:47 UTC (rev 43678)
@@ -1,12 +1,32 @@
 #include <stdio.h>
 #include "ramseg.h"
 
-int size_array(int *ram_seg, int nrows, int ncols)
+#ifdef LARGE_MAPS
+
+long size_array(long *ramseg, int nrows, int ncols)
 {
-    int size, segs_in_col;
+    *ramseg = (long)ncols;
+    
+    return ((long) nrows * ncols);
+}
 
+/* get r, c from seg_index */
+long seg_index_rc(long ramseg, long seg_index, int *r, int *c)
+{
+    *r = seg_index / ramseg;
+    *c = seg_index - *r * ramseg;
+    
+    return 0;
+}
+
+#else
+
+long size_array(long *ramseg, int nrows, int ncols)
+{
+    long size, segs_in_col;
+
     segs_in_col = ((nrows - 1) >> RAMSEGBITS) + 1;
-    *ram_seg = ((ncols - 1) >> RAMSEGBITS) + 1;
+    *ramseg = ((ncols - 1) >> RAMSEGBITS) + 1;
     size = ((((nrows - 1) >> RAMSEGBITS) + 1) << RAMSEGBITS) *
 	((((ncols - 1) >> RAMSEGBITS) + 1) << RAMSEGBITS);
     size -= ((segs_in_col << RAMSEGBITS) - nrows) << RAMSEGBITS;
@@ -15,9 +35,9 @@
 }
 
 /* get r, c from seg_index */
-int seg_index_rc(int ramseg, int seg_index, int *r, int *c)
+long seg_index_rc(long ramseg, long seg_index, int *r, int *c)
 {
-    int seg_no, seg_remainder;
+    long seg_no, seg_remainder;
 
     seg_no = seg_index >> DOUBLEBITS;
     seg_remainder = seg_index - (seg_no << DOUBLEBITS);
@@ -26,3 +46,5 @@
 	seg_remainder - (((*r) & SEGLENLESS) << RAMSEGBITS);
     return seg_no;
 }
+
+#endif

Modified: grass-addons/grass7/raster/r.clump2/ramseg.h
===================================================================
--- grass-addons/grass7/raster/r.clump2/ramseg.h	2010-09-24 15:24:55 UTC (rev 43677)
+++ grass-addons/grass7/raster/r.clump2/ramseg.h	2010-09-24 16:34:47 UTC (rev 43678)
@@ -2,16 +2,26 @@
 #define __RAMSEG_H__
 
 
-#define RAMSEG		int
+#define RAMSEG		long
 #define RAMSEGBITS 	4
 #define DOUBLEBITS 	8	/* 2 * ramsegbits       */
 #define SEGLENLESS 	15	/* 2 ^ ramsegbits - 1   */
 
-#define SEG_INDEX(s,r,c) (int) \
+#define LARGE_MAPS 1
+
+#ifdef LARGE_MAPS
+
+#define SEG_INDEX(s,r,c) (long) ((r) * (s) + (c))
+
+#else
+
+#define SEG_INDEX(s,r,c) (long) \
    (((((r) >> RAMSEGBITS) * (s) + ((c) >> RAMSEGBITS)) << DOUBLEBITS) \
     + (((r) & SEGLENLESS) << RAMSEGBITS) + ((c) & SEGLENLESS))
+    
+#endif
 
-int size_array(int *, int, int);
-int seg_index_rc(int, int, int *, int *);
+long size_array(long *, int, int);
+long seg_index_rc(long, long, int *, int *);
 
 #endif /* __RAMSEG_H__ */



More information about the grass-commit mailing list