main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: d.title
  5. *
  6. * AUTHOR(S): James Westervelt, US Army CERL
  7. *
  8. * PURPOSE: print out title for raster on stdout
  9. *
  10. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <grass/display.h>
  21. #include <grass/gis.h>
  22. #include <grass/raster.h>
  23. #include <grass/spawn.h>
  24. #include <grass/glocale.h>
  25. #include "options.h"
  26. #include "local_proto.h"
  27. const char *map_name;
  28. const char *color;
  29. float size;
  30. int type;
  31. int main(int argc, char **argv)
  32. {
  33. struct Cell_head window;
  34. struct Categories cats;
  35. struct GModule *module;
  36. struct Option *opt1, *opt2, *opt3;
  37. struct Flag *fancy_mode, *simple_mode, *draw;
  38. char *tmpfile;
  39. FILE *fp;
  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. module->description =
  46. _("Create a TITLE for a raster map in a form suitable "
  47. "for display with d.text.");
  48. opt1 = G_define_standard_option(G_OPT_R_MAP);
  49. opt2 = G_define_option();
  50. opt2->key = "color";
  51. opt2->type = TYPE_STRING;
  52. opt2->answer = DEFAULT_FG_COLOR;
  53. opt2->required = NO;
  54. opt2->gisprompt = "old_color,color,color";
  55. opt2->description = _("Sets the text color");
  56. opt3 = G_define_option();
  57. opt3->key = "size";
  58. opt3->type = TYPE_DOUBLE;
  59. opt3->answer = "4.0";
  60. opt3->options = "0-100";
  61. opt3->description =
  62. _("Sets the text size as percentage of the frame's height");
  63. draw = G_define_flag();
  64. draw->key = 'd';
  65. draw->description = _("Draw title on current display");
  66. fancy_mode = G_define_flag();
  67. fancy_mode->key = 'f';
  68. fancy_mode->description = _("Do a fancier title");
  69. /* currently just title, but it doesn't have to be /that/ simple */
  70. simple_mode = G_define_flag();
  71. simple_mode->key = 's';
  72. simple_mode->description = _("Do a simple title");
  73. /* Check command line */
  74. if (G_parser(argc, argv))
  75. exit(EXIT_FAILURE);
  76. map_name = opt1->answer;
  77. color = opt2->answer;
  78. if (opt3->answer != NULL)
  79. sscanf(opt3->answer, "%f", &size);
  80. type = fancy_mode->answer ? FANCY : NORMAL;
  81. if (fancy_mode->answer && simple_mode->answer)
  82. G_fatal_error(_("Title can be fancy or simple, not both"));
  83. if (!strlen(map_name))
  84. G_fatal_error(_("No map name given"));
  85. Rast_get_cellhd(map_name, "", &window);
  86. if (Rast_read_cats(map_name, "", &cats) == -1)
  87. G_fatal_error(_("Unable to read category file of raster map <%s>"),
  88. map_name);
  89. if (draw->answer) {
  90. tmpfile = G_convert_dirseps_to_host(G_tempfile());
  91. if (!(fp = fopen(tmpfile, "w")))
  92. G_fatal_error(_("Unable to open temporary file <%s>"), tmpfile);
  93. }
  94. else
  95. fp = stdout;
  96. if (type == NORMAL)
  97. normal(&window, &cats, simple_mode->answer, fp);
  98. else
  99. fancy(&window, &cats, fp);
  100. if (draw->answer) {
  101. char inarg[GPATH_MAX];
  102. fclose(fp);
  103. sprintf(inarg, "input=%s", tmpfile);
  104. G_spawn("d.text", "d.text", inarg, NULL);
  105. unlink(tmpfile);
  106. /* note a tmp file will remain, created by d.text so it can survive d.redraw */
  107. }
  108. exit(EXIT_SUCCESS);
  109. }