plot1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. /*global render switch */
  34. int render;
  35. /* *************************************************************** */
  36. /* function to use different render methods for polylines ******** */
  37. /* *************************************************************** */
  38. void plot_polyline(double *xf, double *yf, int n)
  39. {
  40. switch (render) {
  41. case RENDER_DP:
  42. D_polyline_abs(xf, yf, n);
  43. break;
  44. case RENDER_DPC:
  45. D_polyline_abs_clip(xf, yf, n);
  46. break;
  47. case RENDER_DPL:
  48. D_polyline_abs_cull(xf, yf, n);
  49. break;
  50. }
  51. }
  52. /* *************************************************************** */
  53. /* function to use different render methods for polygons ******** */
  54. /* *************************************************************** */
  55. void plot_polygon(double *xf, double *yf, int n)
  56. {
  57. switch (render) {
  58. case RENDER_DP:
  59. D_polygon_abs(xf, yf, n);
  60. break;
  61. case RENDER_DPC:
  62. D_polygon_abs_clip(xf, yf, n);
  63. break;
  64. case RENDER_DPL:
  65. D_polygon_abs_cull(xf, yf, n);
  66. break;
  67. }
  68. }
  69. /* *************************************************************** */
  70. /* *************************************************************** */
  71. /* *************************************************************** */
  72. int plot1(struct Map_info *Map, int type, int area, struct cat_list *Clist,
  73. const struct color_rgb *color, const struct color_rgb *fcolor,
  74. int chcat, SYMBOL * Symb, int size, int id_flag,
  75. int table_colors_flag, int cats_color_flag, char *rgb_column,
  76. int default_width, char *width_column, double width_scale,
  77. int z_color_flag, char *style)
  78. {
  79. int i, ltype, nlines = 0, line, cat = -1;
  80. double *x, *y;
  81. struct line_pnts *Points, *PPoints;
  82. struct line_cats *Cats;
  83. double x0, y0;
  84. struct field_info *fi = NULL;
  85. dbDriver *driver = NULL;
  86. dbCatValArray cvarr_rgb, cvarr_width;
  87. dbCatVal *cv_rgb = NULL, *cv_width = NULL;
  88. int nrec_rgb = 0, nrec_width = 0;
  89. int open_db;
  90. int custom_rgb = FALSE;
  91. char colorstring[12]; /* RRR:GGG:BBB */
  92. int red, grn, blu;
  93. RGBA_Color *line_color, *fill_color, *primary_color;
  94. unsigned char which;
  95. int width;
  96. line_color = G_malloc(sizeof(RGBA_Color));
  97. fill_color = G_malloc(sizeof(RGBA_Color));
  98. primary_color = G_malloc(sizeof(RGBA_Color));
  99. primary_color->a = RGBA_COLOR_OPAQUE;
  100. /* change function prototype to pass RGBA_Color instead of color_rgb? */
  101. if (color) {
  102. line_color->r = color->r;
  103. line_color->g = color->g;
  104. line_color->b = color->b;
  105. line_color->a = RGBA_COLOR_OPAQUE;
  106. }
  107. else
  108. line_color->a = RGBA_COLOR_NONE;
  109. if (fcolor) {
  110. fill_color->r = fcolor->r;
  111. fill_color->g = fcolor->g;
  112. fill_color->b = fcolor->b;
  113. fill_color->a = RGBA_COLOR_OPAQUE;
  114. }
  115. else
  116. fill_color->a = RGBA_COLOR_NONE;
  117. Points = Vect_new_line_struct();
  118. PPoints = Vect_new_line_struct();
  119. Cats = Vect_new_cats_struct();
  120. open_db = table_colors_flag || width_column;
  121. if (open_db) {
  122. fi = Vect_get_field(Map, (Clist->field > 0 ? Clist->field : 1));
  123. if (fi == NULL) {
  124. G_fatal_error(_("Cannot read field info"));
  125. }
  126. driver = db_start_driver_open_database(fi->driver, fi->database);
  127. if (driver == NULL)
  128. G_fatal_error(_("Cannot open database %s by driver %s"),
  129. fi->database, fi->driver);
  130. }
  131. if (table_colors_flag) {
  132. /* for reading RRR:GGG:BBB color strings from table */
  133. if (rgb_column == NULL || *rgb_column == '\0')
  134. G_fatal_error(_("Color definition column not specified."));
  135. db_CatValArray_init(&cvarr_rgb);
  136. nrec_rgb = db_select_CatValArray(driver, fi->table, fi->key,
  137. rgb_column, NULL, &cvarr_rgb);
  138. G_debug(3, "nrec_rgb (%s) = %d", rgb_column, nrec_rgb);
  139. if (cvarr_rgb.ctype != DB_C_TYPE_STRING)
  140. G_fatal_error(_("Color definition column (%s) not a string. "
  141. "Column must be of form RRR:GGG:BBB where RGB values range 0-255."),
  142. rgb_column);
  143. if (nrec_rgb < 0)
  144. G_fatal_error(_("Cannot select data (%s) from table"),
  145. rgb_column);
  146. G_debug(2, "\n%d records selected from table", nrec_rgb);
  147. for (i = 0; i < cvarr_rgb.n_values; i++) {
  148. G_debug(4, "cat = %d %s = %s", cvarr_rgb.value[i].cat,
  149. rgb_column, db_get_string(cvarr_rgb.value[i].val.s));
  150. }
  151. }
  152. if (width_column) {
  153. if (*width_column == '\0')
  154. G_fatal_error(_("Line width column not specified."));
  155. db_CatValArray_init(&cvarr_width);
  156. nrec_width = db_select_CatValArray(driver, fi->table, fi->key,
  157. width_column, NULL, &cvarr_width);
  158. G_debug(3, "nrec_width (%s) = %d", width_column, nrec_width);
  159. if (cvarr_width.ctype != DB_C_TYPE_INT &&
  160. cvarr_width.ctype != DB_C_TYPE_DOUBLE)
  161. G_fatal_error(_("Line width column (%s) not a number."),
  162. width_column);
  163. if (nrec_width < 0)
  164. G_fatal_error(_("Cannot select data (%s) from table"),
  165. width_column);
  166. G_debug(2, "\n%d records selected from table", nrec_width);
  167. for (i = 0; i < cvarr_width.n_values; i++) {
  168. G_debug(4, "cat = %d %s = %d", cvarr_width.value[i].cat,
  169. width_column,
  170. (cvarr_width.ctype ==
  171. DB_C_TYPE_INT ? cvarr_width.value[i].val.
  172. i : (int)cvarr_width.value[i].val.d));
  173. }
  174. }
  175. if (open_db)
  176. db_close_database_shutdown_driver(driver);
  177. Vect_rewind(Map);
  178. /* Is it necessary to reset line/label color in each loop ? */
  179. if (color && !table_colors_flag && !cats_color_flag)
  180. D_RGB_color(color->r, color->g, color->b);
  181. if (Vect_level(Map) >= 2)
  182. nlines = Vect_get_num_lines(Map);
  183. line = 0;
  184. while (1) {
  185. if (Vect_level(Map) >= 2) {
  186. line++;
  187. if (line > nlines)
  188. return 0;
  189. if (!Vect_line_alive(Map, line))
  190. continue;
  191. ltype = Vect_read_line(Map, Points, Cats, line);
  192. }
  193. else {
  194. ltype = Vect_read_next_line(Map, Points, Cats);
  195. switch (ltype) {
  196. case -1:
  197. G_warning(_("Unable to read vector map"));
  198. return -1;
  199. case -2: /* EOF */
  200. return 0;
  201. }
  202. }
  203. if (!(type & ltype))
  204. continue;
  205. if (Points->n_points == 0)
  206. continue;
  207. if (chcat) {
  208. int found = 0;
  209. if (id_flag) { /* use line id */
  210. if (!(Vect_cat_in_cat_list(line, Clist)))
  211. continue;
  212. }
  213. else {
  214. for (i = 0; i < Cats->n_cats; i++) {
  215. if (Cats->field[i] == Clist->field &&
  216. Vect_cat_in_cat_list(Cats->cat[i], Clist)) {
  217. found = 1;
  218. break;
  219. }
  220. }
  221. if (!found)
  222. continue;
  223. }
  224. }
  225. else if (Clist->field > 0) {
  226. int found = 0;
  227. for (i = 0; i < Cats->n_cats; i++) {
  228. if (Cats->field[i] == Clist->field) {
  229. found = 1;
  230. break;
  231. }
  232. }
  233. /* lines with no category will be displayed */
  234. if (Cats->n_cats > 0 && !found)
  235. continue;
  236. }
  237. /* z height colors */
  238. if (z_color_flag && Vect_is_3d(Map)) {
  239. BOUND_BOX box;
  240. double zval;
  241. struct Colors colors;
  242. Vect_get_map_box(Map, &box);
  243. zval = Points->z[0];
  244. G_debug(3, "display line %d, cat %d, x: %f, y: %f, z: %f", line,
  245. cat, Points->x[0], Points->y[0], Points->z[0]);
  246. custom_rgb = TRUE;
  247. G_make_fp_colors(&colors, style, box.B, box.T);
  248. G_get_raster_color(&zval, &red, &grn, &blu, &colors, DCELL_TYPE);
  249. G_debug(3, "b %d, g: %d, r %d", blu, grn, red);
  250. }
  251. if (table_colors_flag) {
  252. /* only first category */
  253. cat = Vect_get_line_cat(Map, line,
  254. (Clist->field > 0 ? Clist->field :
  255. (Cats->n_cats >
  256. 0 ? Cats->field[0] : 1)));
  257. if (cat >= 0) {
  258. G_debug(3, "display element %d, cat %d", line, cat);
  259. /* Read RGB colors from db for current area # */
  260. if (db_CatValArray_get_value(&cvarr_rgb, cat, &cv_rgb) !=
  261. DB_OK) {
  262. custom_rgb = FALSE;
  263. }
  264. else {
  265. sprintf(colorstring, "%s", db_get_string(cv_rgb->val.s));
  266. if (*colorstring != '\0') {
  267. G_debug(3, "element %d: colorstring: %s", line,
  268. colorstring);
  269. if (G_str_to_color(colorstring, &red, &grn, &blu) ==
  270. 1) {
  271. custom_rgb = TRUE;
  272. G_debug(3, "element:%d cat %d r:%d g:%d b:%d",
  273. line, cat, red, grn, blu);
  274. }
  275. else {
  276. custom_rgb = FALSE;
  277. G_warning(_("Error in color definition column (%s), element %d "
  278. "with cat %d: colorstring [%s]"),
  279. rgb_column, line, cat, colorstring);
  280. }
  281. }
  282. else {
  283. custom_rgb = FALSE;
  284. G_warning(_("Error in color definition column (%s), element %d with cat %d"),
  285. rgb_column, line, cat);
  286. }
  287. }
  288. } /* end if cat */
  289. else {
  290. custom_rgb = FALSE;
  291. }
  292. } /* end if table_colors_flag */
  293. /* random colors */
  294. if (cats_color_flag) {
  295. custom_rgb = FALSE;
  296. if (Clist->field > 0) {
  297. cat = Vect_get_line_cat(Map, line, Clist->field);
  298. if (cat >= 0) {
  299. G_debug(3, "display element %d, cat %d", line, cat);
  300. /* fetch color number from category */
  301. which = (cat % palette_ncolors);
  302. G_debug(3, "cat:%d which color:%d r:%d g:%d b:%d", cat,
  303. which, palette[which].R, palette[which].G,
  304. palette[which].B);
  305. custom_rgb = TRUE;
  306. red = palette[which].R;
  307. grn = palette[which].G;
  308. blu = palette[which].B;
  309. }
  310. }
  311. else if (Cats->n_cats > 0) {
  312. /* fetch color number from layer */
  313. which = (Cats->field[0] % palette_ncolors);
  314. G_debug(3, "layer:%d which color:%d r:%d g:%d b:%d",
  315. Cats->field[0], which, palette[which].R,
  316. palette[which].G, palette[which].B);
  317. custom_rgb = TRUE;
  318. red = palette[which].R;
  319. grn = palette[which].G;
  320. blu = palette[which].B;
  321. }
  322. }
  323. if (nrec_width) {
  324. /* only first category */
  325. cat = Vect_get_line_cat(Map, line,
  326. (Clist->field > 0 ? Clist->field :
  327. (Cats->n_cats >
  328. 0 ? Cats->field[0] : 1)));
  329. if (cat >= 0) {
  330. G_debug(3, "display element %d, cat %d", line, cat);
  331. /* Read line width from db for current area # */
  332. if (db_CatValArray_get_value(&cvarr_width, cat, &cv_width) !=
  333. DB_OK) {
  334. width = default_width;
  335. }
  336. else {
  337. width =
  338. width_scale * (cvarr_width.ctype ==
  339. DB_C_TYPE_INT ? cv_width->val.
  340. i : (int)cv_width->val.d);
  341. if (width < 0) {
  342. G_warning(_("Error in line width column (%s), element %d "
  343. "with cat %d: line width [%d]"),
  344. width_column, line, cat, width);
  345. width = default_width;
  346. }
  347. }
  348. } /* end if cat */
  349. else {
  350. width = default_width;
  351. }
  352. D_line_width(width);
  353. } /* end if nrec_width */
  354. /* enough of the prep work, lets start plotting stuff */
  355. x = Points->x;
  356. y = Points->y;
  357. if ((ltype & GV_POINTS) && Symb != NULL) {
  358. if (!(color || fcolor || custom_rgb))
  359. continue;
  360. x0 = D_u_to_d_col(x[0]);
  361. y0 = D_u_to_d_row(y[0]);
  362. /* skip if the point is outside of the display window */
  363. /* xy<0 tests make it go ever-so-slightly faster */
  364. if (x0 < 0 || y0 < 0 ||
  365. x0 > D_get_d_east() || x0 < D_get_d_west() ||
  366. y0 > D_get_d_south() || y0 < D_get_d_north())
  367. continue;
  368. /* use random or RGB column color if given, otherwise reset */
  369. /* centroids always use default color to stand out from underlying area */
  370. if (custom_rgb && (ltype != GV_CENTROID)) {
  371. primary_color->r = (unsigned char)red;
  372. primary_color->g = (unsigned char)grn;
  373. primary_color->b = (unsigned char)blu;
  374. D_symbol2(Symb, x0, y0, primary_color, line_color);
  375. }
  376. else
  377. D_symbol(Symb, x0, y0, line_color, fill_color);
  378. }
  379. else if (color || custom_rgb || (z_color_flag && Vect_is_3d(Map))) {
  380. if (!table_colors_flag && !cats_color_flag && !z_color_flag)
  381. D_RGB_color(color->r, color->g, color->b);
  382. else {
  383. if (custom_rgb)
  384. D_RGB_color((unsigned char)red, (unsigned char)grn,
  385. (unsigned char)blu);
  386. else
  387. D_RGB_color(color->r, color->g, color->b);
  388. }
  389. /* Plot the lines */
  390. if (Points->n_points == 1) /* line with one coor */
  391. D_polydots_abs_clip(x, y, Points->n_points);
  392. else /*use different user defined render methods */
  393. plot_polyline(x, y, Points->n_points);
  394. }
  395. }
  396. Vect_destroy_line_struct(Points);
  397. Vect_destroy_cats_struct(Cats);
  398. return 0; /* not reached */
  399. }