describe.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /****************************************************************************
  2. *
  3. * MODULE: r.describe
  4. *
  5. * AUTHOR(S): Michael Shapiro - CERL
  6. *
  7. * PURPOSE: Prints terse list of category values found in a raster
  8. * map layer.
  9. *
  10. * COPYRIGHT: (C) 2006 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. ***************************************************************************/
  17. #include <grass/gis.h>
  18. #include <grass/raster.h>
  19. #include <grass/glocale.h>
  20. #include "local_proto.h"
  21. int describe(const char *name, int compact, char *no_data_str,
  22. int range, int windowed, int nsteps, int as_int, int skip_nulls)
  23. {
  24. int fd;
  25. struct Cell_stats statf;
  26. CELL *buf, *b;
  27. int nrows, ncols;
  28. int row, col;
  29. struct Cell_head window;
  30. CELL negmin = 0, negmax = 0, zero = 0, posmin = 0, posmax = 0;
  31. CELL null = 0;
  32. RASTER_MAP_TYPE map_type;
  33. struct Quant q;
  34. struct FPRange r;
  35. DCELL dmin, dmax;
  36. void (*get_row)(int, CELL *, int);
  37. if (windowed) {
  38. get_row = Rast_get_c_row;
  39. }
  40. else {
  41. Rast_get_cellhd(name, "", &window);
  42. Rast_set_window(&window);
  43. get_row = Rast_get_c_row_nomask;
  44. }
  45. fd = Rast_open_old(name, "");
  46. map_type = Rast_get_map_type(fd);
  47. if (as_int)
  48. map_type = CELL_TYPE; /* read as int */
  49. /* allocate the cell buffer */
  50. buf = Rast_allocate_c_buf();
  51. if (map_type != CELL_TYPE && range)
  52. /* this will make it report fp range */
  53. {
  54. range = 0;
  55. nsteps = 1;
  56. }
  57. /* start the cell stats */
  58. if (!range) {
  59. Rast_init_cell_stats(&statf);
  60. }
  61. else {
  62. zero = 0;
  63. negmin = 0;
  64. negmax = 0;
  65. posmin = 0;
  66. posmax = 0;
  67. null = 0;
  68. dmin = 0.0;
  69. dmax = 0.0;
  70. }
  71. /* set up quantization rules */
  72. if (map_type != CELL_TYPE) {
  73. Rast_quant_init(&q);
  74. Rast_read_fp_range(name, "", &r);
  75. Rast_get_fp_range_min_max(&r, &dmin, &dmax);
  76. Rast_quant_add_rule(&q, dmin, dmax, 1, nsteps);
  77. Rast_set_quant_rules(fd, &q);
  78. }
  79. nrows = Rast_window_rows();
  80. ncols = Rast_window_cols();
  81. G_verbose_message(_("Reading <%s> ..."), name);
  82. for (row = 0; row < nrows; row++) {
  83. G_percent(row, nrows, 2);
  84. (*get_row) (fd, b = buf, row);
  85. if (range) {
  86. for (col = ncols; col-- > 0; b++) {
  87. if (Rast_is_c_null_value(b))
  88. null = 1;
  89. else if (*b == 0)
  90. zero = 1;
  91. else if (*b < 0) {
  92. if (!negmin)
  93. negmin = negmax = *b;
  94. else if (*b > negmax)
  95. negmax = *b;
  96. else if (*b < negmin)
  97. negmin = *b;
  98. }
  99. else {
  100. if (!posmin)
  101. posmin = posmax = *b;
  102. else if (*b > posmax)
  103. posmax = *b;
  104. else if (*b < posmin)
  105. posmin = *b;
  106. }
  107. }
  108. }
  109. else
  110. Rast_update_cell_stats(buf, ncols, &statf);
  111. }
  112. G_percent(nrows, nrows, 2);
  113. Rast_close(fd);
  114. G_free(buf);
  115. if (range) {
  116. if (compact)
  117. compact_range_list(negmin, negmax, zero, posmin, posmax, null,
  118. no_data_str, skip_nulls);
  119. else
  120. range_list(negmin, negmax, zero, posmin, posmax, null,
  121. no_data_str, skip_nulls);
  122. }
  123. else {
  124. Rast_rewind_cell_stats(&statf);
  125. if (compact)
  126. compact_list(&statf, dmin, dmax, no_data_str, skip_nulls,
  127. map_type, nsteps);
  128. else
  129. long_list(&statf, dmin, dmax, no_data_str, skip_nulls, map_type,
  130. nsteps);
  131. Rast_free_cell_stats(&statf);
  132. }
  133. return 1;
  134. }