io.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. ** Original Algorithm: H. Mitasova, L. Mitas, J. Hofierka, M. Zlocha
  3. ** GRASS Implementation: J. Caplan, M. Ruesink 1995
  4. **
  5. ** US Army Construction Engineering Research Lab, University of Illinois
  6. **
  7. ** Copyright J. Caplan, H. Mitasova, L. Mitas, J. Hofierka,
  8. ** M. Zlocha
  9. **
  10. **This program is free software; you can redistribute it and/or
  11. **modify it under the terms of the GNU General Public License
  12. **as published by the Free Software Foundation; either version 2
  13. **of the License, or (at your option) any later version.
  14. **
  15. **This program is distributed in the hope that it will be useful,
  16. **but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. **MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. **GNU General Public License for more details.
  19. **
  20. **You should have received a copy of the GNU General Public License
  21. **along with this program; if not, write to the Free Software
  22. **Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. **
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <math.h>
  29. #include <grass/raster.h>
  30. #include <grass/glocale.h>
  31. #include "r.flow.h"
  32. #include "mem.h"
  33. #define OLD 0 /* magic */
  34. #define NEW 1 /* */
  35. #define TEMP 2 /* numbers */
  36. /****************************** Annoyances ******************************/
  37. static const char *tmp_name(const char *fullname)
  38. {
  39. char element[1024];
  40. const char *mapset = G_mapset();
  41. const char *location = G_location_path();
  42. const char *el = element;
  43. G_temp_element(element);
  44. while (*fullname++ == *location++) ;
  45. while (*fullname++ == *mapset++) ;
  46. while (*fullname++ == *el++) ;
  47. return fullname;
  48. }
  49. /********************************* I/O **********************************/
  50. static int open_existing_cell_file(char *fname, struct Cell_head *chd)
  51. {
  52. const char *mapset = G_find_raster(fname, "");
  53. if (mapset == NULL)
  54. G_fatal_error(_("Raster map <%s> not found"), fname);
  55. if (chd)
  56. Rast_get_cellhd(fname, mapset, chd);
  57. return Rast_open_old(fname, mapset);
  58. }
  59. static int compare_regions(const struct Cell_head *a, const struct Cell_head *b)
  60. {
  61. return (fabs(a->ew_res - b->ew_res) < 1e-6 * b->ew_res &&
  62. fabs(a->ns_res - b->ns_res) < 1e-6 * b->ns_res);
  63. }
  64. void read_input_files(void)
  65. {
  66. DCELL *barc;
  67. int fd, row, col;
  68. struct Cell_head hd;
  69. fd = open_existing_cell_file(parm.elevin, &hd);
  70. if (!compare_regions(&region, &hd))
  71. G_fatal_error(_("Elevation raster map resolution differs from current region resolution"));
  72. G_important_message(_("Reading input raster map <%s>..."), parm.elevin);
  73. for (row = 0; row < region.rows; row++) {
  74. G_percent(row, region.rows, 5);
  75. Rast_get_d_row(fd, el.buf[row], row);
  76. if (parm.seg)
  77. put_row_seg(el, row);
  78. }
  79. G_percent(1, 1, 1);
  80. if (parm.seg)
  81. Segment_flush(el.seg);
  82. Rast_close(fd);
  83. if (parm.aspin) {
  84. fd = open_existing_cell_file(parm.aspin, &hd);
  85. if (!compare_regions(&region, &hd))
  86. G_fatal_error(_("Resolution of aspect file differs from "
  87. "current region resolution"));
  88. G_important_message(_("Reading input raster map <%s>..."), parm.aspin);
  89. for (row = 0; row < region.rows; row++) {
  90. G_percent(row, region.rows, 5);
  91. Rast_get_d_row(fd, as.buf[row], row);
  92. if (parm.seg)
  93. put_row_seg(as, row);
  94. }
  95. G_percent(1, 1, 1);
  96. if (parm.seg)
  97. Segment_flush(as.seg);
  98. Rast_close(fd);
  99. }
  100. if (parm.barin) {
  101. G_message(_("Reading input files: barrier"));
  102. barc = Rast_allocate_d_buf();
  103. fd = open_existing_cell_file(parm.barin, &hd);
  104. for (row = 0; row < region.rows; row++) {
  105. Rast_get_d_row(fd, barc, row);
  106. for (col = 0; col < region.cols; col++) {
  107. BM_set(bitbar, col, row, (barc[col] != 0));
  108. if (parm.dsout && barc[col] != 0)
  109. put(ds, row, col, -1);
  110. }
  111. }
  112. Rast_close(fd);
  113. }
  114. }
  115. static int open_segment_file(const char *name, layer l, int new)
  116. {
  117. int fd;
  118. const char *mapset;
  119. if (new == TEMP)
  120. G_temp_element(string);
  121. else
  122. sprintf(string, "cell_misc/%s", parm.elevin);
  123. if (new || !(mapset = G_find_file2(string, name, ""))) {
  124. if ((fd = G_open_new(string, name)) < 0)
  125. G_fatal_error(_("Cannot create segment file %s"), name);
  126. if (Segment_format(fd, region.rows + l.row_offset * 2,
  127. region.cols + l.col_offset * 2, SEGROWS, SEGCOLS,
  128. sizeof(DCELL)) < 1)
  129. G_fatal_error(_("Cannot format segment file %s"), name);
  130. close(fd);
  131. mapset = G_mapset();
  132. }
  133. if ((fd = G_open_update(string, name)) < 0)
  134. G_fatal_error(_("Cannot open segment file %s"), name);
  135. return fd;
  136. }
  137. void open_output_files(void)
  138. {
  139. if (parm.seg) {
  140. el.sfd = open_segment_file("elevation.seg", el, OLD);
  141. as.sfd = open_segment_file("aspect.seg", as, OLD);
  142. if (parm.dsout)
  143. ds.sfd = open_segment_file(tmp_name(G_tempfile()), ds, TEMP);
  144. }
  145. if (parm.lgout)
  146. lgfd = Rast_open_new(parm.lgout, FCELL_TYPE);
  147. if (parm.flout && (Vect_open_new(&fl, parm.flout, 0) < 0))
  148. G_fatal_error(_("Unable to create vector map <%s>"), parm.flout);
  149. }
  150. void close_files(void)
  151. {
  152. if (parm.seg) {
  153. close(el.sfd);
  154. close(as.sfd);
  155. if (parm.dsout)
  156. close(ds.sfd);
  157. }
  158. /* if (parm.lgout)
  159. Rast_close(lgfd); */
  160. if (parm.flout) {
  161. Vect_build(&fl);
  162. Vect_close(&fl);
  163. }
  164. }
  165. void write_density_file(void)
  166. {
  167. const char *mapset;
  168. int dsfd, row, col;
  169. double dsmax = 0.0;
  170. struct Colors colors;
  171. CELL val1, val2;
  172. Rast_set_output_window(&region);
  173. G_message(_("Writing output raster map <%s>..."), parm.dsout);
  174. dsfd = Rast_open_new(parm.dsout, DCELL_TYPE);
  175. for (row = 0; row < region.rows; row++) {
  176. G_percent(row, region.rows, 5);
  177. Rast_put_row(dsfd, get_row(ds, row), DCELL_TYPE);
  178. for (col = 0; col < region.cols; col++)
  179. if (ds.buf[row][col] > dsmax)
  180. dsmax = ds.buf[row][col];
  181. }
  182. G_percent(1, 1, 1);
  183. Rast_close(dsfd);
  184. Rast_init_colors(&colors);
  185. val1 = -1;
  186. val2 = -1;
  187. Rast_add_c_color_rule(&val1, 0, 0, 0, &val2, 0, 0, 0, &colors);
  188. val1 = 0;
  189. val2 = 5;
  190. Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 0, &colors);
  191. val1 = 5;
  192. val2 = 30;
  193. Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 255, &colors);
  194. val1 = 30;
  195. val2 = 100;
  196. Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 0, 127, 255, &colors);
  197. val1 = 100;
  198. val2 = 1000;
  199. Rast_add_c_color_rule(&val1, 0, 127, 255, &val2, 0, 0, 255, &colors);
  200. val1 = 1000;
  201. val2 = (CELL) dsmax;
  202. Rast_add_c_color_rule(&val1, 0, 0, 255, &val2, 0, 0, 0, &colors);
  203. if ((mapset = G_find_file("cell", parm.dsout, "")) == NULL)
  204. G_fatal_error(_("Raster map <%s> not found"), parm.dsout);
  205. Rast_write_colors(parm.dsout, mapset, &colors);
  206. Rast_free_colors(&colors);
  207. }