main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /****************************************************************************
  2. *
  3. * MODULE: r.water.outlet
  4. * AUTHOR(S): Charles Ehlschlaeger, USACERL (original contributor)
  5. * Markus Neteler <neteler itc.it>,
  6. * Roberto Flor <flor itc.it>,
  7. * Bernhard Reiter <bernhard intevation.de>,
  8. * Huidae Cho <grass4u gmail.com>,
  9. * Glynn Clements <glynn gclements.plus.com>,
  10. * Jan-Oliver Wagner <jan intevation.de>,
  11. * Soeren Gebbert <soeren.gebbert gmx.de>
  12. * PURPOSE: this program makes a watershed basin raster map using the
  13. * drainage pointer map, from an outlet point defined by an
  14. * easting and a northing.
  15. * COPYRIGHT: (C) 1999-2006, 2010, 2013 by the GRASS Development Team
  16. *
  17. * This program is free software under the GNU General Public
  18. * License (>=v2). Read the file COPYING that comes with GRASS
  19. * for details.
  20. *
  21. *****************************************************************************/
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <grass/gis.h>
  26. #include <grass/raster.h>
  27. #include <grass/glocale.h>
  28. #include "basin.h"
  29. #include "outletP.h"
  30. SHORT drain[3][3] = {{ 7,6,5 },{ 8,-17,4 },{ 1,2,3 }};
  31. int nrows, ncols;
  32. char *drain_ptrs;
  33. RAMSEG ba_seg, pt_seg;
  34. CELL *bas;
  35. int main(int argc, char *argv[])
  36. {
  37. struct GModule *module;
  38. struct {
  39. struct Option *input, *output, *coords;
  40. } opt;
  41. double N, E;
  42. int row, col, basin_fd, drain_fd;
  43. CELL *cell_buf;
  44. char drain_name[GNAME_MAX], basin_name[GNAME_MAX];
  45. struct Cell_head window;
  46. struct History hist;
  47. G_gisinit(argv[0]);
  48. module = G_define_module();
  49. module->description = _("Creates watershed basins from a drainage direction map.");
  50. G_add_keyword(_("raster"));
  51. G_add_keyword(_("hydrology"));
  52. G_add_keyword(_("watershed"));
  53. opt.input = G_define_standard_option(G_OPT_R_INPUT);
  54. opt.input->description = _("Name of input drainage direction map");
  55. opt.output = G_define_standard_option(G_OPT_R_OUTPUT);
  56. opt.output->description = _("Name for output watershed basin map");
  57. opt.coords = G_define_standard_option(G_OPT_M_COORDS);
  58. opt.coords->description = _("Coordinates of outlet point");
  59. opt.coords->required = YES;
  60. /* Parse command line */
  61. if (G_parser(argc, argv))
  62. exit(EXIT_FAILURE);
  63. G_get_window(&window);
  64. strcpy(drain_name, opt.input->answer);
  65. strcpy(basin_name, opt.output->answer);
  66. if (!G_scan_easting(opt.coords->answers[0], &E, G_projection()))
  67. G_fatal_error(_("Illegal east coordinate '%s'"), opt.coords->answers[0]);
  68. if (!G_scan_northing(opt.coords->answers[1], &N, G_projection()))
  69. G_fatal_error(_("Illegal north coordinate '%s'"), opt.coords->answers[1]);
  70. G_debug(1, "easting = %.4f northing = %.4f", E, N);
  71. if (E < window.west || E > window.east || N < window.south ||
  72. N > window.north) {
  73. G_warning(_("Ignoring point outside computation region: %.4f,%.4f"),
  74. E, N);
  75. }
  76. G_get_set_window(&window);
  77. nrows = Rast_window_rows();
  78. ncols = Rast_window_cols();
  79. drain_fd = Rast_open_old(drain_name, "");
  80. drain_ptrs =
  81. (char *)G_malloc(sizeof(char) * size_array(&pt_seg, nrows, ncols));
  82. bas = (CELL *) G_calloc(size_array(&ba_seg, nrows, ncols), sizeof(CELL));
  83. cell_buf = Rast_allocate_c_buf();
  84. for (row = 0; row < nrows; row++) {
  85. Rast_get_c_row(drain_fd, cell_buf, row);
  86. for (col = 0; col < ncols; col++) {
  87. drain_ptrs[SEG_INDEX(pt_seg, row, col)] = cell_buf[col];
  88. }
  89. }
  90. G_free(cell_buf);
  91. row = (window.north - N) / window.ns_res;
  92. col = (E - window.west) / window.ew_res;
  93. if (row >= 0 && col >= 0 && row < nrows && col < ncols)
  94. overland_cells(row, col);
  95. G_free(drain_ptrs);
  96. cell_buf = Rast_allocate_c_buf();
  97. basin_fd = Rast_open_c_new(basin_name);
  98. for (row = 0; row < nrows; row++) {
  99. G_percent(row, nrows, 5);
  100. for (col = 0; col < ncols; col++) {
  101. cell_buf[col] = bas[SEG_INDEX(ba_seg, row, col)];
  102. if (cell_buf[col] == 0)
  103. Rast_set_null_value(&cell_buf[col], 1, CELL_TYPE);
  104. }
  105. Rast_put_row(basin_fd, cell_buf, CELL_TYPE);
  106. }
  107. G_percent(1, 1, 1);
  108. G_free(bas);
  109. G_free(cell_buf);
  110. Rast_close(basin_fd);
  111. Rast_put_cell_title(basin_name, "Watershed basin");
  112. Rast_short_history(basin_name, "raster", &hist);
  113. Rast_command_history(&hist);
  114. Rast_write_history(basin_name, &hist);
  115. exit(EXIT_SUCCESS);
  116. }