plot1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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(_("Cannot read field info"));
  91. }
  92. driver = db_start_driver_open_database(fi->driver, fi->database);
  93. if (driver == NULL)
  94. G_fatal_error(_("Cannot open database %s by driver %s"),
  95. fi->database, fi->driver);
  96. }
  97. if (table_colors_flag) {
  98. /* for reading RRR:GGG:BBB color strings from table */
  99. if (rgb_column == NULL || *rgb_column == '\0')
  100. G_fatal_error(_("Color definition column not specified"));
  101. db_CatValArray_init(&cvarr_rgb);
  102. nrec_rgb = db_select_CatValArray(driver, fi->table, fi->key,
  103. rgb_column, NULL, &cvarr_rgb);
  104. G_debug(3, "nrec_rgb (%s) = %d", rgb_column, nrec_rgb);
  105. if (cvarr_rgb.ctype != DB_C_TYPE_STRING)
  106. G_fatal_error(_("Color definition column (%s) not a string. "
  107. "Column must be of form RRR:GGG:BBB where RGB values range 0-255."),
  108. rgb_column);
  109. if (nrec_rgb < 0)
  110. G_fatal_error(_("Cannot select data (%s) from table"),
  111. rgb_column);
  112. G_debug(2, "\n%d records selected from table", nrec_rgb);
  113. for (i = 0; i < cvarr_rgb.n_values; i++) {
  114. G_debug(4, "cat = %d %s = %s", cvarr_rgb.value[i].cat,
  115. rgb_column, db_get_string(cvarr_rgb.value[i].val.s));
  116. }
  117. }
  118. if (width_column) {
  119. if (*width_column == '\0')
  120. G_fatal_error(_("Line width column not specified."));
  121. db_CatValArray_init(&cvarr_width);
  122. nrec_width = db_select_CatValArray(driver, fi->table, fi->key,
  123. width_column, NULL, &cvarr_width);
  124. G_debug(3, "nrec_width (%s) = %d", width_column, nrec_width);
  125. if (cvarr_width.ctype != DB_C_TYPE_INT &&
  126. cvarr_width.ctype != DB_C_TYPE_DOUBLE)
  127. G_fatal_error(_("Line width column (%s) not a number."),
  128. width_column);
  129. if (nrec_width < 0)
  130. G_fatal_error(_("Cannot select data (%s) from table"),
  131. width_column);
  132. G_debug(2, "\n%d records selected from table", nrec_width);
  133. for (i = 0; i < cvarr_width.n_values; i++) {
  134. G_debug(4, "cat = %d %s = %d", cvarr_width.value[i].cat,
  135. width_column,
  136. (cvarr_width.ctype ==
  137. DB_C_TYPE_INT ? cvarr_width.value[i].val.
  138. i : (int)cvarr_width.value[i].val.d));
  139. }
  140. }
  141. if (open_db)
  142. db_close_database_shutdown_driver(driver);
  143. Vect_rewind(Map);
  144. /* Is it necessary to reset line/label color in each loop ? */
  145. if (color && !table_colors_flag && !cats_color_flag)
  146. D_RGB_color(color->r, color->g, color->b);
  147. if (Vect_level(Map) >= 2)
  148. nlines = Vect_get_num_lines(Map);
  149. line = 0;
  150. while (1) {
  151. if (Vect_level(Map) >= 2) {
  152. line++;
  153. if (line > nlines)
  154. break;
  155. if (!Vect_line_alive(Map, line))
  156. continue;
  157. ltype = Vect_read_line(Map, Points, Cats, line);
  158. }
  159. else {
  160. ltype = Vect_read_next_line(Map, Points, Cats);
  161. switch (ltype) {
  162. case -1:
  163. G_warning(_("Unable to read vector map"));
  164. return -1;
  165. case -2: /* EOF */
  166. break;
  167. }
  168. }
  169. if (!(type & ltype))
  170. continue;
  171. if (Points->n_points == 0)
  172. continue;
  173. if (chcat) {
  174. int found = 0;
  175. if (id_flag) { /* use line id */
  176. if (!(Vect_cat_in_cat_list(line, Clist)))
  177. continue;
  178. }
  179. else {
  180. for (i = 0; i < Cats->n_cats; i++) {
  181. if (Cats->field[i] == Clist->field &&
  182. Vect_cat_in_cat_list(Cats->cat[i], Clist)) {
  183. found = 1;
  184. break;
  185. }
  186. }
  187. if (!found)
  188. continue;
  189. }
  190. }
  191. else if (Clist->field > 0) {
  192. int found = 0;
  193. for (i = 0; i < Cats->n_cats; i++) {
  194. if (Cats->field[i] == Clist->field) {
  195. found = 1;
  196. break;
  197. }
  198. }
  199. /* lines with no category will be displayed */
  200. if (Cats->n_cats > 0 && !found)
  201. continue;
  202. }
  203. /* z height colors */
  204. if (z_color_flag && Vect_is_3d(Map)) {
  205. BOUND_BOX box;
  206. double zval;
  207. struct Colors colors;
  208. Vect_get_map_box(Map, &box);
  209. zval = Points->z[0];
  210. G_debug(3, "display line %d, cat %d, x: %f, y: %f, z: %f", line,
  211. cat, Points->x[0], Points->y[0], Points->z[0]);
  212. custom_rgb = TRUE;
  213. G_make_fp_colors(&colors, style, box.B, box.T);
  214. G_get_raster_color(&zval, &red, &grn, &blu, &colors, DCELL_TYPE);
  215. G_debug(3, "b %d, g: %d, r %d", blu, grn, red);
  216. }
  217. if (table_colors_flag) {
  218. /* only first category */
  219. cat = Vect_get_line_cat(Map, line,
  220. (Clist->field > 0 ? Clist->field :
  221. (Cats->n_cats >
  222. 0 ? Cats->field[0] : 1)));
  223. if (cat >= 0) {
  224. G_debug(3, "display element %d, cat %d", line, cat);
  225. /* Read RGB colors from db for current area # */
  226. if (db_CatValArray_get_value(&cvarr_rgb, cat, &cv_rgb) !=
  227. DB_OK) {
  228. custom_rgb = FALSE;
  229. }
  230. else {
  231. sprintf(colorstring, "%s", db_get_string(cv_rgb->val.s));
  232. if (*colorstring != '\0') {
  233. G_debug(3, "element %d: colorstring: %s", line,
  234. colorstring);
  235. if (G_str_to_color(colorstring, &red, &grn, &blu) ==
  236. 1) {
  237. custom_rgb = TRUE;
  238. G_debug(3, "element:%d cat %d r:%d g:%d b:%d",
  239. line, cat, red, grn, blu);
  240. }
  241. else {
  242. custom_rgb = FALSE;
  243. G_important_message(_("Error in color definition column '%s', feature id %d "
  244. "with cat %d: colorstring '%s'"),
  245. rgb_column, line, cat, colorstring);
  246. nerror_rgb++;
  247. }
  248. }
  249. else {
  250. custom_rgb = FALSE;
  251. G_important_message(_("Error in color definition column '%s', feature id %d "
  252. "with cat %d"),
  253. rgb_column, line, cat);
  254. nerror_rgb++;
  255. }
  256. }
  257. } /* end if cat */
  258. else {
  259. custom_rgb = FALSE;
  260. }
  261. } /* end if table_colors_flag */
  262. /* random colors */
  263. if (cats_color_flag) {
  264. custom_rgb = FALSE;
  265. if (Clist->field > 0) {
  266. cat = Vect_get_line_cat(Map, line, Clist->field);
  267. if (cat >= 0) {
  268. G_debug(3, "display element %d, cat %d", line, cat);
  269. /* fetch color number from category */
  270. which = (cat % palette_ncolors);
  271. G_debug(3, "cat:%d which color:%d r:%d g:%d b:%d", cat,
  272. which, palette[which].R, palette[which].G,
  273. palette[which].B);
  274. custom_rgb = TRUE;
  275. red = palette[which].R;
  276. grn = palette[which].G;
  277. blu = palette[which].B;
  278. }
  279. }
  280. else if (Cats->n_cats > 0) {
  281. /* fetch color number from layer */
  282. which = (Cats->field[0] % palette_ncolors);
  283. G_debug(3, "layer:%d which color:%d r:%d g:%d b:%d",
  284. Cats->field[0], which, palette[which].R,
  285. palette[which].G, palette[which].B);
  286. custom_rgb = TRUE;
  287. red = palette[which].R;
  288. grn = palette[which].G;
  289. blu = palette[which].B;
  290. }
  291. }
  292. if (nrec_width) {
  293. /* only first category */
  294. cat = Vect_get_line_cat(Map, line,
  295. (Clist->field > 0 ? Clist->field :
  296. (Cats->n_cats >
  297. 0 ? Cats->field[0] : 1)));
  298. if (cat >= 0) {
  299. G_debug(3, "display element %d, cat %d", line, cat);
  300. /* Read line width from db for current area # */
  301. if (db_CatValArray_get_value(&cvarr_width, cat, &cv_width) !=
  302. DB_OK) {
  303. width = default_width;
  304. }
  305. else {
  306. width =
  307. width_scale * (cvarr_width.ctype ==
  308. DB_C_TYPE_INT ? cv_width->val.
  309. i : (int)cv_width->val.d);
  310. if (width < 0) {
  311. G_warning(_("Error in line width column (%s), element %d "
  312. "with cat %d: line width [%d]"),
  313. width_column, line, cat, width);
  314. width = default_width;
  315. }
  316. }
  317. } /* end if cat */
  318. else {
  319. width = default_width;
  320. }
  321. D_line_width(width);
  322. } /* end if nrec_width */
  323. /* enough of the prep work, lets start plotting stuff */
  324. x = Points->x;
  325. y = Points->y;
  326. if ((ltype & GV_POINTS) && Symb != NULL) {
  327. if (!(color || fcolor || custom_rgb))
  328. continue;
  329. x0 = x[0];
  330. y0 = y[0];
  331. /* skip if the point is outside of the display window */
  332. /* xy<0 tests make it go ever-so-slightly faster */
  333. if (x0 > D_get_u_east() || x0 < D_get_u_west() ||
  334. y0 < D_get_u_south() || y0 > D_get_u_north())
  335. continue;
  336. /* use random or RGB column color if given, otherwise reset */
  337. /* centroids always use default color to stand out from underlying area */
  338. if (custom_rgb && (ltype != GV_CENTROID)) {
  339. primary_color->r = (unsigned char)red;
  340. primary_color->g = (unsigned char)grn;
  341. primary_color->b = (unsigned char)blu;
  342. D_symbol2(Symb, x0, y0, primary_color, line_color);
  343. }
  344. else
  345. D_symbol(Symb, x0, y0, line_color, fill_color);
  346. }
  347. else if (color || custom_rgb || (z_color_flag && Vect_is_3d(Map))) {
  348. if (!table_colors_flag && !cats_color_flag && !z_color_flag)
  349. D_RGB_color(color->r, color->g, color->b);
  350. else {
  351. if (custom_rgb)
  352. D_RGB_color((unsigned char)red, (unsigned char)grn,
  353. (unsigned char)blu);
  354. else
  355. D_RGB_color(color->r, color->g, color->b);
  356. }
  357. /* Plot the lines */
  358. if (Points->n_points == 1) /* line with one coor */
  359. D_polydots_abs(x, y, Points->n_points);
  360. else /*use different user defined render methods */
  361. D_polyline_abs(x, y, Points->n_points);
  362. }
  363. }
  364. if (nerror_rgb > 0) {
  365. G_warning(_("Error in color definition column '%s': %d features affected"),
  366. rgb_column, nerror_rgb);
  367. }
  368. Vect_destroy_line_struct(Points);
  369. Vect_destroy_cats_struct(Cats);
  370. return 0; /* not reached */
  371. }