[GRASS-SVN] r38509 - grass/trunk/display/d.vect

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jul 23 04:22:46 EDT 2009


Author: hamish
Date: 2009-07-23 04:22:45 -0400 (Thu, 23 Jul 2009)
New Revision: 38509

Modified:
   grass/trunk/display/d.vect/local_proto.h
   grass/trunk/display/d.vect/main.c
   grass/trunk/display/d.vect/plot1.c
Log:
new options for dynamic symbol size and rotation (trac #600; merge from devbr6)

Modified: grass/trunk/display/d.vect/local_proto.h
===================================================================
--- grass/trunk/display/d.vect/local_proto.h	2009-07-23 08:13:53 UTC (rev 38508)
+++ grass/trunk/display/d.vect/local_proto.h	2009-07-23 08:22:45 UTC (rev 38509)
@@ -1,12 +1,12 @@
-#include <grass/symbol.h>
 #include <grass/colors.h>
 #include "plot.h"
 
 FILE *open_vect(char *, char *);
 int close_vect(FILE *);
 int plot1(struct Map_info *, int, int, struct cat_list *,
-	  const struct color_rgb *, const struct color_rgb *, int, SYMBOL *,
-	  int, int, int, int, char *, int, char *, double, int, char *);
+	  const struct color_rgb *, const struct color_rgb *, int, char *,
+	  int, char *, char *, int, int, int, char *, int, char *, double,
+	  int, char *);
 int label(struct Map_info *, int, int, struct cat_list *, LATTR *, int);
 int topo(struct Map_info *, int, int, LATTR *);
 int dir(struct Map_info *, int, struct cat_list *, int);

Modified: grass/trunk/display/d.vect/main.c
===================================================================
--- grass/trunk/display/d.vect/main.c	2009-07-23 08:13:53 UTC (rev 38508)
+++ grass/trunk/display/d.vect/main.c	2009-07-23 08:22:45 UTC (rev 38509)
@@ -22,7 +22,6 @@
 #include <grass/display.h>
 #include <grass/vector.h>
 #include <grass/colors.h>
-#include <grass/symbol.h>
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 #include "plot.h"
@@ -93,7 +92,7 @@
     struct Option *map_opt;
     struct Option *color_opt, *fcolor_opt, *rgbcol_opt, *zcol_opt;
     struct Option *type_opt, *display_opt;
-    struct Option *icon_opt, *size_opt;
+    struct Option *icon_opt, *size_opt, *sizecolumn_opt, *rotcolumn_opt;
     struct Option *where_opt;
     struct Option *field_opt, *cat_opt, *lfield_opt;
     struct Option *lcolor_opt, *bgcolor_opt, *bcolor_opt;
@@ -112,7 +111,6 @@
     struct Cell_head window;
     BOUND_BOX box;
     double overlap;
-    SYMBOL *Symb;
 
     /* Initialize the GIS calls */
     G_gisinit(argv[0]);
@@ -229,6 +227,20 @@
     size_opt->guisection = _("Symbols");
     size_opt->description = _("Symbol size");
 
+    sizecolumn_opt = G_define_standard_option(G_OPT_COLUMN);
+    sizecolumn_opt->key = "size_column";
+    sizecolumn_opt->guisection = _("Symbols");
+    sizecolumn_opt->description =
+	_("Name of numeric column containing symbol size");
+
+    rotcolumn_opt = G_define_standard_option(G_OPT_COLUMN);
+    rotcolumn_opt->key = "rot_column";
+    rotcolumn_opt->guisection = _("Symbols");
+    rotcolumn_opt->label =
+	_("Name of numeric column containing symbol rotation angle");
+    rotcolumn_opt->description =
+	_("Measured in degrees CCW from east");
+
     /* Labels */
     lfield_opt = G_define_standard_option(G_OPT_V_FIELD);
     lfield_opt->key = "llayer";
@@ -423,11 +435,6 @@
     }
 
     size = atoi(size_opt->answer);
-    Symb = S_read(icon_opt->answer);
-    if (Symb == NULL)
-	G_warning(_("Unable to read symbol, unable todisplay points"));
-    else
-	S_stroke(Symb, size, 0.0, 0);
 
     /* if where_opt was specified select categories from db 
      * otherwise parse cat_opt */
@@ -634,7 +641,8 @@
 	    else {
 		stat = plot1(&Map, type, area, Clist,
 			     has_color ? &color : NULL,
-			     has_fcolor ? &fcolor : NULL, chcat, Symb, size,
+			     has_fcolor ? &fcolor : NULL, chcat, icon_opt->answer,
+			     size, sizecolumn_opt->answer, rotcolumn_opt->answer,
 			     (int)id_flag->answer, table_acolors_flag->answer,
 			     cats_acolors_flag->answer, rgbcol_opt->answer,
 			     default_width, wcolumn_opt->answer, width_scale,

Modified: grass/trunk/display/d.vect/plot1.c
===================================================================
--- grass/trunk/display/d.vect/plot1.c	2009-07-23 08:13:53 UTC (rev 38508)
+++ grass/trunk/display/d.vect/plot1.c	2009-07-23 08:22:45 UTC (rev 38509)
@@ -40,10 +40,11 @@
 /* *************************************************************** */
 int plot1(struct Map_info *Map, int type, int area, struct cat_list *Clist,
 	  const struct color_rgb *color, const struct color_rgb *fcolor,
-	  int chcat, SYMBOL * Symb, int size, int id_flag,
-	  int table_colors_flag, int cats_color_flag, char *rgb_column,
-	  int default_width, char *width_column, double width_scale,
-	  int z_color_flag, char *style)
+	  int chcat, char *symbol_name, int size, char *size_column,
+	  char *rot_column, int id_flag, int table_colors_flag,
+	  int cats_color_flag, char *rgb_column, int default_width,
+	  char *width_column, double width_scale, int z_color_flag,
+	  char *style)
 {
     int i, ltype, nlines = 0, line, cat = -1;
     double *x, *y;
@@ -53,9 +54,9 @@
 
     struct field_info *fi = NULL;
     dbDriver *driver = NULL;
-    dbCatValArray cvarr_rgb, cvarr_width;
-    dbCatVal *cv_rgb = NULL, *cv_width = NULL;
-    int nrec_rgb = 0, nrec_width = 0;
+    dbCatValArray cvarr_rgb, cvarr_width, cvarr_size, cvarr_rot;
+    dbCatVal *cv_rgb = NULL, *cv_width = NULL, *cv_size = NULL, *cv_rot = NULL;
+    int nrec_rgb = 0, nrec_width = 0, nrec_size = 0, nrec_rot = 0;
     int nerror_rgb;
     
     int open_db;
@@ -65,9 +66,13 @@
     RGBA_Color *line_color, *fill_color, *primary_color;
     unsigned char which;
     int width;
+    SYMBOL *Symb;
+    double var_size, rotation;
 
+    var_size = (double)size;
+    rotation = 0.0;
     nerror_rgb = 0;
-    
+
     line_color = G_malloc(sizeof(RGBA_Color));
     fill_color = G_malloc(sizeof(RGBA_Color));
     primary_color = G_malloc(sizeof(RGBA_Color));
@@ -98,7 +103,7 @@
     PPoints = Vect_new_line_struct();
     Cats = Vect_new_cats_struct();
 
-    open_db = table_colors_flag || width_column;
+    open_db = table_colors_flag || width_column || size_column || rot_column;
 
     if (open_db) {
 	fi = Vect_get_field(Map, (Clist->field > 0 ? Clist->field : 1));
@@ -156,17 +161,17 @@
 
 	if (cvarr_width.ctype != DB_C_TYPE_INT &&
 	    cvarr_width.ctype != DB_C_TYPE_DOUBLE)
-	    G_fatal_error(_("Line width column (%s) not a number."),
+	    G_fatal_error(_("Line width column (%s) is not numeric."),
 			  width_column);
 
 	if (nrec_width < 0)
 	    G_fatal_error(_("Cannot select data (%s) from table"),
 			  width_column);
 
-	G_debug(2, "\n%d records selected from table", nrec_width);
+	G_debug(2, " %d records selected from table", nrec_width);
 
 	for (i = 0; i < cvarr_width.n_values; i++) {
-	    G_debug(4, "cat = %d  %s = %d", cvarr_width.value[i].cat,
+	    G_debug(4, "(width) cat = %d  %s = %d", cvarr_width.value[i].cat,
 		    width_column,
 		    (cvarr_width.ctype ==
 		     DB_C_TYPE_INT ? cvarr_width.value[i].val.
@@ -174,6 +179,76 @@
 	}
     }
 
+    if (size_column) {
+	if (*size_column == '\0')
+	    G_fatal_error(_("Symbol size column not specified."));
+
+	db_CatValArray_init(&cvarr_size);
+
+	nrec_size = db_select_CatValArray(driver, fi->table, fi->key,
+					   size_column, NULL, &cvarr_size);
+
+	G_debug(3, "nrec_size (%s) = %d", size_column, nrec_size);
+
+	if (cvarr_size.ctype != DB_C_TYPE_INT &&
+	    cvarr_size.ctype != DB_C_TYPE_DOUBLE)
+	    G_fatal_error(_("Symbol size column (%s) is not numeric."),
+			  size_column);
+
+	if (nrec_size < 0)
+	    G_fatal_error(_("Cannot select data (%s) from table"),
+			  size_column);
+
+	G_debug(2, " %d records selected from table", nrec_size);
+
+	for (i = 0; i < cvarr_size.n_values; i++) {
+	    G_debug(4, "(size) cat = %d  %s = %d", cvarr_size.value[i].cat,
+		    size_column,
+		    (cvarr_size.ctype ==
+		     DB_C_TYPE_INT ? cvarr_size.value[i].val.
+		     i : (int)cvarr_size.value[i].val.d));
+	}
+    }
+
+    if (rot_column) {
+	if (*rot_column == '\0')
+	    G_fatal_error(_("Symbol rotation column not specified."));
+
+	db_CatValArray_init(&cvarr_rot);
+
+	nrec_rot = db_select_CatValArray(driver, fi->table, fi->key,
+					   rot_column, NULL, &cvarr_rot);
+
+	G_debug(3, "nrec_rot (%s) = %d", rot_column, nrec_rot);
+
+	if (cvarr_rot.ctype != DB_C_TYPE_INT &&
+	    cvarr_rot.ctype != DB_C_TYPE_DOUBLE)
+	    G_fatal_error(_("Symbol rotation column (%s) is not numeric."),
+			  rot_column);
+
+	if (nrec_rot < 0)
+	    G_fatal_error(_("Cannot select data (%s) from table"),
+			  rot_column);
+
+	G_debug(2, " %d records selected from table", nrec_rot);
+
+	for (i = 0; i < cvarr_rot.n_values; i++) {
+	    G_debug(4, "(rot) cat = %d  %s = %d", cvarr_rot.value[i].cat,
+		    rot_column,
+		    (cvarr_rot.ctype ==
+		     DB_C_TYPE_INT ? cvarr_rot.value[i].val.
+		     i : (int)cvarr_rot.value[i].val.d));
+	}
+    }
+
+    if( !(nrec_size || nrec_rot) ) {
+	Symb = S_read(symbol_name);
+	if (Symb == NULL)
+	    G_warning(_("Unable to read symbol, unable to display points"));
+	else
+	    S_stroke(Symb, size, 0.0, 0);
+    }
+
     if (open_db)
 	db_close_database_shutdown_driver(driver);
 
@@ -353,12 +428,11 @@
 			 (Clist->field > 0 ? Clist->field :
 			  (Cats->n_cats >
 			   0 ? Cats->field[0] : 1)), &cat);
-	    
+
 	    if (cat >= 0) {
 		G_debug(3, "display element %d, cat %d", line, cat);
 
 		/* Read line width from db for current area # */
-
 		if (db_CatValArray_get_value(&cvarr_width, cat, &cv_width) !=
 		    DB_OK) {
 		    width = default_width;
@@ -375,20 +449,20 @@
 			width = default_width;
 		    }
 		}
-	    }			/* end if cat */
+	    }		/* end if cat */
 	    else {
 		width = default_width;
 	    }
 
 	    D_line_width(width);
-	}			/* end if nrec_width */
+	}		/* end if nrec_width */
 
 
 	/* enough of the prep work, lets start plotting stuff */
 	x = Points->x;
 	y = Points->y;
 
-	if ((ltype & GV_POINTS) && Symb != NULL) {
+	if ((ltype & GV_POINTS) && (Symb != NULL || (nrec_size || nrec_rot)) ) {
 	    if (!(color || fcolor || custom_rgb))
 		continue;
 
@@ -401,6 +475,81 @@
 		y0 < D_get_u_south() || y0 > D_get_u_north())
 		continue;
 
+	    /* dynamic symbol size */
+	    if (nrec_size) {
+		/* only first category */
+		Vect_cat_get(Cats,
+			     (Clist->field > 0 ? Clist->field :
+			      (Cats->n_cats > 0 ?
+			       Cats->field[0] : 1)), &cat);
+
+		if (cat >= 0) {
+		    G_debug(3, "display element %d, cat %d", line, cat);
+
+		    /* Read symbol size from db for current symbol # */
+		    if (db_CatValArray_get_value(&cvarr_size, cat, &cv_size) !=
+			DB_OK) {
+			var_size = (double)size;
+		    }
+		    else {
+			var_size =
+			    (cvarr_size.ctype == DB_C_TYPE_INT ?
+			     (double)cv_size->val.i : cv_size->val.d);
+
+			if (var_size < 0.0) {
+			    G_warning(_("Error in symbol size column (%s), element %d "
+					"with cat %d: symbol size [%f]"),
+				      size_column, line, cat, var_size);
+			    var_size = (double)size;
+			}
+		    }
+		}		/* end if cat */
+		else {
+		    var_size = (double)size;
+		}
+	    }		/* end if nrec_size */
+
+	    /* dynamic symbol rotation */
+	    if (nrec_rot) {
+		/* only first category */
+		Vect_cat_get(Cats,
+			     (Clist->field > 0 ? Clist->field :
+			      (Cats->n_cats > 0 ?
+			       Cats->field[0] : 1)), &cat);
+
+		if (cat >= 0) {
+		    G_debug(3, "display element %d, cat %d", line, cat);
+
+		    /* Read symbol rotation from db for current symbol # */
+		    if (db_CatValArray_get_value(&cvarr_rot, cat, &cv_rot) !=
+			DB_OK) {
+			rotation = 0.0;
+		    }
+		    else {
+			rotation =
+			    (cvarr_rot.ctype == DB_C_TYPE_INT ?
+			     (double)cv_rot->val.i : cv_rot->val.d);
+		    }
+		}		/* end if cat */
+		else {
+		    rotation = 0.0;
+		}
+	    }		/* end if nrec_rot */
+
+	    if(nrec_size || nrec_rot) {
+		G_debug(3, ". dynamic symbol: cat=%d  size=%d  rotation=%.2f",
+			cat, (int)(var_size + 0.5), rotation);
+
+		/* symbol stroking is cumulative, so we need to reread it each time */
+		if(Symb) /* unclean free() on first iteration if variables are not init'd to NULL? */
+		    G_free(Symb);
+		Symb = S_read(symbol_name);
+		if (Symb == NULL)
+		    G_warning(_("Unable to read symbol, unable to display points"));
+		else
+		    S_stroke(Symb, (int)(var_size + 0.5), rotation, 0);
+	    }
+
 	    /* use random or RGB column color if given, otherwise reset */
 	    /* centroids always use default color to stand out from underlying area */
 	    if (custom_rgb && (ltype != GV_CENTROID)) {
@@ -412,7 +561,9 @@
 	    else
 		D_symbol(Symb, x0, y0, line_color, fill_color);
 
-
+	    /* reset to defaults */
+	    var_size = (double)size;
+	    rotation = 0.0;
 	}
 	else if (color || custom_rgb || (z_color_flag && Vect_is_3d(Map))) {
 	    if (!table_colors_flag && !cats_color_flag && !z_color_flag)



More information about the grass-commit mailing list