r_vpoints.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* Function: vectfile
  2. **
  3. ** This PostScript version is just slightly modified p.map code.
  4. **
  5. ** Modified by: Paul W. Carlson March 1992
  6. ** Modified by: Janne Soimasuo August 1994 line_cat added
  7. ** Modified by: Radim Blazek Jan 2000 acolor, label added
  8. ** Modified by: Hamish Bowman Sept 2005, sizecol, scale added
  9. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <grass/gis.h>
  13. #include <grass/colors.h>
  14. #include <grass/raster.h>
  15. #include <grass/vector.h>
  16. #include <grass/glocale.h>
  17. #include "vector.h"
  18. #include "local_proto.h"
  19. #define KEY(x) (strcmp(key,x)==0)
  20. static char *help[] = {
  21. "masked [y|n]",
  22. "color color",
  23. "fcolor color",
  24. "rgbcolumn column",
  25. "icon iconfile",
  26. "eps epsfile",
  27. "size #",
  28. "sizecolumn column",
  29. "layer #",
  30. "scale factor",
  31. "rotate #",
  32. "rotatecolumn column",
  33. "font fontname",
  34. "label label",
  35. "lpos #",
  36. ""
  37. };
  38. int read_vpoints(char *name, char *mapset)
  39. {
  40. char fullname[100];
  41. char buf[1024];
  42. char *key, *data;
  43. double width, size, scale, rotate;
  44. int itmp, vec;
  45. int r, g, b;
  46. int ret;
  47. struct Map_info Map;
  48. vector_alloc(); /* allocate space */
  49. sprintf(fullname, "%s in %s", name, mapset);
  50. Vect_set_open_level(2);
  51. if (2 > Vect_open_old(&Map, name, mapset)) {
  52. error(fullname, "", "can't open vector map");
  53. gobble_input();
  54. return 0;
  55. }
  56. Vect_close(&Map);
  57. vec = vector.count;
  58. vector.layer[vec].type = VPOINTS;
  59. vector.layer[vec].name = G_store(name);
  60. vector.layer[vec].mapset = G_store(mapset);
  61. vector.layer[vec].ltype = GV_POINT;
  62. vector.layer[vec].masked = 0;
  63. vector.layer[vec].field = 1;
  64. vector.layer[vec].cats = NULL;
  65. vector.layer[vec].where = NULL;
  66. vector.layer[vec].width = 1.;
  67. set_color(&(vector.layer[vec].color), 0, 0, 0);
  68. set_color(&(vector.layer[vec].fcolor), 255, 0, 0);
  69. vector.layer[vec].rgbcol = NULL;
  70. vector.layer[vec].label = NULL;
  71. vector.layer[vec].lpos = -1;
  72. vector.layer[vec].symbol = G_store("basic/diamond");
  73. vector.layer[vec].size = 6.0;
  74. vector.layer[vec].sizecol = NULL;
  75. vector.layer[vec].scale = 1.0;
  76. vector.layer[vec].rotate = 0.0;
  77. vector.layer[vec].rotcol = NULL;
  78. vector.layer[vec].epstype = 0;
  79. while (input(2, buf, help)) {
  80. if (!key_data(buf, &key, &data))
  81. continue;
  82. if (KEY("masked")) {
  83. vector.layer[vec].masked = yesno(key, data);
  84. if (vector.layer[vec].masked)
  85. PS.mask_needed = 1;
  86. continue;
  87. }
  88. if (KEY("type")) {
  89. G_strip(data);
  90. vector.layer[vec].ltype = 0;
  91. if (strstr(data, "point"))
  92. vector.layer[vec].ltype |= GV_POINT;
  93. if (strstr(data, "centroid"))
  94. vector.layer[vec].ltype |= GV_CENTROID;
  95. continue;
  96. }
  97. if (KEY("layer")) {
  98. G_strip(data);
  99. vector.layer[vec].field = atoi(data);
  100. continue;
  101. }
  102. if (KEY("cats")) {
  103. G_strip(data);
  104. vector.layer[vec].cats = G_store(data);
  105. continue;
  106. }
  107. if (KEY("where")) {
  108. G_strip(data);
  109. vector.layer[vec].where = G_store(data);
  110. continue;
  111. }
  112. if (KEY("width")) {
  113. width = -1.;
  114. *mapset = 0;
  115. if (sscanf(data, "%lf%s", &width, mapset) < 1 || width < 0.) {
  116. width = 1.;
  117. error(key, data, "illegal width (vpoints)");
  118. continue;
  119. }
  120. if (mapset[0] == 'i')
  121. width = width / 72.;
  122. vector.layer[vec].width = width;
  123. continue;
  124. }
  125. if (KEY("color")) {
  126. ret = G_str_to_color(data, &r, &g, &b);
  127. if (ret == 1)
  128. set_color(&(vector.layer[vec].color), r, g, b);
  129. else if (ret == 2)
  130. unset_color(&(vector.layer[vec].color));
  131. else
  132. error(key, data, "illegal color request");
  133. continue;
  134. }
  135. if (KEY("fcolor")) { /* fill color */
  136. ret = G_str_to_color(data, &r, &g, &b);
  137. if (ret == 1)
  138. set_color(&(vector.layer[vec].fcolor), r, g, b);
  139. else if (ret == 2)
  140. unset_color(&(vector.layer[vec].fcolor));
  141. else
  142. error(key, data, "illegal color request (vpoints)");
  143. continue;
  144. }
  145. if (KEY("rgbcolumn")) {
  146. G_strip(data);
  147. vector.layer[vec].rgbcol = G_store(data);
  148. continue;
  149. }
  150. if (KEY("label")) { /* map legend label */
  151. G_strip(data);
  152. vector.layer[vec].label = G_store(data);
  153. continue;
  154. }
  155. if (KEY("lpos")) {
  156. if (sscanf(data, "%d", &itmp) < 1 || itmp < 0) {
  157. itmp = -1;
  158. error(key, data, "illegal lpos");
  159. continue;
  160. }
  161. vector.layer[vec].lpos = itmp;
  162. continue;
  163. }
  164. if (KEY("symbol")) {
  165. /* TODO: test here if isymbol exists */
  166. vector.layer[vec].symbol = G_store(data);
  167. continue;
  168. }
  169. if (KEY("eps")) {
  170. char *cc;
  171. G_chop(data);
  172. vector.layer[vec].epspre = G_store(data);
  173. /* epstype: 0 - no eps, 1 - common eps, 2 - eps for each category */
  174. vector.layer[vec].epstype = 1;
  175. /* find dynamic filename by cat number character */
  176. /* pre is filename before the $, suf is filename after the $ */
  177. cc = (char *)strchr(vector.layer[vec].epspre, '$');
  178. if (cc != NULL) {
  179. *cc = '\0';
  180. vector.layer[vec].epssuf = G_store(cc + sizeof(char));
  181. vector.layer[vec].epstype = 2;
  182. G_debug(2, "epstype=%d, pre=[%s], suf=[%s]",
  183. vector.layer[vec].epstype, vector.layer[vec].epspre,
  184. vector.layer[vec].epssuf);
  185. }
  186. else {
  187. G_debug(2, "epstype=%d, eps file=[%s]",
  188. vector.layer[vec].epstype, vector.layer[vec].epspre);
  189. }
  190. continue;
  191. }
  192. if (KEY("size")) {
  193. if (sscanf(data, "%lf", &size) != 1 || size <= 0.0) {
  194. size = 1.0;
  195. error(key, data, "illegal size request (vpoints)");
  196. }
  197. vector.layer[vec].size = size;
  198. continue;
  199. }
  200. /*
  201. GRASS 6.3: sizecol renamed to sizecolumn
  202. remove sizecol test and the warning in GRASS7
  203. */
  204. if (KEY("sizecol")) {
  205. G_warning(_("The mapping instruction <%s> will be renamed to <%s> "
  206. "in future versions of GRASS. Please use <%s> instead."),
  207. "sizecol", "sizecolumn", "sizecolumn");
  208. }
  209. if (KEY("sizecol") || KEY("sizecolumn")) {
  210. G_strip(data);
  211. vector.layer[vec].sizecol = G_store(data);
  212. continue;
  213. }
  214. if (KEY("scale")) {
  215. if (sscanf(data, "%lf", &scale) != 1 || scale <= 0.0) {
  216. scale = 1.0;
  217. error(key, data, "illegal scale request (vpoints)");
  218. }
  219. vector.layer[vec].scale = scale;
  220. continue;
  221. }
  222. if (KEY("rotate")) {
  223. if (sscanf(data, "%lf", &rotate) != 1) {
  224. rotate = 0.0;
  225. error(key, data, "illegal rotation request (vpoints)");
  226. }
  227. vector.layer[vec].rotate = rotate;
  228. continue;
  229. }
  230. if (KEY("rotatecolumn")) {
  231. G_strip(data);
  232. vector.layer[vec].rotcol = G_store(data);
  233. continue;
  234. }
  235. error(key, "", "illegal request (vpoints)");
  236. }
  237. vector.count++;
  238. return 1;
  239. }