[GRASS-dev] nviz colored vectors
Radek Bartoň
blackhex at post.cz
Tue May 30 09:56:16 EDT 2006
> Hamish wrote:
> > > Here is colored vector patch for nviz. It uses grassrgb attribute to
> > > get line colors. Displaying of line vectors should work, area vectors
> > > aren't implemented.
> >
> > Hi Radek,
> >
> > thanks for the patch, can you please re-send it as a "unidiff"?
> >
> > cvs diff -u grass6/lib/ogsf/
>
> Better still, add "diff -u" to your ~/.cvsrc file.
>
> Nobody uses diff's default output format; most people use either the
> context (-c) or unified (-u) formats.
OK, here is new diff.
--
الظفر بالضعيف هزيمة
-------------- next part --------------
Index: include/gstypes.h
===================================================================
RCS file: /home/grass/grassrepository/grass6/include/gstypes.h,v
retrieving revision 1.1
diff -u -r1.1 gstypes.h
--- include/gstypes.h 5 May 2006 23:28:59 -0000 1.1
+++ include/gstypes.h 30 May 2006 13:53:02 -0000
@@ -159,6 +159,7 @@
int type;
float norm[3];
int dims, npts;
+ int color;
Point3 *p3;
Point2 *p2;
struct g_line *next;
@@ -170,7 +171,7 @@
int drape_surf_id[MAX_SURFS]; /* if you want 'em flat, define the surface */
int flat_val;
int n_surfs;
- int color, width;
+ int color, width; /* Color is deprecated. */
char filename[NAME_SIZ];
float x_trans, y_trans, z_trans;
/* also maybe center & rotate? */
Index: lib/ogsf/Gv3.c
===================================================================
RCS file: /home/grass/grassrepository/grass6/lib/ogsf/Gv3.c,v
retrieving revision 1.9
diff -u -r1.9 Gv3.c
--- lib/ogsf/Gv3.c 9 Feb 2006 03:08:57 -0000 1.9
+++ lib/ogsf/Gv3.c 30 May 2006 13:53:02 -0000
@@ -10,6 +10,9 @@
#include <stdlib.h>
#include <grass/gis.h>
#include <grass/Vect.h>
+#include <grass/dbmi.h>
+#include <grass/display.h>
+#include <grass/glocale.h>
#include <grass/gstypes.h>
@@ -21,15 +24,34 @@
/* This loads to memory.
The other alternative may be to load to a tmp file. */
-geoline *Gv_load_vect(char *grassname, int *nlines)
+ geoline *Gv_load_vect(char *grassname, int *nlines)
{
struct Map_info map;
struct line_pnts *points;
+ struct line_cats *cats;
geoline *top, *gln, *prev;
- int np, i, n, nareas, nl=0, area, type, is3d;
+ int np, i, n, nareas, ncols, nl=0, area, leftarea, rightarea, line, type,
+ cat, is3d;
struct Cell_head wind;
float vect[2][3];
+ /* Variables for field info. */
+ struct Option *fieldopt;
+ struct cat_list *catlist;
+ struct field_info *fieldinfo;
+
+ /* Database variables. */
+ dbDriver *driver;
+ dbString *tablestr;
+ dbTable *table;
+ dbColumn *column;
+ dbValue value;
+
+ /* Color translate variables. */
+ int has_grassrgb;
+ const char * colorstr;
+ int red, green, blue;
+
/* TODO: handle error messages */
Vect_set_open_level (2);
@@ -46,13 +68,50 @@
#endif
points = Vect_new_line_struct ();
+ cats = Vect_new_cats_struct();
G_get_set_window (&wind) ;
Vect_set_constraint_region(&map,wind.north,wind.south,wind.east,wind.west,
PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);
is3d = Vect_is_3d ( &map );
-
+
+ /* Get field and field info */
+ fieldopt = G_define_standard_option(G_OPT_V_FIELD);
+ catlist = Vect_new_cat_list();
+ catlist->field = atoi(fieldopt->answer);
+ if ((fieldinfo = Vect_get_field(&map, catlist->field)) == NULL)
+ {
+ G_fatal_error(_("Database connection not defined"));
+ }
+
+ /* Connect database. */
+ if ((driver = db_start_driver_open_database(fieldinfo->driver,
+ fieldinfo->database)) == NULL)
+ {
+ G_fatal_error(_("Can't open driver %s"), fieldinfo->driver);
+ }
+
+ /* Check if grassrgb attribute is present in table. */
+ db_init_string(tablestr);
+ db_set_string(tablestr, fieldinfo->table);
+ if (db_describe_table(driver, tablestr, &table) != DB_OK)
+ {
+ G_fatal_error(_("Can't get schema of table %s"), fieldinfo->table);
+ }
+ has_grassrgb = 0;
+ ncols = db_get_table_number_of_columns(table);
+ for (n = 0; n < ncols; n++)
+ {
+ column = db_get_table_column(table, n);
+ if (!strcmp(db_get_column_name(column), "grassrgb") ||
+ !strcmp(db_get_column_name(column), "GRASSRGB"))
+ {
+ has_grassrgb = 1;
+ break;
+ }
+ }
+
/* Read areas */
n = Vect_get_num_areas (&map);
nareas = 0;
@@ -125,7 +184,9 @@
/* Read all lines */
G_debug(3, "Reading vector lines ...");
- while (-1 < (type = Vect_read_next_line(&map, points, NULL))) {
+ n = Vect_get_num_lines (&map);
+ for ( line = 1; line <= n; line++ ) {
+ type = Vect_read_line (&map, points, cats, line);
G_debug(3, "line type = %d", type);
if ( type & ( GV_LINES | GV_FACE ) ) {
@@ -181,7 +242,38 @@
GS_v3cross( vect[1], vect[0], gln->norm );
G_debug ( 3, "norm %f %f %f", gln->norm[0], gln->norm[1], gln->norm[2] );
}
-
+
+ if (has_grassrgb)
+ {
+ /* Get line or area category. */
+ if ((cat = Vect_get_line_cat(&map, line, catlist->field)) < 0)
+ {
+ gln->color = 0x00000000;
+ continue;
+ }
+
+ /* Get grassrgb attribute value. */
+ if (db_select_value(driver, fieldinfo->table, fieldinfo->key, cat,
+ "grassrgb", &value) == -1)
+ {
+ G_fatal_error (_("Can't get grassrgb attribute value for line %d"),
+ cat);
+ }
+ colorstr = db_get_value_string(&value);
+ if (!colorstr)
+ {
+ G_fatal_error (_("Color definition column (%s) not a string. "
+ "Column must be of form RRR:GGG:BBB where RGB values range 0-255."),
+ "grassrgb");
+ }
+ G_str_to_color(colorstr, &red, &green, &blue);
+ gln->color = red | (green << 8) | (blue << 16) | 0xFF000000;
+ }
+ else
+ {
+ gln->color = 0x00000000;
+ }
+
if (NULL == (gln->next=(geoline *)malloc (sizeof(geoline)))) {
fprintf(stderr,"Can't malloc.\n"); /* CLEAN UP */
return(NULL);
@@ -206,7 +298,11 @@
#ifdef TRAK_MEM
Tot_mem-=sizeof(geoline);
#endif
-
+
+ if (driver)
+ {
+ db_close_database_shutdown_driver(driver);
+ }
Vect_close (&map);
fprintf(stderr,"Vector file %s loaded.\n",grassname);
Index: lib/ogsf/gvd.c
===================================================================
RCS file: /home/grass/grassrepository/grass6/lib/ogsf/gvd.c,v
retrieving revision 1.6
diff -u -r1.6 gvd.c
--- lib/ogsf/gvd.c 9 Feb 2006 03:08:57 -0000 1.6
+++ lib/ogsf/gvd.c 30 May 2006 13:53:02 -0000
@@ -139,6 +139,12 @@
}
}
+ /* Set line color. */
+ if (gln->color != 0x00000000)
+ {
+ gsd_color_func(gln->color);
+ }
+
if ( gln->type == OGSF_LINE ) { /* line */
if ( gln->dims == 2 ) { /* 2d line */
G_debug(3, "Draw 2D vector line.");
More information about the grass-dev
mailing list