attr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <string.h>
  2. #include <grass/gis.h>
  3. #include <grass/vector.h>
  4. #include <grass/display.h>
  5. #include <grass/dbmi.h>
  6. #include <grass/glocale.h>
  7. #include "local_proto.h"
  8. #include "plot.h"
  9. int display_attr(struct Map_info *Map, int type, char *attrcol,
  10. struct cat_list *Clist, LATTR *lattr, int chcat)
  11. {
  12. int i, ltype, more;
  13. struct line_pnts *Points;
  14. struct line_cats *Cats;
  15. int cat;
  16. char buf[2000];
  17. struct field_info *fi;
  18. dbDriver *driver;
  19. dbString stmt, valstr, text;
  20. dbCursor cursor;
  21. dbTable *table;
  22. dbColumn *column;
  23. G_debug(2, "attr()");
  24. if (attrcol == NULL || *attrcol == '\0') {
  25. G_fatal_error(_("attrcol not specified, cannot display attributes"));
  26. }
  27. Points = Vect_new_line_struct();
  28. Cats = Vect_new_cats_struct();
  29. db_init_string(&stmt);
  30. db_init_string(&valstr);
  31. db_init_string(&text);
  32. fi = Vect_get_field(Map, lattr->field);
  33. if (fi == NULL)
  34. return 1;
  35. driver = db_start_driver_open_database(fi->driver, fi->database);
  36. if (driver == NULL)
  37. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  38. fi->database,
  39. fi->driver);
  40. Vect_rewind(Map);
  41. while (1) {
  42. ltype = Vect_read_next_line(Map, Points, Cats);
  43. if (ltype == -1)
  44. G_fatal_error(_("Unable to read vector map"));
  45. else if (ltype == -2) /* EOF */
  46. break;
  47. if (!(type & ltype) && !((type & GV_AREA) && (ltype & GV_CENTROID)))
  48. continue; /* used for both lines and labels */
  49. D_RGB_color(lattr->color.R, lattr->color.G, lattr->color.B);
  50. D_text_size(lattr->size, lattr->size);
  51. if (lattr->font)
  52. D_font(lattr->font);
  53. if (lattr->enc)
  54. D_encoding(lattr->enc);
  55. if (chcat) {
  56. int found = 0;
  57. for (i = 0; i < Cats->n_cats; i++) {
  58. if (Cats->field[i] == Clist->field &&
  59. Vect_cat_in_cat_list(Cats->cat[i], Clist)) {
  60. found = 1;
  61. break;
  62. }
  63. }
  64. if (!found)
  65. continue;
  66. }
  67. else if (Clist->field > 0) {
  68. int found = 0;
  69. for (i = 0; i < Cats->n_cats; i++) {
  70. if (Cats->field[i] == Clist->field) {
  71. found = 1;
  72. break;
  73. }
  74. }
  75. /* lines with no category will be displayed */
  76. if (Cats->n_cats > 0 && !found)
  77. continue;
  78. }
  79. if (Vect_cat_get(Cats, lattr->field, &cat)) {
  80. int ncats = 0;
  81. /* Read attribute from db */
  82. db_free_string(&text);
  83. for (i = 0; i < Cats->n_cats; i++) {
  84. int nrows;
  85. if (Cats->field[i] != lattr->field)
  86. continue;
  87. db_init_string(&stmt);
  88. sprintf(buf, "select %s from %s where %s = %d", attrcol,
  89. fi->table, fi->key, Cats->cat[i]);
  90. G_debug(2, "SQL: %s", buf);
  91. db_append_string(&stmt, buf);
  92. if (db_open_select_cursor
  93. (driver, &stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
  94. G_fatal_error(_("Unable to open select cursor: '%s'"),
  95. db_get_string(&stmt));
  96. nrows = db_get_num_rows(&cursor);
  97. if (ncats > 0)
  98. db_append_string(&text, "/");
  99. if (nrows > 0) {
  100. table = db_get_cursor_table(&cursor);
  101. column = db_get_table_column(table, 0); /* first column */
  102. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
  103. continue;
  104. db_convert_column_value_to_string(column, &valstr);
  105. db_append_string(&text, db_get_string(&valstr));
  106. }
  107. else {
  108. G_warning(_("No attribute found for cat %d: %s"), cat,
  109. db_get_string(&stmt));
  110. }
  111. db_close_cursor(&cursor);
  112. ncats++;
  113. }
  114. show_label_line(Points, ltype, lattr, db_get_string(&text));
  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. }