plot1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* plot1() - Level One vector reading */
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/Vect.h>
  5. #include <grass/display.h>
  6. #include <grass/raster.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. int z_color_flag, char *style)
  42. {
  43. int i, ltype, nlines = 0, line, cat = -1;
  44. double *x, *y;
  45. struct line_pnts *Points, *PPoints;
  46. struct line_cats *Cats;
  47. double 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 nerror_rgb;
  54. int open_db;
  55. int custom_rgb = FALSE;
  56. char colorstring[12]; /* RRR:GGG:BBB */
  57. int red, grn, blu;
  58. RGBA_Color *line_color, *fill_color, *primary_color;
  59. unsigned char which;
  60. int width;
  61. nerror_rgb = 0;
  62. line_color = G_malloc(sizeof(RGBA_Color));
  63. fill_color = G_malloc(sizeof(RGBA_Color));
  64. primary_color = G_malloc(sizeof(RGBA_Color));
  65. primary_color->a = RGBA_COLOR_OPAQUE;
  66. /* change function prototype to pass RGBA_Color instead of color_rgb? */
  67. if (color) {
  68. line_color->r = color->r;
  69. line_color->g = color->g;
  70. line_color->b = color->b;
  71. line_color->a = RGBA_COLOR_OPAQUE;
  72. }
  73. else
  74. line_color->a = RGBA_COLOR_NONE;
  75. if (fcolor) {
  76. fill_color->r = fcolor->r;
  77. fill_color->g = fcolor->g;
  78. fill_color->b = fcolor->b;
  79. fill_color->a = RGBA_COLOR_OPAQUE;
  80. }
  81. else
  82. fill_color->a = RGBA_COLOR_NONE;
  83. Points = Vect_new_line_struct();
  84. PPoints = Vect_new_line_struct();
  85. Cats = Vect_new_cats_struct();
  86. open_db = table_colors_flag || width_column;
  87. if (open_db) {
  88. fi = Vect_get_field(Map, (Clist->field > 0 ? Clist->field : 1));
  89. if (fi == NULL) {
  90. G_fatal_error(_("Database connection not defined for layer %d"),
  91. (Clist->field > 0 ? Clist->field : 1));
  92. }
  93. driver = db_start_driver_open_database(fi->driver, fi->database);
  94. if (driver == NULL)
  95. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  96. fi->database, fi->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. break;
  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. G_warning(_("Unable to read vector map"));
  165. return -1;
  166. case -2: /* EOF */
  167. break;
  168. }
  169. }
  170. if (!(type & ltype))
  171. continue;
  172. if (Points->n_points == 0)
  173. continue;
  174. if (chcat) {
  175. int found = 0;
  176. if (id_flag) { /* use line id */
  177. if (!(Vect_cat_in_cat_list(line, Clist)))
  178. continue;
  179. }
  180. else {
  181. for (i = 0; i < Cats->n_cats; i++) {
  182. if (Cats->field[i] == Clist->field &&
  183. Vect_cat_in_cat_list(Cats->cat[i], Clist)) {
  184. found = 1;
  185. break;
  186. }
  187. }
  188. if (!found)
  189. continue;
  190. }
  191. }
  192. else if (Clist->field > 0) {
  193. int found = 0;
  194. for (i = 0; i < Cats->n_cats; i++) {
  195. if (Cats->field[i] == Clist->field) {
  196. found = 1;
  197. break;
  198. }
  199. }
  200. /* lines with no category will be displayed */
  201. if (Cats->n_cats > 0 && !found)
  202. continue;
  203. }
  204. /* z height colors */
  205. if (z_color_flag && Vect_is_3d(Map)) {
  206. BOUND_BOX box;
  207. double zval;
  208. struct Colors colors;
  209. Vect_get_map_box(Map, &box);
  210. zval = Points->z[0];
  211. G_debug(3, "display line %d, cat %d, x: %f, y: %f, z: %f", line,
  212. cat, Points->x[0], Points->y[0], Points->z[0]);
  213. custom_rgb = TRUE;
  214. G_make_fp_colors(&colors, style, box.B, box.T);
  215. G_get_raster_color(&zval, &red, &grn, &blu, &colors, DCELL_TYPE);
  216. G_debug(3, "b %d, g: %d, r %d", blu, grn, red);
  217. }
  218. if (table_colors_flag) {
  219. /* only first category */
  220. cat = Vect_get_line_cat(Map, line,
  221. (Clist->field > 0 ? Clist->field :
  222. (Cats->n_cats >
  223. 0 ? Cats->field[0] : 1)));
  224. if (cat >= 0) {
  225. G_debug(3, "display element %d, cat %d", line, cat);
  226. /* Read RGB colors from db for current area # */
  227. if (db_CatValArray_get_value(&cvarr_rgb, cat, &cv_rgb) !=
  228. DB_OK) {
  229. custom_rgb = FALSE;
  230. }
  231. else {
  232. sprintf(colorstring, "%s", db_get_string(cv_rgb->val.s));
  233. if (*colorstring != '\0') {
  234. G_debug(3, "element %d: colorstring: %s", line,
  235. colorstring);
  236. if (G_str_to_color(colorstring, &red, &grn, &blu) ==
  237. 1) {
  238. custom_rgb = TRUE;
  239. G_debug(3, "element:%d cat %d r:%d g:%d b:%d",
  240. line, cat, red, grn, blu);
  241. }
  242. else {
  243. custom_rgb = FALSE;
  244. G_important_message(_("Error in color definition column '%s', feature id %d "
  245. "with cat %d: colorstring '%s'"),
  246. rgb_column, line, cat, colorstring);
  247. nerror_rgb++;
  248. }
  249. }
  250. else {
  251. custom_rgb = FALSE;
  252. G_important_message(_("Error in color definition column '%s', feature id %d "
  253. "with cat %d"),
  254. rgb_column, line, cat);
  255. nerror_rgb++;
  256. }
  257. }
  258. } /* end if cat */
  259. else {
  260. custom_rgb = FALSE;
  261. }
  262. } /* end if table_colors_flag */
  263. /* random colors */
  264. if (cats_color_flag) {
  265. custom_rgb = FALSE;
  266. if (Clist->field > 0) {
  267. cat = Vect_get_line_cat(Map, line, Clist->field);
  268. if (cat >= 0) {
  269. G_debug(3, "display element %d, cat %d", line, cat);
  270. /* fetch color number from category */
  271. which = (cat % palette_ncolors);
  272. G_debug(3, "cat:%d which color:%d r:%d g:%d b:%d", cat,
  273. which, palette[which].R, palette[which].G,
  274. palette[which].B);
  275. custom_rgb = TRUE;
  276. red = palette[which].R;
  277. grn = palette[which].G;
  278. blu = palette[which].B;
  279. }
  280. }
  281. else if (Cats->n_cats > 0) {
  282. /* fetch color number from layer */
  283. which = (Cats->field[0] % palette_ncolors);
  284. G_debug(3, "layer:%d which color:%d r:%d g:%d b:%d",
  285. Cats->field[0], which, palette[which].R,
  286. palette[which].G, palette[which].B);
  287. custom_rgb = TRUE;
  288. red = palette[which].R;
  289. grn = palette[which].G;
  290. blu = palette[which].B;
  291. }
  292. }
  293. if (nrec_width) {
  294. /* only first category */
  295. cat = Vect_get_line_cat(Map, line,
  296. (Clist->field > 0 ? Clist->field :
  297. (Cats->n_cats >
  298. 0 ? Cats->field[0] : 1)));
  299. if (cat >= 0) {
  300. G_debug(3, "display element %d, cat %d", line, cat);
  301. /* Read line width from db for current area # */
  302. if (db_CatValArray_get_value(&cvarr_width, cat, &cv_width) !=
  303. DB_OK) {
  304. width = default_width;
  305. }
  306. else {
  307. width =
  308. width_scale * (cvarr_width.ctype ==
  309. DB_C_TYPE_INT ? cv_width->val.
  310. i : (int)cv_width->val.d);
  311. if (width < 0) {
  312. G_warning(_("Error in line width column (%s), element %d "
  313. "with cat %d: line width [%d]"),
  314. width_column, line, cat, width);
  315. width = default_width;
  316. }
  317. }
  318. } /* end if cat */
  319. else {
  320. width = default_width;
  321. }
  322. D_line_width(width);
  323. } /* end if nrec_width */
  324. /* enough of the prep work, lets start plotting stuff */
  325. x = Points->x;
  326. y = Points->y;
  327. if ((ltype & GV_POINTS) && Symb != NULL) {
  328. if (!(color || fcolor || custom_rgb))
  329. continue;
  330. x0 = x[0];
  331. y0 = y[0];
  332. /* skip if the point is outside of the display window */
  333. /* xy<0 tests make it go ever-so-slightly faster */
  334. if (x0 > D_get_u_east() || x0 < D_get_u_west() ||
  335. y0 < D_get_u_south() || y0 > D_get_u_north())
  336. continue;
  337. /* use random or RGB column color if given, otherwise reset */
  338. /* centroids always use default color to stand out from underlying area */
  339. if (custom_rgb && (ltype != GV_CENTROID)) {
  340. primary_color->r = (unsigned char)red;
  341. primary_color->g = (unsigned char)grn;
  342. primary_color->b = (unsigned char)blu;
  343. D_symbol2(Symb, x0, y0, primary_color, line_color);
  344. }
  345. else
  346. D_symbol(Symb, x0, y0, line_color, fill_color);
  347. }
  348. else if (color || custom_rgb || (z_color_flag && Vect_is_3d(Map))) {
  349. if (!table_colors_flag && !cats_color_flag && !z_color_flag)
  350. D_RGB_color(color->r, color->g, color->b);
  351. else {
  352. if (custom_rgb)
  353. D_RGB_color((unsigned char)red, (unsigned char)grn,
  354. (unsigned char)blu);
  355. else
  356. D_RGB_color(color->r, color->g, color->b);
  357. }
  358. /* Plot the lines */
  359. if (Points->n_points == 1) /* line with one coor */
  360. D_polydots_abs(x, y, Points->n_points);
  361. else /*use different user defined render methods */
  362. D_polyline_abs(x, y, Points->n_points);
  363. }
  364. }
  365. if (nerror_rgb > 0) {
  366. G_warning(_("Error in color definition column '%s': %d features affected"),
  367. rgb_column, nerror_rgb);
  368. }
  369. Vect_destroy_line_struct(Points);
  370. Vect_destroy_cats_struct(Cats);
  371. return 0; /* not reached */
  372. }