[GRASS-SVN] r59113 - grass/trunk/vector/v.colors

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Feb 21 09:54:42 PST 2014


Author: martinl
Date: 2014-02-21 09:54:42 -0800 (Fri, 21 Feb 2014)
New Revision: 59113

Modified:
   grass/trunk/vector/v.colors/Makefile
   grass/trunk/vector/v.colors/local_proto.h
   grass/trunk/vector/v.colors/main.c
   grass/trunk/vector/v.colors/scan_attr.c
   grass/trunk/vector/v.colors/v.colors.html
Log:
v.colors: implement color rules built from z-coordinate (points only)
          various minor fixes
          manual updated accordingly


Modified: grass/trunk/vector/v.colors/Makefile
===================================================================
--- grass/trunk/vector/v.colors/Makefile	2014-02-21 17:52:50 UTC (rev 59112)
+++ grass/trunk/vector/v.colors/Makefile	2014-02-21 17:54:42 UTC (rev 59113)
@@ -11,13 +11,6 @@
 
 default: cmd
 
-# Insert thumbnail previews
-v.colors.tmp.html: $(BIN)/v.colors$(EXE) thumbnails
-	$(call htmldesc,$<,$@)
-	sed 's!^<dd><b>\([a-z0-9._]*\)</b>:!<dd><img width="80" height="12" src="Colortable_\1.png"> <b>\1</b>:!' "$@" > "$@.tmp"
-	mv -f "$@.tmp" "$@"
-	$(MAKE) thumbnails
-
 thumbnails: $(BIN)/r.mapcalc$(EXE)
 	-$(call run_grass, $(GRASS_HOME)/tools/thumbnails.py)
 

Modified: grass/trunk/vector/v.colors/local_proto.h
===================================================================
--- grass/trunk/vector/v.colors/local_proto.h	2014-02-21 17:52:50 UTC (rev 59112)
+++ grass/trunk/vector/v.colors/local_proto.h	2014-02-21 17:54:42 UTC (rev 59113)
@@ -1,3 +1,8 @@
+/* use option */
+#define USE_ATTR 1
+#define USE_CAT  2
+#define USE_Z    3
+
 /* make_colors.c */
 void make_colors(struct Colors *, const char *, DCELL, DCELL, int);
 void load_colors(struct Colors *, const char *, DCELL, DCELL, int);
@@ -5,12 +10,16 @@
 /* scan_attr.c */
 int scan_attr(const struct Map_info *, int, const char *, const char *,
 	      const char *, const struct FPRange *, struct Colors *);
-	      
+void color_rules_to_cats(dbCatValArray *, int, struct Colors *, struct Colors *);  
 
 /* scan_cats.c */
 void scan_cats(const struct Map_info *, int, const char *, const char *,
 	       const struct FPRange *, struct Colors *);
 
+/* scan_z.c */
+void scan_z(struct Map_info *, int, const char *, const char *,
+            const struct FPRange *, struct Colors *);
+
 /* write_rgb.c */
 void write_rgb_values(const struct Map_info *, int, const char *,
 		      struct Colors *);

Modified: grass/trunk/vector/v.colors/main.c
===================================================================
--- grass/trunk/vector/v.colors/main.c	2014-02-21 17:52:50 UTC (rev 59112)
+++ grass/trunk/vector/v.colors/main.c	2014-02-21 17:54:42 UTC (rev 59113)
@@ -35,11 +35,11 @@
 
     struct {
 	struct Option *map, *field, *colr, *rast, *volume, *rules,
-	    *attrcol, *rgbcol, *range;
+          *attrcol, *rgbcol, *range, *use;
     } opt;
 
     int layer;
-    int overwrite, remove, is_from_stdin, stat, have_colors, convert;
+    int overwrite, remove, is_from_stdin, stat, have_colors, convert, use;
     const char *mapset, *cmapset;
     const char *style, *rules, *cmap, *attrcolumn, *rgbcolumn;
     char *name;
@@ -61,9 +61,23 @@
 
     opt.field = G_define_standard_option(G_OPT_V_FIELD);
 
+    opt.use = G_define_option();
+    opt.use->key = "use";
+    opt.use->type = TYPE_STRING;
+    opt.use->required = YES;
+    opt.use->multiple = NO;
+    opt.use->options = "attr,cat,z";
+    opt.use->description = _("Source values");
+    G_asprintf((char **) &(opt.use->descriptions),
+	       "attr;%s;cat;%s;z;%s",
+	       _("read values from attribute table (requires <column> option)"),
+	       _("use category values"),
+	       _("use z coordinate (3D points or centroids only)"));
+    opt.use->answer = "cat";
+    
     opt.attrcol = G_define_standard_option(G_OPT_DB_COLUMN);
     opt.attrcol->label = _("Name of column containing numeric data");
-    opt.attrcol->description = _("If not given categories are used");
+    opt.attrcol->description = _("Required for use=attr");
     opt.attrcol->guisection = _("Define");
 
     opt.range = G_define_option();
@@ -161,10 +175,34 @@
     attrcolumn = opt.attrcol->answer;
     rgbcolumn = opt.rgbcol->answer;
     convert = flag.c->answer;
+    use = USE_CAT;
+    if (opt.use->answer) {
+        switch (opt.use->answer[0]) {
+        case 'a':
+            use = USE_ATTR;
+            break;
+        case 'c':
+            use = USE_CAT;
+            break;
+        case 'z':
+            use = USE_Z;
+            break;
+        default:
+            break;
+        }
+    }
+    G_debug(1, "use=%d", use);
     
     if (!name)
         G_fatal_error(_("No vector map specified"));
 
+    if (use == USE_ATTR && !attrcolumn)
+        G_fatal_error(_("Option <%s> required"), opt.attrcol->key); 
+    if (use != USE_ATTR && attrcolumn) {
+        G_important_message(_("Option <%s> given, assuming <use=attr>..."), opt.attrcol->key);
+        use = USE_ATTR;
+    }
+
     if (opt.rast->answer && opt.volume->answer)
         G_fatal_error(_("Options <%s> and <%s> are mutually exclusive"),
 		      opt.rast->key, opt.volume->key);
@@ -212,7 +250,7 @@
     }
 
     G_suppress_warnings(TRUE);
-    have_colors = Vect_read_colors(name, mapset, &colors);
+    have_colors = Vect_read_colors(name, mapset, NULL);
 
     if (have_colors > 0 && !overwrite) {
         G_fatal_error(_("Color table exists. Exiting."));
@@ -223,7 +261,10 @@
     /* open map and get min/max values */
     Vect_set_open_level(1); /* no topology required */
     Vect_open_old2(&Map, name, mapset, opt.field->answer);
-
+    Vect_set_error_handler_io(&Map, NULL);
+    if (use == USE_Z && !Vect_is_3d(&Map))
+        G_fatal_error(_("Vector map <%s> is not 3D"), Vect_get_full_name(&Map));
+    
     layer = Vect_get_field_number(&Map, opt.field->answer);
     if (layer < 1)
 	G_fatal_error(_("Layer <%s> not found"), opt.field->answer);
@@ -236,6 +277,7 @@
 			  opt.range->key);
     }
 
+    Rast_init_colors(&colors);
     if (is_from_stdin) {
 	/*
         if (!read_color_rules(stdin, &colors, min, max, fp))
@@ -245,12 +287,17 @@
 	if (style && !G_find_color_rule(style))
 	    G_fatal_error(_("Color table <%s> not found"), style);
 	
-	if (!attrcolumn) {
+	if (use == USE_CAT) {
 	    scan_cats(&Map, layer, style, rules,
 		      opt.range->answer ? &range : NULL,
 		      &colors);
-	}
-	else {
+        }
+        else if (use == USE_Z) {
+	    scan_z(&Map, layer, style, rules,
+		      opt.range->answer ? &range : NULL,
+		      &colors);
+        }
+        else {
 	    scan_attr(&Map, layer, attrcolumn, style, rules,
 		      opt.range->answer ? &range : NULL,
 		      &colors);

Modified: grass/trunk/vector/v.colors/scan_attr.c
===================================================================
--- grass/trunk/vector/v.colors/scan_attr.c	2014-02-21 17:52:50 UTC (rev 59112)
+++ grass/trunk/vector/v.colors/scan_attr.c	2014-02-21 17:54:42 UTC (rev 59113)
@@ -9,14 +9,12 @@
 	      const char *style, const char *rules,
 	      const struct FPRange *range, struct Colors *colors)
 {
-    int ctype, is_fp, nrec, i, cat;
-    int red, grn, blu;
+    int ctype, is_fp, nrec;
     double fmin, fmax;
     
     struct field_info *fi;
     struct Colors vcolors;
     dbDriver *driver;
-    dbCatVal *cv;
     dbCatValArray cvarr;
 
     Rast_init_colors(colors);
@@ -91,12 +89,27 @@
 	load_colors(&vcolors, rules, (DCELL) fmin, (DCELL) fmax, is_fp);
 
     /* color table for categories */
-    for (i = 0; i < cvarr.n_values; i++) {
-	cv = &(cvarr.value[i]);
+    color_rules_to_cats(&cvarr, is_fp, &vcolors, colors);
+
+    db_close_database(driver);
+
+    return is_fp;
+}
+
+void color_rules_to_cats(dbCatValArray *cvarr, int is_fp,
+                         struct Colors *vcolors, struct Colors *colors)
+{
+    int i, cat;
+    dbCatVal *cv;
+    int red, grn, blu;
+
+    /* color table for categories */
+    for (i = 0; i < cvarr->n_values; i++) {
+	cv = &(cvarr->value[i]);
 	cat = cv->cat;
 	if (is_fp) {
 	    if (Rast_get_d_color((const DCELL *) &(cv->val.d), &red, &grn, &blu,
-				 &vcolors) == 0) {
+				 vcolors) == 0) {
 		/* G_warning(_("No color rule defined for value %f"), cv->val.d); */
 		G_debug(3, "scan_attr(): cat=%d, val=%f -> no color rule", cat, cv->val.d);
 		continue;
@@ -104,7 +117,7 @@
 	}
 	else {
 	    if (Rast_get_c_color((const CELL *) &(cv->val.i), &red, &grn, &blu,
-				 &vcolors) == 0) {
+				 vcolors) == 0) {
 		/* G_warning(_("No color rule defined for value %d"), cv->val.i); */
 		G_debug(3, "scan_attr(): cat=%d, val=%d -> no color rule", cat, cv->val.i);
 		continue;
@@ -115,8 +128,4 @@
 	Rast_add_c_color_rule((const CELL*) &cat, red, grn, blu,
 			      (const CELL*) &cat, red, grn, blu, colors);
     }
-    
-    db_close_database(driver);
-
-    return is_fp;
 }

Modified: grass/trunk/vector/v.colors/v.colors.html
===================================================================
--- grass/trunk/vector/v.colors/v.colors.html	2014-02-21 17:52:50 UTC (rev 59112)
+++ grass/trunk/vector/v.colors/v.colors.html	2014-02-21 17:54:42 UTC (rev 59113)
@@ -1,50 +1,45 @@
 <h2>DESCRIPTION</h2>
 
-<em>v.colors</em> allows the user to create and/or modify the color
-table for a vector map. The vector map (specified on the command line
-by <b>map</b>) must exist in the user's current mapset search path.
-<p>As input a vector map with a numeric data column is required which is
-used to apply the color rules. The color values are stored in a string
-column (default name "GRASSRGB", varchar(11)) by saving the RRR:GGG:BBB
-values suitable for use with '<tt>d.vect -a</tt>'. If this RGB column
-is not found in the attribute table, it is automatically created.
-<p>The <b>rast</b> option allows user to specify a raster map <i>name</i>
-from which to copy the color map.
+<em>v.colors</em> allows to create or modify color table associated
+with a vector map similarly
+to <em><a href="r.colors.html">r.colors</a></em> for raster maps.
 
-<p>The <b>volume</b> option allows user to specify a volume (3D raster)
-map <i>name</i> from which to copy the color map.
+<p>Color rules are built from features category values
+(<b>use=cat</b>) or numeric data column (<b>use=attr</b>) defined
+by <b>column</b> option. For 3D vector maps is allowed to define color
+rules based on points or centroids z-coordinate (<b>use=z</b>). 3D
+vector lines are not supported.
 
-<p>If the user specifies the <b>-w</b> flag, the current color table file
-for the input map will not be overwritten. This means that the color
-table is created only if the <i>map</i> does not already have a color
-table. If this option is not specified, the color table will be
-created if one does not exist, or modified if it does.
+<p>The <b>rast</b> option allows user to specify a raster map from
+which to copy the color table, similarly <b>volume</b> option for 3D
+raster map.
 
-<p>In general, tables which associate colors with percentages (aspect,
-bcyr, byg, byr, elevation, grey, gyr, rainbow, ramp, ryb, ryg and
-wave) can be applied to any data, while those which use absolute
-values (aspectcolr, curvature, etopo2, evi, ndvi, population, slope,
-srtm, and terrain) only make sense for data with certain ranges.
+<p>The <b>rules</b> color table type will cause <em>v.colors</em> to
+read color table specifications from given file and will build the
+color table accordingly. See
+<em><a href="r.colors.html">r.colors</a></em> manual page for details.
 
-One can get a rough idea of the applicability of a colour table by
-reading the corresponding rules file
-(<tt>$GISBASE/etc/colors/<name></tt>). For example
-the <em>ryg</em> rule is defined as:
+<p>If the user specifies the <b>-w</b> flag, the current color table
+file for the input map will not be overwritten. This means that the
+color table is created only if the vector map does not already have a
+color table. If this option is not specified, the color table will be
+created if one does not exist, or modified if it does.
 
-<div class="code"><pre>
-0% red
-50% yellow
-100% green
-</pre></div>
+<p>Alternatively the color rules can be stored in a string column
+(<b>rgb_column</b>) by saving the RRR:GGG:BBB values suitable for use
+with <em><a href="d.vect.html">d.vect</a></em>.
 
-<p>The <b>rules</b> color table type will cause <em>r.colors</em> to read
-color table specifications from standard input and will build
-the color table accordingly.
+<h2>NOTES</h2>
 
-<p>For details see <em><a href="r.colors.html">r.colors</a></em> manual page.
+For vector maps with a large number of features it's more convenient
+to store color rules in an attribute column (given by <b>rgb_column</b>)
+rather then in a color table file. Reading color tables with more then 1000
+items is slow.
 
 <h2>EXAMPLES</h2>
 
+<h3>Define color table</h3>
+
 Define color table <tt>wave</tt> based on categories from layer 1
 
 <div class="code"><pre>
@@ -56,14 +51,14 @@
 
 <div class="code"><pre>
 v.to.db map=soils_general layer=1 option=area column=AREA
-v.colors map=soils_general layer=1 color=wave column=AREA
+v.colors map=soils_general layer=1 color=wave use=attr column=AREA
 </pre></div>
 
 Write color values to the attribute table (column <tt>GRASSRGB</tt>)
 instead of creating color table.
 
 <div class="code"><pre>
-v.colors map=soils_general layer=1 color=wave column=AREA rgb_column=GRASSRGB
+v.colors map=soils_general layer=1 color=wave use=attr column=AREA rgb_column=GRASSRGB
 
 # See some GRASSRGB values:
 v.db.select map=soils_general | head -n 4
@@ -73,6 +68,8 @@
 3|3|0|87572.882812|4|3|NC097|212:42:127
 </pre></div>
 
+<h3>Convert color table into RGB values</h3>
+
 Convert existing RGB values to color table rules.
 
 <div class="code"><pre>
@@ -85,6 +82,8 @@
 with GRASSRGB attribute column. Also note that color table is prefered
 over RGB values stored in attribute table.
 
+<h3>Remove existing color table</h3>
+
 <p>
 Existing color table can be removed by <b>-r</b> flag.
 



More information about the grass-commit mailing list