plot.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <grass/gis.h>
  2. #include <grass/vector.h>
  3. #include <grass/dbmi.h>
  4. #include <grass/display.h>
  5. #include <grass/symbol.h>
  6. #include <grass/glocale.h>
  7. #include "global.h"
  8. /* Returns 0 - ok , 1 - error */
  9. int
  10. plot(int ctype, struct Map_info *Map, int type, int field,
  11. char *columns, int ncols, char *sizecol, int size, double scale,
  12. COLOR * ocolor, COLOR * colors, int y_center, double *max_reference)
  13. {
  14. int ltype, nlines, line, col, more, coltype, nselcols;
  15. double x, y, csize, len;
  16. struct line_pnts *Points;
  17. struct line_cats *Cats;
  18. int cat;
  19. double *val;
  20. char buf[2000];
  21. struct field_info *Fi;
  22. dbDriver *driver;
  23. dbValue *value;
  24. dbString sql;
  25. dbCursor cursor;
  26. dbTable *table;
  27. dbColumn *column;
  28. Points = Vect_new_line_struct();
  29. Cats = Vect_new_cats_struct();
  30. db_init_string(&sql);
  31. Fi = Vect_get_field(Map, field);
  32. if (Fi == NULL)
  33. G_fatal_error(_("Database connection not defined for layer %d"),
  34. field);
  35. /* Open driver */
  36. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  37. if (driver == NULL) {
  38. G_warning(_("Unable to open database <%s> by driver <%s>"),
  39. Fi->database,
  40. Fi->driver);
  41. return 1;
  42. }
  43. val = (double *)G_malloc((ncols + 1) * sizeof(double)); /* + 1 for sizecol */
  44. Vect_rewind(Map);
  45. nlines = Vect_get_num_lines(Map);
  46. /* loop through each vector feature */
  47. for (line = 1; line <= nlines; line++) {
  48. G_debug(3, "line = %d", line);
  49. ltype = Vect_read_line(Map, Points, Cats, line);
  50. if (!(ltype & type))
  51. continue;
  52. Vect_cat_get(Cats, field, &cat);
  53. if (cat < 0)
  54. continue;
  55. /* Select values from DB */
  56. if (ctype == CTYPE_PIE && sizecol != NULL) {
  57. sprintf(buf, "select %s, %s from %s where %s = %d", columns,
  58. sizecol, Fi->table, Fi->key, cat);
  59. nselcols = ncols + 1;
  60. }
  61. else {
  62. sprintf(buf, "select %s from %s where %s = %d", columns,
  63. Fi->table, Fi->key, cat);
  64. nselcols = ncols;
  65. }
  66. db_set_string(&sql, buf);
  67. G_debug(3, "SQL: %s", buf);
  68. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  69. DB_OK) {
  70. G_warning(_("Unable to open select cursor: '%s'"),
  71. buf);
  72. return 1;
  73. }
  74. table = db_get_cursor_table(&cursor);
  75. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK || !more)
  76. continue;
  77. for (col = 0; col < nselcols; col++) {
  78. column = db_get_table_column(table, col);
  79. value = db_get_column_value(column);
  80. coltype = db_sqltype_to_Ctype(db_get_column_sqltype(column));
  81. switch (coltype) {
  82. case DB_C_TYPE_INT:
  83. val[col] = (double)db_get_value_int(value);
  84. break;
  85. case DB_C_TYPE_DOUBLE:
  86. val[col] = db_get_value_double(value);
  87. break;
  88. default:
  89. G_warning("Column type not supported (must be INT or FLOAT)");
  90. return 1;
  91. }
  92. G_debug(4, " val[%d]: %f", col, val[col]);
  93. }
  94. db_close_cursor(&cursor);
  95. /* Center of chart */
  96. if (ltype & GV_LINES) { /* find center */
  97. len = Vect_line_length(Points) / 2;
  98. Vect_point_on_line(Points, len, &x, &y, NULL, NULL, NULL);
  99. }
  100. else {
  101. x = Points->x[0];
  102. y = Points->y[0];
  103. }
  104. if (ctype == CTYPE_PIE) {
  105. if (sizecol != NULL) {
  106. csize = val[ncols];
  107. size = scale * csize;
  108. }
  109. pie(x, y, size, val, ncols, ocolor, colors);
  110. }
  111. else {
  112. bar(x, y, size, scale, val, ncols, ocolor, colors, y_center,
  113. max_reference);
  114. }
  115. }
  116. db_close_database_shutdown_driver(driver);
  117. Vect_destroy_line_struct(Points);
  118. Vect_destroy_cats_struct(Cats);
  119. return 0;
  120. }