main.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. * Adam Laza <ad.laza32@gmail.com>, GSoC 2016
  16. *
  17. * PURPOSE: Displays a barscale on graphics monitor
  18. *
  19. * COPYRIGHT: (C) 1999-2013 by the GRASS Development Team
  20. *
  21. * This program is free software under the GNU General Public
  22. * License (>=v2). Read the file COPYING that comes with GRASS
  23. * for details.
  24. *
  25. *****************************************************************************/
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include <grass/gis.h>
  31. #include <grass/display.h>
  32. #include <grass/glocale.h>
  33. #include "options.h"
  34. int fg_color, bg_color;
  35. int use_feet;
  36. int do_background = TRUE;
  37. int north_arrow;
  38. int main(int argc, char **argv)
  39. {
  40. struct GModule *module;
  41. struct Option *bg_color_opt, *fg_color_opt, *coords, *fsize, *barstyle,
  42. *text_placement, *length_opt, *segm_opt, *units_opt, *label_opt;
  43. struct Flag *feet, *no_text, *n_symbol;
  44. struct Cell_head W;
  45. double east, north;
  46. double fontsize;
  47. int bar_style, text_position, units;
  48. double length;
  49. int segm;
  50. char *label;
  51. /* Initialize the GIS calls */
  52. G_gisinit(argv[0]);
  53. module = G_define_module();
  54. G_add_keyword(_("display"));
  55. G_add_keyword(_("cartography"));
  56. module->description = _("Displays a barscale on the graphics monitor.");
  57. feet = G_define_flag();
  58. feet->key = 'f';
  59. feet->description = _("Use feet/miles instead of meters");
  60. no_text = G_define_flag();
  61. no_text->key = 't';
  62. no_text->description = _("Draw the scale bar without text");
  63. no_text->guisection = _("Text");
  64. n_symbol = G_define_flag();
  65. n_symbol->key = 'n';
  66. n_symbol->description = _("Display north-arrow symbol.");
  67. n_symbol->guisection = _("Style");
  68. barstyle = G_define_option();
  69. barstyle->key = "style";
  70. barstyle->description = _("Type of barscale to draw");
  71. barstyle->options =
  72. "classic,line,solid,hollow,full_checker,part_checker,mixed_checker,tail_checker,up_ticks,down_ticks,both_ticks,arrow_ends";
  73. barstyle->answer = "classic";
  74. barstyle->gisprompt = "old,barscale,barscale";
  75. barstyle->guisection = _("Style");
  76. G_asprintf((char **)&(barstyle->descriptions),
  77. "classic;%s;"
  78. "line;%s;"
  79. "solid;%s;"
  80. "hollow;%s;"
  81. "full_checker;%s;"
  82. "part_checker;%s;"
  83. "mixed_checker;%s;"
  84. "tail_checker;%s;"
  85. "up_ticks;%s;"
  86. "down_ticks;%s;"
  87. "both_ticks;%s;"
  88. "arrow_ends;%s",
  89. _("Classic style"),
  90. _("Line style"),
  91. _("Solid style"),
  92. _("Hollow style"),
  93. _("Full checker style"),
  94. _("Part checker style"),
  95. _("Mixed checker style"),
  96. _("Tail checker style"),
  97. _("Up ticks style"),
  98. _("Down ticks style"),
  99. _("Both ticks style"), _("Arrow ends style"));
  100. coords = G_define_option();
  101. coords->key = "at";
  102. coords->key_desc = "x,y";
  103. coords->type = TYPE_DOUBLE;
  104. coords->answer = "0.0,10.0";
  105. coords->options = "0-100";
  106. coords->label =
  107. _("Screen coordinates of the rectangle's top-left corner");
  108. coords->description = _("(0,0) is lower-left of the display frame");
  109. length_opt = G_define_option();
  110. length_opt->key = "length";
  111. length_opt->key_desc = "integer";
  112. length_opt->type = TYPE_INTEGER;
  113. length_opt->answer = "0";
  114. length_opt->options = "0-";
  115. length_opt->label = _("Length of barscale in map units");
  116. units_opt = G_define_option();
  117. units_opt->key = "units";
  118. units_opt->description = _("Barscale units to display");
  119. units_opt->options = "meters, kilometers, feet, miles";
  120. label_opt = G_define_option();
  121. label_opt->key = "label";
  122. label_opt->description = _("Custom label of unit");
  123. label_opt->type = TYPE_STRING;
  124. label_opt->guisection = _("Text");
  125. segm_opt = G_define_option();
  126. segm_opt->key = "segment";
  127. segm_opt->type = TYPE_INTEGER;
  128. segm_opt->answer = "10";
  129. segm_opt->options = "1-100";
  130. segm_opt->label = _("Number of segments");
  131. segm_opt->guisection = _("Style");
  132. fg_color_opt = G_define_standard_option(G_OPT_C);
  133. fg_color_opt->label = _("Bar scale and text color");
  134. fg_color_opt->guisection = _("Colors");
  135. bg_color_opt = G_define_standard_option(G_OPT_CN);
  136. bg_color_opt->key = "bgcolor";
  137. bg_color_opt->answer = "white";
  138. bg_color_opt->label = _("Background color (drawn behind the bar)");
  139. bg_color_opt->guisection = _("Colors");
  140. text_placement = G_define_option();
  141. text_placement->key = "text_position";
  142. text_placement->description = _("Text position");
  143. text_placement->options = "under,over,left,right";
  144. text_placement->answer = "right";
  145. text_placement->guisection = _("Text");
  146. fsize = G_define_option();
  147. fsize->key = "fontsize";
  148. fsize->type = TYPE_DOUBLE;
  149. fsize->required = NO;
  150. fsize->answer = "12";
  151. fsize->options = "1-360";
  152. fsize->description = _("Font size");
  153. fsize->guisection = _("Text");
  154. G_option_exclusive(feet, units_opt, NULL);
  155. if (G_parser(argc, argv))
  156. exit(EXIT_FAILURE);
  157. G_get_window(&W);
  158. if (W.proj == PROJECTION_LL)
  159. G_fatal_error(_("%s does not work with a latitude-longitude location"),
  160. argv[0]);
  161. north_arrow = n_symbol->answer ? TRUE : FALSE;
  162. switch (barstyle->answer[0]) {
  163. case 'c':
  164. bar_style = STYLE_CLASSIC_BAR;
  165. break;
  166. case 'p':
  167. bar_style = STYLE_PART_CHECKER;
  168. break;
  169. case 'f':
  170. bar_style = STYLE_FULL_CHECKER;
  171. break;
  172. case 'm':
  173. bar_style = STYLE_MIXED_CHECKER;
  174. break;
  175. case 't':
  176. bar_style = STYLE_TAIL_CHECKER;
  177. break;
  178. case 'l':
  179. bar_style = STYLE_THIN_WITH_ENDS;
  180. break;
  181. case 's':
  182. bar_style = STYLE_SOLID_BAR;
  183. break;
  184. case 'h':
  185. bar_style = STYLE_HOLLOW_BAR;
  186. break;
  187. case 'u':
  188. bar_style = STYLE_TICKS_UP;
  189. break;
  190. case 'd':
  191. bar_style = STYLE_TICKS_DOWN;
  192. break;
  193. case 'b':
  194. bar_style = STYLE_TICKS_BOTH;
  195. break;
  196. case 'a':
  197. bar_style = STYLE_ARROW_ENDS;
  198. break;
  199. default:
  200. G_fatal_error(_("Programmer error"));
  201. }
  202. switch (text_placement->answer[0]) {
  203. case 'u':
  204. text_position = TEXT_UNDER;
  205. break;
  206. case 'o':
  207. text_position = TEXT_OVER;
  208. break;
  209. case 'l':
  210. text_position = TEXT_LEFT;
  211. break;
  212. case 'r':
  213. text_position = TEXT_RIGHT;
  214. break;
  215. default:
  216. G_fatal_error(_("Programmer error"));
  217. }
  218. sscanf(coords->answers[0], "%lf", &east);
  219. sscanf(coords->answers[1], "%lf", &north);
  220. length = atof(length_opt->answer);
  221. sscanf(segm_opt->answer, "%d", &segm);
  222. if (feet->answer == 1){
  223. use_feet = 1;
  224. units = U_FEET;
  225. label = "ft";
  226. }
  227. else {
  228. if (!units_opt->answer)
  229. units = G_database_unit();
  230. else
  231. units = G_units(units_opt->answer);
  232. switch (units) {
  233. case U_METERS:
  234. label = "m";
  235. break;
  236. case U_KILOMETERS:
  237. label = "km";
  238. break;
  239. case U_FEET:
  240. use_feet = 1;
  241. label = "ft";
  242. break;
  243. case U_USFEET:
  244. use_feet = 1;
  245. label = "ft";
  246. break;
  247. case U_MILES:
  248. use_feet = 1;
  249. label = "mi";
  250. break;
  251. default:
  252. units = U_METERS;
  253. label = "m";
  254. }
  255. }
  256. if (label_opt->answer){
  257. label = label_opt->answer;
  258. }
  259. fontsize = atof(fsize->answer);
  260. if (no_text->answer)
  261. fontsize = -1;
  262. /* Parse and select foreground color */
  263. fg_color = D_parse_color(fg_color_opt->answer, 0);
  264. /* Parse and select background color */
  265. bg_color = D_parse_color(bg_color_opt->answer, 1);
  266. if (bg_color == 0)
  267. do_background = FALSE;
  268. D_open_driver();
  269. D_setup(0);
  270. draw_scale(east, north, length, segm, units, label, bar_style, text_position, fontsize);
  271. D_save_command(G_recreate_command());
  272. D_close_driver();
  273. exit(EXIT_SUCCESS);
  274. }