main.c 4.9 KB

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