main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_b 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/raster.h>
  35. #include <grass/display.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. G_add_keyword(_("display"));
  75. G_add_keyword(_("histogram"));
  76. G_add_keyword(_("statistics"));
  77. module->description =
  78. _("Displays a histogram in the form of a pie or bar chart "
  79. "for a user-specified raster map.");
  80. opt1 = G_define_standard_option(G_OPT_R_MAP);
  81. opt1->description = _("Raster map for which histogram will be displayed");
  82. opt4 = G_define_option();
  83. opt4->key = "style";
  84. opt4->description = _("Indicate if a pie or bar chart is desired");
  85. opt4->type = TYPE_STRING;
  86. opt4->required = NO;
  87. opt4->options = "pie,bar";
  88. opt4->answer = "bar";
  89. /* The color option specifies the color for the labels, tic-marks,
  90. * and borders of the chart. */
  91. opt2 = G_define_standard_option(G_OPT_C_FG);
  92. opt2->label = _("Color for text and axes");
  93. bg_opt = G_define_standard_option(G_OPT_C_BG);
  94. #ifdef CAN_DO_AREAS
  95. opt3 = G_define_option();
  96. opt3->key = "type";
  97. opt3->description =
  98. _("Indicate if cell counts or map areas should be displayed");
  99. opt3->type = TYPE_STRING;
  100. opt3->required = NO;
  101. opt3->answer = "count";
  102. opt3->options = "count,area";
  103. #endif
  104. opt5 = G_define_option();
  105. opt5->key = "nsteps";
  106. opt5->description =
  107. _("Number of steps to divide the data range into (fp maps only)");
  108. opt5->type = TYPE_INTEGER;
  109. opt5->required = NO;
  110. opt5->answer = "255";
  111. flag1 = G_define_flag();
  112. flag1->key = 'n';
  113. flag1->description = _("Display information for null cells");
  114. flag2 = G_define_flag();
  115. flag2->key = 'q';
  116. flag2->description = _("Gather the histogram quietly");
  117. flag3 = G_define_flag();
  118. flag3->key = 'C';
  119. flag3->description =
  120. _("Report for ranges defined in cats file (fp maps only)");
  121. if (G_parser(argc, argv))
  122. exit(EXIT_FAILURE);
  123. map_name = opt1->answer;
  124. color = D_parse_color(opt2->answer, FALSE);
  125. type = COUNT;
  126. #ifdef CAN_DO_AREAS
  127. if (strcmp(opt3->answer, "count") == 0)
  128. type = COUNT;
  129. else
  130. type = AREA;
  131. #endif
  132. if (strcmp(opt4->answer, "bar") == 0)
  133. style = BAR;
  134. else
  135. style = PIE;
  136. if (sscanf(opt5->answer, "%d", &nsteps) != 1)
  137. G_fatal_error(_("Invalid number of steps: %s"), opt5->answer);
  138. cat_ranges = flag3->answer;
  139. if (cat_ranges && nsteps != 255)
  140. G_warning(_("When -C flag is set, the nsteps argument is ignored"));
  141. nodata = flag1->answer;
  142. quiet = flag2->answer ? YES : NO;
  143. if (Rast_read_colors(map_name, "", &pcolors) == -1)
  144. G_fatal_error(_("Color file for <%s> not available"), map_name);
  145. if (Rast_read_cats(map_name, "", &cats) == -1)
  146. G_fatal_error(_("Category file for <%s> not available"), map_name);
  147. if (Rast_read_range(map_name, "", &range) == -1)
  148. G_fatal_error(_("Range information for <%s> not available"),
  149. map_name);
  150. /* get the distribution statistics */
  151. get_stats(map_name, &dist_stats, quiet);
  152. /* set up the graphics driver and initialize its color-table */
  153. if (D_open_driver() != 0)
  154. G_fatal_error(_("No graphics device selected. "
  155. "Use d.mon to select graphics device."));
  156. D_setup_unity(0); /* 0 = don't clear frame */
  157. D_get_src(&t, &b, &l, &r);
  158. /* clear the frame, if requested to do so */
  159. if (strcmp(bg_opt->answer, "none") != 0)
  160. D_erase(bg_opt->answer);
  161. /* draw a title for */
  162. sprintf(title, "%s", map_name);
  163. text_height = (b - t) * 0.05;
  164. text_width = (r - l) * 0.05 * 0.50;
  165. D_text_size(text_width, text_height);
  166. D_get_text_box(title, &tt, &tb, &tl, &tr);
  167. D_pos_abs(l + (r - l) / 2 - (tr - tl) / 2,
  168. t + (b - t) * 0.07);
  169. D_use_color(color);
  170. D_text(title);
  171. /* plot the distributrion statistics */
  172. if (style == PIE)
  173. pie(&dist_stats, &pcolors);
  174. else
  175. bar(&dist_stats, &pcolors);
  176. D_save_command(G_recreate_command());
  177. D_close_driver();
  178. exit(EXIT_SUCCESS);
  179. }