check.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. #include "local_proto.h"
  6. /*
  7. * check_stats() - Check and update statistics
  8. *
  9. * RETURN: EXIT_SUCCESS / EXIT_FAILURE
  10. */
  11. int check_stats(const char *name)
  12. {
  13. RASTER_MAP_TYPE data_type;
  14. struct Histogram histogram;
  15. struct Categories cats;
  16. struct Range range;
  17. struct FPRange fprange;
  18. int i;
  19. int cats_ok;
  20. int max;
  21. data_type = Rast_map_type(name, "");
  22. G_message(_("\n Updating statistics for [%s]"), name);
  23. if (do_histogram(name) < 0)
  24. return 0;
  25. if (Rast_read_histogram(name, "", &histogram) <= 0)
  26. return 0;
  27. /* Init histogram range */
  28. if (data_type == CELL_TYPE)
  29. Rast_init_range(&range);
  30. else
  31. Rast_init_fp_range(&fprange);
  32. /* Update histogram range */
  33. i = Rast_get_histogram_num(&histogram);
  34. while (i >= 0) {
  35. if (data_type == CELL_TYPE)
  36. Rast_update_range(Rast_get_histogram_cat(i--, &histogram), &range);
  37. else
  38. Rast_update_fp_range((DCELL) Rast_get_histogram_cat(i--, &histogram),
  39. &fprange);
  40. }
  41. /* Write histogram range */
  42. if (data_type == CELL_TYPE)
  43. Rast_write_range(name, &range);
  44. else
  45. Rast_write_fp_range(name, &fprange);
  46. /* Get category status and max */
  47. cats_ok = (Rast_read_cats(name, "", &cats) >= 0);
  48. max = (data_type == CELL_TYPE ? range.max : fprange.max);
  49. /* Further category checks */
  50. if (!cats_ok)
  51. Rast_init_cats("", &cats);
  52. else if (cats.num != max) {
  53. cats.num = max;
  54. cats_ok = 0;
  55. }
  56. /* Update categories if needed */
  57. if (!cats_ok) {
  58. G_message(_(" Updating the number of categories for "
  59. "[%s]\n\n"), name);
  60. Rast_write_cats(name, &cats);
  61. }
  62. Rast_free_histogram(&histogram);
  63. Rast_free_cats(&cats);
  64. return 0;
  65. }