plot.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. db_set_error_handler_driver(driver);
  44. val = (double *)G_malloc((ncols + 1) * sizeof(double)); /* + 1 for sizecol */
  45. Vect_rewind(Map);
  46. nlines = Vect_get_num_lines(Map);
  47. /* loop through each vector feature */
  48. for (line = 1; line <= nlines; line++) {
  49. G_debug(3, "line = %d", line);
  50. ltype = Vect_read_line(Map, Points, Cats, line);
  51. if (!(ltype & type))
  52. continue;
  53. Vect_cat_get(Cats, field, &cat);
  54. if (cat < 0)
  55. continue;
  56. /* Select values from DB */
  57. if (ctype == CTYPE_PIE && sizecol != NULL) {
  58. sprintf(buf, "select %s, %s from %s where %s = %d", columns,
  59. sizecol, Fi->table, Fi->key, cat);
  60. nselcols = ncols + 1;
  61. }
  62. else {
  63. sprintf(buf, "select %s from %s where %s = %d", columns,
  64. Fi->table, Fi->key, cat);
  65. nselcols = ncols;
  66. }
  67. db_set_string(&sql, buf);
  68. G_debug(3, "SQL: %s", buf);
  69. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  70. DB_OK) {
  71. G_warning(_("Unable to open select cursor: '%s'"),
  72. buf);
  73. return 1;
  74. }
  75. table = db_get_cursor_table(&cursor);
  76. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK || !more)
  77. continue;
  78. for (col = 0; col < nselcols; col++) {
  79. column = db_get_table_column(table, col);
  80. value = db_get_column_value(column);
  81. coltype = db_sqltype_to_Ctype(db_get_column_sqltype(column));
  82. switch (coltype) {
  83. case DB_C_TYPE_INT:
  84. val[col] = (double)db_get_value_int(value);
  85. break;
  86. case DB_C_TYPE_DOUBLE:
  87. val[col] = db_get_value_double(value);
  88. break;
  89. default:
  90. G_warning("Column type not supported (must be INT or FLOAT)");
  91. return 1;
  92. }
  93. G_debug(4, " val[%d]: %f", col, val[col]);
  94. }
  95. db_close_cursor(&cursor);
  96. /* Center of chart */
  97. if (ltype & GV_LINES) { /* find center */
  98. len = Vect_line_length(Points) / 2;
  99. Vect_point_on_line(Points, len, &x, &y, NULL, NULL, NULL);
  100. }
  101. else {
  102. x = Points->x[0];
  103. y = Points->y[0];
  104. }
  105. if (ctype == CTYPE_PIE) {
  106. if (sizecol != NULL) {
  107. csize = val[ncols];
  108. size = scale * csize;
  109. }
  110. pie(x, y, size, val, ncols, ocolor, colors);
  111. }
  112. else {
  113. bar(x, y, size, scale, val, ncols, ocolor, colors, y_center,
  114. max_reference);
  115. }
  116. }
  117. db_close_database_shutdown_driver(driver);
  118. Vect_destroy_line_struct(Points);
  119. Vect_destroy_cats_struct(Cats);
  120. return 0;
  121. }