ps_vpoints.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/colors.h>
  8. #include <grass/raster.h>
  9. #include <grass/vector.h>
  10. #include <grass/dbmi.h>
  11. #include <grass/symbol.h>
  12. #include <grass/glocale.h>
  13. #include "clr.h"
  14. #include "local_proto.h"
  15. #include "vector.h"
  16. int PS_vpoints_plot(struct Map_info *P_map, int vec)
  17. {
  18. struct line_pnts *Points;
  19. int line, ltype, cat, nlines, ret;
  20. struct line_cats *Cats;
  21. char eps[50], epsfile[1024], sname[100];
  22. double nn, ee;
  23. double size, x, y, xt, yt;
  24. double llx, lly, urx, ury;
  25. int x_int, y_int, eps_exist;
  26. SYMBOL *Symb;
  27. struct varray *Varray = NULL;
  28. /* Attributes if sizecol is used */
  29. dbCatValArray cvarr_size;
  30. int size_val_int;
  31. double size_val;
  32. /* rotation column */
  33. dbCatValArray cvarr_rot;
  34. double rotate;
  35. int rot_val_int;
  36. /* rgbcol */
  37. dbCatValArray cvarr_rgb;
  38. dbCatVal *cv_rgb;
  39. int red, grn, blu;
  40. char *rgbstring = NULL;
  41. PSCOLOR color;
  42. cv_rgb = NULL;
  43. Symb = NULL;
  44. /* Create vector array if required */
  45. if (vector.layer[vec].cats != NULL || vector.layer[vec].where != NULL) {
  46. Varray = Vect_new_varray(Vect_get_num_lines(P_map));
  47. if (vector.layer[vec].cats != NULL) {
  48. ret =
  49. Vect_set_varray_from_cat_string(P_map,
  50. vector.layer[vec].field,
  51. vector.layer[vec].cats,
  52. vector.layer[vec].ltype, 1,
  53. Varray);
  54. }
  55. else {
  56. ret = Vect_set_varray_from_db(P_map, vector.layer[vec].field,
  57. vector.layer[vec].where,
  58. vector.layer[vec].ltype, 1, Varray);
  59. }
  60. G_debug(3, "%d items selected for vector %d", ret, vec);
  61. if (ret == -1)
  62. G_fatal_error(_("Cannot load data from table"));
  63. }
  64. /* allocate memory for coordinates */
  65. Points = Vect_new_line_struct();
  66. Cats = Vect_new_cats_struct();
  67. /* process only vectors in current window */
  68. Vect_set_constraint_region(P_map, PS.w.north, PS.w.south, PS.w.east,
  69. PS.w.west, PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);
  70. /* Read symbol */
  71. if (vector.layer[vec].symbol != NULL) {
  72. sprintf(sname, "SITESYMBOL%d", vec);
  73. Symb = S_read(vector.layer[vec].symbol);
  74. if (Symb == NULL) {
  75. G_warning(_("Cannot read symbol, using default icon"));
  76. }
  77. symbol_save(Symb, &(vector.layer[vec].color),
  78. &(vector.layer[vec].fcolor), sname);
  79. vector.layer[vec].symbol_ps = G_store(sname);
  80. }
  81. /* if eps file is specified as common for all points then
  82. read bbox and save eps to PS file */
  83. if (vector.layer[vec].epstype == 1) {
  84. if (!eps_bbox(vector.layer[vec].epspre, &llx, &lly, &urx, &ury)) {
  85. vector.layer[vec].epstype = 0; /* eps file can't be read */
  86. }
  87. else { /* save to PS */
  88. sprintf(eps, "SITEEPSF%d", vec);
  89. eps_save(PS.fp, vector.layer[vec].epspre, eps);
  90. }
  91. }
  92. /* Load attributes if sizecol used */
  93. if (vector.layer[vec].sizecol != NULL)
  94. load_catval_array_size(P_map, vec, &cvarr_size);
  95. /* load attributes if rgbcol used */
  96. if (vector.layer[vec].rgbcol != NULL)
  97. load_catval_array_rgb(P_map, vec, &cvarr_rgb);
  98. /* Load attributes if rotatecolumn used */
  99. if (vector.layer[vec].rotcol != NULL)
  100. load_catval_array_rot(P_map, vec, &cvarr_rot);
  101. /* read and plot vectors */
  102. nlines = Vect_get_num_lines(P_map);
  103. for (line = 1; line <= nlines; line++) {
  104. if (!Vect_line_alive(P_map, line))
  105. continue;
  106. ltype = Vect_read_line(P_map, Points, Cats, line);
  107. if (!(ltype & GV_POINTS))
  108. continue;
  109. if (!(ltype & vector.layer[vec].ltype))
  110. continue;
  111. if (Varray != NULL && Varray->c[line] == 0)
  112. continue; /* is not in array */
  113. Vect_cat_get(Cats, 1, &cat);
  114. nn = Points->y[0];
  115. ee = Points->x[0];
  116. if (nn > PS.w.north || nn < PS.w.south)
  117. continue;
  118. if (ee > PS.w.east || ee < PS.w.west)
  119. continue;
  120. G_plot_where_xy(ee, nn, &x_int, &y_int);
  121. x = (double)x_int / 10.;
  122. y = (double)y_int / 10.;
  123. /* symbol size */
  124. if (vector.layer[vec].sizecol == NULL)
  125. size = vector.layer[vec].size;
  126. else { /* get value from sizecol column */
  127. if (cvarr_size.ctype == DB_C_TYPE_INT) {
  128. ret =
  129. db_CatValArray_get_value_int(&cvarr_size, cat,
  130. &size_val_int);
  131. if (ret != DB_OK) {
  132. G_warning(_("No record for category [%d]"), cat);
  133. continue;
  134. }
  135. size_val = (double)size_val_int;
  136. }
  137. if (cvarr_size.ctype == DB_C_TYPE_DOUBLE) {
  138. ret =
  139. db_CatValArray_get_value_double(&cvarr_size, cat,
  140. &size_val);
  141. if (ret != DB_OK) {
  142. G_warning(_("No record for category [%d]"), cat);
  143. continue;
  144. }
  145. }
  146. if (size_val < 0.0) {
  147. G_warning(_("Attribute is of invalid size [%.3f] for category [%d]"),
  148. size_val, cat);
  149. continue;
  150. }
  151. if (size_val == 0.0)
  152. continue;
  153. size = size_val * vector.layer[vec].scale;
  154. G_debug(3, " dynamic symbol size = %.2f", size);
  155. }
  156. /* symbol color */
  157. if (vector.layer[vec].rgbcol != NULL) {
  158. rgbstring = NULL;
  159. ret = db_CatValArray_get_value(&cvarr_rgb, cat, &cv_rgb);
  160. if (ret != DB_OK) {
  161. G_warning(_("No record for category [%d]"), cat);
  162. }
  163. else {
  164. rgbstring = db_get_string(cv_rgb->val.s);
  165. if (rgbstring == NULL ||
  166. G_str_to_color(rgbstring, &red, &grn, &blu) != 1) {
  167. G_warning(_("Invalid RGB color definition in column <%s> for category [%d]"),
  168. vector.layer[vec].rgbcol, cat);
  169. rgbstring = NULL;
  170. }
  171. }
  172. if (rgbstring) {
  173. /* TODO: do not duplicate save symbol */
  174. G_debug(3, " dynamic symbol rgb color = %s", rgbstring);
  175. set_color(&color, red, grn, blu);
  176. sprintf(sname, "SITESYMBOL%d_%d", vec, line);
  177. symbol_save(Symb, &(vector.layer[vec].color), &color, sname);
  178. }
  179. else { /* use default symbol */
  180. G_debug(3, " static symbol rgb color = %d:%d:%d",
  181. vector.layer[vec].color.r,
  182. vector.layer[vec].color.g, vector.layer[vec].color.b);
  183. sprintf(sname, "SITESYMBOL%d", vec);
  184. }
  185. }
  186. /* symbol rotation */
  187. if (vector.layer[vec].rotcol == NULL)
  188. rotate = vector.layer[vec].rotate;
  189. else { /* get value from rotcol column */
  190. if (cvarr_rot.ctype == DB_C_TYPE_INT) {
  191. ret =
  192. db_CatValArray_get_value_int(&cvarr_rot, cat,
  193. &rot_val_int);
  194. if (ret != DB_OK) {
  195. G_warning(_("No record for category [%d]"), cat);
  196. continue;
  197. }
  198. rotate = (double)rot_val_int;
  199. }
  200. if (cvarr_rot.ctype == DB_C_TYPE_DOUBLE) {
  201. ret =
  202. db_CatValArray_get_value_double(&cvarr_rot, cat, &rotate);
  203. if (ret != DB_OK) {
  204. G_warning(_("No record for category [%d]"), cat);
  205. continue;
  206. }
  207. }
  208. G_debug(3, " dynamic rotation value = %.2f", rotate);
  209. }
  210. if (vector.layer[vec].epstype == 1) { /* draw common eps */
  211. /* calculate translation */
  212. eps_trans(llx, lly, urx, ury, x, y, size, rotate, &xt, &yt);
  213. eps_draw_saved(eps, xt, yt, size, rotate);
  214. }
  215. else if (vector.layer[vec].epstype == 2) { /* draw epses */
  216. sprintf(epsfile, "%s%d%s", vector.layer[vec].epspre, cat,
  217. vector.layer[vec].epssuf);
  218. if ((eps_exist = eps_bbox(epsfile, &llx, &lly, &urx, &ury))) {
  219. eps_trans(llx, lly, urx, ury, x, y, size, rotate, &xt, &yt);
  220. eps_draw(PS.fp, epsfile, xt, yt, size, rotate);
  221. }
  222. }
  223. /* draw the icon */
  224. if ((vector.layer[vec].epstype == 0) ||
  225. (vector.layer[vec].epstype == 2 && !eps_exist)) {
  226. if (Symb != NULL) {
  227. symbol_draw(sname, x, y, size, rotate,
  228. vector.layer[vec].width);
  229. }
  230. }
  231. } /* for (line) */
  232. fprintf(PS.fp, "\n");
  233. return 0;
  234. }