main.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: d.vect.legend
  5. * AUTHOR(S): Adam Laza, CTU, GSoC 2016
  6. * PURPOSE: Display a vector layer
  7. * COPYRIGHT: (C) 2007-2016 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. *****************************************************************************/
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <grass/display.h>
  18. #include <grass/glocale.h>
  19. #include "local_proto.h"
  20. int main(int argc, char **argv)
  21. {
  22. struct GModule *module;
  23. // struct Option *opt_input, *opt_sep;
  24. struct Option *opt_at, *opt_cols, *opt_font, *opt_fontsize,
  25. *opt_fontcolor, *opt_title, *opt_tit_font, *opt_tit_fontsize, *opt_sub_font,
  26. *opt_sub_fontsize, *opt_bcolor, *opt_bgcolor, *opt_symb_size,
  27. *opt_bg_width, *opt_output, *opt_input, *opt_sep;
  28. struct Flag *fl_bg;
  29. double LL, LT;
  30. char *title, *file_name;
  31. int bcolor, bgcolor, do_bg;
  32. int fontsize, fontcolor, tit_size, sub_size;
  33. char *font, *tit_font, *sub_font;
  34. int cols, symb_size, bg_width;
  35. char *out_file;
  36. FILE *source;
  37. char buf[BUFFSIZE];
  38. char *sep;
  39. size_t nread;
  40. /* Initialize the GIS calls */
  41. G_gisinit(argv[0]);
  42. module = G_define_module();
  43. G_add_keyword(_("display"));
  44. G_add_keyword(_("cartography"));
  45. G_add_keyword(_("vector"));
  46. G_add_keyword(_("legend"));
  47. module->description =
  48. _("Displays a vector legend "
  49. "in the active graphics frame.");
  50. opt_at = G_define_option();
  51. opt_at->key = "at";
  52. opt_at->key_desc = "left,top";
  53. opt_at->type = TYPE_DOUBLE;
  54. opt_at->options = "0-100";
  55. opt_at->answer = "10,40";
  56. opt_at->required = NO;
  57. opt_at->description =
  58. _("Screen position of legend to be drawn (percentage, [0,0] is lower left)");
  59. opt_cols = G_define_option();
  60. opt_cols->key = "columns";
  61. opt_cols->type = TYPE_INTEGER;
  62. opt_cols->answer = "1";
  63. opt_cols->required = NO;
  64. opt_cols->description =
  65. _("Number of legend columns");
  66. opt_cols->guisection = _("Layout");
  67. opt_title = G_define_option();
  68. opt_title->key = "title";
  69. opt_title->type = TYPE_STRING;
  70. opt_title->required = NO;
  71. opt_title->description = _("Legend title");
  72. opt_title->guisection = _("Title");
  73. opt_symb_size = G_define_option();
  74. opt_symb_size->key = "symbol_size";
  75. opt_symb_size->type = TYPE_INTEGER;
  76. opt_symb_size->required = NO;
  77. opt_symb_size->description = _("Symbol size");
  78. opt_symb_size->answer = "20";
  79. opt_symb_size->guisection = _("Layout");
  80. opt_bcolor = G_define_standard_option(G_OPT_CN);
  81. opt_bcolor->key = "border_color";
  82. opt_bcolor->answer = "black";
  83. opt_bcolor->label = _("Border color");
  84. opt_bcolor->guisection = _("Background");
  85. opt_bgcolor = G_define_standard_option(G_OPT_CN);
  86. opt_bgcolor->key = "bgcolor";
  87. opt_bgcolor->answer = "white";
  88. opt_bgcolor->label = _("Background color");
  89. opt_bgcolor->guisection = _("Background");
  90. opt_bg_width = G_define_option();
  91. opt_bg_width->type = TYPE_INTEGER;
  92. opt_bg_width->key = "border_width";
  93. opt_bg_width->answer = "2";
  94. opt_bg_width->label = _("Background border width");
  95. opt_bg_width->guisection = _("Background");
  96. opt_font = G_define_option();
  97. opt_font->key = "font";
  98. opt_font->type = TYPE_STRING;
  99. opt_font->required = NO;
  100. opt_font->description = _("Font name");
  101. opt_font->guisection = _("Font settings");
  102. opt_fontsize = G_define_option();
  103. opt_fontsize->key = "fontsize";
  104. opt_fontsize->type = TYPE_DOUBLE;
  105. opt_fontsize->required = NO;
  106. opt_fontsize->options = "1-360";
  107. opt_fontsize->label = _("Font size");
  108. opt_fontsize->description = _("Default: 12");
  109. opt_fontsize->guisection = _("Font settings");
  110. opt_tit_font = G_define_option();
  111. opt_tit_font->key = "title_font";
  112. opt_tit_font->type = TYPE_STRING;
  113. opt_tit_font->required = NO;
  114. opt_tit_font->description = _("Title font name");
  115. opt_tit_font->guisection = _("Font settings");
  116. opt_tit_fontsize = G_define_option();
  117. opt_tit_fontsize->key = "title_fontsize";
  118. opt_tit_fontsize->type = TYPE_DOUBLE;
  119. opt_tit_fontsize->required = NO;
  120. opt_tit_fontsize->options = "1-360";
  121. opt_tit_fontsize->label = _("Title font size");
  122. opt_tit_fontsize->description = _("Default: 18");
  123. opt_tit_fontsize->guisection = _("Title");
  124. opt_sub_font = G_define_option();
  125. opt_sub_font->key = "sub_font";
  126. opt_sub_font->type = TYPE_STRING;
  127. opt_sub_font->required = NO;
  128. opt_sub_font->description = _("Subtitle font name");
  129. opt_sub_font->guisection = _("Font settings");
  130. opt_sub_fontsize = G_define_option();
  131. opt_sub_fontsize->key = "sub_fontsize";
  132. opt_sub_fontsize->type = TYPE_DOUBLE;
  133. opt_sub_fontsize->required = NO;
  134. opt_sub_fontsize->options = "1-360";
  135. opt_sub_fontsize->label = _("Subtitle font size");
  136. opt_sub_fontsize->description = _("Default: 14");
  137. opt_sub_fontsize->guisection = _("Font settings");
  138. opt_fontcolor = G_define_standard_option(G_OPT_C);
  139. opt_fontcolor->key = "fontcolor";
  140. opt_fontcolor->answer = "black";
  141. opt_fontcolor->label = _("Font color");
  142. opt_fontcolor->guisection = _("Font settings");
  143. fl_bg = G_define_flag();
  144. fl_bg->key = 'b';
  145. fl_bg->description = _("Display legend background");
  146. fl_bg->guisection = _("Background");
  147. opt_sep = G_define_standard_option(G_OPT_F_SEP);
  148. opt_sep->guisection = _("In/Out");
  149. opt_sep->label = _("Field separator for input file");
  150. opt_input = G_define_standard_option(G_OPT_F_INPUT);
  151. opt_input->label = _("Input legend file");
  152. opt_input->description = _("Path to legend file ");
  153. opt_input->required = NO;
  154. opt_input->guisection = _("In/Out");
  155. opt_output = G_define_standard_option(G_OPT_F_OUTPUT);
  156. opt_output->label = _("Output csv file");
  157. opt_output->description = _("Path to output file or '-' "
  158. "for standard output");
  159. opt_output->required = NO;
  160. opt_output->guisection = _("In/Out");
  161. /* Check command line */
  162. if (G_parser(argc, argv)) {
  163. exit(EXIT_FAILURE);
  164. }
  165. D_open_driver();
  166. D_setup_unity(0);
  167. /* parse and check options and flags */
  168. if (opt_at->answer) {
  169. sscanf(opt_at->answers[0], "%lf", &LL);
  170. sscanf(opt_at->answers[1], "%lf", &LT);
  171. }
  172. else {
  173. LL = 10;
  174. LT = 40;
  175. }
  176. if (opt_title->answer)
  177. title = opt_title->answer;
  178. else
  179. title = "";
  180. if (opt_cols->answer)
  181. sscanf(opt_cols->answer, "%d", &cols);
  182. else
  183. cols = 1;
  184. sscanf(opt_symb_size->answer, "%d", &symb_size);
  185. sscanf(opt_bg_width->answer, "%d", &bg_width);
  186. /* Background */
  187. do_bg = fl_bg->answer;
  188. bcolor = D_parse_color(opt_bcolor->answer, TRUE);
  189. bgcolor = D_parse_color(opt_bgcolor->answer, TRUE);
  190. /* Font settings */
  191. if (opt_font->answer)
  192. font = opt_font->answer;
  193. else
  194. font = "sans";
  195. if (opt_fontsize->answer != NULL)
  196. sscanf(opt_fontsize->answer, "%d", &fontsize);
  197. else
  198. fontsize = 14;
  199. if (opt_tit_font->answer)
  200. tit_font = opt_tit_font->answer;
  201. else
  202. tit_font = font;
  203. if (opt_tit_fontsize->answer)
  204. sscanf(opt_tit_fontsize->answer, "%d", &tit_size);
  205. else
  206. tit_size = fontsize;
  207. if (opt_sub_font->answer)
  208. sub_font = opt_sub_font->answer;
  209. else
  210. sub_font = font;
  211. if (opt_sub_fontsize->answer)
  212. sscanf(opt_sub_fontsize->answer, "%d", &sub_size);
  213. else
  214. sub_size = fontsize;
  215. fontcolor = D_parse_color(opt_fontcolor->answer, FALSE); /*default color: black */
  216. /* I/O */
  217. if (opt_input->answer) {
  218. sep = G_option_to_separator(opt_sep);
  219. file_name = opt_input->answer;
  220. if (!file_name)
  221. G_fatal_error(_("Unable to open input file <%s>"), file_name);
  222. }
  223. else {
  224. sep = "|";
  225. file_name = getenv("GRASS_LEGEND_FILE");
  226. if (!file_name)
  227. G_fatal_error("No legend file defined.");
  228. }
  229. if (opt_output->answer) {
  230. if (strcmp(opt_output->answer,"-") == 0) {
  231. source = fopen(file_name, "r");
  232. if (!source)
  233. G_fatal_error(_("Unable to open input file <%s>"), file_name);
  234. while ((nread = fread(buf, 1, sizeof(buf), source)))
  235. fwrite(buf, 1, nread, stdout);
  236. fclose(source);
  237. }
  238. else {
  239. out_file = opt_output->answer;
  240. G_copy_file(file_name, out_file);
  241. }
  242. }
  243. /* Pre-calculate the layout */
  244. if (do_bg)
  245. draw(file_name, LL, LT, title, cols, bgcolor, bcolor, bg_width, 1, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor, symb_size, sep);
  246. /* Draw legend */
  247. draw(file_name, LL, LT, title, cols, bgcolor, bcolor, bg_width, 0, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor, symb_size, sep);
  248. D_close_driver();
  249. exit(EXIT_SUCCESS);
  250. }