main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /****************************************************************************
  2. *
  3. * MODULE: d.histogram
  4. * AUTHOR(S): Dave Johnson, DBA Systems, Inc. (original contributor)
  5. * 10560 Arrowhead Drive Fairfax, Virginia 22030
  6. * Markus Neteler <neteler itc.it>
  7. * Bernhard Reiter <bernhard intevation.de>,
  8. * Eric G. Miller <egm2 jps.net>,
  9. * Glynn Clements <glynn gclements.plus.com>,
  10. * Hamish Bowman <hamish_nospam yahoo.com>,
  11. * Jan-Oliver Wagner <jan intevation.de>
  12. * PURPOSE: draw a bar-chart or a pie-chart representing the
  13. * histogram statistics of a cell-file
  14. * COPYRIGHT: (C) 1999-2007 by the GRASS Development Team
  15. *
  16. * This program is free software under the GNU General Public
  17. * License (>=v2). Read the file COPYING that comes with GRASS
  18. * for details.
  19. *
  20. *****************************************************************************/
  21. /******************************************************************************
  22. * NOTE (shapiro):
  23. * This program can NOT handle area information.
  24. * Areas (as output by the r.stats command) are doubles.
  25. * This program was written assuming areas are integers.
  26. *
  27. * The area option has been #ifdef'ed out of the code until someone
  28. * upgrades both the get_stats() and the pie() and bar() routines
  29. * as well as the struct stat_list (defined in dhist.h).
  30. *****************************************************************************/
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <grass/gis.h>
  34. #include <grass/display.h>
  35. #include <grass/raster.h>
  36. #include <grass/glocale.h>
  37. #include "options.h"
  38. #include "dhist.h"
  39. struct stat_list dist_stats;
  40. struct Categories cats;
  41. struct FPRange fp_range;
  42. int is_fp;
  43. char *map_name;
  44. int color;
  45. float size;
  46. int style;
  47. int type;
  48. int is_fp;
  49. int nodata;
  50. int nsteps;
  51. int cat_ranges;
  52. int main(int argc, char **argv)
  53. {
  54. int text_height;
  55. int text_width;
  56. struct Categories cats;
  57. struct Range range;
  58. struct Colors pcolors;
  59. char title[GNAME_MAX];
  60. double tt, tb, tl, tr;
  61. double t, b, l, r;
  62. int quiet;
  63. struct GModule *module;
  64. struct Option *opt1;
  65. struct Option *opt2, *bg_opt;
  66. struct Option *opt4;
  67. struct Option *opt5;
  68. struct Flag *flag1;
  69. struct Flag *flag2;
  70. struct Flag *flag3;
  71. /* Initialize the GIS calls */
  72. G_gisinit(argv[0]);
  73. module = G_define_module();
  74. module->keywords = _("display");
  75. module->description =
  76. _("Displays a histogram in the form of a pie or bar chart "
  77. "for a user-specified raster map.");
  78. opt1 = G_define_standard_option(G_OPT_R_MAP);
  79. opt1->description = _("Raster map for which histogram will be displayed");
  80. opt4 = G_define_option();
  81. opt4->key = "style";
  82. opt4->description = _("Indicate if a pie or bar chart is desired");
  83. opt4->type = TYPE_STRING;
  84. opt4->required = NO;
  85. opt4->options = "pie,bar";
  86. opt4->answer = "bar";
  87. /* The color option specifies the color for the labels, tic-marks,
  88. * and borders of the chart. */
  89. opt2 = G_define_standard_option(G_OPT_C_FG);
  90. opt2->label = _("Color for text and axes");
  91. bg_opt = G_define_standard_option(G_OPT_C_BG);
  92. #ifdef CAN_DO_AREAS
  93. opt3 = G_define_option();
  94. opt3->key = "type";
  95. opt3->description =
  96. _("Indicate if cell counts or map areas should be displayed");
  97. opt3->type = TYPE_STRING;
  98. opt3->required = NO;
  99. opt3->answer = "count";
  100. opt3->options = "count,area";
  101. #endif
  102. opt5 = G_define_option();
  103. opt5->key = "nsteps";
  104. opt5->description =
  105. _("Number of steps to divide the data range into (fp maps only)");
  106. opt5->type = TYPE_INTEGER;
  107. opt5->required = NO;
  108. opt5->answer = "255";
  109. flag1 = G_define_flag();
  110. flag1->key = 'n';
  111. flag1->description = _("Display information for null cells");
  112. flag2 = G_define_flag();
  113. flag2->key = 'q';
  114. flag2->description = _("Gather the histogram quietly");
  115. flag3 = G_define_flag();
  116. flag3->key = 'C';
  117. flag3->description =
  118. _("Report for ranges defined in cats file (fp maps only)");
  119. if (G_parser(argc, argv))
  120. exit(EXIT_FAILURE);
  121. map_name = opt1->answer;
  122. color = D_parse_color(opt2->answer, FALSE);
  123. type = COUNT;
  124. #ifdef CAN_DO_AREAS
  125. if (strcmp(opt3->answer, "count") == 0)
  126. type = COUNT;
  127. else
  128. type = AREA;
  129. #endif
  130. if (strcmp(opt4->answer, "bar") == 0)
  131. style = BAR;
  132. else
  133. style = PIE;
  134. if (sscanf(opt5->answer, "%d", &nsteps) != 1)
  135. G_fatal_error(_("Invalid number of steps: %s"), opt5->answer);
  136. cat_ranges = flag3->answer;
  137. if (cat_ranges && nsteps != 255)
  138. G_warning(_("When -C flag is set, the nsteps argument is ignored"));
  139. nodata = flag1->answer;
  140. quiet = flag2->answer ? YES : NO;
  141. if (G_read_colors(map_name, "", &pcolors) == -1)
  142. G_fatal_error(_("Color file for <%s> not available"), map_name);
  143. if (G_read_cats(map_name, "", &cats) == -1)
  144. G_fatal_error(_("Category file for <%s> not available"), map_name);
  145. if (G_read_range(map_name, "", &range) == -1)
  146. G_fatal_error(_("Range information for <%s> not available"),
  147. map_name);
  148. /* get the distribution statistics */
  149. get_stats(map_name, &dist_stats, quiet);
  150. /* set up the graphics driver and initialize its color-table */
  151. if (R_open_driver() != 0)
  152. G_fatal_error(_("No graphics device selected"));
  153. D_setup_unity(0); /* 0 = don't clear frame */
  154. D_get_src(&t, &b, &l, &r);
  155. /* clear the frame, if requested to do so */
  156. if (strcmp(bg_opt->answer, "none") != 0)
  157. D_erase(bg_opt->answer);
  158. /* draw a title for */
  159. sprintf(title, "%s", map_name);
  160. text_height = (b - t) * 0.05;
  161. text_width = (r - l) * 0.05 * 0.50;
  162. R_text_size(text_width, text_height);
  163. D_get_text_box(title, &tt, &tb, &tl, &tr);
  164. D_pos_abs(l + (r - l) / 2 - (tr - tl) / 2,
  165. t + (b - t) * 0.07);
  166. D_use_color(color);
  167. R_text(title);
  168. /* plot the distributrion statistics */
  169. if (style == PIE)
  170. pie(&dist_stats, &pcolors);
  171. else
  172. bar(&dist_stats, &pcolors);
  173. R_close_driver();
  174. exit(EXIT_SUCCESS);
  175. }