main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. struct GModule *module;
  63. struct Option *opt1;
  64. struct Option *opt2, *bg_opt;
  65. struct Option *opt4;
  66. struct Option *opt5;
  67. struct Flag *flag1;
  68. struct Flag *flag2;
  69. /* Initialize the GIS calls */
  70. G_gisinit(argv[0]);
  71. module = G_define_module();
  72. G_add_keyword(_("display"));
  73. G_add_keyword(_("histogram"));
  74. G_add_keyword(_("statistics"));
  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);
  90. opt2->label = _("Color for text and axes");
  91. bg_opt = G_define_standard_option(G_OPT_CN);
  92. bg_opt->key = "bgcolor";
  93. bg_opt->label = _("Background color");
  94. bg_opt->answer = DEFAULT_BG_COLOR;
  95. #ifdef CAN_DO_AREAS
  96. opt3 = G_define_option();
  97. opt3->key = "type";
  98. opt3->description =
  99. _("Indicate if cell counts or map areas should be displayed");
  100. opt3->type = TYPE_STRING;
  101. opt3->required = NO;
  102. opt3->answer = "count";
  103. opt3->options = "count,area";
  104. #endif
  105. opt5 = G_define_option();
  106. opt5->key = "nsteps";
  107. opt5->description =
  108. _("Number of steps to divide the data range into (fp maps only)");
  109. opt5->type = TYPE_INTEGER;
  110. opt5->required = NO;
  111. opt5->answer = "255";
  112. flag1 = G_define_flag();
  113. flag1->key = 'n';
  114. flag1->description = _("Display information for null cells");
  115. flag2 = G_define_flag();
  116. flag2->key = 'c';
  117. flag2->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 = flag2->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. if (Rast_read_colors(map_name, "", &pcolors) == -1)
  141. G_fatal_error(_("Color file for <%s> not available"), map_name);
  142. if (Rast_read_cats(map_name, "", &cats) == -1)
  143. G_fatal_error(_("Category file for <%s> not available"), map_name);
  144. if (Rast_read_range(map_name, "", &range) == -1)
  145. G_fatal_error(_("Range information for <%s> not available"),
  146. map_name);
  147. /* get the distribution statistics */
  148. get_stats(map_name, &dist_stats);
  149. /* set up the graphics driver and initialize its color-table */
  150. D_open_driver();
  151. D_setup_unity(0); /* 0 = don't clear frame */
  152. D_get_src(&t, &b, &l, &r);
  153. /* clear the frame, if requested to do so */
  154. if (strcmp(bg_opt->answer, "none") != 0)
  155. D_erase(bg_opt->answer);
  156. /* draw a title for */
  157. sprintf(title, "%s", map_name);
  158. text_height = (b - t) * 0.05;
  159. text_width = (r - l) * 0.05 * 0.50;
  160. D_text_size(text_width, text_height);
  161. D_get_text_box(title, &tt, &tb, &tl, &tr);
  162. D_pos_abs(l + (r - l) / 2 - (tr - tl) / 2,
  163. t + (b - t) * 0.07);
  164. D_use_color(color);
  165. D_text(title);
  166. /* plot the distributrion statistics */
  167. if (style == PIE)
  168. pie(&dist_stats, &pcolors);
  169. else
  170. bar(&dist_stats, &pcolors);
  171. D_save_command(G_recreate_command());
  172. D_close_driver();
  173. exit(EXIT_SUCCESS);
  174. }