[GRASS-SVN] r54558 - grass/trunk/vector/v.to.db

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jan 7 08:18:57 PST 2013


Author: martinl
Date: 2013-01-07 08:18:57 -0800 (Mon, 07 Jan 2013)
New Revision: 54558

Modified:
   grass/trunk/vector/v.to.db/global.h
   grass/trunk/vector/v.to.db/main.c
   grass/trunk/vector/v.to.db/parse.c
   grass/trunk/vector/v.to.db/report.c
Log:
v.to.db: fix totals flag
         add separator paramater for print mode


Modified: grass/trunk/vector/v.to.db/global.h
===================================================================
--- grass/trunk/vector/v.to.db/global.h	2013-01-07 07:42:06 UTC (rev 54557)
+++ grass/trunk/vector/v.to.db/global.h	2013-01-07 16:18:57 UTC (rev 54558)
@@ -31,6 +31,7 @@
     int total;			/* print totals */
     int units;
     int qfield;			/* query field */
+    char fs;
 };
 
 extern struct options options;

Modified: grass/trunk/vector/v.to.db/main.c
===================================================================
--- grass/trunk/vector/v.to.db/main.c	2013-01-07 07:42:06 UTC (rev 54557)
+++ grass/trunk/vector/v.to.db/main.c	2013-01-07 16:18:57 UTC (rev 54558)
@@ -36,7 +36,7 @@
     G_add_keyword(_("vector"));
     G_add_keyword(_("attribute table"));
     G_add_keyword(_("database"));
-    module->description = _("Populates database values from vector features.");
+    module->description = _("Populates attribute values from vector features.");
 
     parse_command_line(argc, argv);
 
@@ -83,7 +83,7 @@
 
     conv_units();
 
-    if (options.print) {
+    if (options.print || options.total) {
 	report();
     }
     else {
@@ -93,7 +93,7 @@
 
     Vect_close(&Map);
 
-    if (!(options.print && options.total)) {
+    if (!(options.print || options.total)) {
 	print_stat();
     }
 

Modified: grass/trunk/vector/v.to.db/parse.c
===================================================================
--- grass/trunk/vector/v.to.db/parse.c	2013-01-07 07:42:06 UTC (rev 54557)
+++ grass/trunk/vector/v.to.db/parse.c	2013-01-07 16:18:57 UTC (rev 54558)
@@ -22,6 +22,7 @@
 	struct Option *col;
 	struct Option *units;
 	struct Option *qcol;
+        struct Option *fs;
     } parms;
     struct
     {
@@ -41,12 +42,8 @@
     parms.type->description =
 	_("For coor valid point/centroid, "
 	  "for length valid line/boundary");
+    parms.type->guisection = _("Selection");
     
-    parms.qfield = G_define_standard_option(G_OPT_V_FIELD);
-    parms.qfield->key = "qlayer";
-    parms.qfield->label = _("Query layer number or name (read from)");
-    parms.qfield->guisection = _("Query");
-
     parms.option = G_define_option();
     parms.option->key = "option";
     parms.option->type = TYPE_STRING;
@@ -93,12 +90,20 @@
 	       _("line azimuth, calculated as angle between North direction and endnode direction at startnode"));
     parms.option->descriptions = desc;
 
+    parms.col = G_define_standard_option(G_OPT_DB_COLUMNS);
+    parms.col->label = _("Name of attribute column(s) to populate");
+    parms.col->required = YES;
+
     parms.units = G_define_standard_option(G_OPT_M_UNITS);
     parms.units->options =
 	"miles,feet,meters,kilometers,acres,hectares,radians,degrees";
     
-    parms.col = G_define_standard_option(G_OPT_DB_COLUMNS);
-
+    parms.qfield = G_define_standard_option(G_OPT_V_FIELD);
+    parms.qfield->key = "qlayer";
+    parms.qfield->label = _("Query layer number or name (read from)");
+    parms.qfield->guisection = _("Query");
+    parms.qfield->required = NO;
+    
     parms.qcol = G_define_standard_option(G_OPT_DB_COLUMN);
     parms.qcol->key = "qcolumn";
     parms.qcol->label = _("Name of attribute column used for 'query' option");
@@ -106,11 +111,16 @@
     parms.qcol->required = NO;
     parms.qcol->guisection = _("Query");
 
+    parms.fs = G_define_standard_option(G_OPT_F_SEP);
+    parms.fs->label = _("Field separator for print mode");
+    parms.fs->guisection = _("Print");
+    
     flags.p = G_define_flag();
     flags.p->key = 'p';
     flags.p->description = _("Print only");
     flags.p->guisection = _("Print");
-
+    flags.p->suppress_required = YES;
+    
     flags.s = G_define_flag();
     flags.s->key = 's';
     flags.s->description = _("Only print SQL statements");
@@ -119,12 +129,21 @@
     flags.t = G_define_flag();
     flags.t->key = 'c';
     flags.t->description =
-	_("In print mode prints totals for options: length,area,count");
+	_("Prints also totals for options: length,area,count");
     flags.t->guisection = _("Print");
+    flags.t->suppress_required = YES;
 
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
+    /* check for required options */
+    if (!parms.vect->answer)
+        G_fatal_error(_("Required parameter <%s> not set:\n\t(%s)"),
+                      parms.vect->key, parms.vect->description);
+    if (!parms.option->answer)
+        G_fatal_error(_("Required parameter <%s> not set:\n\t(%s)"),
+                      parms.option->key, parms.option->description);
+
     options.print = flags.p->answer;
     options.sql = flags.s->answer;
     options.total = flags.t->answer;
@@ -138,6 +157,8 @@
     options.option = parse_option(parms.option->answer);
     options.units = parse_units(parms.units->answer);
 
+    options.fs = G_option_to_separator(parms.fs);
+    
     /* Check number of columns */
     ncols = 0;
     options.col[0] = NULL;
@@ -148,7 +169,7 @@
 	ncols++;
     }
 
-    if (!options.print) {
+    if (!options.print && ! options.total) {
 	if (options.option == O_AREA || options.option == O_LENGTH || options.option == O_COUNT ||
 	    options.option == O_QUERY || options.option == O_COMPACT || options.option == O_FD ||
 	    options.option == O_PERIMETER || options.option == O_SLOPE || options.option == O_SINUOUS ||

Modified: grass/trunk/vector/v.to.db/report.c
===================================================================
--- grass/trunk/vector/v.to.db/report.c	2013-01-07 07:42:06 UTC (rev 54557)
+++ grass/trunk/vector/v.to.db/report.c	2013-01-07 16:18:57 UTC (rev 54558)
@@ -7,6 +7,13 @@
     int i;
     char left[20], right[20];
 
+    if (!options.print && !(options.option == O_COUNT ||
+                            options.option == O_LENGTH ||
+                            options.option == O_AREA)) {
+        G_warning(_("No totals for selected option"));
+        return 0;
+    }
+    
     switch (options.option) {
     case O_CAT:
 	if (G_verbose() > G_verbose_min())
@@ -16,98 +23,98 @@
 	break;
 
     case O_COUNT:
+        if (options.print) {
+            if (G_verbose() > G_verbose_min())
+                fprintf(stdout, "cat%ccount\n", options.fs);
+            for (i = 0; i < vstat.rcat; i++)
+                fprintf(stdout, "%d%c%d\n", Values[i].cat, options.fs, Values[i].count1);
+        }
 	if (options.total) {
 	    int sum = 0;
 
 	    for (i = 0; i < vstat.rcat; i++) {
 		sum += Values[i].count1;
 	    }
-	    fprintf(stdout, "total count: %d\n", sum);
+	    fprintf(stdout, "total count%c%d\n", options.fs, sum);
 	}
-	else {
-	    if (G_verbose() > G_verbose_min())
-		fprintf(stdout, "cat|count\n");
-	    for (i = 0; i < vstat.rcat; i++)
-		fprintf(stdout, "%d|%d\n", Values[i].cat, Values[i].count1);
-	}
 	break;
 
     case O_AREA:
+        if (options.print) {
+            if (G_verbose() > G_verbose_min())
+                fprintf(stdout, "cat%carea\n", options.fs);
+            for (i = 0; i < vstat.rcat; i++)
+                fprintf(stdout, "%d%c%.15g\n", Values[i].cat, options.fs, Values[i].d1);
+        }
 	if (options.total) {
 	    double sum = 0.0;
 
 	    for (i = 0; i < vstat.rcat; i++) {
 		sum += Values[i].d1;
 	    }
-	    fprintf(stdout, "total area: %.15g\n", sum);
+	    fprintf(stdout, "total area%c%.15g\n", options.fs, sum);
 	}
-	else {
-	    if (G_verbose() > G_verbose_min())
-		fprintf(stdout, "cat|area\n");
-	    for (i = 0; i < vstat.rcat; i++)
-		fprintf(stdout, "%d|%.15g\n", Values[i].cat, Values[i].d1);
-	}
 	break;
 
     case O_COMPACT:
 	if (G_verbose() > G_verbose_min())
-	    fprintf(stdout, "cat|compact\n");
+	    fprintf(stdout, "cat%ccompact\n", options.fs);
 	for (i = 0; i < vstat.rcat; i++)
-	    fprintf(stdout, "%d|%.15g\n", Values[i].cat, Values[i].d1);
+	    fprintf(stdout, "%d%c%.15g\n", Values[i].cat, options.fs, Values[i].d1);
 	break;
 
     case O_PERIMETER:
 	if (G_verbose() > G_verbose_min())
-	    fprintf(stdout, "cat|perimeter\n");
+	    fprintf(stdout, "cat%cperimeter\n", options.fs);
 	for (i = 0; i < vstat.rcat; i++)
-	    fprintf(stdout, "%d|%.15g\n", Values[i].cat, Values[i].d1);
+	    fprintf(stdout, "%d%c%.15g\n", Values[i].cat, options.fs, Values[i].d1);
 	break;
 
     case O_LENGTH:
+        if (options.print) {
+            if (G_verbose() > G_verbose_min())
+                fprintf(stdout, "cat%clength\n", options.fs);
+            for (i = 0; i < vstat.rcat; i++)
+                fprintf(stdout, "%d%c%.15g\n", Values[i].cat, options.fs, Values[i].d1);
+        }
 	if (options.total) {
 	    double sum = 0.0;
-
+            
 	    for (i = 0; i < vstat.rcat; i++) {
 		sum += Values[i].d1;
 	    }
-	    fprintf(stdout, "total length: %.15g\n", sum);
+	    fprintf(stdout, "total length%c%.15g\n", options.fs, sum);
 	}
-	else {
-	    if (G_verbose() > G_verbose_min())
-		fprintf(stdout, "cat|length\n");
-	    for (i = 0; i < vstat.rcat; i++)
-		fprintf(stdout, "%d|%.15g\n", Values[i].cat, Values[i].d1);
-	}
 	break;
     case O_SLOPE:
 	if (G_verbose() > G_verbose_min())
-	    fprintf(stdout, "cat|slope\n");
+	    fprintf(stdout, "cat%cslope\n", options.fs);
 	for (i = 0; i < vstat.rcat; i++)
-	    fprintf(stdout, "%d|%.15g\n", Values[i].cat, Values[i].d1);
+	    fprintf(stdout, "%d%c%.15g\n", Values[i].cat, options.fs, Values[i].d1);
 
 
 	break;
     case O_SINUOUS:
 	if (G_verbose() > G_verbose_min())
-	    fprintf(stdout, "cat|sinuous\n");
+	    fprintf(stdout, "cat%csinuous\n", options.fs);
 	for (i = 0; i < vstat.rcat; i++)
-	    fprintf(stdout, "%d|%.15g\n", Values[i].cat, Values[i].d1);
+	    fprintf(stdout, "%d%c%.15g\n", Values[i].cat, options.fs, Values[i].d1);
 	break;
     case O_COOR:
     case O_START:
     case O_END:
 	if (G_verbose() > G_verbose_min())
-	    fprintf(stdout, "cat|x|y|z\n");
+	    fprintf(stdout, "cat%cx%cy%cz\n", options.fs, options.fs, options.fs);
 	for (i = 0; i < vstat.rcat; i++) {
 	    if (Values[i].count1 == 1)
-		fprintf(stdout, "%d|%.15g|%.15g|%.15g\n", Values[i].cat,
-			Values[i].d1, Values[i].d2, Values[i].d3);
+		fprintf(stdout, "%d%c%.15g%c%.15g%c%.15g\n", Values[i].cat, options.fs,
+			Values[i].d1, options.fs, Values[i].d2, options.fs, Values[i].d3);
 	}
 	break;
 
     case O_SIDES:
 	if (G_verbose() > G_verbose_min())
-	    fprintf(stdout, "cat|left|right\n");
+	    fprintf(stdout, "cat%cleft%cright\n", options.fs, options.fs);
 	for (i = 0; i < vstat.rcat; i++) {
 	    if (Values[i].count1 == 1) {
 		if (Values[i].i1 >= 0)
@@ -138,13 +145,13 @@
 		sprintf(right, "-");
 	    }
 
-	    fprintf(stdout, "%d|%s|%s\n", Values[i].cat, left, right);
+	    fprintf(stdout, "%d%c%s%c%s\n", Values[i].cat, options.fs, left, options.fs, right);
 	}
 	break;
 
     case O_QUERY:
 	if (G_verbose() > G_verbose_min())
-	    fprintf(stdout, "cat|query\n");
+	    fprintf(stdout, "cat%cquery\n", options.fs);
 	for (i = 0; i < vstat.rcat; i++) {
 	    if (Values[i].null) {
 		fprintf(stdout, "%d|-\n", Values[i].cat);
@@ -152,13 +159,13 @@
 	    else {
 		switch (vstat.qtype) {
 		case (DB_C_TYPE_INT):
-		    fprintf(stdout, "%d|%d\n", Values[i].cat, Values[i].i1);
+		    fprintf(stdout, "%d%c%d\n", Values[i].cat, options.fs, Values[i].i1);
 		    break;
 		case (DB_C_TYPE_DOUBLE):
-		    fprintf(stdout, "%d|%15g\n", Values[i].cat, Values[i].d1);
+		    fprintf(stdout, "%d%c%15g\n", Values[i].cat, options.fs, Values[i].d1);
 		    break;
 		case (DB_C_TYPE_STRING):
-		    fprintf(stdout, "%d|%s\n", Values[i].cat, Values[i].str1);
+		    fprintf(stdout, "%d%c%s\n", Values[i].cat, options.fs, Values[i].str1);
 		    break;
 		}
 	    }
@@ -166,9 +173,9 @@
 	break;
     case O_AZIMUTH:
 	if (G_verbose() > G_verbose_min())
-	    fprintf(stdout, "cat|azimuth\n");
+	    fprintf(stdout, "cat%cazimuth\n", options.fs);
 	for (i = 0; i < vstat.rcat; i++)
-		fprintf(stdout, "%d|%.15g\n", Values[i].cat, Values[i].d1);
+		fprintf(stdout, "%d%c%.15g\n", Values[i].cat, options.fs, Values[i].d1);
 	break;
     }
 



More information about the grass-commit mailing list