main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /****************************************************************************
  2. *
  3. * MODULE: r.basins.fill
  4. *
  5. * AUTHOR(S): Dale White - Dept. of Geography, Pennsylvania State U.
  6. * Larry Band - Dept. of Geography, University of Toronto
  7. *
  8. * PURPOSE: Generates a raster map layer showing watershed subbasins.
  9. *
  10. * COPYRIGHT: (C) 2005, 2010 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. ****************************************************************************/
  17. /*====================================================================*/
  18. /* program to propogate the link label into the hillslope areas; */
  19. /* processes CELL files only and works on window derived from link */
  20. /* label map */
  21. /*====================================================================*/
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <grass/gis.h>
  26. #include <grass/raster.h>
  27. #include <grass/glocale.h>
  28. #include "local_proto.h"
  29. #define NOMASK 1
  30. int main(int argc, char *argv[])
  31. {
  32. int partfd;
  33. int nrows, ncols;
  34. const char *drain_name;
  35. const char *ridge_name;
  36. const char *part_name;
  37. CELL *drain, *ridge;
  38. struct Cell_head window;
  39. int row, col, npass, tpass;
  40. struct GModule *module;
  41. struct Option *num_opt, *drain_opt, *ridge_opt, *part_opt;
  42. G_gisinit(argv[0]);
  43. module = G_define_module();
  44. G_add_keyword(_("raster"));
  45. G_add_keyword(_("hydrology"));
  46. module->description =
  47. _("Generates watershed subbasins raster map.");
  48. drain_opt = G_define_standard_option(G_OPT_R_INPUT);
  49. drain_opt->key = "cnetwork";
  50. drain_opt->description = _("Name of input coded stream network raster map");
  51. ridge_opt = G_define_standard_option(G_OPT_R_INPUT);
  52. ridge_opt->key = "tnetwork";
  53. ridge_opt->description = _("Name of input thinned ridge network raster map");
  54. part_opt = G_define_standard_option(G_OPT_R_OUTPUT);
  55. num_opt = G_define_option();
  56. num_opt->key = "number";
  57. num_opt->type = TYPE_INTEGER;
  58. num_opt->required = YES;
  59. num_opt->description = _("Number of passes through the dataset");
  60. if (G_parser(argc, argv))
  61. exit(EXIT_FAILURE);
  62. sscanf(num_opt->answer, "%d", &tpass);
  63. drain_name = drain_opt->answer;
  64. /* this isn't a nice thing to do. Rast_align_window() should be used first */
  65. Rast_get_cellhd(drain_name, "", &window);
  66. Rast_set_window(&window);
  67. nrows = Rast_window_rows();
  68. ncols = Rast_window_cols();
  69. ridge_name = ridge_opt->answer;
  70. part_name = part_opt->answer;
  71. drain = read_map(drain_name, NOMASK, nrows, ncols);
  72. ridge = read_map(ridge_name, NOMASK, nrows, ncols);
  73. partfd = Rast_open_c_new(part_name);
  74. /* run through file and set streams to zero at locations where ridges exist */
  75. for (row = 0; row < nrows; row++) {
  76. for (col = 0; col < ncols; col++)
  77. if (ridge[row * ncols + col] != 0)
  78. drain[row * ncols + col] = 0;
  79. }
  80. for (npass = 1; npass <= tpass; npass++) {
  81. for (row = 1; row < nrows - 1; row++) {
  82. for (col = 1; col < ncols - 1; col++) {
  83. if (drain[row * ncols + col] == 0 &&
  84. ridge[row * ncols + col] == 0) {
  85. if (drain[(row - 1) * ncols + col] != 0 &&
  86. ridge[(row - 1) * ncols + col] == 0)
  87. drain[row * ncols + col] =
  88. drain[(row - 1) * ncols + col];
  89. if (drain[row * ncols + (col - 1)] != 0 &&
  90. ridge[row * ncols + (col - 1)] == 0)
  91. drain[row * ncols + col] =
  92. drain[row * ncols + (col - 1)];
  93. }
  94. }
  95. }
  96. G_message(_("Forward sweep complete"));
  97. for (row = nrows - 3; row > 1; --row) {
  98. for (col = ncols - 3; col > 1; --col) {
  99. if (drain[row * ncols + col] == 0 &&
  100. ridge[row * ncols + col] == 0) {
  101. if (drain[(row + 1) * ncols + col] != 0 &&
  102. ridge[(row + 1) * ncols + col] == 0)
  103. drain[row * ncols + col] =
  104. drain[(row + 1) * ncols + col];
  105. if (drain[row * ncols + (col + 1)] != 0 &&
  106. ridge[row * ncols + (col + 1)] == 0)
  107. drain[row * ncols + col] =
  108. drain[row * ncols + (col + 1)];
  109. }
  110. }
  111. }
  112. G_message(_("Reverse sweep complete"));
  113. }
  114. /* write out partitioned watershed map */
  115. for (row = 0; row < nrows; row++)
  116. Rast_put_row(partfd, drain + (row * ncols), CELL_TYPE);
  117. G_message(_("Creating support files for <%s>..."), part_name);
  118. Rast_close(partfd);
  119. exit(EXIT_SUCCESS);
  120. }