[GRASS-SVN] r34917 - grass/branches/develbranch_6/vector/v.out.ascii

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Dec 17 14:25:09 EST 2008


Author: martinl
Date: 2008-12-17 14:25:09 -0500 (Wed, 17 Dec 2008)
New Revision: 34917

Modified:
   grass/branches/develbranch_6/vector/v.out.ascii/b2a.c
   grass/branches/develbranch_6/vector/v.out.ascii/description.html
   grass/branches/develbranch_6/vector/v.out.ascii/local_proto.h
   grass/branches/develbranch_6/vector/v.out.ascii/out.c
Log:
v.out.ascii: attribute selection added (v.out.ascii.db merged)
	     new parameters: where, layer, columns (point mode point)


Modified: grass/branches/develbranch_6/vector/v.out.ascii/b2a.c
===================================================================
--- grass/branches/develbranch_6/vector/v.out.ascii/b2a.c	2008-12-17 17:09:34 UTC (rev 34916)
+++ grass/branches/develbranch_6/vector/v.out.ascii/b2a.c	2008-12-17 19:25:09 UTC (rev 34917)
@@ -1,11 +1,15 @@
 #include <grass/Vect.h>
 #include <grass/gis.h>
+#include <grass/dbmi.h>
 #include <grass/glocale.h>
 #include "local_proto.h"
 
-int bin_to_asc(FILE * ascii,
-	       FILE * att, struct Map_info *Map, int ver,
-	       int format, int dp, char *fs, int region_flag)
+static int srch(const void *pa, const void *pb);
+
+int bin_to_asc(FILE *ascii,
+	       FILE *att, struct Map_info *Map, int ver,
+	       int format, int dp, char *fs, int region_flag,
+	       int field, char* where, char **columns)
 {
     int type, ctype, i, cat, proj;
     double *xptr, *yptr, *zptr, x, y;
@@ -14,9 +18,47 @@
     char *xstring = NULL, *ystring = NULL, *zstring = NULL;
     struct Cell_head window;
 
+    /* where */
+    struct field_info *Fi;
+    dbDriver *driver;
+    dbValue value;
+    dbHandle handle;
+    int *cats, ncats;
+    
     /* get the region */
     G_get_window(&window);
 
+    ncats = 0;
+    cats = NULL;
+    
+    if (where || columns) {
+	Fi = Vect_get_field(Map, field);
+	if (!Fi) {
+	    G_fatal_error(_("Database connection not defined for layer %d"),
+			  field);
+	}
+
+	driver = db_start_driver(Fi->driver);
+	if (!driver)
+	    G_fatal_error(_("Unable to start driver <%s>"), Fi->driver);
+	
+	db_init_handle(&handle);
+	db_set_handle(&handle, Fi->database, NULL);
+	
+	if (db_open_database(driver, &handle) != DB_OK)
+	    G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
+			  Fi->database, Fi->driver);
+	
+	/* select cats (sorted array) */
+	ncats = db_select_int(driver, Fi->table, Fi->key, where, &cats);
+	G_debug(3, "%d categories selected from table <%s>", ncats, Fi->table);
+
+	if (!columns) {
+	    db_close_database(driver);
+	    db_shutdown_driver(driver);
+	}
+    }
+    
     Points = Vect_new_line_struct();	/* init line_pnts struct */
     Cats = Vect_new_cats_struct();
 
@@ -29,15 +71,40 @@
     Vect_rewind(Map);
 
     while (1) {
-	if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
-	    return (-1);
+	if (-1 == (type = Vect_read_next_line(Map, Points, Cats))) {
+	    if (columns) {
+		db_close_database(driver);
+		db_shutdown_driver(driver);
+	    }
+	    
+	    return -1;
+	}
 
-	if (type == -2)		/* EOF */
-	    return (0);
+	if (type == -2)	{	/* EOF */
+	    if (columns) {
+		db_close_database(driver);
+		db_shutdown_driver(driver);
+	    }
+	    return 0;
+	}
 
 	if (format == FORMAT_POINT && !(type & GV_POINTS))
 	    continue;
 
+	if (cats) {
+	    /* check category */
+	    for (i = 0; i < Cats->n_cats; i++) {
+		if ((int *)bsearch((void *) &(Cats->cat[i]), cats, ncats, sizeof(int),
+				   srch)) {
+		    /* found */
+		    break;
+		}
+	    }
+	    
+	    if (i == Cats->n_cats) 
+		continue;
+	}
+
 	if (ver < 5) {
 	    Vect_cat_get(Cats, 1, &cat);
 	}
@@ -115,8 +182,49 @@
 	    else {
 		fprintf(ascii, "%s%s%s", xstring, fs, ystring);
 	    }
-	    if (Cats->n_cats > 0)
+	    if (Cats->n_cats > 0) {
+		if (Cats->n_cats > 1) {
+		    G_warning(_("Feature has more categories. Only first category (%d) "
+				"is exported."), Cats->cat[0]);
+		}
 		fprintf(ascii, "%s%d", fs, Cats->cat[0]);
+		
+		/* print attributes */
+		if (columns) {
+		    for(i = 0; columns[i]; i++) {
+			if (db_select_value(driver, Fi->table, Fi->key, Cats->cat[0],
+					    columns[i], &value) < 0)
+			    G_fatal_error(_("bUnable to select record from table <%s> (key %s, column %s)"),
+					  Fi->table, Fi->key, columns[i]);
+			
+			if (db_test_value_isnull(&value)) {
+			    fprintf(ascii, "%s", fs);
+			}
+			else {
+			    switch(db_column_Ctype(driver, Fi->table, columns[i]))
+			    {
+			    case DB_C_TYPE_INT: {
+				fprintf(ascii, "%s%d", fs, db_get_value_int(&value));
+				break;
+			    }
+			    case DB_C_TYPE_DOUBLE: {
+				fprintf(ascii, "%s%.*f", fs, dp, db_get_value_double(&value));
+				break;
+			    }
+			    case DB_C_TYPE_STRING: {
+				fprintf(ascii, "%s%s", fs, db_get_value_string(&value));
+				break;
+			    }
+			    case DB_C_TYPE_DATETIME: {
+				break;
+			    }
+			    default: G_fatal_error(_("Column <%s>: unsupported data type"),
+						   columns[i]);
+			    }
+			}
+		    }
+		}
+	    }
 
 	    fprintf(ascii, "\n");
 	}
@@ -187,3 +295,15 @@
 
     /* not reached */
 }
+
+int srch(const void *pa, const void *pb)
+{
+    int *p1 = (int *)pa;
+    int *p2 = (int *)pb;
+    
+    if (*p1 < *p2)
+	return -1;
+    if (*p1 > *p2)
+	return 1;
+    return 0;
+}

Modified: grass/branches/develbranch_6/vector/v.out.ascii/description.html
===================================================================
--- grass/branches/develbranch_6/vector/v.out.ascii/description.html	2008-12-17 17:09:34 UTC (rev 34916)
+++ grass/branches/develbranch_6/vector/v.out.ascii/description.html	2008-12-17 19:25:09 UTC (rev 34917)
@@ -1,68 +1,73 @@
 <h2>DESCRIPTION</h2>
 
-<em>v.out.ascii</em> 
-converts a GRASS vector map in binary format to a GRASS vector map in ASCII 
-format. Using flag <b>-o</b> <em>v.out.ascii</em> output will be in old 
-(version 4) ASCII format.
-<P>
+<em>v.out.ascii</em> converts a GRASS vector map in binary format to a
+GRASS vector map in ASCII format. Using
+flag <b>-o</b> <em>v.out.ascii</em> output will be in old (version 4)
+ASCII format.
+
+<p>
 If the <b>output</b> parameter is not given then the coordinates of any 
 <em>point</em> data within the vector map is sent to stdout.
 
 <h2>NOTES</h2>
 
-The GRASS program  <em><a HREF="v.in.ascii.html">v.in.ascii</a></em> 
-performs the function of <em>v.out.ascii</em> in reverse; i.e., it
-converts vector maps in ASCII format to their binary format. 
-These two companion programs are useful both for importing and exporting 
-vector maps between GRASS and other software, and for transferring data 
+The <em><a href="v.in.ascii.html">v.in.ascii</a></em> module performs
+the function of <em>v.out.ascii</em> in reverse; i.e. it converts
+vector maps in ASCII format to their binary format.  These two
+companion module are useful both for importing and exporting vector
+maps between GRASS and other software, and for transferring data
 between machines.
 
-<P>
-If the <b>format</b> parameter is set to <b>standard</b>, A GRASS ASCII vector map will be exported,
-which may contain a mix of primitives including points, lines, boundaries, centroids, areas, faces, 
-and kernels. The beginning of the output ascii file will contain a header listing any metadata for the
-input vector, if such metadata exists. An example of the <b>standard</b> format is given below.
-<P>
+<o>
+If the <b>format</b> parameter is set to <b>standard</b>, a GRASS
+ASCII vector map will be exported, which may contain a mix of
+primitives including points, lines, boundaries, centroids, areas,
+faces, and kernels. The beginning of the output ascii file will
+contain a header listing any metadata for the input vector map, if
+such metadata exists. An example of the <b>standard</b> format is
+given below.
+
+<p>
 The primitive codes are as follows:
-<UL>
-<LI>'P': point</LI>
-<LI>'L': line</LI>
-<LI>'B': boundary</LI>
-<LI>'C': centroid</LI>
-<LI>'F': face (3D boundary)</LI>
-<LI>'K': kernel (3D centroid)</LI>
-<LI>'A': area (boundary) - better use 'B'; kept only for backward compatibility</LI>
+<ul>
+<li>'P': point</li>
+<li>'L': line</li>
+<li>'B': boundary</li>
+<li>'C': centroid</li>
+<li>'F': face (3D boundary)</li>
+<li>'K': kernel (3D centroid)</li>
+<li>'A': area (boundary) - better use 'B'; kept only for backward compatibility</li>
+</ul>
 
-</UL>
+The coordinates are listed following the initial line containing the
+primitive code, the total number of vectors in the series, and the
+number of categories (1 for a single layer, higher for multiple
+layers). Below that 1 or several lines follow to indicate the layer
+number and the category number.
 
-The coordinates are listed following the initial line containing the
-primitive code, the total number of vectors in the series, and the number
-of categories (1 for a single layer, higher for multiple layers).
-Below that 1 or several lines follow to indicate the layer number and
-the category number (ID).
-<BR>
 <p> 
-The order of coordinates for new (standard) version of ASCII file is <BR><BR>
+The order of coordinates for new (standard) version of ASCII file is
+<div class="code"><pre>
 X Y [Z]
-<BR><BR> 
-the order of coordinates for old version of ASCII file is <BR><BR>
+</pre></div>
+the order of coordinates for old version of ASCII file is
+<div class="code"><pre>
 Y X
+</pre></div>
 
-<p>
-If old version is requested, the <B>output</B> files from <em>v.out.ascii</em> will be placed 
+If old version is requested, the <b>output</b> files from <em>v.out.ascii</em> will be placed 
 in the <tt>$LOCATION/$MAPSET/dig_ascii/</tt> and <tt>$LOCATION/$MAPSET/dig_att</tt> directory.
 
 <p>
-Only features with a category number will be exported. Use <em>v.category</em>
-to add them if needed.
+Only features with a category number will be
+exported. Use <em>v.category</em> to add them if needed.
 
 <p>
-<em>v.out.ascii</em> does not copy the <em>dig_cats</em>
-file associated with the binary vector <em>input</em> map
-to the new <em>output</em> file name.  The user must copy
-the <em>dig_cats</em> file to the new <em>output</em> name
-if this is desired (e.g., using the UNIX <em>cp</em>
-command).
+<em>v.out.ascii</em> does not copy the <em>dig_cats</em> file
+associated with the binary vector <em>input</em> map to the
+new <em>output</em> file name. The user must copy
+the <em>dig_cats</em> file to the new <em>output</em> name if this is
+desired (e.g. using the UNIX <em>cp</em> command).
 
 <p>
 It is possible to output the coordinates of vertices in a non-points vector
@@ -72,7 +77,7 @@
 
 <h2>EXAMPLES</h2>
 
-<h3>Example 1a) - standard mode - using the 'quads' vector from Spearfish dataset:</h3>
+<h3>Standard mode</h3>
 
 <p>
 <div class="code"><pre>
@@ -109,9 +114,8 @@
  1     2
 </pre></div>
 
-<h3>Example 1b) - point mode</h3>
+<h3>Point mode</h3>
 
-<p>
 <div class="code"><pre>
 v.out.ascii input=quads format=point
 
@@ -119,15 +123,24 @@
 604433.84|4921087.1|2
 </pre></div>
 
+<div class="code"><pre>
+v.out.ascii input=archsites format=point where="cat > 5 and cat <= 8" columns=str1
+
+600375|4925235|6|Prairie Site
+606635|4920773|7|Jensen Pass
+595755|4925300|8|No Name
+</pre></div>
+
 <h2>SEE ALSO</h2>
 
 <em>
-<a HREF="v.category.html">v.category</a><BR>
-<a HREF="v.in.ascii.html">v.in.ascii</a><BR>
-<a HREF="v.to.points.html">v.to.points</a></em><BR>
-<a HREF="http://freegis.org/cgi-bin/viewcvs.cgi/~checkout~/grass6/doc/vector/vector.html#ascii">Vector ASCII Format Specification</a>
-<BR><BR>
+<a href="v.category.html">v.category</a>,
+<a href="v.in.ascii.html">v.in.ascii</a>,
+<a href="v.to.points.html">v.to.points</a></em>
 
+<br><br>
+<a HREF="http://freegis.org/cgi-bin/viewcvs.cgi/~checkout~/grass6/doc/vector/vector.html#ascii">Vector
+ASCII Format Specification</a>
 
 <h2>AUTHORS</h2>
 
@@ -140,5 +153,8 @@
 Research Laboratory
 <br>
 Radim Blazek, ITC-Irst, Trento, Italy
+<br>
+Attribute selection added by Martin Landa, CTU in Prague, Czech Republic (2008/12)
 
-<p><i>Last changed: $Date$</i>
+<p>
+<i>Last changed: $Date$</i>

Modified: grass/branches/develbranch_6/vector/v.out.ascii/local_proto.h
===================================================================
--- grass/branches/develbranch_6/vector/v.out.ascii/local_proto.h	2008-12-17 17:09:34 UTC (rev 34916)
+++ grass/branches/develbranch_6/vector/v.out.ascii/local_proto.h	2008-12-17 19:25:09 UTC (rev 34917)
@@ -3,6 +3,6 @@
 
 /* b2a.c */
 int bin_to_asc(FILE *, FILE *, struct Map_info *, int ver, int format, int dp,
-	       char *, int);
+	       char *, int, int, char *, char **);
 /* head.c */
 int write_head(FILE * dascii, struct Map_info *Map);

Modified: grass/branches/develbranch_6/vector/v.out.ascii/out.c
===================================================================
--- grass/branches/develbranch_6/vector/v.out.ascii/out.c	2008-12-17 17:09:34 UTC (rev 34916)
+++ grass/branches/develbranch_6/vector/v.out.ascii/out.c	2008-12-17 19:25:09 UTC (rev 34917)
@@ -5,6 +5,7 @@
  * AUTHOR(S):  Michael Higgins, U.S. Army Construction Engineering Research Laboratory
  *             James Westervelt, U.S. Army Construction Engineering Research Laboratory
  *             Radim Blazek, ITC-Irst, Trento, Italy
+ *             Martin Landa, CTU in Prague, Czech Republic (v.out.ascii.db merged)
  *
  * PURPOSE:    v.out.ascii: writes GRASS vector data as ASCII files
  * COPYRIGHT:  (C) 2000-2008 by the GRASS Development Team
@@ -29,9 +30,10 @@
 int main(int argc, char *argv[])
 {
     FILE *ascii, *att;
-    struct Option *input, *output, *format_opt, *dp_opt, *delim_opt;
+    struct Option *input, *output, *format_opt, *dp_opt, *delim_opt,
+	*field_opt, *column_opt, *where_opt;
     struct Flag *verf, *region_flag;
-    int format, dp;
+    int format, dp, field;
     char *fs;
     struct Map_info Map;
     int ver = 5, pnt = 0;
@@ -40,18 +42,14 @@
     G_gisinit(argv[0]);
 
     module = G_define_module();
-    module->keywords = _("vector");
+    module->keywords = _("vector, export");
     module->description =
 	_("Converts a GRASS binary vector map to a GRASS ASCII vector map.");
 
     input = G_define_standard_option(G_OPT_V_INPUT);
 
-    output = G_define_option();
-    output->key = "output";
-    output->type = TYPE_STRING;
+    output = G_define_standard_option(G_OPT_F_OUTPUT);
     output->required = NO;
-    output->multiple = NO;
-    output->gisprompt = "new_file,file,output";
     output->description =
 	_("Path to resulting ASCII file or ASCII vector name if '-o' is defined");
 
@@ -64,22 +62,30 @@
     format_opt->answer = "point";
     format_opt->description = _("Output format");
 
-    delim_opt = G_define_option();
-    delim_opt->key = "fs";
-    delim_opt->type = TYPE_STRING;
-    delim_opt->required = NO;
+    delim_opt = G_define_standard_option(G_OPT_F_SEP);
     delim_opt->description = _("Field separator (points mode)");
-    delim_opt->answer = "|";
+    delim_opt->guisection = _("Points");
 
     dp_opt = G_define_option();
     dp_opt->key = "dp";
     dp_opt->type = TYPE_INTEGER;
     dp_opt->required = NO;
     dp_opt->options = "0-32";
-    dp_opt->answer = "8";	/*This value is taken from the lib settings in G_format_easting() */
+    dp_opt->answer = "8";	/* This value is taken from the lib settings in G_format_easting() */
     dp_opt->description =
 	_("Number of significant digits (floating point only)");
+    dp_opt->guisection = _("Points");
 
+    field_opt = G_define_standard_option(G_OPT_V_FIELD);
+    field_opt->guisection = _("Selection");
+
+    column_opt = G_define_standard_option(G_OPT_COLUMNS);
+    column_opt->description = _("Name of attribute column(s) to be exported (point mode)");
+    column_opt->guisection = _("Points");
+    
+    where_opt = G_define_standard_option(G_OPT_WHERE);
+    where_opt->guisection = _("Selection");
+
     verf = G_define_flag();
     verf->key = 'o';
     verf->description = _("Create old (version 4) ASCII file");
@@ -88,15 +94,23 @@
     region_flag->key = 'r';
     region_flag->description =
 	_("Only export points falling within current 3D region (points mode)");
+    region_flag->guisection = _("Points");
 
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
+    field = atoi(field_opt->answer);
+        
     if (format_opt->answer[0] == 'p')
 	format = FORMAT_POINT;
     else
 	format = FORMAT_ALL;
 
+    if (format == FORMAT_ALL) {
+	G_warning(_("Parameter '%s' ignored in standard mode"),
+		  column_opt->key);
+    }
+
     if (verf->answer)
 	ver = 4;
 
@@ -127,9 +141,10 @@
 
 
     Vect_set_open_level(1);	/* only need level I */
-    Vect_open_old(&Map, input->answer, "");
-
-
+    if (Vect_open_old(&Map, input->answer, "") < 0)
+	G_fatal_error(_("Unable to open vector map <%s>"),
+		      input->answer);
+    
     if (output->answer) {
 	if (ver == 4) {
 	    ascii = G_fopen_new("dig_ascii", output->answer);
@@ -161,12 +176,14 @@
 	    G_fatal_error(_("dig_att file already exist"));
 
 	if ((att = G_fopen_new("dig_att", output->answer)) == NULL)
-	    G_fatal_error(_("Unable to open dig_att file <%s>\n"),
+	    G_fatal_error(_("Unable to open dig_att file <%s>"),
 			  output->answer);
     }
 
-    bin_to_asc(ascii, att, &Map, ver, format, dp, fs, region_flag->answer);
-
+    bin_to_asc(ascii, att, &Map, ver, format, dp, fs,
+	       region_flag->answer, field, where_opt->answer,
+	       column_opt->answers);
+    
     if (ascii != NULL)
 	fclose(ascii);
     if (att != NULL)



More information about the grass-commit mailing list