[GRASS-SVN] r36602 - grass/trunk/vector/v.what

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Apr 6 09:02:58 EDT 2009


Author: neteler
Date: 2009-04-06 09:02:58 -0400 (Mon, 06 Apr 2009)
New Revision: 36602

Modified:
   grass/trunk/vector/v.what/main.c
   grass/trunk/vector/v.what/v.what.html
   grass/trunk/vector/v.what/what.c
   grass/trunk/vector/v.what/what.h
Log:
shell script style output added

Modified: grass/trunk/vector/v.what/main.c
===================================================================
--- grass/trunk/vector/v.what/main.c	2009-04-06 12:59:34 UTC (rev 36601)
+++ grass/trunk/vector/v.what/main.c	2009-04-06 13:02:58 UTC (rev 36602)
@@ -36,7 +36,7 @@
 
 int main(int argc, char **argv)
 {
-    struct Flag *printattributes, *topo_flag;
+    struct Flag *printattributes, *topo_flag, *shell_flag;
     struct Option *opt1, *coords_opt, *maxdistance;
     struct Cell_head window;
     struct GModule *module;
@@ -82,6 +82,10 @@
     printattributes->key = 'a';
     printattributes->description = _("Print attribute information");
 
+    shell_flag = G_define_flag();
+    shell_flag->key = 'g';
+    shell_flag->description = _("Print the stats in shell script style");
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
@@ -149,7 +153,7 @@
 	    ret = sscanf(buf, "%lf%c%lf", &xval, &ch, &yval);
 	    if (ret == 3 && (ch == ',' || ch == ' ' || ch == '\t')) {
 		what(xval, yval, maxd, topo_flag->answer,
-		     printattributes->answer);
+		     printattributes->answer, shell_flag->answer);
 	    }
 	    else {
 		G_warning(_("Unknown input format, skipping: '%s'"), buf);
@@ -162,7 +166,7 @@
 	for (i = 0; coords_opt->answers[i] != NULL; i += 2) {
 	    xval = atof(coords_opt->answers[i]);
 	    yval = atof(coords_opt->answers[i + 1]);
-	    what(xval, yval, maxd, topo_flag->answer, printattributes->answer);
+	    what(xval, yval, maxd, topo_flag->answer, printattributes->answer, shell_flag->answer);
 	}
     }
 

Modified: grass/trunk/vector/v.what/v.what.html
===================================================================
--- grass/trunk/vector/v.what/v.what.html	2009-04-06 12:59:34 UTC (rev 36601)
+++ grass/trunk/vector/v.what/v.what.html	2009-04-06 13:02:58 UTC (rev 36602)
@@ -4,7 +4,8 @@
 Outputs the category number value(s) associated with user-specified location(s) 
 in user-specified vector map layer(s).  This module was derived from the
 <em>d.what.vect</em> module by removing all interactive code and
-modification of the output for easy parsing.
+modification of the output for easy parsing. Using the <em>-g</em> flag permits to
+generate script style output which is easily parsable.
 
 <h2>EXAMPLE</h2>
 

Modified: grass/trunk/vector/v.what/what.c
===================================================================
--- grass/trunk/vector/v.what/what.c	2009-04-06 12:59:34 UTC (rev 36601)
+++ grass/trunk/vector/v.what/what.c	2009-04-06 13:02:58 UTC (rev 36602)
@@ -13,9 +13,9 @@
 
 #define WDTH 5
 
-static void F_generate(
-    const char *drvname, const char *dbname, const char *tblname, const char *key,
-    int keyval, char **form)
+static void F_generate(const char *drvname, const char *dbname,
+		       const char *tblname, const char *key, int keyval,
+		       char **form)
 {
     int col, ncols, ctype, sqltype, more;
     char buf[5000];
@@ -103,7 +103,7 @@
     db_free_string(&str);
 }
 
-void what(double east, double north, double maxdist, int topo, int showextra)
+void what(double east, double north, double maxdist, int topo, int showextra, int script)
 {
     int type;
     char east_buf[40], north_buf[40];
@@ -149,8 +149,14 @@
 	    G_format_easting(east, east_buf, G_projection());
 	    G_format_northing(north, north_buf, G_projection());
 	    if (line + area > 0 || G_verbose() >= G_verbose_std()) {
-		fprintf(stdout, "\nEast: %s\nNorth: %s\n", east_buf,
-			north_buf);
+		if (script) {
+		    fprintf(stdout, "East=%s\nNorth=%s\n", east_buf,
+			    north_buf);
+		}
+		else {
+		    fprintf(stdout, "\nEast: %s\nNorth: %s\n", east_buf,
+			    north_buf);
+		}
 		if (notty)
 		    fprintf(stderr, "\nEast: %s\nNorth: %s\n", east_buf,
 			    north_buf);
@@ -163,8 +169,14 @@
 	    *str = 0;
 
 	if (line + area > 0 || G_verbose() >= G_verbose_std()) {
-	    fprintf(stdout, "\nMap: %s \nMapset: %s\n", Map[i].name,
-		    Map[i].mapset);
+	    if (script) {
+		fprintf(stdout, "Map=%s\nMapset=%s\n", Map[i].name,
+			Map[i].mapset);
+	    }
+	    else {
+		fprintf(stdout, "\nMap: %s \nMapset: %s\n", Map[i].name,
+			Map[i].mapset);
+	    }
 	    if (notty)
 		fprintf(stderr, "\nMap: %s \nMapset: %s\n", Map[i].name,
 			Map[i].mapset);
@@ -215,18 +227,27 @@
 		float angle;
 
 		Vect_get_line_areas(&(Map[i]), line, &left, &right);
-		fprintf(stdout, "Looking for features within: %f\n",
-			maxdist);
-		fprintf(stdout,
-			_("Id: %d\nType: %s\nLeft: %d\nRight: %d\n"),
-			line, buf, left, right);
+		if (script) {
+		    fprintf(stdout, "Feature_max_distance=%f\n", maxdist);
+		    fprintf(stdout,
+			    "Id=%d\nType=%s\nLeft=%d\nRight=%d\n",
+			    line, buf, left, right);
+		}
+		else {
+		    fprintf(stdout, "Looking for features within: %f\n",
+			    maxdist);
+		    fprintf(stdout,
+			    _("Id: %d\nType: %s\nLeft: %d\nRight: %d\n"),
+			    line, buf, left, right);
+		}
 		if (type & GV_LINES) {
 		    nnodes = 2;
 		    fprintf(stdout, _("Length: %f\n"), l);
 		}
 		else {		/* points */
 		    nnodes = 1;
-		    fprintf(stdout, "\n");
+		    if (!script)
+			fprintf(stdout, "\n");
 		}
 
 		Vect_get_line_nodes(&(Map[i]), line, &node[0], &node[1]);
@@ -237,26 +258,47 @@
 		    nnlines = Vect_get_node_n_lines(&(Map[i]), node[n]);
 
 		    Vect_get_node_coor(&(Map[i]), node[n], &nx, &ny, &nz);
-		    fprintf(stdout,
-			    _("Node[%d]: %d\nNumber of lines: %d\nCoordinates: %.6f, %.6f, %.6f\n"),
-			    n, node[n], nnlines, nx, ny, nz);
+		    if (script) {
+			fprintf(stdout,
+				_("Node[%d]=%d\nNumber_lines=%d\nCoordinates=%.6f,%.6f,%.6f\n"),
+				n, node[n], nnlines, nx, ny, nz);
+		    }
+		    else {
+			fprintf(stdout,
+				_("Node[%d]: %d\nNumber of lines: %d\nCoordinates: %.6f, %.6f, %.6f\n"),
+				n, node[n], nnlines, nx, ny, nz);
+		    }
 
 		    for (nli = 0; nli < nnlines; nli++) {
 			nodeline =
 			    Vect_get_node_line(&(Map[i]), node[n], nli);
 			angle =
 			    Vect_get_node_line_angle(&(Map[i]), node[n], nli);
-			fprintf(stdout, _("Id: %5d\nAngle: %.8f\n"),
-				nodeline, angle);
+			if (script) {
+			    fprintf(stdout, "Id=%5d\nAngle=%.8f\n",
+				    nodeline, angle);
+			}
+			else {
+			    fprintf(stdout, _("Id: %5d\nAngle: %.8f\n"),
+				    nodeline, angle);
+			}
 		    }
 		}
 
 	    }
 	    else {
-		fprintf(stdout, _("Type: %s"), buf);
-		fprintf(stdout, _("Id: %d\n"), line);
-		if (type & GV_LINES)
-		    fprintf(stdout, _("Length: %f\n"), l);
+		if (script) {
+		    fprintf(stdout, "Type=%s\n", buf);
+		    fprintf(stdout, "Id=%d\n", line);
+		    if (type & GV_LINES)
+			fprintf(stdout, "Length=%f\n", l);
+		}
+		else {
+		    fprintf(stdout, _("Type: %s"), buf);
+		    fprintf(stdout, _("Id: %d\n"), line);
+		    if (type & GV_LINES)
+			fprintf(stdout, _("Length: %f\n"), l);
+		}
 	    }
 
 	    /* Height */
@@ -265,7 +307,13 @@
 		double min, max;
 
 		if (type & GV_POINTS) {
-		    fprintf(stdout, _("Point height: %f\n"), Points->z[0]);
+		    if (script) {
+			fprintf(stdout, "Point_height=%f\n", Points->z[0]);
+		    }
+		    else {
+			fprintf(stdout, _("Point height: %f\n"),
+				Points->z[0]);
+		    }
 		}
 		else if (type & GV_LINES) {
 		    min = max = Points->z[0];
@@ -276,12 +324,24 @@
 			    max = Points->z[j];
 		    }
 		    if (min == max) {
-			fprintf(stdout, _("Line height: %f\n"), min);
+			if (script) {
+			    fprintf(stdout, "Line_height=%f\n", min);
+			}
+			else {
+			    fprintf(stdout, _("Line height: %f\n"), min);
+			}
 		    }
 		    else {
-			fprintf(stdout,
-				_("Line height min: %f\nLine height max: %f\n"),
-				min, max);
+			if (script) {
+			    fprintf(stdout,
+				    "Line_height_min=%f\nLine_height_max=%f\n",
+				    min, max);
+			}
+			else {
+			    fprintf(stdout,
+				    _("Line height min: %f\nLine height max: %f\n"),
+				    min, max);
+			}
 		    }
 		}
 	    }			/* if height */
@@ -289,39 +349,74 @@
 
 	if (area > 0) {
 	    if (Map[i].head.with_z && getz) {
-		fprintf(stdout, _("Type: Area\nArea height: %f\n"),
-			z);
+		if (script) {
+		    fprintf(stdout, "Type=Area\nArea_height=%f\n", z);
+		}
+		else {
+		fprintf(stdout, _("Type: Area\nArea height: %f\n"), z);
+		}
 	    }
 	    else {
-		fprintf(stdout, _("Type: Area\n"));
+		if (script) {
+		    fprintf(stdout, "Type=Area\n");
+		}
+		else {
+		    fprintf(stdout, _("Type: Area\n"));
+		}
 	    }
 	    sq_meters = Vect_get_area_area(&Map[i], area);
 	    if (topo) {
 		int nisles, isleidx, isle, isle_area;
 
 		nisles = Vect_get_area_num_isles(&Map[i], area);
-		fprintf(stdout, _("Area: %d\nNumber of isles: %d\n"), area,
-			nisles);
+		if (script) {
+		    fprintf(stdout, "Area=%d\nNumber_isles=%d\n", area,
+			    nisles);
+		}
+		else {
+		    fprintf(stdout, _("Area: %d\nNumber of isles: %d\n"),
+			    area, nisles);
+		}
 
 		for (isleidx = 0; isleidx < nisles; isleidx++) {
 		    isle = Vect_get_area_isle(&Map[i], area, isleidx);
-		    fprintf(stdout, _("Isle[%d]: %d\n"), isleidx, isle);
+		    if (script) {
+			fprintf(stdout, "Isle[%d]=%d\n", isleidx, isle);
+		    }
+		    else {
+			fprintf(stdout, _("Isle[%d]: %d\n"), isleidx, isle);
+		    }
 		}
 
 		isle = Vect_find_island(&Map[i], east, north);
 
 		if (isle) {
 		    isle_area = Vect_get_isle_area(&Map[i], isle);
-		    fprintf(stdout, _("Island: %d In area: %d\n"), isle,
-			    isle_area);
+		    if (script) {
+			fprintf(stdout, "Island=%d\nIsland_area=%d\n", isle,
+				isle_area);
+		    }
+		    else {
+			fprintf(stdout, _("Island: %d In area: %d\n"), isle,
+				isle_area);
+		    }
 		}
 	    }
 	    else {
-		fprintf(stdout, _("Sq Meters: %.3f\nHectares: %.3f\n"),
-			sq_meters, (sq_meters / 10000.));
-		fprintf(stdout, _("Acres: %.3f\nSq Miles: %.4f\n"),
-			((sq_meters * 10.763649) / 43560.),
-			((sq_meters * 10.763649) / 43560.) / 640.);
+		if (script) {
+		    fprintf(stdout, "Sq_Meters=%.3f\nHectares=%.3f\n",
+			    sq_meters, (sq_meters / 10000.));
+		    fprintf(stdout, "Acres=%.3f\nSq_Miles=%.4f\n",
+			    ((sq_meters * 10.763649) / 43560.),
+			    ((sq_meters * 10.763649) / 43560.) / 640.);
+		}
+		else {
+		    fprintf(stdout, _("Sq Meters: %.3f\nHectares: %.3f\n"),
+			    sq_meters, (sq_meters / 10000.));
+		    fprintf(stdout, _("Acres: %.3f\nSq Miles: %.4f\n"),
+			    ((sq_meters * 10.763649) / 43560.),
+			    ((sq_meters * 10.763649) / 43560.) / 640.);
+		}
 		if (notty) {
 		    fprintf(stderr,
 			    _("Sq Meters: %.3f\nHectares: %.3f\n"),
@@ -341,21 +436,43 @@
 
 	if (Cats->n_cats > 0) {
 	    int j;
+	    char *formbuf1, *formbuf2;
 
 	    for (j = 0; j < Cats->n_cats; j++) {
 		G_debug(2, "field = %d  category = %d\n", Cats->field[j],
 			Cats->cat[j]);
-		fprintf(stdout, _("Layer: %d\nCategory: %d\n"),
-			Cats->field[j], Cats->cat[j]);
+		if (script) {
+		    fprintf(stdout, "Layer=%d\nCategory=%d\n", Cats->field[j],
+			    Cats->cat[j]);
+		}
+		else {
+		    fprintf(stdout, _("Layer: %d\nCategory: %d\n"),
+			    Cats->field[j], Cats->cat[j]);
+		}
 		Fi = Vect_get_field(&(Map[i]), Cats->field[j]);
 		if (Fi != NULL && showextra) {
+		    if (script) {
+			fprintf(stdout,
+				"Driver=%s\nDatabase=%s\nTable=%s\nKey_column=%s\n",
+				Fi->driver, Fi->database, Fi->table, Fi->key);
+		    }
+		    else {
+			fprintf(stdout,
+				_("\nDriver: %s\nDatabase: %s\nTable: %s\nKey column: %s\n"),
+				Fi->driver, Fi->database, Fi->table, Fi->key);
+		    }
+		    F_generate(Fi->driver, Fi->database, Fi->table,
+			       Fi->key, Cats->cat[j], &form);
 
-		    fprintf(stdout,
-			    _("\nDriver: %s\nDatabase: %s\nTable: %s\nKey column: %s\n"),
-			    Fi->driver, Fi->database, Fi->table, Fi->key);
-		    F_generate(Fi->driver, Fi->database, Fi->table, Fi->key,
-			       Cats->cat[j], &form);
-		    fprintf(stdout, "%s", form);
+		    if (script) {
+			formbuf1 = G_str_replace(form, " : ", "=");
+			formbuf2 = G_str_replace(formbuf1, " ", "_");
+			fprintf(stdout, "%s", formbuf2);
+			G_free(formbuf1);
+			G_free(formbuf2);
+		    }
+		    else
+			fprintf(stdout, "%s", form);
 		    G_free(form);
 		    G_free(Fi);
 		}

Modified: grass/trunk/vector/v.what/what.h
===================================================================
--- grass/trunk/vector/v.what/what.h	2009-04-06 12:59:34 UTC (rev 36601)
+++ grass/trunk/vector/v.what/what.h	2009-04-06 13:02:58 UTC (rev 36602)
@@ -1,5 +1,5 @@
 /* what.c */
-extern void what(double, double, double, int, int);
+extern void what(double, double, double, int, int, int);
 
 extern char **vect;
 extern int nvects;



More information about the grass-commit mailing list