support.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. /*
  4. * creates new category and color structures from the patching
  5. * files category and color files
  6. *
  7. * the first patch file is used as the basis. Its cats and colr
  8. * are read into the final cats/colr structures.
  9. * Then the other patching layers cats/colr are added to the
  10. * final cats/colr only if these patching layers actually
  11. * contributed new categories to the final result
  12. */
  13. int support(char **names,
  14. struct Cell_stats *statf, int nfiles,
  15. struct Categories *cats,
  16. int *cats_ok,
  17. struct Colors *colr, int *colr_ok, RASTER_MAP_TYPE out_type)
  18. {
  19. int i;
  20. struct Categories pcats;
  21. struct Colors pcolr;
  22. CELL n;
  23. long count;
  24. int red, grn, blu;
  25. int do_cats, do_colr;
  26. *cats_ok = 1;
  27. *colr_ok = 1;
  28. if (Rast_read_cats(names[0], "", cats) < 0)
  29. *cats_ok = 0;
  30. G_suppress_warnings(1);
  31. if (Rast_read_colors(names[0], "", colr) < 0)
  32. *colr_ok = 0;
  33. G_suppress_warnings(0);
  34. if (*cats_ok == 0 && *colr_ok == 0)
  35. return 0;
  36. for (i = 1; i < nfiles; i++) {
  37. do_cats = *cats_ok && (Rast_read_cats(names[i], "", &pcats) >= 0);
  38. G_suppress_warnings(1);
  39. do_colr = *colr_ok && (Rast_read_colors(names[i], "", &pcolr) >= 0);
  40. G_suppress_warnings(0);
  41. if (!do_cats && !do_colr)
  42. continue;
  43. if (out_type == CELL_TYPE) {
  44. Rast_rewind_cell_stats(statf + i);
  45. while (Rast_next_cell_stat(&n, &count, statf + i))
  46. if (n && !Rast_find_cell_stat(n, &count, statf)) {
  47. if (do_cats) {
  48. Rast_update_cell_stats(&n, 1, statf);
  49. Rast_set_c_cat(&n, &n, Rast_get_c_cat((CELL *) &n, &pcats), cats);
  50. }
  51. if (do_colr) {
  52. Rast_get_c_color(&n, &red, &grn, &blu, &pcolr);
  53. Rast_set_c_color(n, red, grn, blu, colr);
  54. }
  55. }
  56. }
  57. else {
  58. /* the color would be the color of the first map,
  59. * possibly not covering the range of the other maps */
  60. *colr_ok = 0;
  61. }
  62. if (do_cats)
  63. Rast_free_cats(&pcats);
  64. if (do_colr)
  65. /* otherwise this memory is used in colr pointer */
  66. Rast_free_colors(&pcolr);
  67. }
  68. return 1;
  69. }