plot.c 3.5 KB

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