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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Nov 14 05:46:47 EST 2011


Author: martinl
Date: 2011-11-14 02:46:47 -0800 (Mon, 14 Nov 2011)
New Revision: 49240

Added:
   grass/trunk/vector/v.colors/read_rgb.c
Modified:
   grass/trunk/vector/v.colors/local_proto.h
   grass/trunk/vector/v.colors/main.c
   grass/trunk/vector/v.colors/v.colors.html
   grass/trunk/vector/v.colors/write_rgb.c
Log:
v.colors: implement -c flag for converting existing RGB values stored in attribute table to color table


Modified: grass/trunk/vector/v.colors/local_proto.h
===================================================================
--- grass/trunk/vector/v.colors/local_proto.h	2011-11-14 09:29:39 UTC (rev 49239)
+++ grass/trunk/vector/v.colors/local_proto.h	2011-11-14 10:46:47 UTC (rev 49240)
@@ -14,4 +14,6 @@
 /* write_rgb.c */
 void write_rgb_values(const struct Map_info *, int, const char *,
 		      struct Colors *);
-
+/* read_rgb.c */
+void rgb2colr(const struct Map_info *, int, const char *,
+	      struct Colors *);

Modified: grass/trunk/vector/v.colors/main.c
===================================================================
--- grass/trunk/vector/v.colors/main.c	2011-11-14 09:29:39 UTC (rev 49239)
+++ grass/trunk/vector/v.colors/main.c	2011-11-14 10:46:47 UTC (rev 49240)
@@ -29,7 +29,7 @@
 {
     struct GModule *module;
     struct {
-	struct Flag *r, *w, *l, *g, *a, *n;
+	struct Flag *r, *w, *l, *g, *a, *n, *c;
     } flag; 
 
     struct {
@@ -38,7 +38,7 @@
     } opt;
 
     int layer;
-    int overwrite, remove, is_from_stdin, stat, have_colors;
+    int overwrite, remove, is_from_stdin, stat, have_colors, convert;
     const char *mapset, *cmapset;
     const char *style, *rules, *cmap, *attrcolumn, *rgbcolumn;
     char *name;
@@ -133,6 +133,11 @@
     flag.a->description = _("Logarithmic-absolute scaling");
     flag.a->guisection = _("Define");
 
+    flag.c = G_define_flag();
+    flag.c->key = 'c';
+    flag.c->label = _("Convert color rules from RGB values to color table");
+    flag.c->description = _("Option 'rgb_column' with valid RGB values required");
+	
     /* TODO ?
     flag.e = G_define_flag();
     flag.e->key = 'e';
@@ -154,7 +159,8 @@
     rules = opt.rules->answer;
     attrcolumn = opt.attrcol->answer;
     rgbcolumn = opt.rgbcol->answer;
-        
+    convert = flag.c->answer;
+    
     if (!name)
         G_fatal_error(_("No vector map specified"));
 
@@ -168,11 +174,11 @@
     if (opt.volume->answer)
         cmap = opt.volume->answer;
     
-    if (!cmap && !style && !rules && !remove)
-        G_fatal_error(_("One of -%c or options <%s>, <%s> or <%s> "
-			"must be specified"), flag.r->key, opt.colr->key,
-			opt.rast->key, opt.rules->key);
-
+    if (!cmap && !style && !rules && !remove && !convert)
+        G_fatal_error(_("One of -%c, -%c or options <%s>, <%s> or <%s> "
+			"must be specified"), flag.r->key, flag.c->key, 
+		      opt.colr->key, opt.rast->key, opt.rules->key);
+    
     if (!!style + !!cmap + !!rules > 1)
         G_fatal_error(_("Options <%s>, <%s>, and <%s> are mutually "
 			"exclusive"), opt.colr->key, opt.rules->key,
@@ -182,6 +188,10 @@
         G_fatal_error(_("Flags -%c and -%c flags are mutually exclusive"),
 		      flag.g->key, flag.a->key);
 
+    if (flag.c->answer && !rgbcolumn) 
+	G_fatal_error(_("Option <%s> required for flag -%c"),
+		      opt.rgbcol->key, flag.c->key);
+
     is_from_stdin = rules && strcmp(rules, "-") == 0;
     if (is_from_stdin)
         rules = NULL;
@@ -254,7 +264,7 @@
 
             if (Rast_read_colors(cmap, cmapset, &colors) < 0)
                 G_fatal_error(_("Unable to read color table for raster map <%s>"), cmap);
-        } else {
+        } else if (opt.volume->answer) {
             cmapset = G_find_raster3d(cmap, "");
             if (!cmapset)
                 G_fatal_error(_("3D raster map <%s> not found"), cmap);
@@ -286,16 +296,24 @@
     }
 
     G_important_message(_("Writing color rules..."));
-    if (rgbcolumn)
-	write_rgb_values(&Map, layer, rgbcolumn, &colors);
-    else
+    
+    if (style || rules || opt.rast->answer || opt.volume->answer) {
+	if (rgbcolumn)
+	    write_rgb_values(&Map, layer, rgbcolumn, &colors);
+	else
+	    Vect_write_colors(name, mapset, &colors);
+    }
+    
+    if (convert) {
+	/* convert RGB values to color tables */
+	rgb2colr(&Map, layer, rgbcolumn, &colors);
 	Vect_write_colors(name, mapset, &colors);
-
+    }
     Vect_close(&Map);
     
     G_message(_("Color table for vector map <%s> set to '%s'"), 
 	      G_fully_qualified_name(name, mapset), 
-              is_from_stdin ? "rules" : style ? style : rules ? rules :
+              is_from_stdin || convert ? "rules" : style ? style : rules ? rules :
               cmap);
     
     exit(EXIT_SUCCESS);

Added: grass/trunk/vector/v.colors/read_rgb.c
===================================================================
--- grass/trunk/vector/v.colors/read_rgb.c	                        (rev 0)
+++ grass/trunk/vector/v.colors/read_rgb.c	2011-11-14 10:46:47 UTC (rev 49240)
@@ -0,0 +1,59 @@
+#include <grass/gis.h>
+#include <grass/vector.h>
+#include <grass/dbmi.h>
+#include <grass/raster.h>
+#include <grass/colors.h>
+#include <grass/glocale.h>
+
+void rgb2colr(const struct Map_info *Map, int layer, const char *rgb_column,
+	      struct Colors *colors)
+{
+    int i, ret;
+    int red, grn, blu;
+    const char *rgb;
+    
+    struct field_info *fi;
+
+    dbDriver *driver;
+    dbCatValArray cvarr;
+    dbCatVal *cv;
+
+    fi = Vect_get_field(Map, layer);
+    if (!fi)
+	G_fatal_error(_("Database connection not defined for layer %d"),
+		      layer);
+
+    driver = db_start_driver_open_database(fi->driver, fi->database);
+    if (!driver)
+	G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
+		      fi->database, fi->driver);
+
+    if (db_column_Ctype(driver, fi->table, rgb_column) != DB_C_TYPE_STRING)	
+	G_fatal_error(_("Data type of RGB column <%s> must be char"),
+		      rgb_column);
+    
+    if (0 > db_select_CatValArray(driver, fi->table, fi->key,
+				  rgb_column, NULL, &cvarr))
+	G_warning(_("No RGB values found"));
+
+    Rast_init_colors(colors);
+    
+    cv = NULL;
+    for (i = 0; i < cvarr.n_values; i++) {
+	cv = &(cvarr.value[i]);
+	rgb = db_get_string(cv->val.s);
+	G_debug(3, "cat = %d RGB = %s", cv->cat, rgb);
+
+	ret = G_str_to_color(rgb, &red, &grn, &blu);
+	if (ret != 1) {
+	    G_warning(_("Invalid RGB value '%s'"), rgb);
+	    continue;
+	}
+
+	Rast_add_c_color_rule((const CELL*) &(cv->cat), red, grn, blu,
+			      (const CELL*) &(cv->cat), red, grn, blu, colors);
+
+    }
+    
+    db_close_database_shutdown_driver(driver);
+}


Property changes on: grass/trunk/vector/v.colors/read_rgb.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Modified: grass/trunk/vector/v.colors/v.colors.html
===================================================================
--- grass/trunk/vector/v.colors/v.colors.html	2011-11-14 09:29:39 UTC (rev 49239)
+++ grass/trunk/vector/v.colors/v.colors.html	2011-11-14 10:46:47 UTC (rev 49240)
@@ -63,7 +63,6 @@
 instead of creating color table.
 
 <div class="code"><pre>
-v.db.addcolumn map=soils_general column="GRASSRGB varchar(12)"
 v.colors map=soils_general layer=1 color=wave column=AREA rgb_column=GRASSRGB
 
 # See some GRASSRGB values:
@@ -74,6 +73,41 @@
 3|3|0|87572.882812|4|3|NC097|212:42:127
 </pre></div>
 
+Convert existing RGB values to color table rules.
+
+<div class="code"><pre>
+v.colors -c map=soils_general rgb_column=GRASSRGB
+</pre></div>
+
+Note that in this case the vector map has a proper color table
+assigned (check
+by <em><a href="v.colors.out.html">v.colors.out</a></em>) together
+with GRASSRGB attribute column. Also note that color table is prefered
+over RGB values stored in attribute table.
+
+<p>
+Existing color table can be removed by <b>-r</b> flag.
+
+<div class="code"><pre>
+v.colors -r map=soils_general
+</pre></div>
+
+Before removing color table you can store color rules to the file
+by <em><a href="v.colors.out.html">v.colors.out</a></em> and later to
+assign by <b>rules</b> option.
+
+<div class="code"><pre>
+v.colors.out map=soils_general > soils.colr
+v.colors map=soils_general rules=soils.colr
+</pre></div>
+
+To drop RGB column
+use <em><a href="v.db.dropcolumn.html">v.db.dropcolumn</a></em>.
+
+<div class="code"><pre>
+v.db.dropcolumn map=soils_general column=GRASSRGB
+</pre></div>
+
 <h2>SEE ALSO</h2>
 
 <em>

Modified: grass/trunk/vector/v.colors/write_rgb.c
===================================================================
--- grass/trunk/vector/v.colors/write_rgb.c	2011-11-14 09:29:39 UTC (rev 49239)
+++ grass/trunk/vector/v.colors/write_rgb.c	2011-11-14 10:46:47 UTC (rev 49240)
@@ -88,5 +88,5 @@
     
     db_commit_transaction(driver);
     
-    db_close_database(driver);
+    db_close_database_shutdown_driver(driver);
 }



More information about the grass-commit mailing list