[GRASS-SVN] r29928 - grass/trunk/vector/v.category

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Feb 2 11:49:32 EST 2008


Author: martinl
Date: 2008-02-02 11:49:32 -0500 (Sat, 02 Feb 2008)
New Revision: 29928

Modified:
   grass/trunk/vector/v.category/description.html
   grass/trunk/vector/v.category/main.c
Log:
v.category: Added 'id' parameter to allow non-global operations, e.g. to print categories for given feature id.
Minor code cleaning (Vect_option_to_types, etc.)


Modified: grass/trunk/vector/v.category/description.html
===================================================================
--- grass/trunk/vector/v.category/description.html	2008-02-02 13:33:41 UTC (rev 29927)
+++ grass/trunk/vector/v.category/description.html	2008-02-02 16:49:32 UTC (rev 29928)
@@ -1,30 +1,41 @@
 <h2>DESCRIPTION</h2>
 
-<em>v.category</em> attaches, deletes or reports vector categories to map
-geometry. These categories (IDs) are used to link a vector map to an
-attribute table.
+<em>v.category</em> attaches, deletes or reports vector categories to
+map geometry. These categories (IDs) are used to link geometry
+object(s) to attribute record (from attribute table linked to vector map).
 
 <h2>NOTES</h2>
+
+Use <em><a href="v.to.db.html">v.to.db</a></em> to upload related categories to
+a linked attribute table.
+
 <p>
-Use <em>v.to.db</em> to upload related categories to a linked attribute
-table.
-<p>
 The <b>type</b> parameter specifies the type of geometry objects to which the category is added;
 it is similar to an input filter - only the geometry specified in 'type' is processed.
+
 <p>
-If the <b>type</b> parameter is set to <b>centroid</b> and the <b>option</b> parameter set to <b>add</b>,
-new categories will be added to existing centroids. Note however, that new centroids cannot be created this way. 
-To do so, they must be added manually using v.digit or by running v.category with the type parameter set 
-to area (see below also).
+If the <b>type</b> parameter is set to <b>centroid</b> and
+the <b>option</b> parameter set to <b>add</b>, new categories will be
+added to existing centroids. Note however, that new centroids cannot
+be created this way.  To do so, they must be added manually
+using <em><a href="v.digit.html">v.digit</a></em> or by
+running <em>v.category</em> with the type parameter set to area (see
+below also).
+
 <p>
 Areas are a special case because it is impossible to attach a cat to an area without a centroid;
 in this case, the module places new centroids in areas automatically.
 
+<p>
+The <b>id</b> parameter specifies the list of feature ids to which the
+operation is performed. By default all vector features are processed.
+
 <h2>EXAMPLES</h2>
 
-<em>1) Report vector categories</em><br>
+<h3>Report vector categories</h3>
+
 <div class="code"><pre>
-v.category in=testmap option=report
+v.category input=testmap option=report
 
 LAYER/TABLE 1/testmap:
 type       count        min        max
@@ -35,18 +46,20 @@
 area           0          0          0
 all         1379          1       1379
 </pre></div>
-<P>
 
-<em>2) Delete all vector categories in layer 1</em><br>
+<h3>Delete all vector categories in layer 1</h3>
+
 <div class="code"><pre>
-v.category in=testmap out=outmap option=del
+v.category input=testmap output=outmap option=del
 </pre></div>
-<p>
 
-<em>3) Add vector categories in layer 1 with step=2</em><br>
+<h3>Add vector categories in layer 1 with step=2</h3>
+
 <div class="code"><pre>
-v.category in=outmap out=stepmap option=add step=2
+v.category input=outmap output=stepmap option=add step=2
 
+# report
+v.category input=stepmap option=report
 LAYER/TABLE 1/outmap:
 type       count        min        max
 point          0          0          0
@@ -56,15 +69,24 @@
 area           0          0          0
 all         1379          1       2757
 </pre></div>
-<P>
 
-<em>4) Add categories/centroids to a vector map without categories</em><br>
+<h3>Add categories/centroids to a vector map without categories</h3>
+
 <div class="code"><pre>
-v.category in=wkt out=wktnew option=add
+v.category input=wkt output=wktnew option=add
 </pre></div>
-<P>
-Results can be tested using <em><a HREF="d.what.vect.html">d.what.vect</a></em>. 
 
+Results can be tested
+using <em><a HREF="d.what.vect.html">d.what.vect</a></em>.
+
+<h3>Print vector categories of given layer</h3>
+
+Print vector categories from the first layer, only for feature ids 1-50.
+
+<div class="code"><pre>
+v.category input=roads option=print layer=1 id=1-50
+</pre></div>
+
 <h2>SEE ALSO</h2>
 
 <em>
@@ -74,6 +96,8 @@
 
 <h2>AUTHOR</h2>
 
-Radim Blazek, ITC-Irst, Trento, Italy
+Radim Blazek, ITC-irst, Trento, Italy<br>
+Modified (the id parameter) by Martin Landa, FBK-irst (formely ITC-irst), Trento, Italy, 2008/02
 
-<p><i>Last changed: $Date$</i>
+<p>
+<i>Last changed: $Date$</i>

Modified: grass/trunk/vector/v.category/main.c
===================================================================
--- grass/trunk/vector/v.category/main.c	2008-02-02 13:33:41 UTC (rev 29927)
+++ grass/trunk/vector/v.category/main.c	2008-02-02 16:49:32 UTC (rev 29928)
@@ -6,7 +6,7 @@
  * *               
  * * PURPOSE:      Category manipulations
  * *               
- * * COPYRIGHT:    (C) 2001 by the GRASS Development Team
+ * * COPYRIGHT:    (C) 2001-2008 by the GRASS Development Team
  * *
  * *               This program is free software under the 
  * *               GNU General Public License (>=v2). 
@@ -51,19 +51,20 @@
 	static struct line_pnts *Points;
 	struct line_cats *Cats;
 	struct field_info *Fi;
-        int    i, j, ret, option, otype, type, with_z, step;
+	struct cat_list *Clist;
+        int    i, j, ret, option, otype, type, with_z, step, id;
 	int    n_areas, centr, new_centr;
 	double x, y;
 	int    cat, ocat, *fields, nfields, field;
 	struct GModule *module;
 	struct Option *in_opt, *out_opt, *option_opt, *type_opt;
-	struct Option *cat_opt, *field_opt, *step_opt;
+	struct Option *cat_opt, *field_opt, *step_opt, *id_opt;
 	struct Flag *shell;
 	FREPORT **freps;
 	int nfreps, rtype, fld;
 
 	module = G_define_module();
-	module->keywords = _("vector");
+	module->keywords = _("vector, category");
 	module->description = 
 	    _("Attach, delete or report vector categories to map geometry.");
 
@@ -102,6 +103,12 @@
 	step_opt->answer = "1";
         step_opt->description = _("Category increment");
 
+	id_opt = G_define_standard_option(G_OPT_V_CAT);
+	id_opt->multiple = YES;
+	id_opt->label = _("Feature id(s)");
+	id_opt->description = _("By default all ids are processed");
+	id_opt->key = "id";
+
 	shell = G_define_flag();
         shell->key         = 'g';
         shell->label       = _("Shell script style, currently only for report");
@@ -138,32 +145,21 @@
 
 	cat = atoi( cat_opt->answer );
 	step = atoi( step_opt->answer );
-        
-	i = 0;
-        otype = 0; 
-	while (type_opt->answers[i])
-	  {
-	    switch ( type_opt->answers[i][0] )
-	      {
-	        case 'p':
-	            otype |= GV_POINT;
-		    break;
-	        case 'l':
-	            otype |= GV_LINE;
-		    break;
-	        case 'b':
-	            otype |= GV_BOUNDARY;
-		    break;
-	        case 'c':
-	            otype |= GV_CENTROID;
-		    break;
-	        case 'a':
-	            otype |= GV_AREA;
-		    break;
-	      }
-	    i++;
-	  }
+	otype = Vect_option_to_types(type_opt);
 
+	/* collect ids */
+	if (id_opt->answer) {
+	    Clist = Vect_new_cat_list ();
+	    Clist->field = atoi (field_opt->answer);
+	    ret = Vect_str_to_cat_list (id_opt->answer, Clist);
+	    if (ret > 0) {
+	        G_warning (_("%d errors in id option"), ret);
+	    }
+	}
+	else {
+	    Clist = NULL;
+	}
+
 	/* read fields */
 	i = 0; nfields = 0;
 	while (field_opt->answers[i]) { nfields++; i++; }
@@ -208,20 +204,23 @@
 	    Vect_hist_command ( &Out );
         }
 
+	id = 0;
+
         switch ( option) {	
 	    case (O_ADD):	  
 		/* Lines */
 	        while ( (type = Vect_read_next_line (&In, Points, Cats)) > 0)
 	          {
-	            if ( type & otype )
-	               {
-                         if( (Vect_cat_get (Cats, fields[0], &ocat)) == 0)
-	                   {
-                             Vect_cat_set (Cats, fields[0], cat);
-	                     cat += step;
-	                   }
-	               }	   
-	            Vect_write_line ( &Out, type, Points, Cats );  
+		      id++;
+		      if ( type & otype && (!Clist || 
+					    (Clist && Vect_cat_in_cat_list(id, Clist) == TRUE))) {
+			  if( (Vect_cat_get (Cats, fields[0], &ocat)) == 0)
+			  {
+			      Vect_cat_set (Cats, fields[0], cat);
+			      cat += step;
+			  }
+		      }	   
+		      Vect_write_line ( &Out, type, Points, Cats );
 	          }
 		/* Areas */
 		if ( otype & GV_AREA ) {
@@ -232,7 +231,7 @@
 			if ( centr > 0 ) continue; /* Centroid exists and may be processed as line */
 			ret = Vect_get_point_in_area ( &In, i, &x, &y );
 			if ( ret < 0 ) {
-			    G_warning (_("Cannot calculate area centroid"));
+			    G_warning (_("Unable to calculate area centroid"));
 			    continue;
 			}
 			Vect_reset_line ( Points );
@@ -250,37 +249,43 @@
 	    case (O_DEL):	  
 	        while ( (type = Vect_read_next_line (&In, Points, Cats)) > 0)
 	          {
-	            if ( type & otype )
+		    id++;
+	            if ( type & otype  && (!Clist || 
+					   (Clist && Vect_cat_in_cat_list(id, Clist) == TRUE)))
 	               {
                          ret = Vect_cat_del (Cats, fields[0]);
 	               }	   
-	            Vect_write_line ( &Out, type, Points, Cats );  
+	            Vect_write_line ( &Out, type, Points, Cats );
 	          }
 		break;
 
 	    case (O_CHFIELD):	  
 	        while ( (type = Vect_read_next_line (&In, Points, Cats)) > 0) {
-	            if ( type & otype ) {
+		    id++;
+	            if ( type & otype && (!Clist || 
+					  (Clist && Vect_cat_in_cat_list(id, Clist) == TRUE))) {
 			for ( i = 0 ; i < Cats->n_cats; i++ ) {
 			   if ( Cats->field[i] == fields[0] ) {
 			       Cats->field[i] = fields[1];
 			   }
 			}
 	            }
-	            Vect_write_line ( &Out, type, Points, Cats );  
+	            Vect_write_line ( &Out, type, Points, Cats );
 	        }
 		break;
 		
 	    case (O_SUM):	  
 	        while ( (type = Vect_read_next_line (&In, Points, Cats)) > 0) {
-	            if ( type & otype ) {
+		    id++;
+	            if ( type & otype && (!Clist || 
+					  (Clist && Vect_cat_in_cat_list(id, Clist) == TRUE))) {
 			for ( i = 0 ; i < Cats->n_cats; i++ ) {
 			   if ( Cats->field[i] == fields[0] ) {
 			       Cats->cat[i] += cat;
 			   }
 			}
 	            }	   
-	            Vect_write_line ( &Out, type, Points, Cats );  
+	            Vect_write_line ( &Out, type, Points, Cats );
 	        }
 		break;
 
@@ -289,6 +294,10 @@
 		freps = NULL;
 	        while ( (type = Vect_read_next_line (&In, Points, Cats)) > 0)
 	          {
+		    id++;
+		    if (Clist && Vect_cat_in_cat_list(id, Clist) == FALSE)
+			continue;
+
                     switch (type)
 		      {
                         case (GV_POINT):
@@ -386,7 +395,7 @@
 				     freps[i]->max[FR_ALL]);
 		    } else {
 		        if (freps[i]->table != '\0') {
-			    G_message("%s: %d / %s", _("Layer / table"),
+			    G_message("%s: %d/%s", _("Layer/table"),
 				      freps[i]->field, freps[i]->table);
 		        }
 		        else {
@@ -419,16 +428,20 @@
 					freps[i]->min[FR_ALL],
 					freps[i]->max[FR_ALL]);
 		    }
-
 		  }
 		break;
 		
 	    case (O_PRN):
 	        while ( (type = Vect_read_next_line (&In, Points, Cats)) > 0) {
+		    id++;
 		    int has = 0;
 		    
-	            if ( !(type & otype) ) continue;
-		    
+	            if ( !(type & otype) )
+			continue;
+
+		    if (Clist && Vect_cat_in_cat_list(id, Clist) == FALSE)
+			continue;
+
 		    /* Check if the line has at least one cat */
 		    for (i=0; i < nfields; i++) {
 			for (j=0; j < Cats->n_cats; j++) {
@@ -460,7 +473,12 @@
 	
 	if (option == O_ADD || option == O_DEL || option == O_CHFIELD || option == O_SUM) {
 	    Vect_copy_tables ( &In, &Out, 0 );
-	    Vect_build (&Out, stderr);
+	    if (G_verbose() > G_verbose_min()) {
+		Vect_build (&Out, stderr);
+	    }
+	    else {
+		Vect_build (&Out, NULL);
+	    }
 	    Vect_close (&Out);
 	}
 	Vect_close (&In);



More information about the grass-commit mailing list