close_maps2.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "Gwater.h"
  2. #include <unistd.h>
  3. #include <grass/glocale.h>
  4. int close_array_seg(void)
  5. {
  6. struct Colors colors;
  7. int incr, max, red, green, blue, rd, gr, bl, flag;
  8. int c, r, map_fd;
  9. CELL *cellrow, value;
  10. CELL *theseg;
  11. RAMSEG thesegseg;
  12. cellrow = Rast_allocate_c_buf();
  13. if (seg_flag || bas_flag || haf_flag) {
  14. if (seg_flag) {
  15. theseg = bas;
  16. thesegseg = bas_seg;
  17. }
  18. else if (bas_flag) {
  19. theseg = bas;
  20. thesegseg = bas_seg;
  21. }
  22. else {
  23. theseg = haf;
  24. thesegseg = haf_seg;
  25. }
  26. max = n_basins;
  27. G_debug(1, "%d basins created", max);
  28. Rast_init_colors(&colors);
  29. if (max > 0)
  30. Rast_make_random_colors(&colors, 1, max);
  31. else {
  32. G_warning(_("No basins were created. Verify threshold and region settings."));
  33. Rast_make_random_colors(&colors, 1, 2);
  34. }
  35. if (max < 1000 && max > 0) {
  36. Rast_set_c_color((CELL) 0, 0, 0, 0, &colors);
  37. r = 1;
  38. incr = 0;
  39. while (incr >= 0) {
  40. G_percent(r, max, 2);
  41. for (gr = 130 + incr; gr <= 255; gr += 20) {
  42. for (rd = 90 + incr; rd <= 255; rd += 30) {
  43. for (bl = 90 + incr; bl <= 255; bl += 40) {
  44. flag = 1;
  45. while (flag) {
  46. Rast_get_c_color(&r, &red, &green, &blue, &colors);
  47. /* if existing rule is too dark then append a new
  48. rule to override it */
  49. if ((blue * .11 + red * .30 + green * .59) <
  50. 100) {
  51. Rast_set_c_color(r, rd, gr, bl, &colors);
  52. flag = 0;
  53. }
  54. if (++r > max) {
  55. gr = rd = bl = 300;
  56. flag = 0;
  57. incr = -1;
  58. }
  59. }
  60. }
  61. }
  62. }
  63. if (incr >= 0) {
  64. incr += 15;
  65. if (incr > 120)
  66. incr = 7;
  67. }
  68. }
  69. G_percent(r - 1, max, 3); /* finish it */
  70. }
  71. else
  72. G_debug(1,
  73. "Too many subbasins to reasonably check for color brightness");
  74. /* using the existing stack of while/for/for/for/while loops ... */
  75. }
  76. /* stream segments map */
  77. if (seg_flag) {
  78. map_fd = Rast_open_c_new(seg_name);
  79. for (r = 0; r < nrows; r++) {
  80. Rast_set_c_null_value(cellrow, ncols); /* reset row to all NULL */
  81. for (c = 0; c < ncols; c++) {
  82. value = FLAG_GET(swale, r, c);
  83. if (value)
  84. cellrow[c] = bas[SEG_INDEX(bas_seg, r, c)];
  85. }
  86. Rast_put_row(map_fd, cellrow, CELL_TYPE);
  87. }
  88. Rast_close(map_fd);
  89. Rast_write_colors(seg_name, this_mapset, &colors);
  90. }
  91. /* basins map */
  92. if (bas_flag) {
  93. map_fd = Rast_open_c_new(bas_name);
  94. for (r = 0; r < nrows; r++) {
  95. for (c = 0; c < ncols; c++) {
  96. cellrow[c] = bas[SEG_INDEX(bas_seg, r, c)];
  97. if (cellrow[c] == 0)
  98. Rast_set_c_null_value(cellrow + c, 1);
  99. }
  100. Rast_put_row(map_fd, cellrow, CELL_TYPE);
  101. }
  102. Rast_close(map_fd);
  103. Rast_write_colors(bas_name, this_mapset, &colors);
  104. }
  105. /* half_basins map */
  106. if (haf_flag) {
  107. map_fd = Rast_open_c_new(haf_name);
  108. for (r = 0; r < nrows; r++) {
  109. for (c = 0; c < ncols; c++) {
  110. cellrow[c] = haf[SEG_INDEX(haf_seg, r, c)];
  111. if (cellrow[c] == 0)
  112. Rast_set_c_null_value(cellrow + c, 1);
  113. }
  114. Rast_put_row(map_fd, cellrow, CELL_TYPE);
  115. }
  116. Rast_close(map_fd);
  117. Rast_write_colors(haf_name, this_mapset, &colors);
  118. }
  119. if (seg_flag || bas_flag || haf_flag)
  120. Rast_free_colors(&colors);
  121. G_free(haf);
  122. G_free(bas);
  123. G_free(cellrow);
  124. if (arm_flag)
  125. fclose(fp);
  126. close_maps();
  127. return 0;
  128. }