load.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <grass/raster.h>
  2. #include <grass/glocale.h>
  3. #include "local_proto.h"
  4. /* need elevation map, do A* search on elevation like for r.watershed */
  5. int ele_round(double x)
  6. {
  7. if (x >= 0.0)
  8. return x + .5;
  9. else
  10. return x - .5;
  11. }
  12. /*
  13. * loads elevation and optional flow accumulation map to memory and
  14. * gets start points for A* Search
  15. * start points are edges
  16. */
  17. int load_maps(int ele_fd, int acc_fd)
  18. {
  19. int r, c;
  20. void *ele_buf, *ptr, *acc_buf = NULL, *acc_ptr = NULL;
  21. CELL ele_value, *stream_id;
  22. DCELL dvalue, acc_value;
  23. size_t ele_size, acc_size = 0;
  24. int ele_map_type, acc_map_type = 0;
  25. WAT_ALT *wabuf;
  26. ASP_FLAG *afbuf;
  27. if (acc_fd < 0)
  28. G_message(_("Loading elevation raster map..."));
  29. else
  30. G_message(_("Loading input raster maps..."));
  31. n_search_points = n_points = 0;
  32. ele_map_type = Rast_get_map_type(ele_fd);
  33. ele_size = Rast_cell_size(ele_map_type);
  34. ele_buf = Rast_allocate_buf(ele_map_type);
  35. if (ele_buf == NULL) {
  36. G_warning(_("Unable to allocate memory"));
  37. return -1;
  38. }
  39. if (acc_fd >= 0) {
  40. acc_map_type = Rast_get_map_type(acc_fd);
  41. acc_size = Rast_cell_size(acc_map_type);
  42. acc_buf = Rast_allocate_buf(acc_map_type);
  43. if (acc_buf == NULL) {
  44. G_warning(_("Unable to allocate memory"));
  45. return -1;
  46. }
  47. }
  48. ele_scale = 1;
  49. if (ele_map_type == FCELL_TYPE || ele_map_type == DCELL_TYPE)
  50. ele_scale = 1000; /* should be enough to do the trick */
  51. wabuf = G_malloc(ncols * sizeof(WAT_ALT));
  52. afbuf = G_malloc(ncols * sizeof(ASP_FLAG));
  53. stream_id = G_malloc(ncols * sizeof(CELL));
  54. G_debug(1, "start loading %d rows, %d cols", nrows, ncols);
  55. for (r = 0; r < nrows; r++) {
  56. G_percent(r, nrows, 2);
  57. Rast_get_row(ele_fd, ele_buf, r, ele_map_type);
  58. ptr = ele_buf;
  59. if (acc_fd >= 0) {
  60. Rast_get_row(acc_fd, acc_buf, r, acc_map_type);
  61. acc_ptr = acc_buf;
  62. }
  63. for (c = 0; c < ncols; c++) {
  64. afbuf[c].flag = 0;
  65. afbuf[c].asp = 0;
  66. stream_id[c] = 0;
  67. /* check for masked and NULL cells */
  68. if (Rast_is_null_value(ptr, ele_map_type)) {
  69. FLAG_SET(afbuf[c].flag, NULLFLAG);
  70. FLAG_SET(afbuf[c].flag, INLISTFLAG);
  71. FLAG_SET(afbuf[c].flag, WORKEDFLAG);
  72. FLAG_SET(afbuf[c].flag, WORKED2FLAG);
  73. Rast_set_c_null_value(&ele_value, 1);
  74. /* flow accumulation */
  75. if (acc_fd >= 0) {
  76. if (!Rast_is_null_value(acc_ptr, acc_map_type))
  77. G_fatal_error(_("Elevation raster map is NULL but accumulation map is not NULL"));
  78. }
  79. Rast_set_d_null_value(&acc_value, 1);
  80. }
  81. else {
  82. switch (ele_map_type) {
  83. case CELL_TYPE:
  84. ele_value = *((CELL *) ptr);
  85. break;
  86. case FCELL_TYPE:
  87. dvalue = *((FCELL *) ptr);
  88. dvalue *= ele_scale;
  89. ele_value = ele_round(dvalue);
  90. break;
  91. case DCELL_TYPE:
  92. dvalue = *((DCELL *) ptr);
  93. dvalue *= ele_scale;
  94. ele_value = ele_round(dvalue);
  95. break;
  96. }
  97. if (acc_fd < 0)
  98. acc_value = 1;
  99. else {
  100. if (Rast_is_null_value(acc_ptr, acc_map_type)) {
  101. /* can this be ok after weighing ? */
  102. G_fatal_error(_("Accumulation raster map is NULL but elevation map is not NULL"));
  103. }
  104. switch (acc_map_type) {
  105. case CELL_TYPE:
  106. acc_value = *((CELL *) acc_ptr);
  107. break;
  108. case FCELL_TYPE:
  109. acc_value = *((FCELL *) acc_ptr);
  110. break;
  111. case DCELL_TYPE:
  112. acc_value = *((DCELL *) acc_ptr);
  113. break;
  114. }
  115. }
  116. n_points++;
  117. }
  118. wabuf[c].wat = acc_value;
  119. wabuf[c].ele = ele_value;
  120. ptr = G_incr_void_ptr(ptr, ele_size);
  121. if (acc_fd >= 0)
  122. acc_ptr = G_incr_void_ptr(acc_ptr, acc_size);
  123. }
  124. seg_put_row(&watalt, (char *) wabuf, r);
  125. seg_put_row(&aspflag, (char *) afbuf, r);
  126. cseg_put_row(&stream, stream_id, r);
  127. }
  128. G_percent(nrows, nrows, 1); /* finish it */
  129. Rast_close(ele_fd);
  130. G_free(ele_buf);
  131. G_free(wabuf);
  132. G_free(afbuf);
  133. G_free(stream_id);
  134. if (acc_fd >= 0) {
  135. Rast_close(acc_fd);
  136. G_free(acc_buf);
  137. }
  138. G_debug(1, "%lld non-NULL cells", n_points);
  139. return n_points;
  140. }