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

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Aug 12 16:05:22 EDT 2011


Author: martinl
Date: 2011-08-12 13:05:22 -0700 (Fri, 12 Aug 2011)
New Revision: 47603

Added:
   grass/trunk/vector/v.colors/write_rgb.c
Modified:
   grass/trunk/vector/v.colors/local_proto.h
   grass/trunk/vector/v.colors/main.c
Log:
v.colors: write rgb values if rgbcolumn is defined


Modified: grass/trunk/vector/v.colors/local_proto.h
===================================================================
--- grass/trunk/vector/v.colors/local_proto.h	2011-08-12 19:15:44 UTC (rev 47602)
+++ grass/trunk/vector/v.colors/local_proto.h	2011-08-12 20:05:22 UTC (rev 47603)
@@ -4,3 +4,7 @@
 
 /* scan_cats */
 void scan_cats(const struct Map_info *, int, double *, double*);
+
+/* 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	2011-08-12 19:15:44 UTC (rev 47602)
+++ grass/trunk/vector/v.colors/main.c	2011-08-12 20:05:22 UTC (rev 47603)
@@ -42,7 +42,7 @@
     int have_stats, is_fp;
     int overwrite, remove, is_from_stdin, stat, have_colors;
     const char *mapset, *cmapset;
-    const char *name, *style, *rules, *cmap, *attrcolumn;
+    const char *name, *style, *rules, *cmap, *attrcolumn, *rgbcolumn;
 
     struct Map_info Map;
     struct Colors colors, colors_tmp;
@@ -140,6 +140,7 @@
     style = opt.colr->answer;
     rules = opt.rules->answer;
     attrcolumn = opt.attrcol->answer;
+    rgbcolumn = opt.rgbcol->answer;
     have_stats = FALSE;
     
     if (!name)
@@ -294,7 +295,10 @@
         colors = colors_tmp;
     }
 
-    Vect_write_colors(name, mapset, &colors);
+    if (rgbcolumn)
+	write_rgb_values(&Map, layer, rgbcolumn, &colors);
+    else
+	Vect_write_colors(name, mapset, &colors);
 
     G_message(_("Color table for vector map <%s> set to '%s'"), 
 	      G_fully_qualified_name(name, mapset), 

Added: grass/trunk/vector/v.colors/write_rgb.c
===================================================================
--- grass/trunk/vector/v.colors/write_rgb.c	                        (rev 0)
+++ grass/trunk/vector/v.colors/write_rgb.c	2011-08-12 20:05:22 UTC (rev 47603)
@@ -0,0 +1,61 @@
+#include <grass/gis.h>
+#include <grass/vector.h>
+#include <grass/dbmi.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+
+void write_rgb_values(const struct Map_info *Map, int layer, const char *column_name,
+		      struct Colors *colors)
+{
+    int ctype, nrec, i;
+    int red, grn, blu;
+    int *pval;
+    char buf[1024];
+    struct field_info *fi;
+    dbDriver *driver;
+    dbString stmt;
+
+    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);
+
+    ctype = db_column_Ctype(driver, fi->table, column_name);
+    if (ctype == -1)
+	G_fatal_error(_("Column <%s> not found in table <%s>"),
+		      column_name, fi->table);
+    if (ctype != DB_C_TYPE_STRING)
+	G_fatal_error(_("Data type of column <%s> must be char"), column_name);
+	
+    nrec = db_select_int(driver, fi->table, fi->key, NULL, &pval);
+    if (nrec < 1) {
+	G_warning(_("No categories found"));
+	return;
+    }
+    
+    db_init_string(&stmt);
+    db_begin_transaction(driver);
+    
+    for (i = 0; i < nrec; i++) {
+	if (Rast_get_c_color((const CELL *) &(pval[i]), &red, &grn, &blu,
+			     colors) == 0)
+	    G_warning(_("No color value defined for category %d"), pval[i]);
+
+	sprintf(buf, "UPDATE %s SET %s='%d:%d:%d' WHERE %s=%d", fi->table,
+		   column_name, red, grn, blu, fi->key, pval[i]);
+	G_debug(3, "\tSQL: %s", buf);
+
+	db_set_string(&stmt, buf);
+	if (db_execute_immediate(driver, &stmt) != DB_OK)
+	    G_fatal_error(_("Unable to update RGB values"));
+    }
+    
+    db_commit_transaction(driver);
+    
+    db_close_database(driver);
+}


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



More information about the grass-commit mailing list