main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /****************************************************************************
  2. *
  3. * MODULE: d.northarrow
  4. *
  5. * AUTHOR(S): Hamish Bowman, Dunedin, NZ <hamish_b yahoo.com>
  6. *
  7. * PURPOSE: Displays a north arrow on graphics monitor
  8. *
  9. * COPYRIGHT: (C) 2013 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/display.h>
  22. #include <grass/glocale.h>
  23. #include "options.h"
  24. int fg_color, bg_color;
  25. int do_background = TRUE;
  26. int main(int argc, char **argv)
  27. {
  28. struct GModule *module;
  29. struct Option *bg_color_opt, *fg_color_opt, *coords, *n_arrow, *fsize,
  30. *width_opt;
  31. struct Flag *no_text;
  32. double east, north;
  33. double fontsize, line_width;
  34. /* Initialize the GIS calls */
  35. G_gisinit(argv[0]);
  36. module = G_define_module();
  37. G_add_keyword(_("display"));
  38. G_add_keyword(_("cartography"));
  39. module->description = _("Displays a north arrow on the graphics monitor.");
  40. n_arrow = G_define_option();
  41. n_arrow->key = "style";
  42. n_arrow->description = _("North arrow style");
  43. n_arrow->options = "1a,1b,2,3,4,5,6,7a,7b,8a,8b,9,fancy_compass,basic_compass";
  44. n_arrow->answer = "1a";
  45. n_arrow->guisection = _("Style");
  46. n_arrow->gisprompt = "old,northarrow,northarrow";
  47. coords = G_define_option();
  48. coords->key = "at";
  49. coords->key_desc = "x,y";
  50. coords->type = TYPE_DOUBLE;
  51. coords->answer = "85.0,15.0";
  52. coords->options = "0-100";
  53. coords->label =
  54. _("Screen coordinates of the rectangle's top-left corner");
  55. coords->description = _("(0,0) is lower-left of the display frame");
  56. fg_color_opt = G_define_standard_option(G_OPT_C_FG);
  57. fg_color_opt->label = _("Line color");
  58. fg_color_opt->guisection = _("Colors");
  59. bg_color_opt = G_define_standard_option(G_OPT_C_BG);
  60. bg_color_opt->key = "fill_color";
  61. bg_color_opt->label = _("Fill color");
  62. bg_color_opt->answer = _("black");
  63. bg_color_opt->guisection = _("Colors");
  64. width_opt = G_define_option();
  65. width_opt->key = "width";
  66. width_opt->type = TYPE_DOUBLE;
  67. width_opt->answer = "0";
  68. width_opt->description = _("Line width");
  69. fsize = G_define_option();
  70. fsize->key = "fontsize";
  71. fsize->type = TYPE_DOUBLE;
  72. fsize->required = NO;
  73. fsize->answer = "14";
  74. fsize->options = "1-360";
  75. fsize->description = _("Font size");
  76. fsize->guisection = _("Text");
  77. no_text = G_define_flag();
  78. no_text->key = 't';
  79. no_text->description = _("Draw the symbol without text");
  80. no_text->guisection = _("Text");
  81. /* TODO:
  82. - add rotation= option to rotate the north arrow by an arbitrary amount.
  83. do a bit of trig to figure out where to put the "N" (and rotate it too).
  84. - add a -n flag to rotate to match true north instead of grid north.
  85. Similar to 'g.region -n' but use the at=x,y coord for the convergence
  86. angle calc. (assuming that's the center of the icon)
  87. */
  88. if (G_parser(argc, argv))
  89. exit(EXIT_FAILURE);
  90. sscanf(coords->answers[0], "%lf", &east);
  91. sscanf(coords->answers[1], "%lf", &north);
  92. fontsize = atof(fsize->answer);
  93. if (no_text->answer)
  94. fontsize = -1;
  95. /* Parse and select foreground color */
  96. fg_color = D_parse_color(fg_color_opt->answer, 0);
  97. /* Parse and select background color */
  98. bg_color = D_parse_color(bg_color_opt->answer, 1);
  99. if (bg_color == 0)
  100. do_background = FALSE;
  101. line_width = atof(width_opt->answer);
  102. if (line_width < 0)
  103. line_width = 0;
  104. else if (line_width > 72)
  105. line_width = 72;
  106. if (D_open_driver() != 0)
  107. G_fatal_error(_("No graphics device selected. "
  108. "Use d.mon to select graphics device."));
  109. D_setup(0);
  110. draw_n_arrow(east, north, fontsize, n_arrow->answer, line_width);
  111. D_save_command(G_recreate_command());
  112. D_close_driver();
  113. exit(EXIT_SUCCESS);
  114. }