[GRASS-SVN] r61169 - grass/trunk/raster/r.clump

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jul 7 08:21:53 PDT 2014


Author: hcho
Date: 2014-07-07 08:21:53 -0700 (Mon, 07 Jul 2014)
New Revision: 61169

Modified:
   grass/trunk/raster/r.clump/clump.c
   grass/trunk/raster/r.clump/local_proto.h
   grass/trunk/raster/r.clump/main.c
Log:
r.clump: Add -p flag to print only the number of clumps in shell script style

Modified: grass/trunk/raster/r.clump/clump.c
===================================================================
--- grass/trunk/raster/r.clump/clump.c	2014-07-07 14:07:29 UTC (rev 61168)
+++ grass/trunk/raster/r.clump/clump.c	2014-07-07 15:21:53 UTC (rev 61169)
@@ -29,7 +29,7 @@
 
 #define INCR 1024
 
-CELL clump(int in_fd, int out_fd, int diag)
+CELL clump(int in_fd, int out_fd, int diag, int print)
 {
     register int col;
     register int n;
@@ -250,35 +250,41 @@
     /* rewind temp file */
     lseek(cfd, 0, SEEK_SET);
 
-    /****************************************************
-     *                      PASS 2                      *
-     * apply renumbering scheme to initial clump labels *
-     ****************************************************/
+    if (print) {
+	fprintf(stdout, "clumps=%d\n", cat - 1);
+    }
+    else {
+	/****************************************************
+	 *                      PASS 2                      *
+	 * apply renumbering scheme to initial clump labels *
+	 ****************************************************/
 
-    /* the input raster is no longer needed, 
-     * using instead the temp file with initial clump labels */
+	/* the input raster is no longer needed, 
+	 * using instead the temp file with initial clump labels */
 
-    G_message(_("Pass 2..."));
-    for (row = 0; row < nrows; row++) {
+	G_message(_("Pass 2..."));
+	for (row = 0; row < nrows; row++) {
 
-	G_percent(row, nrows, 4);
+	    G_percent(row, nrows, 4);
 	
-	if (read(cfd, cur_clump, csize) != csize)
-	    G_fatal_error(_("Unable to read from temp file"));
+	    if (read(cfd, cur_clump, csize) != csize)
+		G_fatal_error(_("Unable to read from temp file"));
 
-	temp_clump = cur_clump;
-	temp_cell = out_cell;
+	    temp_clump = cur_clump;
+	    temp_cell = out_cell;
 
-	for (col = 0; col < ncols; col++) {
-	    *temp_cell = renumber[index[*temp_clump]];
-	    if (*temp_cell == 0)
-		Rast_set_c_null_value(temp_cell, 1);
-	    temp_clump++;
-	    temp_cell++;
+	    for (col = 0; col < ncols; col++) {
+		*temp_cell = renumber[index[*temp_clump]];
+		if (*temp_cell == 0)
+		    Rast_set_c_null_value(temp_cell, 1);
+		temp_clump++;
+		temp_cell++;
+	    }
+	    Rast_put_row(out_fd, out_cell, CELL_TYPE);
 	}
-	Rast_put_row(out_fd, out_cell, CELL_TYPE);
+	G_percent(1, 1, 1);
     }
-    G_percent(1, 1, 1);
+
     close(cfd);
     unlink(cname);
 

Modified: grass/trunk/raster/r.clump/local_proto.h
===================================================================
--- grass/trunk/raster/r.clump/local_proto.h	2014-07-07 14:07:29 UTC (rev 61168)
+++ grass/trunk/raster/r.clump/local_proto.h	2014-07-07 15:21:53 UTC (rev 61169)
@@ -20,7 +20,7 @@
 #define __LOCAL_PROTO_H__
 
 /* clump.c */
-CELL clump(int, int, int);
+CELL clump(int, int, int, int);
 int print_time(long *);
 
 /* main.c */

Modified: grass/trunk/raster/r.clump/main.c
===================================================================
--- grass/trunk/raster/r.clump/main.c	2014-07-07 14:07:29 UTC (rev 61168)
+++ grass/trunk/raster/r.clump/main.c	2014-07-07 15:21:53 UTC (rev 61169)
@@ -40,6 +40,7 @@
     struct Option *opt_out;
     struct Option *opt_title;
     struct Flag *flag_diag;
+    struct Flag *flag_print;
 
     G_gisinit(argv[0]);
 
@@ -56,6 +57,7 @@
     opt_in = G_define_standard_option(G_OPT_R_INPUT);
 
     opt_out = G_define_standard_option(G_OPT_R_OUTPUT);
+    opt_out->required = NO;
 
     opt_title = G_define_option();
     opt_title->key = "title";
@@ -66,48 +68,59 @@
     flag_diag = G_define_flag();
     flag_diag->key = 'd';
     flag_diag->label = _("Clump also diagonal cells");
-    flag_diag->description = _("Clumps are also traced along diagonal neighboring cells"); 
+    flag_diag->description = _("Clumps are also traced along diagonal neighboring cells");
 
+    flag_print = G_define_flag();
+    flag_print->key = 'p';
+    flag_print->label = _("Print only the number of clumps in shell script style");
+
+    G_option_exclusive(flag_print, opt_out, NULL);
+    G_option_required(flag_print, opt_out, NULL);
+
     /* parse options */
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
     INPUT = opt_in->answer;
-    OUTPUT = opt_out->answer;
-
     strcpy(name, INPUT);
 
     in_fd = Rast_open_old(name, "");
 
-    out_fd = Rast_open_c_new(OUTPUT);
+    if (!flag_print->answer) {
+	OUTPUT = opt_out->answer;
+	out_fd = Rast_open_c_new(OUTPUT);
+    }
 
-    clump(in_fd, out_fd, flag_diag->answer);
+    clump(in_fd, out_fd, flag_diag->answer, flag_print->answer);
 
-    G_debug(1, "Creating support files...");
-
     Rast_close(in_fd);
-    Rast_close(out_fd);
 
-    /* build title */
-    if (opt_title->answer != NULL)
-	strcpy(title, opt_title->answer);
-    else
-	sprintf(title, "clump of <%s@%s>", name, G_mapset());
-    Rast_put_cell_title(OUTPUT, title);
+    if (!flag_print->answer) {
+	Rast_close(out_fd);
 
-    /* colors */
-    Rast_read_range(OUTPUT, G_mapset(), &range);
-    Rast_get_range_min_max(&range, &min, &max);
-    Rast_make_random_colors(&colr, min, max);
-    Rast_write_colors(OUTPUT, G_mapset(), &colr);
+	G_debug(1, "Creating support files...");
 
-    /* history */
-    Rast_short_history(OUTPUT, "raster", &hist);
-    Rast_set_history(&hist, HIST_DATSRC_1, INPUT);
-    Rast_command_history(&hist);
-    Rast_write_history(OUTPUT, &hist);
+	/* build title */
+	if (opt_title->answer != NULL)
+	    strcpy(title, opt_title->answer);
+	else
+	    sprintf(title, "clump of <%s@%s>", name, G_mapset());
+	Rast_put_cell_title(OUTPUT, title);
 
-    G_done_msg(_n("%d clump.", "%d clumps.", range.max), range.max);
+	/* colors */
+	Rast_read_range(OUTPUT, G_mapset(), &range);
+	Rast_get_range_min_max(&range, &min, &max);
+	Rast_make_random_colors(&colr, min, max);
+	Rast_write_colors(OUTPUT, G_mapset(), &colr);
 
+	/* history */
+	Rast_short_history(OUTPUT, "raster", &hist);
+	Rast_set_history(&hist, HIST_DATSRC_1, INPUT);
+	Rast_command_history(&hist);
+	Rast_write_history(OUTPUT, &hist);
+
+	G_done_msg(_n("%d clump.", "%d clumps.", range.max), range.max);
+    }
+
     exit(EXIT_SUCCESS);
 }



More information about the grass-commit mailing list