main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 propagate 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. G_add_keyword(_("watershed"));
  47. module->description =
  48. _("Generates watershed subbasins raster map.");
  49. drain_opt = G_define_standard_option(G_OPT_R_INPUT);
  50. drain_opt->key = "cnetwork";
  51. drain_opt->description = _("Name of input coded stream network raster map");
  52. ridge_opt = G_define_standard_option(G_OPT_R_INPUT);
  53. ridge_opt->key = "tnetwork";
  54. ridge_opt->description = _("Name of input thinned ridge network raster map");
  55. part_opt = G_define_standard_option(G_OPT_R_OUTPUT);
  56. num_opt = G_define_option();
  57. num_opt->key = "number";
  58. num_opt->type = TYPE_INTEGER;
  59. num_opt->required = YES;
  60. num_opt->description = _("Number of passes through the dataset");
  61. if (G_parser(argc, argv))
  62. exit(EXIT_FAILURE);
  63. sscanf(num_opt->answer, "%d", &tpass);
  64. drain_name = drain_opt->answer;
  65. /* this isn't a nice thing to do. Rast_align_window() should be used first */
  66. Rast_get_cellhd(drain_name, "", &window);
  67. Rast_set_window(&window);
  68. nrows = Rast_window_rows();
  69. ncols = Rast_window_cols();
  70. ridge_name = ridge_opt->answer;
  71. part_name = part_opt->answer;
  72. drain = read_map(drain_name, NOMASK, nrows, ncols);
  73. ridge = read_map(ridge_name, NOMASK, nrows, ncols);
  74. partfd = Rast_open_c_new(part_name);
  75. /* run through file and set streams to zero at locations where ridges exist */
  76. for (row = 0; row < nrows; row++) {
  77. for (col = 0; col < ncols; col++)
  78. if (ridge[row * ncols + col] != 0)
  79. drain[row * ncols + col] = 0;
  80. }
  81. for (npass = 1; npass <= tpass; npass++) {
  82. for (row = 1; row < nrows - 1; row++) {
  83. for (col = 1; col < ncols - 1; col++) {
  84. if (drain[row * ncols + col] == 0 &&
  85. ridge[row * ncols + col] == 0) {
  86. if (drain[(row - 1) * ncols + col] != 0 &&
  87. ridge[(row - 1) * ncols + col] == 0)
  88. drain[row * ncols + col] =
  89. drain[(row - 1) * ncols + col];
  90. if (drain[row * ncols + (col - 1)] != 0 &&
  91. ridge[row * ncols + (col - 1)] == 0)
  92. drain[row * ncols + col] =
  93. drain[row * ncols + (col - 1)];
  94. }
  95. }
  96. }
  97. G_message(_("Forward sweep complete"));
  98. for (row = nrows - 3; row > 1; --row) {
  99. for (col = ncols - 3; col > 1; --col) {
  100. if (drain[row * ncols + col] == 0 &&
  101. ridge[row * ncols + col] == 0) {
  102. if (drain[(row + 1) * ncols + col] != 0 &&
  103. ridge[(row + 1) * ncols + col] == 0)
  104. drain[row * ncols + col] =
  105. drain[(row + 1) * ncols + col];
  106. if (drain[row * ncols + (col + 1)] != 0 &&
  107. ridge[row * ncols + (col + 1)] == 0)
  108. drain[row * ncols + col] =
  109. drain[row * ncols + (col + 1)];
  110. }
  111. }
  112. }
  113. G_message(_("Reverse sweep complete"));
  114. }
  115. /* write out partitioned watershed map */
  116. for (row = 0; row < nrows; row++)
  117. Rast_put_row(partfd, drain + (row * ncols), CELL_TYPE);
  118. G_message(_("Creating support files for <%s>..."), part_name);
  119. Rast_close(partfd);
  120. exit(EXIT_SUCCESS);
  121. }