shape.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include <grass/vector.h>
  2. #include <grass/dbmi.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. #include "local_proto.h"
  6. static void error_handler(void *p)
  7. {
  8. dbDriver *driver = (dbDriver *) p;
  9. db_close_database_shutdown_driver(driver);
  10. }
  11. int display_shape(struct Map_info *Map, int type, struct cat_list *Clist, const struct Cell_head *window,
  12. const struct color_rgb *bcolor, const struct color_rgb *fcolor, int chcat,
  13. const char *icon, double size, const char *size_column, int sqrt_flag, const char *rot_column, /* lines only */
  14. int id_flag, int table_colors_flag, int cats_colors_flag, char *rgb_column,
  15. int default_width, char *width_column, double width_scale,
  16. int z_color_flag, char *z_style)
  17. {
  18. int open_db, field, i, stat;
  19. dbCatValArray cvarr_rgb, cvarr_width, cvarr_size, cvarr_rot;
  20. struct field_info *fi;
  21. dbDriver *driver;
  22. int nrec_rgb, nrec_width, nrec_size, nrec_rot, have_colors;
  23. struct Colors colors, zcolors;
  24. struct bound_box box;
  25. stat = 0;
  26. nrec_rgb = nrec_width = nrec_size = nrec_rot = 0;
  27. open_db = table_colors_flag || width_column || size_column || rot_column;
  28. if (open_db) {
  29. field = Clist->field > 0 ? Clist->field : 1;
  30. fi = Vect_get_field(Map, field);
  31. if (!fi) {
  32. G_fatal_error(_("Database connection not defined for layer %d"),
  33. field);
  34. }
  35. driver = db_start_driver_open_database(fi->driver, fi->database);
  36. if (!driver)
  37. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  38. fi->database, fi->driver);
  39. /* add error handler */
  40. G_add_error_handler(error_handler, driver);
  41. }
  42. /* fisrt search for color table */
  43. have_colors = Vect_read_colors(Vect_get_name(Map), Vect_get_mapset(Map),
  44. &colors);
  45. if (table_colors_flag) {
  46. /* read RRR:GGG:BBB color strings from table */
  47. if (!rgb_column || *rgb_column == '\0')
  48. G_fatal_error(_("Color definition column not specified"));
  49. db_CatValArray_init(&cvarr_rgb);
  50. nrec_rgb = db_select_CatValArray(driver, fi->table, fi->key,
  51. rgb_column, NULL, &cvarr_rgb);
  52. G_debug(3, "nrec_rgb (%s) = %d", rgb_column, nrec_rgb);
  53. if (cvarr_rgb.ctype != DB_C_TYPE_STRING)
  54. G_warning(_("Color definition column ('%s') not a string. "
  55. "Column must be of form 'RRR:GGG:BBB' where RGB values range 0-255. "
  56. "You can use '%s' module to define color rules. "
  57. "Unable to colorize features."),
  58. rgb_column, "v.colors");
  59. else {
  60. if (nrec_rgb < 0)
  61. G_fatal_error(_("Unable to select data ('%s') from table"),
  62. rgb_column);
  63. G_debug(2, "\n%d records selected from table", nrec_rgb);
  64. }
  65. }
  66. if (width_column) {
  67. if (*width_column == '\0')
  68. G_fatal_error(_("Line width column not specified"));
  69. db_CatValArray_init(&cvarr_width);
  70. nrec_width = db_select_CatValArray(driver, fi->table, fi->key,
  71. width_column, NULL, &cvarr_width);
  72. G_debug(3, "nrec_width (%s) = %d", width_column, nrec_width);
  73. if (cvarr_width.ctype != DB_C_TYPE_INT &&
  74. cvarr_width.ctype != DB_C_TYPE_DOUBLE)
  75. G_fatal_error(_("Line width column ('%s') not a number"),
  76. width_column);
  77. if (nrec_width < 0)
  78. G_fatal_error(_("Unable to select data ('%s') from table"),
  79. width_column);
  80. G_debug(2, "\n%d records selected from table", nrec_width);
  81. for (i = 0; i < cvarr_width.n_values; i++) {
  82. G_debug(4, "cat = %d %s = %d", cvarr_width.value[i].cat,
  83. width_column,
  84. (cvarr_width.ctype ==
  85. DB_C_TYPE_INT ? cvarr_width.value[i].val.
  86. i : (int)cvarr_width.value[i].val.d));
  87. }
  88. }
  89. if (size_column) {
  90. if (*size_column == '\0')
  91. G_fatal_error(_("Symbol size column not specified"));
  92. db_CatValArray_init(&cvarr_size);
  93. nrec_size = db_select_CatValArray(driver, fi->table, fi->key,
  94. size_column, NULL, &cvarr_size);
  95. G_debug(3, "nrec_size (%s) = %d", size_column, nrec_size);
  96. if (cvarr_size.ctype != DB_C_TYPE_INT &&
  97. cvarr_size.ctype != DB_C_TYPE_DOUBLE)
  98. G_fatal_error(_("Symbol size column ('%s') is not numeric"),
  99. size_column);
  100. if (nrec_size < 0)
  101. G_fatal_error(_("Unable to select data ('%s') from table"),
  102. size_column);
  103. G_debug(2, " %d records selected from table", nrec_size);
  104. for (i = 0; i < cvarr_size.n_values; i++) {
  105. G_debug(4, "(size) cat = %d %s = %.2f", cvarr_size.value[i].cat,
  106. size_column,
  107. (cvarr_size.ctype ==
  108. DB_C_TYPE_INT ? (double)cvarr_size.value[i].val.i
  109. : cvarr_size.value[i].val.d));
  110. }
  111. }
  112. if (rot_column) {
  113. if (*rot_column == '\0')
  114. G_fatal_error(_("Symbol rotation column not specified"));
  115. db_CatValArray_init(&cvarr_rot);
  116. nrec_rot = db_select_CatValArray(driver, fi->table, fi->key,
  117. rot_column, NULL, &cvarr_rot);
  118. G_debug(3, "nrec_rot (%s) = %d", rot_column, nrec_rot);
  119. if (cvarr_rot.ctype != DB_C_TYPE_INT &&
  120. cvarr_rot.ctype != DB_C_TYPE_DOUBLE)
  121. G_fatal_error(_("Symbol rotation column ('%s') is not numeric"),
  122. rot_column);
  123. if (nrec_rot < 0)
  124. G_fatal_error(_("Unable to select data ('%s') from table"),
  125. rot_column);
  126. G_debug(2, " %d records selected from table", nrec_rot);
  127. for (i = 0; i < cvarr_rot.n_values; i++) {
  128. G_debug(4, "(rot) cat = %d %s = %.2f", cvarr_rot.value[i].cat,
  129. rot_column,
  130. (cvarr_rot.ctype ==
  131. DB_C_TYPE_INT ? (double)cvarr_rot.value[i].val.i
  132. : cvarr_rot.value[i].val.d));
  133. }
  134. }
  135. if (open_db) {
  136. /* remove error handler */
  137. G_remove_error_handler(error_handler, driver);
  138. db_close_database_shutdown_driver(driver);
  139. }
  140. if (z_color_flag) {
  141. if (!Vect_is_3d(Map)) {
  142. G_warning(_("Vector map is not 3D. Unable to colorize features based on z-coordinates."));
  143. z_color_flag = 0;
  144. }
  145. else {
  146. Vect_get_map_box(Map, &box);
  147. Rast_make_fp_colors(&zcolors, z_style, box.B, box.T);
  148. }
  149. }
  150. stat = 0;
  151. if (type & GV_AREA && Vect_get_num_primitives(Map, GV_CENTROID | GV_BOUNDARY) > 0)
  152. stat += display_area(Map, Clist, window,
  153. bcolor, fcolor, chcat,
  154. id_flag, cats_colors_flag,
  155. default_width, width_scale,
  156. z_color_flag ? &zcolors : NULL,
  157. table_colors_flag ? &cvarr_rgb : NULL,
  158. have_colors ? &colors : NULL,
  159. &cvarr_width, nrec_width);
  160. stat += display_lines(Map, type, Clist,
  161. bcolor, fcolor, chcat,
  162. icon, size, sqrt_flag,
  163. id_flag, cats_colors_flag,
  164. default_width, width_scale,
  165. z_color_flag ? &zcolors : NULL,
  166. table_colors_flag ? &cvarr_rgb : NULL,
  167. have_colors ? &colors : NULL,
  168. &cvarr_width, nrec_width,
  169. &cvarr_size, nrec_size,
  170. &cvarr_rot, nrec_rot);
  171. return stat;
  172. }
  173. int get_table_color(int cat, int line,
  174. struct Colors *colors, dbCatValArray *cvarr,
  175. int *red, int *grn, int *blu)
  176. {
  177. int custom_rgb;
  178. char colorstring[12]; /* RRR:GGG:BBB */
  179. dbCatVal *cv;
  180. custom_rgb = FALSE;
  181. cv = NULL;
  182. if (cat < 0)
  183. return custom_rgb;
  184. if (colors) {
  185. /* read color table */
  186. if (Rast_get_c_color(&cat, red, grn, blu, colors) == 1) {
  187. custom_rgb = TRUE;
  188. G_debug(3, "\tb: %d, g: %d, r: %d", *blu, *grn, *red);
  189. }
  190. }
  191. /* read RGB colors from db for current area # */
  192. if (cvarr && db_CatValArray_get_value(cvarr, cat, &cv) == DB_OK) {
  193. sprintf(colorstring, "%s", db_get_string(cv->val.s));
  194. if (*colorstring != '\0') {
  195. G_debug(3, "element %d: colorstring: %s", line,
  196. colorstring);
  197. if (G_str_to_color(colorstring, red, grn, blu) == 1) {
  198. custom_rgb = TRUE;
  199. G_debug(3, "element:%d cat %d r:%d g:%d b:%d",
  200. line, cat, *red, *grn, *blu);
  201. }
  202. else {
  203. G_important_message(_("Error in color definition (%s) - feature %d with category %d"),
  204. colorstring, line, cat);
  205. }
  206. }
  207. else {
  208. G_important_message(_("Error in color definition (%s) - feature %d with category %d"),
  209. colorstring, line, cat);
  210. }
  211. }
  212. return custom_rgb;
  213. }
  214. int get_cat_color(int line, const struct line_cats *Cats, const struct cat_list *Clist,
  215. int *red, int *grn, int *blu)
  216. {
  217. int custom_rgb;
  218. unsigned char which;
  219. int cat;
  220. custom_rgb = FALSE;
  221. if (Clist->field > 0) {
  222. Vect_cat_get(Cats, Clist->field, &cat);
  223. if (cat >= 0) {
  224. G_debug(3, "display element %d, cat %d", line, cat);
  225. /* fetch color number from category */
  226. which = (cat % palette_ncolors);
  227. G_debug(3, "cat:%d which color:%d r:%d g:%d b:%d", cat,
  228. which, palette[which].R, palette[which].G,
  229. palette[which].B);
  230. custom_rgb = TRUE;
  231. *red = palette[which].R;
  232. *grn = palette[which].G;
  233. *blu = palette[which].B;
  234. }
  235. }
  236. else if (Cats->n_cats > 0) {
  237. /* fetch color number from layer */
  238. which = (Cats->field[0] % palette_ncolors);
  239. G_debug(3, "layer:%d which color:%d r:%d g:%d b:%d",
  240. Cats->field[0], which, palette[which].R,
  241. palette[which].G, palette[which].B);
  242. custom_rgb = TRUE;
  243. *red = palette[which].R;
  244. *grn = palette[which].G;
  245. *blu = palette[which].B;
  246. }
  247. return custom_rgb;
  248. }
  249. double get_property(int cat, int line, dbCatValArray *cvarr, double scale, double default_value)
  250. {
  251. double value;
  252. dbCatVal *cv;
  253. cv = NULL;
  254. if (cat < 0)
  255. return default_value;
  256. /* Read line width from db for current area # */
  257. if (db_CatValArray_get_value(cvarr, cat, &cv) != DB_OK) {
  258. value = default_value;
  259. }
  260. else {
  261. value = scale * (cvarr->ctype ==
  262. DB_C_TYPE_INT ? (double) cv->val.i : cv->val.d);
  263. if (value < 0) {
  264. G_important_message(_("Invalid negative value - feature %d with category %d"),
  265. line, cat);
  266. value = default_value;
  267. }
  268. }
  269. return value;
  270. }