[GRASS-SVN] r73147 - grass/trunk/imagery/i.group

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Aug 23 13:16:48 PDT 2018


Author: mmetz
Date: 2018-08-23 13:16:48 -0700 (Thu, 23 Aug 2018)
New Revision: 73147

Modified:
   grass/trunk/imagery/i.group/main.c
Log:
i.group: add file option for a file with a list of raster maps (#3627)

Modified: grass/trunk/imagery/i.group/main.c
===================================================================
--- grass/trunk/imagery/i.group/main.c	2018-08-23 14:38:58 UTC (rev 73146)
+++ grass/trunk/imagery/i.group/main.c	2018-08-23 20:16:48 UTC (rev 73147)
@@ -40,9 +40,10 @@
 int main(int argc, char *argv[])
 {
     char group[GNAME_MAX], mapset[GMAPSET_MAX];
+    char **rasters = NULL;
     int m, k = 0;
 
-    struct Option *grp, *rast, *sgrp;
+    struct Option *grp, *rast, *rastf, *sgrp;
     struct Flag *r, *l, *s, *simple_flag;
     struct GModule *module;
 
@@ -67,6 +68,11 @@
     rast->description = _("Name of raster map(s) to include in group");
     rast->guisection = _("Maps");
 
+    rastf = G_define_standard_option(G_OPT_F_INPUT);
+    rastf->key = "file";
+    rastf->description = _("Input file with one raster map name per line");
+    rastf->required = NO;
+
     r = G_define_flag();
     r->key = 'r';
     r->description =
@@ -97,14 +103,47 @@
     if ((simple_flag->answer && !s->answer) && !l->answer)
 	l->answer = TRUE;
 
-    /* Determine number of files to include */
+    /* Determine number of raster maps to include */
     if (rast->answers) {
 	for (m = 0; rast->answers[m]; m++) {
 	    k = m;
 	}
 	k++;
+	rasters = rast->answers;
     }
+    /* process the input maps from the file */
+    else if (rastf->answer) {
+	FILE *in;
+    
+	m = 10;
+	rasters = G_malloc(m * sizeof(char *));
+	in = fopen(rastf->answer, "r");
+	if (!in)
+	    G_fatal_error(_("Unable to open input file <%s>"), rastf->answer);
+    
+	for (;;) {
+	    char buf[GNAME_MAX];
+	    char *name;
 
+	    if (!G_getl2(buf, sizeof(buf), in))
+		break;
+
+	    name = G_chop(buf);
+
+	    /* Ignore empty lines */
+	    if (!*name)
+		continue;
+
+	    if (m <= k) {
+		m += 10;
+		rasters = G_realloc(rasters, m * sizeof(char *));
+	    }
+	    rasters[k] = G_store(name);
+	    k++;
+	}
+	fclose(in);
+    }
+
     if (k < 1 && !(l->answer || s->answer))	/* remove if input is requirement */
 	G_fatal_error(_("No input raster map(s) specified"));
 
@@ -131,12 +170,12 @@
 	if (sgrp->answer) {
 	    G_verbose_message(_("Removing raster maps from subgroup <%s>..."),
 			      sgrp->answer);
-	    remove_subgroup_files(group, sgrp->answer, rast->answers, k);
+	    remove_subgroup_files(group, sgrp->answer, rasters, k);
 	}
 	else {
 	    G_verbose_message(_("Removing raster maps from group <%s>..."),
 			      group);
-	    remove_group_files(group, rast->answers, k);
+	    remove_group_files(group, rasters, k);
 	}
     }
     else {
@@ -188,16 +227,16 @@
 	    if (sgrp->answer) {
 		G_verbose_message(_("Adding raster maps to group <%s>..."),
 				  group);
-		add_or_update_group(group, rast->answers, k);
+		add_or_update_group(group, rasters, k);
 
 		G_verbose_message(_("Adding raster maps to subgroup <%s>..."),
 				  sgrp->answer);
-		add_or_update_subgroup(group, sgrp->answer, rast->answers, k);
+		add_or_update_subgroup(group, sgrp->answer, rasters, k);
 	    }
 	    else {
 		G_verbose_message(_("Adding raster maps to group <%s>..."),
 				  group);
-		add_or_update_group(group, rast->answers, k);
+		add_or_update_group(group, rasters, k);
 	    }
 	}
     }



More information about the grass-commit mailing list