[GRASS-SVN] r35918 - in grass/branches/develbranch_6: include lib/vector/Vlib vector/v.out.ascii

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Feb 18 08:31:10 EST 2009


Author: martinl
Date: 2009-02-18 08:31:10 -0500 (Wed, 18 Feb 2009)
New Revision: 35918

Modified:
   grass/branches/develbranch_6/include/Vect.h
   grass/branches/develbranch_6/lib/vector/Vlib/cats.c
   grass/branches/develbranch_6/vector/v.out.ascii/b2a.c
Log:
vlib: Vect_field_cat_get() added
v.out.ascii: fix trac #495


Modified: grass/branches/develbranch_6/include/Vect.h
===================================================================
--- grass/branches/develbranch_6/include/Vect.h	2009-02-18 05:40:19 UTC (rev 35917)
+++ grass/branches/develbranch_6/include/Vect.h	2009-02-18 13:31:10 UTC (rev 35918)
@@ -58,6 +58,7 @@
 int Vect_cat_get(struct line_cats *, int, int *);
 int Vect_cat_del(struct line_cats *, int);
 int Vect_field_cat_del(struct line_cats *, int, int);
+int Vect_field_cat_get(struct line_cats *, int, struct ilist *);
 int Vect_cat_in_array(int, int *, int);
 int Vect_reset_cats(struct line_cats *);
 int Vect_destroy_cats_struct(struct line_cats *);

Modified: grass/branches/develbranch_6/lib/vector/Vlib/cats.c
===================================================================
--- grass/branches/develbranch_6/lib/vector/Vlib/cats.c	2009-02-18 05:40:19 UTC (rev 35917)
+++ grass/branches/develbranch_6/lib/vector/Vlib/cats.c	2009-02-18 13:31:10 UTC (rev 35918)
@@ -5,7 +5,7 @@
  *
  * Higher level functions for reading/writing/manipulating vectors.
  *
- * (C) 2001-2008 by the GRASS Development Team
+ * (C) 2001-2009 by the GRASS Development Team
  *
  * This program is free software under the 
  * GNU General Public License (>=v2). 
@@ -13,9 +13,9 @@
  * for details.
  *
  * \author Original author CERL, probably Dave Gerdes or Mike
- * Higgins. Update to GRASS 5.7 Radim Blazek and David D. Gray.
- *
- * \date 2001-2008
+ * Higgins
+ * \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
+ * \author Various updates by Martin Landa <landa.martin gmail.com>
  */
 
 #include <stdlib.h>
@@ -48,7 +48,7 @@
 }
 
 /*!
-   \brief Creates and initializes line_cats structure.
+   \brief Creates and initializes line_cats structure (lower level fn)
 
    This structure is used for reading and writing vector cats. The
    library routines handle all memory allocation.
@@ -179,6 +179,37 @@
 }
 
 /*!
+   \brief Get list of categories of given field.
+
+   \param Cats line_cats structure
+   \param field layer number
+   \param[out] cats pointer to list where cats will be written
+
+   \return number of found categories
+   \return -1 on invalid field
+ */
+int Vect_field_cat_get(struct line_cats *Cats, int field, struct ilist *cats)
+{
+    int n;
+    
+    /* reset list of categories */
+    Vect_reset_list(cats);
+
+    /* check input value */
+    if (field < 1 || field > GV_FIELD_MAX)
+	return -1;
+    
+    /* go through cats and find if field exist */
+    for (n = 0; n < Cats->n_cats; n++) {
+	if (Cats->field[n] != field)
+	    continue;
+	Vect_list_append(cats, Cats->cat[n]);
+    }
+
+    return cats->n_values;
+}
+
+/*!
    \brief Delete all categories of given layer
 
    \param[in] Cats line_cats structure

Modified: grass/branches/develbranch_6/vector/v.out.ascii/b2a.c
===================================================================
--- grass/branches/develbranch_6/vector/v.out.ascii/b2a.c	2009-02-18 05:40:19 UTC (rev 35917)
+++ grass/branches/develbranch_6/vector/v.out.ascii/b2a.c	2009-02-18 13:31:10 UTC (rev 35918)
@@ -17,6 +17,7 @@
     struct line_cats *Cats;
     char *xstring = NULL, *ystring = NULL, *zstring = NULL;
     struct Cell_head window;
+    struct ilist *fcats;
 
     /* where */
     struct field_info *Fi;
@@ -61,6 +62,7 @@
     
     Points = Vect_new_line_struct();	/* init line_pnts struct */
     Cats = Vect_new_cats_struct();
+    fcats = Vect_new_list();
 
     proj = Vect_get_proj(Map);
 
@@ -182,17 +184,20 @@
 	    else {
 		fprintf(ascii, "%s%s%s", xstring, fs, ystring);
 	    }
-	    if (Cats->n_cats > 0) {
-		if (Cats->n_cats > 1) {
+
+	    Vect_field_cat_get(Cats, field, fcats);
+	    
+	    if (fcats->n_values > 0) {
+		if (fcats->n_values > 1) {
 		    G_warning(_("Feature has more categories. Only first category (%d) "
-				"is exported."), Cats->cat[0]);
+				"is exported."), fcats->value[0]);
 		}
-		fprintf(ascii, "%s%d", fs, Cats->cat[0]);
+		fprintf(ascii, "%s%d", fs, fcats->value[0]);
 		
 		/* print attributes */
 		if (columns) {
 		    for(i = 0; columns[i]; i++) {
-			if (db_select_value(driver, Fi->table, Fi->key, Cats->cat[0],
+			if (db_select_value(driver, Fi->table, Fi->key, fcats->value[0],
 					    columns[i], &value) < 0)
 			    G_fatal_error(_("Unable to select record from table <%s> (key %s, column %s)"),
 					  Fi->table, Fi->key, columns[i]);



More information about the grass-commit mailing list