lines.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* plot1() - Level One vector reading */
  2. #include <string.h>
  3. #include <math.h>
  4. #include <grass/gis.h>
  5. #include <grass/raster.h>
  6. #include <grass/vector.h>
  7. #include <grass/display.h>
  8. #include <grass/symbol.h>
  9. #include <grass/glocale.h>
  10. #include <grass/dbmi.h>
  11. #include "plot.h"
  12. #include "local_proto.h"
  13. int palette_ncolors = 16;
  14. struct rgb_color palette[16] = {
  15. {198, 198, 198}, /* 1: light gray */
  16. {127, 127, 127}, /* 2: medium/dark gray */
  17. {255, 0, 0}, /* 3: bright red */
  18. {139, 0, 0}, /* 4: dark red */
  19. {0, 255, 0}, /* 5: bright green */
  20. {0, 139, 0}, /* 6: dark green */
  21. {0, 0, 255}, /* 7: bright blue */
  22. {0, 0, 139}, /* 8: dark blue */
  23. {255, 255, 0}, /* 9: yellow */
  24. {139, 126, 10}, /* 10: olivey brown */
  25. {255, 165, 0}, /* 11: orange */
  26. {255, 192, 203}, /* 12: pink */
  27. {255, 0, 255}, /* 13: magenta */
  28. {139, 0, 139}, /* 14: dark magenta */
  29. {0, 255, 255}, /* 15: cyan */
  30. {0, 139, 139} /* 16: dark cyan */
  31. };
  32. static int draw_line(int, int, int,
  33. const struct line_pnts *, const struct line_cats *,
  34. const struct color_rgb *, const struct color_rgb *, int,
  35. const char *, int, int,
  36. int, int,
  37. int, double,
  38. struct Colors *,
  39. dbCatValArray *, struct Colors *,
  40. dbCatValArray *, int,
  41. dbCatValArray *, int,
  42. dbCatValArray *, int,
  43. const struct cat_list *, SYMBOL *,
  44. RGBA_Color *, RGBA_Color *,
  45. RGBA_Color *,
  46. int *, int *, int *, int *, int *);
  47. int display_lines(struct Map_info *Map, int type, struct cat_list *Clist,
  48. const struct color_rgb *color, const struct color_rgb *fcolor, int chcat,
  49. const char *symbol_name, double size, int sqrt_flag,
  50. int id_flag, int cats_color_flag,
  51. int default_width, double width_scale,
  52. struct Colors* zcolors,
  53. dbCatValArray *cvarr_rgb, struct Colors *colors,
  54. dbCatValArray *cvarr_width, int nrec_width,
  55. dbCatValArray *cvarr_size, int nrec_size,
  56. dbCatValArray *cvarr_rot, int nrec_rot)
  57. {
  58. int ltype, line, nlines;
  59. struct line_pnts *Points;
  60. struct line_cats *Cats;
  61. int n_points, n_lines, n_centroids, n_boundaries, n_faces;
  62. RGBA_Color *line_color, *fill_color, *primary_color;
  63. SYMBOL *Symb;
  64. Symb = NULL;
  65. line_color = G_malloc(sizeof(RGBA_Color));
  66. fill_color = G_malloc(sizeof(RGBA_Color));
  67. primary_color = G_malloc(sizeof(RGBA_Color));
  68. primary_color->a = RGBA_COLOR_OPAQUE;
  69. /* change function prototype to pass RGBA_Color instead of color_rgb? */
  70. if (color) {
  71. line_color->r = color->r;
  72. line_color->g = color->g;
  73. line_color->b = color->b;
  74. line_color->a = RGBA_COLOR_OPAQUE;
  75. }
  76. else
  77. line_color->a = RGBA_COLOR_NONE;
  78. if (fcolor) {
  79. fill_color->r = fcolor->r;
  80. fill_color->g = fcolor->g;
  81. fill_color->b = fcolor->b;
  82. fill_color->a = RGBA_COLOR_OPAQUE;
  83. }
  84. else
  85. fill_color->a = RGBA_COLOR_NONE;
  86. Points = Vect_new_line_struct();
  87. Cats = Vect_new_cats_struct();
  88. /* dynamic symbols for points */
  89. if (!(nrec_size || nrec_rot)) {
  90. Symb = S_read(symbol_name);
  91. if (!Symb)
  92. G_warning(_("Unable to read symbol <%s>, unable to display points"),
  93. symbol_name);
  94. else
  95. S_stroke(Symb, size, 0.0, 0);
  96. }
  97. Vect_rewind(Map);
  98. if (color && !cvarr_rgb && !cats_color_flag)
  99. D_RGB_color(color->r, color->g, color->b);
  100. nlines = -1;
  101. if (id_flag) {
  102. if (Vect_level(Map) < 2)
  103. G_fatal_error(_("Unable to display features by id, topology not available. "
  104. "Please try to rebuild topology using "
  105. "v.build or v.build.all."));
  106. nlines = Vect_get_num_lines(Map);
  107. }
  108. line = 0;
  109. n_points = n_lines = 0;
  110. n_centroids = n_boundaries = 0;
  111. n_faces = 0;
  112. while (TRUE) {
  113. line++;
  114. if (nlines > -1) {
  115. if (line > nlines)
  116. break;
  117. ltype = Vect_read_line(Map, Points, Cats, line);
  118. }
  119. else {
  120. ltype = Vect_read_next_line(Map, Points, Cats);
  121. if (ltype == -1) {
  122. G_fatal_error(_("Unable to read vector map"));
  123. }
  124. else if (ltype == -2) { /* EOF */
  125. break;
  126. }
  127. }
  128. draw_line(type, ltype, line,
  129. Points, Cats,
  130. color, fcolor, chcat,
  131. symbol_name, size, sqrt_flag,
  132. id_flag, cats_color_flag,
  133. default_width, width_scale,
  134. zcolors,
  135. cvarr_rgb, colors,
  136. cvarr_width, nrec_width,
  137. cvarr_size, nrec_size,
  138. cvarr_rot, nrec_rot,
  139. Clist, Symb,
  140. line_color, fill_color,
  141. primary_color,
  142. &n_points, &n_lines, &n_centroids, &n_boundaries,
  143. &n_faces);
  144. }
  145. if (n_points > 0)
  146. G_verbose_message(_("%d points plotted"), n_points);
  147. if (n_lines > 0)
  148. G_verbose_message(_("%d lines plotted"), n_lines);
  149. if (n_centroids > 0)
  150. G_verbose_message(_("%d centroids plotted"), n_centroids);
  151. if (n_boundaries > 0)
  152. G_verbose_message(_("%d boundaries plotted"), n_boundaries);
  153. if (n_faces > 0)
  154. G_verbose_message(_("%d faces plotted"), n_faces);
  155. Vect_destroy_line_struct(Points);
  156. Vect_destroy_cats_struct(Cats);
  157. G_free(line_color);
  158. G_free(fill_color);
  159. G_free(primary_color);
  160. return 0;
  161. }
  162. int draw_line(int type, int ltype, int line,
  163. const struct line_pnts *Points, const struct line_cats *Cats,
  164. const struct color_rgb *color, const struct color_rgb *fcolor, int chcat,
  165. const char *symbol_name, int size, int sqrt_flag,
  166. int id_flag, int cats_color_flag,
  167. int default_width, double width_scale,
  168. struct Colors *zcolors,
  169. dbCatValArray *cvarr_rgb, struct Colors *colors,
  170. dbCatValArray *cvarr_width, int nrec_width,
  171. dbCatValArray *cvarr_size, int nrec_size,
  172. dbCatValArray *cvarr_rot, int nrec_rot,
  173. const struct cat_list *Clist, SYMBOL *Symb,
  174. RGBA_Color *line_color, RGBA_Color *fill_color,
  175. RGBA_Color *primary_color,
  176. int *n_points, int *n_lines, int *n_centroids, int *n_boundaries,
  177. int *n_faces)
  178. {
  179. double var_size, rotation;
  180. int i, width;
  181. int red, grn, blu;
  182. int custom_rgb;
  183. double x0, y0;
  184. double *x, *y;
  185. int found, cat;
  186. rotation = 0.0;
  187. var_size = size;
  188. custom_rgb = FALSE;
  189. cat = -1;
  190. if (!(type & ltype))
  191. return 0;
  192. if (Points->n_points == 0)
  193. return 0;
  194. found = FALSE;
  195. if (chcat) {
  196. if (id_flag) { /* use line id */
  197. if (!(Vect_cat_in_cat_list(line, Clist)))
  198. return 0;
  199. }
  200. else {
  201. for (i = 0; i < Cats->n_cats; i++) {
  202. if (Cats->field[i] == Clist->field &&
  203. Vect_cat_in_cat_list(Cats->cat[i], Clist)) {
  204. found = TRUE;
  205. break;
  206. }
  207. }
  208. if (!found)
  209. return 0;
  210. }
  211. }
  212. else if (Clist->field > 0) {
  213. for (i = 0; i < Cats->n_cats; i++) {
  214. if (Cats->field[i] == Clist->field) {
  215. found = TRUE;
  216. break;
  217. }
  218. }
  219. /* lines with no category will be displayed */
  220. if (Cats->n_cats > 0 && !found)
  221. return 0;
  222. }
  223. G_debug(3, "\tdisplay feature %d, cat %d", line, cat);
  224. /* z height colors */
  225. if (zcolors) {
  226. if (Rast_get_d_color(&Points->z[0], &red, &grn, &blu, zcolors) == 1)
  227. custom_rgb = TRUE;
  228. else
  229. custom_rgb = FALSE;
  230. }
  231. if (colors || cvarr_rgb ||
  232. nrec_width > 0 || nrec_size > 0 || nrec_rot > 0)
  233. /* only first category */
  234. Vect_cat_get(Cats,
  235. (Clist->field > 0 ? Clist->field : (Cats->n_cats > 0 ? Cats->field[0] : 1)),
  236. &cat);
  237. /* custom colors */
  238. if (colors || cvarr_rgb) {
  239. custom_rgb = get_table_color(cat, line, colors, cvarr_rgb,
  240. &red, &grn, &blu);
  241. }
  242. /* random colors */
  243. if (cats_color_flag) {
  244. custom_rgb = get_cat_color(line, Cats, Clist,
  245. &red, &grn, &blu);
  246. }
  247. /* line width */
  248. if (nrec_width) {
  249. width = (int) get_property(cat, line, cvarr_width,
  250. (double) width_scale,
  251. (double) default_width);
  252. D_line_width(width);
  253. }
  254. /* enough of the prep work, lets start plotting stuff */
  255. x = Points->x;
  256. y = Points->y;
  257. if ((ltype & GV_POINTS) && (Symb != NULL || (nrec_size || nrec_rot)) ) {
  258. if (!(color || fcolor || custom_rgb))
  259. return 0;
  260. x0 = x[0];
  261. y0 = y[0];
  262. /* skip if the point is outside of the display window */
  263. /* xy < 0 tests make it go ever-so-slightly faster */
  264. if (x0 > D_get_u_east() || x0 < D_get_u_west() ||
  265. y0 < D_get_u_south() || y0 > D_get_u_north())
  266. return 0;
  267. /* dynamic symbol size */
  268. if (nrec_size)
  269. var_size = get_property(cat, line, cvarr_size, size, size);
  270. if (sqrt_flag)
  271. var_size = sqrt(var_size);
  272. /* dynamic symbol rotation */
  273. if (nrec_rot)
  274. rotation = get_property(cat, line, cvarr_rot, 1.0, 0.0);
  275. if(nrec_size || nrec_rot) {
  276. G_debug(3, "\tdynamic symbol: cat=%d size=%.2f rotation=%.2f",
  277. cat, var_size, rotation);
  278. /* symbol stroking is cumulative, so we need to reread it each time */
  279. if(Symb) /* unclean free() on first iteration if variables are not init'd to NULL? */
  280. G_free(Symb);
  281. Symb = S_read(symbol_name);
  282. if (Symb == NULL)
  283. G_warning(_("Unable to read symbol <%s>, unable to display points"),
  284. symbol_name);
  285. else
  286. S_stroke(Symb, var_size, rotation, 0);
  287. }
  288. /* use random or RGB column color if given, otherwise reset */
  289. /* centroids always use default color to stand out from underlying area */
  290. if (custom_rgb && (ltype != GV_CENTROID)) {
  291. primary_color->r = (unsigned char)red;
  292. primary_color->g = (unsigned char)grn;
  293. primary_color->b = (unsigned char)blu;
  294. D_symbol2(Symb, x0, y0, primary_color, line_color);
  295. }
  296. else
  297. D_symbol(Symb, x0, y0, line_color, fill_color);
  298. /* reset to defaults */
  299. var_size = size;
  300. rotation = 0.0;
  301. }
  302. else if (color || custom_rgb || zcolors) {
  303. if (!cvarr_rgb && !cats_color_flag && !zcolors && !colors)
  304. D_RGB_color(color->r, color->g, color->b);
  305. else {
  306. if (custom_rgb)
  307. D_RGB_color((unsigned char)red, (unsigned char)grn,
  308. (unsigned char)blu);
  309. else
  310. D_RGB_color(color->r, color->g, color->b);
  311. }
  312. /* Plot the lines */
  313. if (Points->n_points == 1) /* line with one coor */
  314. D_polydots_abs(x, y, Points->n_points);
  315. else /* use different user defined render methods */
  316. D_polyline_abs(x, y, Points->n_points);
  317. }
  318. switch (ltype) {
  319. case GV_POINT:
  320. (*n_points)++;
  321. break;
  322. case GV_LINE:
  323. (*n_lines)++;
  324. break;
  325. case GV_CENTROID:
  326. (*n_centroids)++;
  327. break;
  328. case GV_BOUNDARY:
  329. (*n_boundaries)++;
  330. break;
  331. case GV_FACE:
  332. (*n_faces)++;
  333. break;
  334. default:
  335. break;
  336. }
  337. return 1;
  338. }