main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /****************************************************************************
  2. *
  3. * MODULE: d.barscale
  4. *
  5. * AUTHOR(S): unknown but from CERL code (original contributor)
  6. * Markus Neteler <neteler itc.it>,
  7. * Bernhard Reiter <bernhard intevation.de>,
  8. * Cedric Shock <cedricgrass shockfamily.net>,
  9. * Huidae Cho <grass4u gmail.com>,
  10. * Eric G. Miller <egm2 jps.net>,
  11. * Glynn Clements <glynn gclements.plus.com>,
  12. * Hamish Bowman <hamish_b yahoo.com>,
  13. * Jan-Oliver Wagner <jan intevation.de>
  14. * Major rewrite for GRASS 7 by Hamish Bowman, June 2013
  15. *
  16. * PURPOSE: Displays a barscale on graphics monitor
  17. *
  18. * COPYRIGHT: (C) 1999-2013 by the GRASS Development Team
  19. *
  20. * This program is free software under the GNU General Public
  21. * License (>=v2). Read the file COPYING that comes with GRASS
  22. * for details.
  23. *
  24. *****************************************************************************/
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <grass/gis.h>
  30. #include <grass/display.h>
  31. #include <grass/glocale.h>
  32. #include "options.h"
  33. int fg_color, bg_color;
  34. int use_feet;
  35. int do_background = TRUE;
  36. int main(int argc, char **argv)
  37. {
  38. struct GModule *module;
  39. struct Option *bg_color_opt, *fg_color_opt, *coords, *fsize,
  40. *barstyle, *text_placement;
  41. struct Flag *feet, *no_text;
  42. struct Cell_head W;
  43. double east, north;
  44. double fontsize;
  45. int bar_style, text_position;
  46. /* Initialize the GIS calls */
  47. G_gisinit(argv[0]);
  48. module = G_define_module();
  49. G_add_keyword(_("display"));
  50. G_add_keyword(_("cartography"));
  51. module->description = _("Displays a barscale on the graphics monitor.");
  52. feet = G_define_flag();
  53. feet->key = 'f';
  54. feet->description = _("Use feet/miles instead of meters");
  55. no_text = G_define_flag();
  56. no_text->key = 't';
  57. no_text->description = _("Draw the scale bar without text");
  58. no_text->guisection = _("Text");
  59. barstyle = G_define_option();
  60. barstyle->key = "style";
  61. barstyle->description = _("Type of barscale to draw");
  62. barstyle->options =
  63. "classic,line,solid,hollow,full_checker,part_checker,mixed_checker,tail_checker,up_ticks,down_ticks,both_ticks,arrow_ends";
  64. barstyle->answer = "classic";
  65. barstyle->gisprompt = "old,barscale,barscale";
  66. barstyle->guisection = _("Style");
  67. G_asprintf((char **) &(barstyle->descriptions),
  68. "classic;%s;"
  69. "line;%s;"
  70. "solid;%s;"
  71. "hollow;%s;"
  72. "full_checker;%s;"
  73. "part_checker;%s;"
  74. "mixed_checker;%s;"
  75. "tail_checker;%s;"
  76. "up_ticks;%s;"
  77. "down_ticks;%s;"
  78. "both_ticks;%s;"
  79. "arrow_ends;%s",
  80. _("Classic style"),
  81. _("Line style"),
  82. _("Solid style"),
  83. _("Hollow style"),
  84. _("Full checker style"),
  85. _("Part checker style"),
  86. _("Mixed checker style"),
  87. _("Tail checker style"),
  88. _("Up ticks style"),
  89. _("Down ticks style"),
  90. _("Both ticks style"),
  91. _("Arrow ends style"));
  92. coords = G_define_option();
  93. coords->key = "at";
  94. coords->key_desc = "x,y";
  95. coords->type = TYPE_DOUBLE;
  96. coords->answer = "0.0,5.0";
  97. coords->options = "0-100";
  98. coords->label =
  99. _("Screen coordinates of the rectangle's top-left corner");
  100. coords->description = _("(0,0) is lower-left of the display frame");
  101. fg_color_opt = G_define_standard_option(G_OPT_C_FG);
  102. fg_color_opt->label = _("Bar scale and text color");
  103. fg_color_opt->guisection = _("Colors");
  104. bg_color_opt = G_define_standard_option(G_OPT_C_BG);
  105. bg_color_opt->label = _("Background color (drawn behind the bar)");
  106. bg_color_opt->guisection = _("Colors");
  107. text_placement = G_define_option();
  108. text_placement->key = "text_position";
  109. text_placement->description = _("Text position");
  110. text_placement->options = "under,over,left,right";
  111. text_placement->answer = "right";
  112. text_placement->guisection = _("Text");
  113. fsize = G_define_option();
  114. fsize->key = "fontsize";
  115. fsize->type = TYPE_DOUBLE;
  116. fsize->required = NO;
  117. fsize->answer = "12";
  118. fsize->options = "1-360";
  119. fsize->description = _("Font size");
  120. fsize->guisection = _("Text");
  121. if (G_parser(argc, argv))
  122. exit(EXIT_FAILURE);
  123. G_get_window(&W);
  124. if (W.proj == PROJECTION_LL)
  125. G_fatal_error(_("%s does not work with a latitude-longitude location"),
  126. argv[0]);
  127. use_feet = feet->answer ? TRUE : FALSE;
  128. switch (barstyle->answer[0]) {
  129. case 'c':
  130. bar_style = STYLE_CLASSIC_BAR;
  131. break;
  132. case 'p':
  133. bar_style = STYLE_PART_CHECKER;
  134. break;
  135. case 'f':
  136. bar_style = STYLE_FULL_CHECKER;
  137. break;
  138. case 'm':
  139. bar_style = STYLE_MIXED_CHECKER;
  140. break;
  141. case 't':
  142. bar_style = STYLE_TAIL_CHECKER;
  143. break;
  144. case 'l':
  145. bar_style = STYLE_THIN_WITH_ENDS;
  146. break;
  147. case 's':
  148. bar_style = STYLE_SOLID_BAR;
  149. break;
  150. case 'h':
  151. bar_style = STYLE_HOLLOW_BAR;
  152. break;
  153. case 'u':
  154. bar_style = STYLE_TICKS_UP;
  155. break;
  156. case 'd':
  157. bar_style = STYLE_TICKS_DOWN;
  158. break;
  159. case 'b':
  160. bar_style = STYLE_TICKS_BOTH;
  161. break;
  162. case 'a':
  163. bar_style = STYLE_ARROW_ENDS;
  164. break;
  165. default:
  166. G_fatal_error(_("Programmer error"));
  167. }
  168. switch (text_placement->answer[0]) {
  169. case 'u':
  170. text_position = TEXT_UNDER;
  171. break;
  172. case 'o':
  173. text_position = TEXT_OVER;
  174. break;
  175. case 'l':
  176. text_position = TEXT_LEFT;
  177. break;
  178. case 'r':
  179. text_position = TEXT_RIGHT;
  180. break;
  181. default:
  182. G_fatal_error(_("Programmer error"));
  183. }
  184. sscanf(coords->answers[0], "%lf", &east);
  185. sscanf(coords->answers[1], "%lf", &north);
  186. fontsize = atof(fsize->answer);
  187. if (no_text->answer)
  188. fontsize = -1;
  189. /* Parse and select foreground color */
  190. fg_color = D_parse_color(fg_color_opt->answer, 0);
  191. /* Parse and select background color */
  192. bg_color = D_parse_color(bg_color_opt->answer, 1);
  193. if (bg_color == 0)
  194. do_background = FALSE;
  195. if (D_open_driver() != 0)
  196. G_fatal_error(_("No graphics device selected. "
  197. "Use d.mon to select graphics device."));
  198. D_setup(0);
  199. draw_scale(east, north, bar_style, text_position, fontsize);
  200. D_save_command(G_recreate_command());
  201. D_close_driver();
  202. exit(EXIT_SUCCESS);
  203. }