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

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Aug 13 09:51:13 EDT 2011


Author: martinl
Date: 2011-08-13 06:51:12 -0700 (Sat, 13 Aug 2011)
New Revision: 47607

Modified:
   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/scan_cats.c
Log:
v.colors: fix creating color tables when `column` given


Modified: grass/trunk/vector/v.colors/local_proto.h
===================================================================
--- grass/trunk/vector/v.colors/local_proto.h	2011-08-13 12:58:53 UTC (rev 47606)
+++ grass/trunk/vector/v.colors/local_proto.h	2011-08-13 13:51:12 UTC (rev 47607)
@@ -1,9 +1,10 @@
 /* scan_attr */
-int scan_attr(const struct Map_info *, int, const char *,
-	       double *, double *);
+int scan_attr(const struct Map_info *, int, const char *, const char *,
+	      struct Colors *, int *, int *);
 
 /* scan_cats */
-void scan_cats(const struct Map_info *, int, double *, double*);
+void scan_cats(const struct Map_info *, int, const char *,
+	       struct Colors *, int *, int *);
 
 /* write_rgb.c */
 void write_rgb_values(const struct Map_info *, int, const char *,

Modified: grass/trunk/vector/v.colors/main.c
===================================================================
--- grass/trunk/vector/v.colors/main.c	2011-08-13 12:58:53 UTC (rev 47606)
+++ grass/trunk/vector/v.colors/main.c	2011-08-13 13:51:12 UTC (rev 47607)
@@ -37,13 +37,13 @@
 	struct Option *map, *field, *colr, *rast, *volume, *rules, *attrcol, *rgbcol;
     } opt;
 
-    int layer;
-    double fmin, fmax;
-    int have_stats, is_fp;
+    int layer, cmin, cmax;
+    int have_stats;
     int overwrite, remove, is_from_stdin, stat, have_colors;
     const char *mapset, *cmapset;
-    const char *name, *style, *rules, *cmap, *attrcolumn, *rgbcolumn;
-
+    const char *style, *rules, *cmap, *attrcolumn, *rgbcolumn;
+    char *name;
+    
     struct Map_info Map;
     struct Colors colors, colors_tmp;
     /* struct Cell_stats statf; */
@@ -174,11 +174,10 @@
     if (is_from_stdin)
         rules = NULL;
 
-    /* open map and get min/max values */
-    Vect_open_old2(&Map, name, "", opt.field->answer);
-    name   = Vect_get_name(&Map);
-    mapset = Vect_get_mapset(&Map);
-
+    mapset = G_find_vector(name, "");
+    if (!mapset)
+	G_fatal_error(_("Vector map <%s> not found"), name);
+    
     if (strcmp(mapset, G_mapset()) != 0)
       G_fatal_error(_("Module currently allows to modify only vector maps from the current mapset"));
 
@@ -201,30 +200,32 @@
 
     G_suppress_warnings(FALSE);
 
+    /* open map and get min/max values */
+    Vect_open_old2(&Map, name, mapset, opt.field->answer);
+
     layer = Vect_get_field_number(&Map, opt.field->answer);
     if (layer < 1)
 	G_fatal_error(_("Layer <%s> not found"), opt.field->answer);
     
-    if (!attrcolumn) {
-	is_fp = FALSE;
-	scan_cats(&Map, layer, &fmin, &fmax);
-    }
-    else {
-	is_fp = scan_attr(&Map, layer, attrcolumn, &fmin, &fmax);
-    }
-    
     if (is_from_stdin) {
 	/*
         if (!read_color_rules(stdin, &colors, min, max, fp))
             exit(EXIT_FAILURE);
 	*/
-    } else if (style) {
+    } else if (style) {	
+	if (!G_find_color_rule(style))
+	    G_fatal_error(_("Color table <%s> not found"), style);
+	
+	if (!attrcolumn) {
+	    scan_cats(&Map, layer, style, &colors, &cmin, &cmax);
+	}
+	else {
+	    scan_attr(&Map, layer, attrcolumn, style,
+		      &colors, &cmin, &cmax);
+	}
+	
 	if (strcmp(style, "random") == 0) {
-	    if (is_fp)
-		G_fatal_error(_("Color table 'random' is not supported for "
-				"floating point attributes"));
-	    else
-		Rast_make_random_colors(&colors, (CELL) fmin, (CELL) fmax);
+	    Rast_make_random_colors(&colors, (CELL) cmin, (CELL) cmax);
 	} else if (strcmp(style, "grey.eq") == 0) {
 	    G_fatal_error(_("Color table <%s> not supported"), "grey.eq");
 	    /*
@@ -240,17 +241,10 @@
 	      Rast_make_histogram_log_colors(&colors, &statf, (CELL) min,
 	      (CELL) max);
 	    */
-	} else if (G_find_color_rule(style)) {
-	    if (is_fp)
-		Rast_make_fp_colors(&colors, style, (CELL) fmin, (CELL) fmax);
-	    else
-		Rast_make_colors(&colors, style, (DCELL) fmin, (DCELL) fmax);
 	}
-        else
-            G_fatal_error(_("Unknown color request '%s'"), style);
     }
     else if (rules) {
-	if (!Rast_load_fp_colors(&colors, rules, fmin, fmax))
+	if (!Rast_load_colors(&colors, rules, (CELL) cmin, (CELL) cmax))
 	    G_fatal_error(_("Unable to load rules file <%s>"), rules);
     }
     else {
@@ -300,6 +294,8 @@
     else
 	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 :

Modified: grass/trunk/vector/v.colors/scan_attr.c
===================================================================
--- grass/trunk/vector/v.colors/scan_attr.c	2011-08-13 12:58:53 UTC (rev 47606)
+++ grass/trunk/vector/v.colors/scan_attr.c	2011-08-13 13:51:12 UTC (rev 47607)
@@ -1,25 +1,27 @@
 #include <grass/vector.h>
 #include <grass/dbmi.h>
+#include <grass/raster.h>
 #include <grass/glocale.h>
 
 #include "local_proto.h"
 
 int scan_attr(const struct Map_info *Map, int layer, const char *column_name,
-	      double *fmin, double *fmax)
+	      const char *style, struct Colors *colors, int *cmin, int *cmax)
 {
-    int ctype, is_fp, more, first;
-    double fval;
+    int ctype, is_fp, nrec, i, cat;
+    int red, grn, blu;
+    double fmin, fmax;
     
-    char buf[1024];
     struct field_info *fi;
+    struct Colors vcolors;
     dbDriver *driver;
-    dbTable *table;
-    dbColumn *column;
-    dbString stmt;
-    dbCursor cursor;
-    dbValue *value;
-     
-    *fmin = *fmax = -1;
+    dbCatVal *cv;
+    dbCatValArray cvarr;
+
+    *cmin = *cmax = -1;
+    Rast_init_colors(colors);
+    Rast_init_colors(&vcolors);
+    
     fi = Vect_get_field(Map, layer);
     if (!fi)
 	G_fatal_error(_("Database connection not defined for layer %d"),
@@ -39,45 +41,58 @@
 
     is_fp = ctype == DB_C_TYPE_DOUBLE;
 
-    G_snprintf(buf, 1023, "SELECT %s FROM %s", column_name, fi->table);
-    G_debug(3, "scan_attr() SQL: %s", buf);
-     
-    db_init_string(&stmt);
-    db_append_string(&stmt, buf);
+    nrec = db_select_CatValArray(driver, fi->table, fi->key, column_name,
+				 NULL, &cvarr);
+    if (nrec < 1) {
+	G_important_message(_("No data selected"));
+	return 0;
+    }
     
-    if (db_open_select_cursor(driver, &stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
-	G_fatal_error(_("Unable to select data"));
-    
-    table = db_get_cursor_table(&cursor);
-    column = db_get_table_column(table, 0);
-    value = db_get_column_value(column);
-    
-    /* fetch the data */
-    first = TRUE;
-    while (TRUE) {
-	if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
-	    return -1;
+    /* color table for values */
+    db_CatValArray_sort_by_value(&cvarr);
+    if (is_fp) {
+	fmin = cvarr.value[0].val.d;
+	fmax = cvarr.value[cvarr.n_values-1].val.d;
+	Rast_make_fp_colors(&vcolors, style, (DCELL) fmin, (DCELL) fmax);
+    }
+    else {
+	fmin = cvarr.value[0].val.i;
+	fmax = cvarr.value[cvarr.n_values-1].val.i;
+	Rast_make_colors(&vcolors, style, (CELL) fmin, (CELL) fmax);
+    }
 
-	if (!more)
-	    break;
+    /* 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) {
+		G_warning(_("No color rule defined for value %f"), cv->val.d);
+		continue;
+	    }
+	}
+	else {
+	    if (Rast_get_c_color((const CELL *) &(cv->val.i), &red, &grn, &blu,
+				 &vcolors) == 0) {
+		G_warning(_("No color rule defined for value %d"), cv->val.i);
+		continue;
+	    }
+	}
+	Rast_add_c_color_rule((const CELL*) &cat, red, grn, blu,
+			      (const CELL*) &cat, red, grn, blu, colors);
 
-	if (is_fp)
-	    fval = db_get_value_double(value);
-	else
-	    fval = db_get_value_int(value);
-
-	if (first) {
-	    *fmin = *fmax = fval;
-	    first = FALSE;
-	    continue;
+	if (i == 0) {
+	    *cmin = *cmax = cat;
 	}
-
-	if (fval <= *fmin)
-	    *fmin = fval;
-	if (fval >= *fmax)
-	    *fmax = fval;
+	else {
+	    if (cat <= *cmin)
+		*cmin = cat;
+	    if (cat >= *cmax)
+		*cmax = cat;
+	}
     }
-
+	
     db_close_database(driver);
 
     return is_fp;

Modified: grass/trunk/vector/v.colors/scan_cats.c
===================================================================
--- grass/trunk/vector/v.colors/scan_cats.c	2011-08-13 12:58:53 UTC (rev 47606)
+++ grass/trunk/vector/v.colors/scan_cats.c	2011-08-13 13:51:12 UTC (rev 47607)
@@ -1,17 +1,18 @@
 #include <grass/vector.h>
+#include <grass/raster.h>
 #include <grass/glocale.h>
 
 #include "local_proto.h"
 
 static void scan_layer(int, const struct line_cats *, int *, int *);
 
-void scan_cats(const struct Map_info *Map, int field, double *min, double* max)
+void scan_cats(const struct Map_info *Map, int field, const char *style,
+	       struct Colors *colors, int *cmin, int *cmax)
 {
-    int ltype, cmin, cmax;
+    int ltype, lmin, lmax;
     struct line_cats *Cats;
 
-    *min = *max = -1;
-    
+    *cmin = *cmax = -1;
     Cats = Vect_new_cats_struct();
 
     while(TRUE) {
@@ -21,14 +22,16 @@
 	if (ltype == -2)
 	    break; /* EOF */
 
-	scan_layer(field, Cats, &cmin, &cmax);
+	scan_layer(field, Cats, &lmin, &lmax);
 
-	if (*min == -1 || cmin <= *min)
-	    *min = cmin;
-	if (*max == -1 || cmax >= *max)
-	    *max = cmax;
+	if (*cmin == -1 || lmin <= *cmin)
+	    *cmin = lmin;
+	if (*cmax == -1 || lmax >= *cmax)
+	    *cmax = lmax;
     }
 
+    Rast_make_colors(colors, style, (CELL) *cmin, (CELL) *cmax);
+
     Vect_destroy_cats_struct(Cats);
 }
 



More information about the grass-commit mailing list