source: grass/branches/develbranch_6/raster/r.report/prt_report.c

Last change on this file was 32527, checked in by neteler, 16 years ago

indent -bad -bap -bbb -br -bli0 -bls -cli0 -ncs -fc1 -hnl -i4 \

-nbbo -nbc -nbfda -nbfde -ncdb -ncdw -nce -nfca -npcs -nprs \
-npsl -nsc -nsob -saf -sai -saw -sbi0 -ss -ts8 -ut

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id
  • Property svn:mime-type set to text/x-csrc
File size: 7.2 KB
Line 
1#include <stdlib.h>
2#include <string.h>
3#include "global.h"
4
5int print_report(int unit1, int unit2)
6{
7 int ns, nl, nx;
8 char num[100];
9 int len, new;
10 CELL *cats, *prev;
11 int first;
12 int i;
13 int divider_level;
14 int after_header;
15 int need_format;
16 int with_stats;
17 char *cp;
18 int spacing;
19 char dot;
20
21 /* examine units, determine output format */
22 for (i = unit1; i <= unit2; i++) {
23 need_format = 1;
24 unit[i].label[0] = "";
25 unit[i].label[1] = "";
26
27 switch (unit[i].type) {
28 case CELL_COUNTS:
29 need_format = 0;
30 unit[i].len = 5;
31 unit[i].label[0] = " cell";
32 unit[i].label[1] = "count";
33 ns = 0;
34 sprintf(num, "%ld", count_sum(&ns, -1));
35 len = strlen(num);
36 if (len > unit[i].len)
37 unit[i].len = len;
38 break;
39
40 case PERCENT_COVER:
41 need_format = 0;
42 unit[i].dp = 2;
43 unit[i].len = 6;
44 unit[i].label[0] = " % ";
45 unit[i].label[1] = "cover";
46 unit[i].eformat = 0;
47 break;
48
49 case SQ_METERS:
50 unit[i].label[0] = "square";
51 unit[i].label[1] = "meters";
52 unit[i].factor = 1.0;
53 break;
54
55 case SQ_KILOMETERS:
56 unit[i].label[0] = " square ";
57 unit[i].label[1] = "kilometers";
58 unit[i].factor = 1.0e-6;
59 break;
60
61 case ACRES:
62 unit[i].label[0] = "";
63 unit[i].label[1] = "acres";
64 unit[i].factor = 2.47105381467165e-4; /* 640 acres in a sq mile */
65 break;
66
67 case HECTARES:
68 unit[i].label[0] = "";
69 unit[i].label[1] = "hectares";
70 unit[i].factor = 1.0e-4;
71 break;
72
73 case SQ_MILES:
74 unit[i].label[0] = "square";
75 unit[i].label[1] = " miles";
76 unit[i].factor = 3.86102158542446e-7; /* 1 / ( (0.0254m/in * 12in/ft * 5280ft/mi)^2 ) */
77 break;
78
79 default:
80 G_fatal_error("Unit %d not yet supported", unit[i].type);
81 }
82 if (need_format) {
83 unit[i].dp = 6;
84 unit[i].len = 10;
85 unit[i].eformat = 0;
86 ns = 0;
87 format_parms(area_sum(&ns, -1) * unit[i].factor,
88 &unit[i].len, &unit[i].dp, &(unit[i].eformat),
89 e_format);
90 }
91 }
92
93 /* figure out how big the category numbers are when printed */
94 for (nl = 0; nl < nlayers; nl++)
95 layers[nl].nlen = 0;
96
97 for (ns = 0; ns < nstats; ns++) {
98 cats = Gstats[ns].cats;
99 for (nl = 0; nl < nlayers; nl++) {
100 construct_val_str(nl, &cats[nl], num);
101 len = strlen(num);
102 if (len > layers[nl].nlen)
103 layers[nl].nlen = len;
104 }
105 }
106
107 /* compute maximum category description lengths */
108 len = page_width - 2;
109 for (i = unit1; i <= unit2; i++)
110 len -= (unit[i].len + 1);
111 for (nl = 0; nl < nlayers; nl++) {
112 len -= (layers[nl].nlen + 1);
113 layers[nl].clen = len;
114 }
115
116 /* print the report */
117
118 header(unit1, unit2);
119 after_header = 1;
120 new = 1;
121
122 divider_level = -1;
123 for (ns = 0; ns < nstats; ns++) {
124 int NS;
125
126 cats = Gstats[ns].cats;
127
128 /* determine the number of lines needed to print the cat labels
129 * by pretending to print the labels and counting the number of
130 * print calls needed
131 */
132
133 if (page_length > 0) {
134 i = 0;
135 for (nl = 0; nl < nlayers; nl++) {
136 cp = construct_cat_label(nl, cats[nl]);
137
138 while (cp) {
139 i++;
140 cp = print_label(cp, layers[nl].clen, 0, 0, ' ');
141 }
142 }
143 if (nunits)
144 i += nlayers; /* divider lines */
145
146 /* if we don't have enough lines, go to a new page */
147 if (nlines <= i + 2) {
148 trailer();
149 header(unit1, unit2);
150 after_header = 1;
151 new = 2;
152 }
153 }
154
155 /* print the report */
156 for (nl = 0; nl < nlayers; nl++) {
157 if (new || (prev[nl] != cats[nl])) {
158 /* divider line between layers */
159
160 if (nunits && divider_level != nl && !after_header) {
161 for (nx = 0; nx < nl; nx++)
162 fprintf(stdout, "|%*s", layers[nx].nlen, "");
163 fprintf(stdout, "|");
164 for (nx = layers[nl].clen + layers[nx].nlen + 1; nx > 0;
165 nx--)
166 fprintf(stdout, "-");
167 for (i = unit1; i <= unit2; i++) {
168 fprintf(stdout, "|");
169 for (nx = unit[i].len; nx > 0; nx--)
170 fprintf(stdout, "-");
171 }
172 fprintf(stdout, "|");
173 newline();
174 }
175 divider_level = nl;
176 after_header = 0;
177
178 first = 1;
179 if (!new)
180 new = 1;
181
182 cp = construct_cat_label(nl, cats[nl]);
183
184 while (cp) {
185 for (nx = 0; nx < nl; nx++)
186 fprintf(stdout, "|%*s", layers[nx].nlen, "");
187 if (first) {
188 construct_val_str(nl, &cats[nl], num);
189 fprintf(stdout, "|%*s|", layers[nl].nlen, num);
190 }
191 else
192 fprintf(stdout, "|%*s|", layers[nl].nlen, "");
193
194 with_stats = nunits && first;
195 /*
196 if (new == 2 && nl != nlayers-1)
197 with_stats = 0;
198 */
199 if (with_stats)
200 /* if it's not the lowest level of the table */
201 {
202 if (nl != nlayers - 1) {
203 if (new != 2)
204 NS = ns; /* to memorise total */
205
206 /* if new is 2 then the total for this class should be reprinted on the
207 top of the page. So we need to remember ns of total in case we need to
208 print it again later */
209 spacing = 0;
210 dot = '_';
211 }
212 else {
213 spacing = 2;
214 dot = '.';
215 }
216 }
217 else {
218 spacing = 0;
219 dot = ' ';
220 }
221 cp = print_label(cp, layers[nl].clen, 1, spacing, dot);
222 if (with_stats) {
223 for (i = unit1; i <= unit2; i++)
224 if (nl != nlayers - 1)
225 print_unit(i, NS, nl);
226 else
227 print_unit(i, ns, nl);
228 }
229 else {
230 for (i = unit1; i <= unit2; i++)
231 fprintf(stdout, "|%*s", unit[i].len, "");
232 }
233 fprintf(stdout, "|");
234 newline();
235 first = 0;
236 }
237 }
238 }
239 new = 0;
240 prev = cats;
241 }
242 /* overall totals */
243 if (nunits) {
244 divider("|");
245 print_label("|TOTAL", layers[0].nlen + layers[0].clen + 2, 1, 0, ' ');
246 for (i = unit1; i <= unit2; i++)
247 print_unit(i, 0, -1);
248 /*
249 print_unit(i,-1,-1);
250 */
251 fprintf(stdout, "|");
252 newline();
253 }
254 trailer();
255
256 return 0;
257}
258
259int construct_val_str(int nl, CELL * pval, char *str)
260{
261 char str1[50], str2[50];
262 char *descr;
263 DCELL dLow, dHigh;
264
265 if (G_is_c_null_value(pval))
266 sprintf(str, "%s", no_data_str);
267 else if (!is_fp[nl] || as_int)
268 sprintf(str, "%d", *pval);
269 else { /* find out which floating point range to print */
270
271 if (cat_ranges)
272 descr = G_get_ith_d_raster_cat(&layers[nl].labels, *pval,
273 &dLow, &dHigh);
274 else {
275 dLow = (DMAX[nl] - DMIN[nl]) / nsteps *
276 (double)(*pval - 1) + DMIN[nl];
277 dHigh = (DMAX[nl] - DMIN[nl]) / nsteps * (double)*pval + DMIN[nl];
278 }
279 sprintf(str1, "%10f", dLow);
280 sprintf(str2, "%10f", dHigh);
281 G_strip(str1);
282 G_strip(str2);
283 G_trim_decimal(str1);
284 G_trim_decimal(str2);
285 sprintf(str, "%s-%s", str1, str2);
286 }
287
288 return 0;
289}
290
291char *construct_cat_label(int nl, CELL cat)
292{
293 DCELL dLow, dHigh;
294 CELL tmp = cat;
295 static char str[500];
296
297 if (!is_fp[nl] || as_int)
298 return G_get_cat(cat, &layers[nl].labels);
299 else { /* find or construct the label for
300 floating point range to print */
301 if (G_is_c_null_value(&tmp))
302 return G_store("no data");
303 if (cat_ranges)
304 return G_get_ith_d_raster_cat(&layers[nl].labels, cat,
305 &dLow, &dHigh);
306 else {
307 dLow = (DMAX[nl] - DMIN[nl]) / (double)nsteps *
308 (double)(cat - 1) + DMIN[nl];
309 dHigh = (DMAX[nl] - DMIN[nl]) / (double)nsteps *
310 (double)cat + DMIN[nl];
311 sprintf(str, "from %s to %s",
312 G_get_d_raster_cat(&dLow, &layers[nl].labels),
313 G_get_d_raster_cat(&dHigh, &layers[nl].labels));
314 return str;
315 }
316 } /* fp label */
317
318 return 0;
319}
Note: See TracBrowser for help on using the repository browser.