cats.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /****************************************************************************
  2. *
  3. * MODULE: r.cats
  4. *
  5. * AUTHOR(S): Michael Shapiro - CERL
  6. *
  7. * PURPOSE: Prints category values and labels associated with
  8. * user-specified raster map layers.
  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 <stdlib.h>
  18. #include <grass/gis.h>
  19. #include <grass/raster.h>
  20. #include <grass/glocale.h>
  21. #include "local_proto.h"
  22. static struct Cell_stats statf;
  23. int get_cats(const char *name, const char *mapset)
  24. {
  25. int fd;
  26. int row, nrows, ncols;
  27. CELL *cell;
  28. struct Cell_head cellhd;
  29. /* set the window to the cell header */
  30. Rast_get_cellhd(name, mapset, &cellhd);
  31. Rast_set_window(&cellhd);
  32. /* open the raster map */
  33. fd = Rast_open_old(name, mapset);
  34. nrows = Rast_window_rows();
  35. ncols = Rast_window_cols();
  36. cell = Rast_allocate_c_buf();
  37. Rast_init_cell_stats(&statf);
  38. /* read the raster map */
  39. G_verbose_message(_("Reading <%s> in <%s>"), name, mapset);
  40. for (row = 0; row < nrows; row++) {
  41. if (G_verbose() > G_verbose_std())
  42. G_percent(row, nrows, 2);
  43. Rast_get_c_row_nomask(fd, cell, row);
  44. Rast_update_cell_stats(cell, ncols, &statf);
  45. }
  46. /* done */
  47. if (G_verbose() > G_verbose_std())
  48. G_percent(row, nrows, 2);
  49. Rast_close(fd);
  50. G_free(cell);
  51. Rast_rewind_cell_stats(&statf);
  52. return 0;
  53. }
  54. int next_cat(long *x)
  55. {
  56. long count;
  57. CELL cat;
  58. if (Rast_next_cell_stat(&cat, &count, &statf)) {
  59. *x = cat;
  60. return 1;
  61. }
  62. return 0;
  63. }