report.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include <math.h>
  2. #include <grass/dbmi.h>
  3. #include <grass/glocale.h>
  4. #include "global.h"
  5. int report(void)
  6. {
  7. int i;
  8. char left[20], right[20];
  9. if (!options.print && !(options.option == O_COUNT ||
  10. options.option == O_LENGTH ||
  11. options.option == O_AREA)) {
  12. G_warning(_("No totals for selected option"));
  13. return 0;
  14. }
  15. switch (options.option) {
  16. case O_CAT:
  17. if (G_verbose() > G_verbose_min())
  18. fprintf(stdout, "cat\n");
  19. for (i = 0; i < vstat.rcat; i++)
  20. fprintf(stdout, "%d\n", Values[i].cat);
  21. break;
  22. case O_COUNT:
  23. if (options.print) {
  24. if (G_verbose() > G_verbose_min())
  25. fprintf(stdout, "cat%scount\n", options.fs);
  26. for (i = 0; i < vstat.rcat; i++)
  27. fprintf(stdout, "%d%s%d\n", Values[i].cat, options.fs, Values[i].count1);
  28. }
  29. if (options.total) {
  30. int sum = 0;
  31. for (i = 0; i < vstat.rcat; i++) {
  32. sum += Values[i].count1;
  33. }
  34. fprintf(stdout, "total count%s%d\n", options.fs, sum);
  35. }
  36. break;
  37. case O_AREA:
  38. if (options.print) {
  39. if (G_verbose() > G_verbose_min())
  40. fprintf(stdout, "cat%sarea\n", options.fs);
  41. for (i = 0; i < vstat.rcat; i++)
  42. fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs, Values[i].d1);
  43. }
  44. if (options.total) {
  45. double sum = 0.0;
  46. for (i = 0; i < vstat.rcat; i++) {
  47. sum += Values[i].d1;
  48. }
  49. fprintf(stdout, "total area%s%.15g\n", options.fs, sum);
  50. }
  51. break;
  52. case O_COMPACT:
  53. /* perimeter / perimeter of equivalent circle
  54. * perimeter of equivalent circle: 2.0 * sqrt(M_PI * area) */
  55. if (G_verbose() > G_verbose_min())
  56. fprintf(stdout, "cat%scompact\n", options.fs);
  57. for (i = 0; i < vstat.rcat; i++) {
  58. Values[i].d1 = Values[i].d2 / (2.0 * sqrt(M_PI * Values[i].d1));
  59. fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs, Values[i].d1);
  60. }
  61. break;
  62. case O_FD:
  63. /* 2.0 * log(perimeter) / log(area)
  64. * this is neither
  65. * log(perimeter) / log(perimeter of equivalent circle)
  66. * perimeter of equivalent circle: 2 * sqrt(M_PI * area)
  67. * nor
  68. * log(area of equivalent circle) / log(area)
  69. * area of equivalent circle: (perimeter / (2 * sqrt(M_PI))^2
  70. *
  71. * avoid division by zero:
  72. * 2.0 * log(1 + perimeter) / log(1 + area) */
  73. if (G_verbose() > G_verbose_min())
  74. fprintf(stdout, "cat%sfd\n", options.fs);
  75. for (i = 0; i < vstat.rcat; i++) {
  76. if (Values[i].d1 == 1) /* log(1) == 0 */
  77. Values[i].d1 += 0.000001;
  78. Values[i].d1 = 2.0 * log(Values[i].d2) / log(Values[i].d1);
  79. fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs, Values[i].d1);
  80. }
  81. break;
  82. case O_PERIMETER:
  83. if (G_verbose() > G_verbose_min())
  84. fprintf(stdout, "cat%sperimeter\n", options.fs);
  85. for (i = 0; i < vstat.rcat; i++)
  86. fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs, Values[i].d1);
  87. break;
  88. case O_BBOX:
  89. if (G_verbose() > G_verbose_min())
  90. fprintf(stdout, "cat%sN%sS%sE%sW\n", options.fs, options.fs, options.fs,options.fs);
  91. for (i = 0; i < vstat.rcat; i++) {
  92. fprintf(stdout, "%d%s%.15g%s%.15g%s%.15g%s%.15g\n", Values[i].cat, options.fs,
  93. Values[i].d1, options.fs, Values[i].d2, options.fs, Values[i].d3,
  94. options.fs, Values[i].d4);
  95. }
  96. break;
  97. case O_LENGTH:
  98. if (options.print) {
  99. if (G_verbose() > G_verbose_min())
  100. fprintf(stdout, "cat%slength\n", options.fs);
  101. for (i = 0; i < vstat.rcat; i++)
  102. fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs, Values[i].d1);
  103. }
  104. if (options.total) {
  105. double sum = 0.0;
  106. for (i = 0; i < vstat.rcat; i++) {
  107. sum += Values[i].d1;
  108. }
  109. fprintf(stdout, "total length%s%.15g\n", options.fs, sum);
  110. }
  111. break;
  112. case O_SLOPE:
  113. if (G_verbose() > G_verbose_min())
  114. fprintf(stdout, "cat%sslope\n", options.fs);
  115. for (i = 0; i < vstat.rcat; i++)
  116. fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs, Values[i].d1);
  117. break;
  118. case O_SINUOUS:
  119. if (G_verbose() > G_verbose_min())
  120. fprintf(stdout, "cat%ssinuous\n", options.fs);
  121. for (i = 0; i < vstat.rcat; i++)
  122. fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs, Values[i].d1);
  123. break;
  124. case O_COOR:
  125. case O_START:
  126. case O_END:
  127. if (G_verbose() > G_verbose_min())
  128. fprintf(stdout, "cat%sx%sy%sz\n", options.fs, options.fs, options.fs);
  129. for (i = 0; i < vstat.rcat; i++) {
  130. if (Values[i].count1 == 1)
  131. fprintf(stdout, "%d%s%.15g%s%.15g%s%.15g\n", Values[i].cat, options.fs,
  132. Values[i].d1, options.fs, Values[i].d2, options.fs, Values[i].d3);
  133. }
  134. break;
  135. case O_SIDES:
  136. if (G_verbose() > G_verbose_min())
  137. fprintf(stdout, "cat%sleft%sright\n", options.fs, options.fs);
  138. for (i = 0; i < vstat.rcat; i++) {
  139. if (Values[i].count1 == 1) {
  140. if (Values[i].i1 >= 0)
  141. sprintf(left, "%d", Values[i].i1);
  142. else
  143. sprintf(left, "-1"); /* NULL, no area/cat */
  144. }
  145. else if (Values[i].count1 > 1) {
  146. sprintf(left, "-");
  147. }
  148. else { /* Values[i].count1 == 0 */
  149. /* It can be OK if the category is assigned to an element
  150. type which is not GV_BOUNDARY */
  151. /* -> TODO: print only if there is boundary with that cat */
  152. sprintf(left, "-");
  153. }
  154. if (Values[i].count2 == 1) {
  155. if (Values[i].i2 >= 0)
  156. sprintf(right, "%d", Values[i].i2);
  157. else
  158. sprintf(right, "-1"); /* NULL, no area/cat */
  159. }
  160. else if (Values[i].count2 > 1) {
  161. sprintf(right, "-");
  162. }
  163. else { /* Values[i].count1 == 0 */
  164. sprintf(right, "-");
  165. }
  166. fprintf(stdout, "%d%s%s%s%s\n", Values[i].cat, options.fs, left, options.fs, right);
  167. }
  168. break;
  169. case O_QUERY:
  170. if (G_verbose() > G_verbose_min())
  171. fprintf(stdout, "cat%squery\n", options.fs);
  172. for (i = 0; i < vstat.rcat; i++) {
  173. if (Values[i].null) {
  174. fprintf(stdout, "%d|-\n", Values[i].cat);
  175. }
  176. else {
  177. switch (vstat.qtype) {
  178. case (DB_C_TYPE_INT):
  179. fprintf(stdout, "%d%s%d\n", Values[i].cat, options.fs, Values[i].i1);
  180. break;
  181. case (DB_C_TYPE_DOUBLE):
  182. fprintf(stdout, "%d%s%15g\n", Values[i].cat, options.fs, Values[i].d1);
  183. break;
  184. case (DB_C_TYPE_STRING):
  185. fprintf(stdout, "%d%s%s\n", Values[i].cat, options.fs, Values[i].str1);
  186. break;
  187. }
  188. }
  189. }
  190. break;
  191. case O_AZIMUTH:
  192. if (G_verbose() > G_verbose_min())
  193. fprintf(stdout, "cat%sazimuth\n", options.fs);
  194. for (i = 0; i < vstat.rcat; i++)
  195. fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs, Values[i].d1);
  196. break;
  197. }
  198. return 0;
  199. }
  200. int print_stat(void)
  201. {
  202. if (vstat.rcat > 0) {
  203. int rcat_report;
  204. if (find_cat(-1, 0) != -1)
  205. rcat_report = vstat.rcat - 1;
  206. else
  207. rcat_report = vstat.rcat;
  208. G_message(_("%d categories read from vector map (layer %d)"),
  209. rcat_report, options.field); /* don't report cat -1 */
  210. }
  211. if (vstat.select > 0)
  212. G_message(_("%d records selected from table (layer %d)"),
  213. vstat.select, options.qfield);
  214. if (vstat.exist > 0)
  215. G_message(_("%d categories read from vector map exist in selection from table"),
  216. vstat.exist);
  217. if (vstat.notexist > 0)
  218. G_message(_("%d categories read from vector map don't exist in selection from table"),
  219. vstat.notexist);
  220. G_message(_("%d records updated/inserted (layer %d)"),
  221. vstat.update, options.field);
  222. if (vstat.error > 0)
  223. G_message(_("%d update/insert errors (layer %d)"),
  224. vstat.error, options.field);
  225. if (vstat.dupl > 0)
  226. G_message(_("%d categories with more points (coordinates not loaded)"),
  227. vstat.dupl);
  228. return 0;
  229. }