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

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Aug 16 14:22:25 EDT 2011


Author: martinl
Date: 2011-08-16 11:22:25 -0700 (Tue, 16 Aug 2011)
New Revision: 47681

Added:
   grass/trunk/vector/v.colors.out/make_colors.c
Modified:
   grass/trunk/vector/v.colors.out/Makefile
   grass/trunk/vector/v.colors.out/local_proto.h
   grass/trunk/vector/v.colors.out/main.c
   grass/trunk/vector/v.colors.out/scan_cats.c
Log:
v.colors.out: optionally refer color table to the given attribute column


Modified: grass/trunk/vector/v.colors.out/Makefile
===================================================================
--- grass/trunk/vector/v.colors.out/Makefile	2011-08-16 16:58:53 UTC (rev 47680)
+++ grass/trunk/vector/v.colors.out/Makefile	2011-08-16 18:22:25 UTC (rev 47681)
@@ -2,8 +2,8 @@
 
 PGM = v.colors.out
 
-LIBES = $(VECTORLIB) $(GISLIB) $(RASTERLIB)
-DEPENDENCIES = $(VECTORDEP) $(GISDEP) $(RASTERDEP)
+LIBES = $(VECTORLIB) $(GISLIB) $(RASTERLIB) $(DBMILIB)
+DEPENDENCIES = $(VECTORDEP) $(GISDEP) $(RASTERDEP) $(DBMIDEP)
 EXTRA_INC = $(VECT_INC)
 EXTRA_CFLAGS = $(VECT_CFLAGS)
 

Modified: grass/trunk/vector/v.colors.out/local_proto.h
===================================================================
--- grass/trunk/vector/v.colors.out/local_proto.h	2011-08-16 16:58:53 UTC (rev 47680)
+++ grass/trunk/vector/v.colors.out/local_proto.h	2011-08-16 18:22:25 UTC (rev 47681)
@@ -1,2 +1,6 @@
+/* make_colors */
+struct Colors *make_colors(const char *, const char *, const char *,
+			   struct Colors *);
+
 /* scan_cats.c */
 void scan_cats(const char *, const char *, int *, int *);

Modified: grass/trunk/vector/v.colors.out/main.c
===================================================================
--- grass/trunk/vector/v.colors.out/main.c	2011-08-16 16:58:53 UTC (rev 47680)
+++ grass/trunk/vector/v.colors.out/main.c	2011-08-16 18:22:25 UTC (rev 47681)
@@ -29,16 +29,16 @@
     struct GModule *module;
     struct
     {
-	struct Option *map, *field, *file;
+	struct Option *map, *field, *file, *col;
     } opt;
     struct
     {
 	struct Flag *p;
     } flag;
-    struct Colors colors;
+    struct Colors cat_colors, *colors;
     
     int min, max;
-    const char *file, *name, *layer;
+    const char *file, *name, *layer, *column;
     FILE *fp;
     
     G_gisinit(argv[0]);
@@ -60,9 +60,15 @@
     opt.file->description = _("\"-\" to write to stdout");
     opt.file->answer = "-";
     
+    opt.col = G_define_standard_option(G_OPT_DB_COLUMN);
+    opt.col->label = _("Name of attribute (numeric) column to which refer color rules");
+    opt.col->description = _("If not given color rules are refered to categories");
+    opt.col->guisection = _("Settings");
+
     flag.p = G_define_flag();
     flag.p->key = 'p';
     flag.p->description = _("Output values as percentages");
+    flag.p->guisection = _("Settings");
 
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
@@ -70,8 +76,9 @@
     name = opt.map->answer;
     layer = opt.field->answer;
     file = opt.file->answer;
+    column = opt.col->answer;
     
-    if (Vect_read_colors(name, "", &colors) < 0)
+    if (Vect_read_colors(name, "", &cat_colors) < 0)
 	G_fatal_error(_("Unable to read color table for vector map <%s>"),
 		      opt.map->answer);
     
@@ -87,8 +94,13 @@
 	if (!fp)
 	    G_fatal_error(_("Unable to open output file <%s>"), file);
     }
+
+    if (column)
+	colors = make_colors(name, layer, column, &cat_colors);
+    else
+	colors = &cat_colors;
     
-    Rast_print_colors(&colors, (DCELL) min, (DCELL) max, fp,
+    Rast_print_colors(colors, (DCELL) min, (DCELL) max, fp,
 		      flag.p->answer ? 1 : 0);
     
     exit(EXIT_SUCCESS);

Added: grass/trunk/vector/v.colors.out/make_colors.c
===================================================================
--- grass/trunk/vector/v.colors.out/make_colors.c	                        (rev 0)
+++ grass/trunk/vector/v.colors.out/make_colors.c	2011-08-16 18:22:25 UTC (rev 47681)
@@ -0,0 +1,78 @@
+#include <grass/gis.h>
+#include <grass/vector.h>
+#include <grass/raster.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+struct Colors *make_colors(const char *name, const char *layer,
+			   const char *column, struct Colors *cat_colors)
+{
+    int field, is_fp, ctype, nrec, cat, i;
+    int red, grn, blu;
+    
+    struct Map_info Map;
+    struct field_info *fi;
+    struct Colors *colors;
+    
+    dbDriver *driver;
+    dbCatValArray cvarr;
+    dbCatVal *cv;
+    
+    Vect_set_open_level(1); /* no topology required */
+    Vect_open_old2(&Map, name, "", layer);
+
+    field = Vect_get_field_number(&Map, layer);
+    if (field < 1)
+	G_fatal_error(_("Layer <%s> not found"), layer);
+
+    fi = Vect_get_field(&Map, field);
+    if (!fi)
+	G_fatal_error(_("Database connection not defined for layer <%s>"),
+		      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);
+    if (ctype == -1)
+	G_fatal_error(_("Column <%s> not found in table <%s>"),
+		      column, fi->table);
+    if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE)
+	G_fatal_error(_("Column <%s> is not numeric"), column);
+
+    is_fp = ctype == DB_C_TYPE_DOUBLE;
+
+    nrec = db_select_CatValArray(driver, fi->table, fi->key, column,
+				 NULL, &cvarr);
+    if (nrec < 1) {
+	G_important_message(_("No data selected"));
+	return 0;
+    }
+    
+    colors = (struct Colors *) G_malloc(sizeof(struct Colors));
+    Rast_init_colors(colors);
+
+    for (i = 0; i < cvarr.n_values; i++) {
+	cv = &(cvarr.value[i]);
+	cat = cv->cat;
+	if (Rast_get_c_color((const CELL *) &cat, &red, &grn, &blu,
+			     cat_colors) == 0)
+	    continue;
+	
+	if (is_fp)
+	    Rast_add_d_color_rule((const DCELL*) &(cv->val.d), red, grn, blu,
+				  (const DCELL*) &(cv->val.d), red, grn, blu,
+				  colors);
+	else
+	    Rast_add_c_color_rule((const CELL*) &(cv->val.i), red, grn, blu,
+				  (const CELL*) &(cv->val.i), red, grn, blu,
+				  colors);
+	
+    }
+    
+    Vect_close(&Map);
+    
+    return colors;
+}


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

Modified: grass/trunk/vector/v.colors.out/scan_cats.c
===================================================================
--- grass/trunk/vector/v.colors.out/scan_cats.c	2011-08-16 16:58:53 UTC (rev 47680)
+++ grass/trunk/vector/v.colors.out/scan_cats.c	2011-08-16 18:22:25 UTC (rev 47681)
@@ -15,6 +15,7 @@
 
     *cmin = *cmax = -1;
 
+    Vect_set_open_level(1); /* no topology required */
     Vect_open_old2(&Map, name, "", layer);
     ilayer = Vect_get_field_number(&Map, layer);
     if (ilayer < 1)
@@ -39,6 +40,8 @@
     }
 
     Vect_destroy_cats_struct(Cats);
+
+    Vect_close(&Map);
 }
 
 void scan_layer(int field, const struct line_cats *Cats, int *cmin, int *cmax)



More information about the grass-commit mailing list