histogram.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. #define LIST struct Histogram_list
  6. static FILE *fopen_histogram_new(const char *);
  7. static int cmp(const void *, const void *);
  8. static int cmp_count(const void *, const void *);
  9. /*!
  10. * \brief initializes the histogram structure
  11. *
  12. * initializes the histogram structure for calls to Rast_set_histogram()
  13. * and Rast_add_histogram()
  14. * \param histogram
  15. * \return
  16. */
  17. void Rast_init_histogram(struct Histogram *histogram)
  18. {
  19. histogram->num = 0;
  20. histogram->list = NULL;
  21. }
  22. /*!
  23. * \brief read the histogram information
  24. *
  25. * Reads the histogram information associated with map layer "map"
  26. * in mapset "mapset" into the structure "histogram".
  27. *
  28. * note: a warning message is printed if the file is missing or incorrect
  29. * \param name: name of map
  30. * \param mapset: mapset that map belongs to
  31. * \param histogram: struct for histogram
  32. * \return 1 if successful,
  33. * 0 if no histogram file,
  34. */
  35. int Rast_read_histogram(const char *name, const char *mapset,
  36. struct Histogram *histogram)
  37. {
  38. FILE *fd = NULL;
  39. long cat;
  40. long count;
  41. char buf[200];
  42. Rast_init_histogram(histogram);
  43. if (!G_find_file2_misc("cell_misc", "histogram", name, mapset)) {
  44. G_warning(_("Histogram for [%s in %s] missing (run r.support)"), name,
  45. mapset);
  46. return 0;
  47. }
  48. fd = G_fopen_old_misc("cell_misc", "histogram", name, mapset);
  49. if (!fd)
  50. G_fatal_error(_("Can't read histogram for [%s in %s]"), name, mapset);
  51. while (fgets(buf, sizeof buf, fd)) {
  52. if (sscanf(buf, "%ld:%ld", &cat, &count) != 2)
  53. G_fatal_error(_("Invalid histogram file for [%s in %s]"),
  54. name, mapset);
  55. Rast_extend_histogram((CELL) cat, count, histogram);
  56. }
  57. fclose(fd);
  58. if (histogram->num == 0)
  59. G_fatal_error(_("Invalid histogram file for [%s in %s]"), name, mapset);
  60. Rast_sort_histogram(histogram);
  61. return 1;
  62. }
  63. /*!
  64. * \brief Writes the histogram information
  65. *
  66. * Writes the histogram information associated with map layer "name"
  67. * \param name: name of map
  68. * \param histogram: struct for histogram
  69. * \return void
  70. */
  71. void Rast_write_histogram(const char *name, const struct Histogram *histogram)
  72. {
  73. FILE *fp;
  74. int n;
  75. LIST *list;
  76. fp = fopen_histogram_new(name);
  77. list = histogram->list;
  78. for (n = 0; n < histogram->num; n++) {
  79. if (list[n].count)
  80. fprintf(fp, "%ld:%ld\n", (long)list[n].cat, list[n].count);
  81. }
  82. fclose(fp);
  83. }
  84. /*!
  85. * \brief Writes the histogram based on cell statistics to file
  86. *
  87. * \param name: name of map
  88. * \param statf: cell statistics
  89. * \return void
  90. */
  91. void Rast_write_histogram_cs(const char *name, struct Cell_stats *statf)
  92. {
  93. FILE *fp;
  94. CELL cat;
  95. long count;
  96. fp = fopen_histogram_new(name);
  97. Rast_rewind_cell_stats(statf);
  98. while (Rast_next_cell_stat(&cat, &count, statf)) {
  99. if (count > 0)
  100. fprintf(fp, "%ld:%ld\n", (long)cat, count);
  101. }
  102. fclose(fp);
  103. }
  104. /*!
  105. * \brief Creates histogram based on cell statistics
  106. *
  107. * \param statf: cell statistics
  108. * \param histogram: raster histogram
  109. * \return
  110. */
  111. void Rast_make_histogram_cs(struct Cell_stats *statf,
  112. struct Histogram *histogram)
  113. {
  114. CELL cat;
  115. long count;
  116. Rast_init_histogram(histogram);
  117. Rast_rewind_cell_stats(statf);
  118. while (Rast_next_cell_stat(&cat, &count, statf))
  119. Rast_add_histogram(cat, count, histogram);
  120. Rast_sort_histogram(histogram);
  121. }
  122. /*!
  123. * \brief Sorts the histogram in ascending order by counts then category
  124. *
  125. * Sorts the histogram in ascending order by counts then category.
  126. * No combining is done.
  127. * \param histogram: struct for histogram
  128. * \return 1 if successful,
  129. * -1 on fail
  130. */
  131. int Rast_get_histogram_num(const struct Histogram *histogram)
  132. {
  133. return histogram->num;
  134. }
  135. /*!
  136. * \brief Returns cat for the nth element in the histogram
  137. *
  138. * Returns cat for the nth element in the histogram
  139. * \param histogram: struct for histogram
  140. * \return CELL
  141. */
  142. CELL Rast_get_histogram_cat(int n, const struct Histogram * histogram)
  143. {
  144. if (n < 0 || n >= histogram->num)
  145. return 0;
  146. return histogram->list[n].cat;
  147. }
  148. /*!
  149. * \brief Returns count for the nth element in the histogram
  150. *
  151. * Returns count for the nth element in the histogram
  152. * \param n: nth element
  153. * \param histogram: struct for histogram
  154. * \return count
  155. */
  156. long Rast_get_histogram_count(int n, const struct Histogram *histogram)
  157. {
  158. if (n < 0 || n >= histogram->num)
  159. return 0;
  160. return histogram->list[n].count;
  161. }
  162. /*!
  163. * \brief Frees memory allocated for the histogram
  164. *
  165. * frees the memory allocated for the histogram
  166. * \param histogram: struct for histogram
  167. * \return
  168. */
  169. void Rast_free_histogram(struct Histogram *histogram)
  170. {
  171. if (histogram->num > 0)
  172. G_free(histogram->list);
  173. histogram->num = 0;
  174. histogram->list = NULL;
  175. }
  176. /*!
  177. * \brief Sorts the histogram
  178. *
  179. * Sorts the histogram in ascending order by category,
  180. * combining (by adding) elements that have the same category.
  181. * \param histogram: struct for histogram
  182. * \return 0 if successful,
  183. * 1 on fail
  184. */
  185. int Rast_sort_histogram(struct Histogram *histogram)
  186. {
  187. int a, b, n;
  188. LIST *list;
  189. /* if histogram only has 1 entry, nothing to do */
  190. if ((n = histogram->num) <= 1)
  191. return 1;
  192. list = histogram->list;
  193. /* quick check to see if sorting needed */
  194. for (a = 1; a < n; a++)
  195. if (list[a - 1].cat >= list[a].cat)
  196. break;
  197. if (a >= n)
  198. return 1;
  199. /* sort */
  200. qsort(list, n, sizeof(LIST), &cmp);
  201. /* sum duplicate entries */
  202. for (a = 0, b = 1; b < n; b++) {
  203. if (list[a].cat != list[b].cat) {
  204. a++;
  205. list[a].count = list[b].count;
  206. list[a].cat = list[b].cat;
  207. }
  208. else {
  209. list[a].count += list[b].count;
  210. }
  211. }
  212. histogram->num = a + 1;
  213. return 0;
  214. }
  215. static int cmp(const void *aa, const void *bb)
  216. {
  217. const LIST *a = aa, *b = bb;
  218. if (a->cat < b->cat)
  219. return -1;
  220. if (a->cat > b->cat)
  221. return 1;
  222. return 0;
  223. }
  224. /*!
  225. * \brief Sorts the histogram by counts
  226. *
  227. * Sorts the histogram in ascending order by counts then category.
  228. * No combining is done.
  229. * \param histogram: struct for histogram
  230. * \return 0 if successful,
  231. * 1 on fail
  232. */
  233. int Rast_sort_histogram_by_count(struct Histogram *histogram)
  234. {
  235. int n;
  236. LIST *list;
  237. /* if histogram only has 1 entry, nothing to do */
  238. if ((n = histogram->num) <= 1)
  239. return 1;
  240. list = histogram->list;
  241. /* sort */
  242. qsort(list, n, sizeof(LIST), &cmp_count);
  243. return 0;
  244. }
  245. static int cmp_count(const void *aa, const void *bb)
  246. {
  247. const LIST *a = aa, *b = bb;
  248. if (a->count < b->count)
  249. return -1;
  250. if (a->count > b->count)
  251. return 1;
  252. if (a->cat < b->cat)
  253. return -1;
  254. if (a->cat > b->cat)
  255. return 1;
  256. return 0;
  257. }
  258. static FILE *fopen_histogram_new(const char *name)
  259. {
  260. FILE *fp;
  261. fp = G_fopen_new_misc("cell_misc", "histogram", name);
  262. if (!fp)
  263. G_fatal_error(_("Unable to create histogram file for <%s>"), name);
  264. return fp;
  265. }
  266. /*!
  267. * \brief Removes the histogram
  268. *
  269. * Removes the histogram information associated with map layer "name"
  270. * \param name: name of map
  271. * \return
  272. */
  273. void Rast_remove_histogram(const char *name)
  274. {
  275. G_remove_misc("cell_misc", "histogram", name);
  276. }
  277. /*!
  278. * \brief adds count to the histogram value for cat
  279. *
  280. * adds count to the histogram value for cat
  281. * \param cat: category
  282. * \param count
  283. * \param histogram: struct for histogram
  284. * \return 0 if successful,
  285. * 1 on fail
  286. */
  287. int Rast_add_histogram(CELL cat, long count, struct Histogram *histogram)
  288. {
  289. int i;
  290. for (i = 0; i < histogram->num; i++) {
  291. if (histogram->list[i].cat == cat) {
  292. histogram->list[i].count += count;
  293. return 1;
  294. }
  295. }
  296. Rast_extend_histogram(cat, count, histogram);
  297. return 0;
  298. }
  299. /*!
  300. * \brief sets the histogram value for cat to count
  301. *
  302. * sets the histogram value for cat to count
  303. * \param cat: category
  304. * \param count
  305. * \param histogram: struct for histogram
  306. * \return 0 if successful,
  307. * 1 on fail
  308. */
  309. int Rast_set_histogram(CELL cat, long count, struct Histogram *histogram)
  310. {
  311. int i;
  312. for (i = 0; i < histogram->num; i++) {
  313. if (histogram->list[i].cat == cat) {
  314. histogram->list[i].count = count;
  315. return 1;
  316. }
  317. }
  318. Rast_extend_histogram(cat, count, histogram);
  319. return 0;
  320. }
  321. /*!
  322. * \brief Extends histogram struct to accommodate a new value
  323. *
  324. * \param cat: category
  325. * \param count
  326. * \param histogram: struct for histogram
  327. * \return
  328. */
  329. void Rast_extend_histogram(CELL cat, long count, struct Histogram *histogram)
  330. {
  331. histogram->num++;
  332. histogram->list =
  333. (LIST *) G_realloc(histogram->list, histogram->num * sizeof(LIST));
  334. histogram->list[histogram->num - 1].cat = cat;
  335. histogram->list[histogram->num - 1].count = count;
  336. }
  337. /*!
  338. * \brief Zero out histogram struct
  339. *
  340. * \param histogram: struct for histogram
  341. * \return
  342. */
  343. void Rast_zero_histogram(struct Histogram *histogram)
  344. {
  345. int i;
  346. for (i = 0; i < histogram->num; i++)
  347. histogram->list[i].count = 0;
  348. }