File: plot.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (128 lines) | stat: -rw-r--r-- 3,559 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "gis.h"
#include "Vect.h"
#include "dbmi.h"
#include "display.h"
#include "raster.h"
#include "symbol.h"
#include "global.h"

/* Returns 0 - ok , 1 - error */
int 
plot ( int ctype, struct Map_info *Map, int type, int field, 
       char *columns, int ncols, char *sizecol, int size, double scale, 
       COLOR *ocolor, COLOR *colors)
{
    int    ltype, nlines, line, col, more, coltype, nselcols;
    double x, y, csize, len;
    struct line_pnts *Points;
    struct line_cats *Cats;
    int cat;
    double *val;
    char buf[2000];
    struct field_info *Fi;
    dbDriver *driver;
    dbValue *value;
    dbString sql;
    dbCursor cursor;
    dbTable *table;
    dbColumn *column;

    Points = Vect_new_line_struct ();
    Cats = Vect_new_cats_struct ();
    db_init_string (&sql);
    
    Fi = Vect_get_field ( Map, field);
    if ( Fi == NULL )
	G_fatal_error( "No database table defined for selected field <%d>", field );

    /* Open driver */
    driver = db_start_driver_open_database ( Fi->driver, Fi->database );
    if ( driver == NULL ) {
	G_warning ( "Cannot open database %s by driver %s", Fi->database, Fi->driver );
	return 1;
    }

    val = (double *) G_malloc ( (ncols + 1) * sizeof (double ) ); /* + 1 for sizecol */
    
    Vect_rewind ( Map );
    
    nlines = Vect_get_num_lines ( Map );

    for ( line = 1; line <= nlines; line++ ) {
	G_debug ( 3, "line = %d", line );
	ltype =  Vect_read_line (Map, Points, Cats, line);   

        if ( !(ltype & type) ) continue;

	Vect_cat_get ( Cats, field, &cat );
	if ( cat < 0 ) continue;

	/* Select values from DB */
	if ( ctype == CTYPE_PIE && sizecol != NULL ) {
	    sprintf ( buf, "select %s, %s from %s where %s = %d", columns, sizecol,
		                            Fi->table, Fi->key, cat );
	    nselcols = ncols + 1;
	} else {
	    sprintf ( buf, "select %s from %s where %s = %d", columns, Fi->table, Fi->key, cat );
	    nselcols = ncols;
	}
	
	db_set_string ( &sql, buf );
	G_debug ( 3, "SQL: %s", buf );

        if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
	    G_warning ( "Cannot open select cursor: %s", buf );
            return 1;
	}
	
        table = db_get_cursor_table (&cursor);
	if( db_fetch (&cursor, DB_NEXT, &more) != DB_OK || !more ) continue;

        for(col = 0; col < nselcols; col++) {
            column = db_get_table_column(table, col);
	    value  = db_get_column_value(column);
            coltype = db_sqltype_to_Ctype( db_get_column_sqltype (column) );
	    switch ( coltype ) {
		case DB_C_TYPE_INT:
		    val[col] = (double) db_get_value_int( value );
		    break;
		case DB_C_TYPE_DOUBLE:
		    val[col] = db_get_value_double( value );
		    break;
		default:
		    G_warning ( "Column type not supported (must be INT or FLOAT)" );
		    return 1;
	    }
	    G_debug ( 4, "  val[%d]: %f", col, val[col] );
	}

	db_close_cursor(&cursor);
	
	/* Center of chart */
        if ( ltype & GV_LINES) { /* find center */
	    len = Vect_line_length ( Points ) / 2;
	    Vect_point_on_line ( Points, len, &x, &y, NULL, NULL, NULL);
	} else { 
	    x = Points->x[0];
	    y = Points->y[0];
	}

	if ( ctype == CTYPE_PIE ) {
	    if ( sizecol != NULL ) {
               csize = val[ncols];
	       size = scale * csize;
	    }
	    pie ( x, y, size, val, ncols, ocolor, colors );
	} else { 
	    bar ( x, y, size, scale, val, ncols, ocolor, colors );
	}
    }

    db_close_database_shutdown_driver ( driver );
    Vect_destroy_line_struct (Points);
    Vect_destroy_cats_struct (Cats);
    
    return 0;
}