Explorar o código

vlib: Vect_field_cat_get() added
v.out.ascii: fix trac https://trac.osgeo.org/grass/ticket/495
(merge from trunk, https://trac.osgeo.org/grass/changeset/35918)


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@35936 15284696-431f-4ddb-bdfa-cd5b030d7da7

Martin Landa %!s(int64=16) %!d(string=hai) anos
pai
achega
34ab9abc21
Modificáronse 3 ficheiros con 47 adicións e 10 borrados
  1. 1 0
      include/Vect.h
  2. 36 5
      lib/vector/Vlib/cats.c
  3. 10 5
      vector/v.out.ascii/b2a.c

+ 1 - 0
include/Vect.h

@@ -58,6 +58,7 @@ int Vect_cat_set(struct line_cats *, int, int);
 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 *);

+ 36 - 5
lib/vector/Vlib/cats.c

@@ -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 @@ struct line_cats *Vect_new_cats_struct()
 }
 
 /*!
-   \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 @@ int Vect_cat_get(struct line_cats *Cats, int field, int *cat)
 }
 
 /*!
+   \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

+ 10 - 5
vector/v.out.ascii/b2a.c

@@ -17,6 +17,7 @@ int bin_to_asc(FILE *ascii,
     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 @@ int bin_to_asc(FILE *ascii,
     
     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 @@ int bin_to_asc(FILE *ascii,
 	    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]);