check.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. **********************************************************************
  3. *
  4. * MODULE: r.support.stats
  5. *
  6. * AUTHOR(S): Brad Douglas <rez touchofmadness com>
  7. *
  8. * PURPOSE: Update raster statistics
  9. *
  10. * COPYRIGHT: (C) 2006 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General
  13. * Purpose License (>=v2). Read the file COPYING that
  14. * comes with GRASS 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. /*
  23. * check_stats() - Check and update statistics
  24. *
  25. * RETURN: 0 on success / 1 on failure
  26. */
  27. int check_stats(const char *name)
  28. {
  29. RASTER_MAP_TYPE data_type;
  30. struct Histogram histogram;
  31. struct Categories cats;
  32. struct Range range;
  33. struct FPRange fprange;
  34. int i, histo_num;
  35. int cats_ok;
  36. int max;
  37. data_type = Rast_map_type(name, "");
  38. G_message(_("Updating statistics for [%s]..."), name);
  39. if (!do_histogram(name))
  40. return 1;
  41. if (Rast_read_histogram(name, "", &histogram) <= 0)
  42. return 1;
  43. /* Init histogram range */
  44. if (data_type == CELL_TYPE)
  45. Rast_init_range(&range);
  46. else
  47. Rast_init_fp_range(&fprange);
  48. G_message(_("Updating histogram range..."));
  49. i = histo_num = Rast_get_histogram_num(&histogram);
  50. while (i >= 0) {
  51. G_percent(i, histo_num, 2);
  52. if (data_type == CELL_TYPE)
  53. Rast_update_range(Rast_get_histogram_cat(i--, &histogram), &range);
  54. else
  55. Rast_update_fp_range((DCELL) Rast_get_histogram_cat(i--, &histogram),
  56. &fprange);
  57. }
  58. /* Write histogram range */
  59. if (data_type == CELL_TYPE)
  60. Rast_write_range(name, &range);
  61. else
  62. Rast_write_fp_range(name, &fprange);
  63. /* Get category status and max */
  64. cats_ok = (Rast_read_cats(name, "", &cats) >= 0);
  65. max = (data_type == CELL_TYPE ? range.max : fprange.max);
  66. /* Further category checks */
  67. if (!cats_ok)
  68. Rast_init_cats("", &cats);
  69. else if (cats.num != max) {
  70. cats.num = max;
  71. cats_ok = 0;
  72. }
  73. /* Update categories if needed */
  74. if (!cats_ok) {
  75. G_message(_("Updating the number of categories for [%s]..."), name);
  76. Rast_write_cats(name, &cats);
  77. }
  78. Rast_free_histogram(&histogram);
  79. Rast_free_cats(&cats);
  80. return 0;
  81. }