plot1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* plot1() - Level One vector reading */
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/raster.h>
  5. #include <grass/vector.h>
  6. #include <grass/display.h>
  7. #include "plot.h"
  8. #include "local_proto.h"
  9. #include <grass/symbol.h>
  10. #include <grass/glocale.h>
  11. #include <grass/dbmi.h>
  12. #define RENDER_POLYLINE 0
  13. #define RENDER_POLYGON 1
  14. int palette_ncolors = 16;
  15. struct rgb_color palette[16] = {
  16. {198, 198, 198}, /* 1: light gray */
  17. {127, 127, 127}, /* 2: medium/dark gray */
  18. {255, 0, 0}, /* 3: bright red */
  19. {139, 0, 0}, /* 4: dark red */
  20. {0, 255, 0}, /* 5: bright green */
  21. {0, 139, 0}, /* 6: dark green */
  22. {0, 0, 255}, /* 7: bright blue */
  23. {0, 0, 139}, /* 8: dark blue */
  24. {255, 255, 0}, /* 9: yellow */
  25. {139, 126, 10}, /* 10: olivey brown */
  26. {255, 165, 0}, /* 11: orange */
  27. {255, 192, 203}, /* 12: pink */
  28. {255, 0, 255}, /* 13: magenta */
  29. {139, 0, 139}, /* 14: dark magenta */
  30. {0, 255, 255}, /* 15: cyan */
  31. {0, 139, 139} /* 16: dark cyan */
  32. };
  33. /* *************************************************************** */
  34. /* *************************************************************** */
  35. /* *************************************************************** */
  36. int plot1(struct Map_info *Map, int type, int area, struct cat_list *Clist,
  37. const struct color_rgb *color, const struct color_rgb *fcolor,
  38. int chcat, SYMBOL * Symb, int size, int id_flag,
  39. int table_colors_flag, int cats_color_flag, char *rgb_column,
  40. int default_width, char *width_column, double width_scale)
  41. {
  42. int i, ltype, nlines = 0, line, cat = -1;
  43. double *x, *y;
  44. struct line_pnts *Points, *PPoints;
  45. struct line_cats *Cats;
  46. double msize;
  47. int x0, y0;
  48. struct field_info *fi = NULL;
  49. dbDriver *driver = NULL;
  50. dbCatValArray cvarr_rgb, cvarr_width;
  51. dbCatVal *cv_rgb = NULL, *cv_width = NULL;
  52. int nrec_rgb = 0, nrec_width = 0;
  53. int open_db;
  54. int custom_rgb = FALSE;
  55. char colorstring[12]; /* RRR:GGG:BBB */
  56. int red, grn, blu;
  57. RGBA_Color *line_color, *fill_color, *primary_color;
  58. unsigned char which;
  59. int width;
  60. line_color = G_malloc(sizeof(RGBA_Color));
  61. fill_color = G_malloc(sizeof(RGBA_Color));
  62. primary_color = G_malloc(sizeof(RGBA_Color));
  63. primary_color->a = RGBA_COLOR_OPAQUE;
  64. /* change function prototype to pass RGBA_Color instead of color_rgb? */
  65. if (color) {
  66. line_color->r = color->r;
  67. line_color->g = color->g;
  68. line_color->b = color->b;
  69. line_color->a = RGBA_COLOR_OPAQUE;
  70. }
  71. else
  72. line_color->a = RGBA_COLOR_NONE;
  73. if (fcolor) {
  74. fill_color->r = fcolor->r;
  75. fill_color->g = fcolor->g;
  76. fill_color->b = fcolor->b;
  77. fill_color->a = RGBA_COLOR_OPAQUE;
  78. }
  79. else
  80. fill_color->a = RGBA_COLOR_NONE;
  81. msize = size * (D_d_to_u_col(2.0) - D_d_to_u_col(1.0)); /* do it better */
  82. Points = Vect_new_line_struct();
  83. PPoints = Vect_new_line_struct();
  84. Cats = Vect_new_cats_struct();
  85. open_db = table_colors_flag || width_column;
  86. if (open_db) {
  87. fi = Vect_get_field(Map, (Clist->field > 0 ? Clist->field : 1));
  88. if (fi == NULL) {
  89. G_fatal_error(_("Database connection not defined for layer %d"),
  90. (Clist->field > 0 ? Clist->field : 1));
  91. }
  92. driver = db_start_driver_open_database(fi->driver, fi->database);
  93. if (driver == NULL)
  94. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  95. fi->database, fi->driver);
  96. db_set_error_handler_driver(driver);
  97. }
  98. if (table_colors_flag) {
  99. /* for reading RRR:GGG:BBB color strings from table */
  100. if (rgb_column == NULL || *rgb_column == '\0')
  101. G_fatal_error(_("Color definition column not specified"));
  102. db_CatValArray_init(&cvarr_rgb);
  103. nrec_rgb = db_select_CatValArray(driver, fi->table, fi->key,
  104. rgb_column, NULL, &cvarr_rgb);
  105. G_debug(3, "nrec_rgb (%s) = %d", rgb_column, nrec_rgb);
  106. if (cvarr_rgb.ctype != DB_C_TYPE_STRING)
  107. G_fatal_error(_("Color definition column (%s) not a string. "
  108. "Column must be of form RRR:GGG:BBB where RGB values range 0-255."),
  109. rgb_column);
  110. if (nrec_rgb < 0)
  111. G_fatal_error(_("Cannot select data (%s) from table"),
  112. rgb_column);
  113. G_debug(2, "\n%d records selected from table", nrec_rgb);
  114. for (i = 0; i < cvarr_rgb.n_values; i++) {
  115. G_debug(4, "cat = %d %s = %s", cvarr_rgb.value[i].cat,
  116. rgb_column, db_get_string(cvarr_rgb.value[i].val.s));
  117. }
  118. }
  119. if (width_column) {
  120. if (*width_column == '\0')
  121. G_fatal_error(_("Line width column not specified."));
  122. db_CatValArray_init(&cvarr_width);
  123. nrec_width = db_select_CatValArray(driver, fi->table, fi->key,
  124. width_column, NULL, &cvarr_width);
  125. G_debug(3, "nrec_width (%s) = %d", width_column, nrec_width);
  126. if (cvarr_width.ctype != DB_C_TYPE_INT &&
  127. cvarr_width.ctype != DB_C_TYPE_DOUBLE)
  128. G_fatal_error(_("Line width column (%s) not a number."),
  129. width_column);
  130. if (nrec_width < 0)
  131. G_fatal_error(_("Cannot select data (%s) from table"),
  132. width_column);
  133. G_debug(2, "\n%d records selected from table", nrec_width);
  134. for (i = 0; i < cvarr_width.n_values; i++) {
  135. G_debug(4, "cat = %d %s = %d", cvarr_width.value[i].cat,
  136. width_column,
  137. (cvarr_width.ctype ==
  138. DB_C_TYPE_INT ? cvarr_width.value[i].val.
  139. i : (int)cvarr_width.value[i].val.d));
  140. }
  141. }
  142. if (open_db)
  143. db_close_database_shutdown_driver(driver);
  144. Vect_rewind(Map);
  145. /* Is it necessary to reset line/label color in each loop ? */
  146. if (color && !table_colors_flag && !cats_color_flag)
  147. D_RGB_color(color->r, color->g, color->b);
  148. if (Vect_level(Map) >= 2)
  149. nlines = Vect_get_num_lines(Map);
  150. line = 0;
  151. while (1) {
  152. if (Vect_level(Map) >= 2) {
  153. line++;
  154. if (line > nlines)
  155. return 0;
  156. if (!Vect_line_alive(Map, line))
  157. continue;
  158. ltype = Vect_read_line(Map, Points, Cats, line);
  159. }
  160. else {
  161. ltype = Vect_read_next_line(Map, Points, Cats);
  162. switch (ltype) {
  163. case -1:
  164. fprintf(stderr, _("\nERROR: vector map - can't read\n"));
  165. return -1;
  166. case -2: /* EOF */
  167. return 0;
  168. }
  169. }
  170. if (!(type & ltype))
  171. continue;
  172. if (chcat) {
  173. int found = 0;
  174. if (id_flag) { /* use line id */
  175. if (!(Vect_cat_in_cat_list(line, Clist)))
  176. continue;
  177. }
  178. else {
  179. for (i = 0; i < Cats->n_cats; i++) {
  180. if (Cats->field[i] == Clist->field &&
  181. Vect_cat_in_cat_list(Cats->cat[i], Clist)) {
  182. found = 1;
  183. break;
  184. }
  185. }
  186. if (!found)
  187. continue;
  188. }
  189. }
  190. else if (Clist->field > 0) {
  191. int found = 0;
  192. for (i = 0; i < Cats->n_cats; i++) {
  193. if (Cats->field[i] == Clist->field) {
  194. found = 1;
  195. break;
  196. }
  197. }
  198. /* lines with no category will be displayed */
  199. if (Cats->n_cats > 0 && !found)
  200. continue;
  201. }
  202. if (table_colors_flag) {
  203. /* only first category */
  204. cat = Vect_get_line_cat(Map, line,
  205. (Clist->field > 0 ? Clist->field :
  206. (Cats->n_cats >
  207. 0 ? Cats->field[0] : 1)));
  208. if (cat >= 0) {
  209. G_debug(3, "display element %d, cat %d", line, cat);
  210. /* Read RGB colors from db for current area # */
  211. if (db_CatValArray_get_value(&cvarr_rgb, cat, &cv_rgb) !=
  212. DB_OK) {
  213. custom_rgb = FALSE;
  214. }
  215. else {
  216. sprintf(colorstring, "%s", db_get_string(cv_rgb->val.s));
  217. if (*colorstring != '\0') {
  218. G_debug(3, "element %d: colorstring: %s", line,
  219. colorstring);
  220. if (G_str_to_color(colorstring, &red, &grn, &blu) ==
  221. 1) {
  222. custom_rgb = TRUE;
  223. G_debug(3, "element:%d cat %d r:%d g:%d b:%d",
  224. line, cat, red, grn, blu);
  225. }
  226. else {
  227. custom_rgb = FALSE;
  228. G_warning(_("Error in color definition column (%s), element %d "
  229. "with cat %d: colorstring [%s]"),
  230. rgb_column, line, cat, colorstring);
  231. }
  232. }
  233. else {
  234. custom_rgb = FALSE;
  235. G_warning(_("Error in color definition column (%s), element %d with cat %d"),
  236. rgb_column, line, cat);
  237. }
  238. }
  239. } /* end if cat */
  240. else {
  241. custom_rgb = FALSE;
  242. }
  243. } /* end if table_colors_flag */
  244. /* random colors */
  245. if (cats_color_flag) {
  246. custom_rgb = FALSE;
  247. if (Clist->field > 0) {
  248. cat = Vect_get_line_cat(Map, line, Clist->field);
  249. if (cat >= 0) {
  250. G_debug(3, "display element %d, cat %d", line, cat);
  251. /* fetch color number from category */
  252. which = (cat % palette_ncolors);
  253. G_debug(3, "cat:%d which color:%d r:%d g:%d b:%d", cat,
  254. which, palette[which].R, palette[which].G,
  255. palette[which].B);
  256. custom_rgb = TRUE;
  257. red = palette[which].R;
  258. grn = palette[which].G;
  259. blu = palette[which].B;
  260. }
  261. }
  262. else if (Cats->n_cats > 0) {
  263. /* fetch color number from layer */
  264. which = (Cats->field[0] % palette_ncolors);
  265. G_debug(3, "layer:%d which color:%d r:%d g:%d b:%d",
  266. Cats->field[0], which, palette[which].R,
  267. palette[which].G, palette[which].B);
  268. custom_rgb = TRUE;
  269. red = palette[which].R;
  270. grn = palette[which].G;
  271. blu = palette[which].B;
  272. }
  273. }
  274. if (nrec_width) {
  275. /* only first category */
  276. cat = Vect_get_line_cat(Map, line,
  277. (Clist->field > 0 ? Clist->field :
  278. (Cats->n_cats >
  279. 0 ? Cats->field[0] : 1)));
  280. if (cat >= 0) {
  281. G_debug(3, "display element %d, cat %d", line, cat);
  282. /* Read line width from db for current area # */
  283. if (db_CatValArray_get_value(&cvarr_width, cat, &cv_width) !=
  284. DB_OK) {
  285. width = default_width;
  286. }
  287. else {
  288. width =
  289. width_scale * (cvarr_width.ctype ==
  290. DB_C_TYPE_INT ? cv_width->val.
  291. i : (int)cv_width->val.d);
  292. if (width < 0) {
  293. G_warning(_("Error in line width column (%s), element %d "
  294. "with cat %d: line width [%d]"),
  295. width_column, line, cat, width);
  296. width = default_width;
  297. }
  298. }
  299. } /* end if cat */
  300. else {
  301. width = default_width;
  302. }
  303. D_line_width(width);
  304. } /* end if nrec_width */
  305. /* enough of the prep work, lets start plotting stuff */
  306. x = Points->x;
  307. y = Points->y;
  308. if ((ltype & GV_POINTS) && Symb != NULL) {
  309. if (!(color || fcolor || custom_rgb))
  310. continue;
  311. x0 = D_u_to_d_col(x[0]);
  312. y0 = D_u_to_d_row(y[0]);
  313. /* skip if the point is outside of the display window */
  314. /* xy<0 tests make it go ever-so-slightly faster */
  315. if (x0 < 0 || y0 < 0 ||
  316. x0 > D_get_d_east() || x0 < D_get_d_west() ||
  317. y0 > D_get_d_south() || y0 < D_get_d_north())
  318. continue;
  319. /* use random or RGB column color if given, otherwise reset */
  320. /* centroids always use default color to stand out from underlying area */
  321. if (custom_rgb && (ltype != GV_CENTROID)) {
  322. primary_color->r = (unsigned char)red;
  323. primary_color->g = (unsigned char)grn;
  324. primary_color->b = (unsigned char)blu;
  325. D_symbol2(Symb, x0, y0, primary_color, line_color);
  326. }
  327. else
  328. D_symbol(Symb, x0, y0, line_color, fill_color);
  329. }
  330. else if (color || custom_rgb) {
  331. if (!table_colors_flag && !cats_color_flag)
  332. D_RGB_color(color->r, color->g, color->b);
  333. else {
  334. if (custom_rgb)
  335. D_RGB_color((unsigned char)red, (unsigned char)grn,
  336. (unsigned char)blu);
  337. else
  338. D_RGB_color(color->r, color->g, color->b);
  339. }
  340. /* Plot the lines */
  341. if (Points->n_points == 1) /* line with one coor */
  342. D_polydots_abs(x, y, Points->n_points);
  343. else /*use different user defined render methods */
  344. D_polyline_abs(x, y, Points->n_points);
  345. }
  346. }
  347. Vect_destroy_line_struct(Points);
  348. Vect_destroy_cats_struct(Cats);
  349. return 0; /* not reached */
  350. }