ps_vlines.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Functions: PS_vector_plot
  2. **
  3. ** Modified by: Janne Soimasuo August 1994 line_cat added
  4. ** Author: Paul W. Carlson March 1992
  5. ** modified to use G_plot_line() by Olga Waupotitsch on dec,93
  6. */
  7. #include <grass/gis.h>
  8. #include <grass/colors.h>
  9. #include <grass/raster.h>
  10. #include <grass/glocale.h>
  11. #include <grass/vector.h>
  12. #include <grass/dbmi.h>
  13. #include "ps_info.h"
  14. #include "clr.h"
  15. #include "local_proto.h"
  16. #include "vector.h"
  17. int PS_vlines_plot(struct Map_info *P_map, int vec, int type)
  18. {
  19. struct line_pnts *Points, *nPoints, *pPoints;
  20. int i, k, np, line, cat, nlines, ret;
  21. double *xarray, *yarray, tol = 0.1, width;
  22. struct line_cats *Cats;
  23. struct varray *Varray = NULL;
  24. /* rgbcol */
  25. dbCatValArray cvarr_rgb;
  26. dbCatVal *cv_rgb;
  27. int red, grn, blu;
  28. char *rgbstring = NULL;
  29. PSCOLOR color;
  30. cv_rgb = NULL;
  31. fprintf(PS.fp, "1 setlinejoin\n"); /* set line join to round */
  32. /* Create vector array if required */
  33. if (vector.layer[vec].cats != NULL || vector.layer[vec].where != NULL) {
  34. Varray = Vect_new_varray(Vect_get_num_lines(P_map));
  35. if (vector.layer[vec].cats != NULL) {
  36. ret =
  37. Vect_set_varray_from_cat_string(P_map,
  38. vector.layer[vec].field,
  39. vector.layer[vec].cats,
  40. vector.layer[vec].ltype, 1,
  41. Varray);
  42. }
  43. else {
  44. ret = Vect_set_varray_from_db(P_map, vector.layer[vec].field,
  45. vector.layer[vec].where,
  46. vector.layer[vec].ltype, 1, Varray);
  47. }
  48. G_debug(3, "%d items selected for vector %d", ret, vec);
  49. if (ret == -1)
  50. G_fatal_error(_("Cannot load data from table"));
  51. }
  52. /* allocate memory for coordinates */
  53. Points = Vect_new_line_struct();
  54. nPoints = Vect_new_line_struct();
  55. Cats = Vect_new_cats_struct();
  56. /* process only vectors in current window */
  57. Vect_set_constraint_region(P_map, PS.w.north, PS.w.south, PS.w.east,
  58. PS.w.west, PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);
  59. tol /= PS.ew_to_x; /* tolerance for parallel map units */
  60. width = vector.layer[vec].width;
  61. /* load attributes if rgbcol used */
  62. if (vector.layer[vec].rgbcol != NULL) {
  63. load_catval_array_rgb(P_map, vec, &cvarr_rgb);
  64. }
  65. /* read and plot vectors */
  66. k = 0;
  67. nlines = Vect_get_num_lines(P_map);
  68. for (line = 1; line <= nlines; line++) {
  69. double d;
  70. if (0 > (ret = Vect_read_line(P_map, Points, Cats, line))) {
  71. if (ret == -1)
  72. G_warning(_("Read error in vector map"));
  73. break;
  74. }
  75. if (!(ret & GV_LINES))
  76. continue;
  77. if (!(ret & vector.layer[vec].ltype))
  78. continue;
  79. if (Varray != NULL && Varray->c[line] == 0)
  80. continue; /* is not in array */
  81. pPoints = Points;
  82. Vect_cat_get(Cats, 1, &cat);
  83. if (vector.layer[vec].cwidth) {
  84. if (cat == 0) /* don't draw zero width line */
  85. continue;
  86. if (type == LINE_DRAW_HIGHLITE)
  87. width =
  88. cat * vector.layer[vec].cwidth +
  89. 2. * vector.layer[vec].hwidth;
  90. if (type == LINE_DRAW_LINE)
  91. width = cat * vector.layer[vec].cwidth;
  92. fprintf(PS.fp, "%.8f W\n", width);
  93. }
  94. /* load line color from rgbcol */
  95. if (vector.layer[vec].rgbcol != NULL) {
  96. rgbstring = NULL;
  97. ret = db_CatValArray_get_value(&cvarr_rgb, cat, &cv_rgb);
  98. if (ret != DB_OK) {
  99. G_warning(_("No record for category [%d]"), cat);
  100. }
  101. else {
  102. rgbstring = db_get_string(cv_rgb->val.s);
  103. if (rgbstring == NULL ||
  104. G_str_to_color(rgbstring, &red, &grn, &blu) != 1) {
  105. G_warning(_("Invalid RGB color definition in column <%s> for category [%d]"),
  106. vector.layer[vec].rgbcol, cat);
  107. rgbstring = NULL;
  108. }
  109. }
  110. if (rgbstring) {
  111. G_debug(3, " dynamic symbol rgb color = %s", rgbstring);
  112. set_color(&color, red, grn, blu);
  113. set_ps_color(&color);
  114. }
  115. else { /* use default symbol */
  116. G_debug(3, " static symbol rgb color = %d:%d:%d",
  117. vector.layer[vec].color.r,
  118. vector.layer[vec].color.g, vector.layer[vec].color.b);
  119. set_ps_color(&(vector.layer[vec].color));
  120. }
  121. }
  122. if (vector.layer[vec].coffset != 0 || vector.layer[vec].offset != 0) {
  123. if (vector.layer[vec].coffset != 0)
  124. d = cat * vector.layer[vec].coffset / PS.ew_to_x;
  125. else
  126. d = vector.layer[vec].offset / PS.ew_to_x;
  127. adjust_line(Points); /* LL projection */
  128. Vect_line_parallel(Points, d, tol, 1, nPoints);
  129. pPoints = nPoints;
  130. }
  131. if (vector.layer[vec].ref == LINE_REF_CENTER) {
  132. np = pPoints->n_points;
  133. xarray = pPoints->x;
  134. yarray = pPoints->y;
  135. if (pPoints->n_points > 1) {
  136. start_line(xarray[0], yarray[0]);
  137. for (i = 0; i < np - 1; i++) {
  138. sec_draw = 0;
  139. G_plot_line(xarray[0], yarray[0], xarray[1], yarray[1]);
  140. if (k == 2) {
  141. fprintf(PS.fp, "\n");
  142. k = 0;
  143. }
  144. else {
  145. fprintf(PS.fp, " ");
  146. k++;
  147. }
  148. xarray++;
  149. yarray++;
  150. }
  151. fprintf(PS.fp, "D\n");
  152. }
  153. }
  154. else {
  155. /* draw line as filled polygon between original line and
  156. parallel line (offset=width) */
  157. d = width / PS.ew_to_x;
  158. if (vector.layer[vec].ref == LINE_REF_RIGHT)
  159. d = -d;
  160. adjust_line(Points); /* LL projection */
  161. Vect_line_parallel(Points, d, tol, 1, nPoints);
  162. Vect_line_reverse(nPoints);
  163. fprintf(PS.fp, "NP\n");
  164. if (Points->n_points > 0) {
  165. construct_path(Points, 0, START_PATH);
  166. construct_path(nPoints, 0, CLOSE_PATH);
  167. }
  168. else {
  169. construct_path(Points, 0, WHOLE_PATH);
  170. }
  171. fprintf(PS.fp, "F\n");
  172. }
  173. Vect_reset_line(Points);
  174. }
  175. fprintf(PS.fp, "\n");
  176. fprintf(PS.fp, "0 setlinejoin\n"); /* reset line join to miter */
  177. return 0;
  178. }