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