main.c 9.4 KB

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