r.drain.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: r.drain
  5. # AUTHOR(S): Markus Metz
  6. # PURPOSE: Wrapper for r.path. If no input directions are provided,
  7. # directions are determined with r.fill.dir
  8. # COPYRIGHT: (C) 2017 by metz, and the GRASS Development Team
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (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. ############################################################################
  21. # %module
  22. # % description: Traces a flow through an elevation model or cost surface on a raster map.
  23. # % keyword: raster
  24. # % keyword: hydrology
  25. # % keyword: cost surface
  26. # %end
  27. # %option
  28. # % key: input
  29. # % type: string
  30. # % required: yes
  31. # % multiple: no
  32. # % key_desc: name
  33. # % description: Name of input elevation or cost surface raster map
  34. # % gisprompt: old,cell,raster
  35. # %end
  36. # %option
  37. # % key: direction
  38. # % type: string
  39. # % required: no
  40. # % multiple: no
  41. # % key_desc: name
  42. # % label: Name of input movement direction map associated with the cost surface
  43. # % description: Direction in degrees CCW from east
  44. # % gisprompt: old,cell,raster
  45. # % guisection: Cost surface
  46. # %end
  47. # %option
  48. # % key: output
  49. # % type: string
  50. # % required: yes
  51. # % multiple: no
  52. # % key_desc: name
  53. # % description: Name for output raster map
  54. # % gisprompt: new,cell,raster
  55. # %end
  56. # %option
  57. # % key: drain
  58. # % type: string
  59. # % required: no
  60. # % multiple: no
  61. # % key_desc: name
  62. # % label: Name for output drain vector map
  63. # % description: Recommended for cost surface made using knight's move
  64. # % gisprompt: new,vector,vector
  65. # %end
  66. # %option
  67. # % key: start_coordinates
  68. # % type: double
  69. # % required: no
  70. # % multiple: yes
  71. # % key_desc: east,north
  72. # % description: Coordinates of starting point(s) (E,N)
  73. # % gisprompt: old,coords,coords
  74. # % guisection: Start
  75. # %end
  76. # %option
  77. # % key: start_points
  78. # % type: string
  79. # % required: no
  80. # % multiple: yes
  81. # % key_desc: name
  82. # % label: Name of starting vector points map(s)
  83. # % gisprompt: old,vector,vector
  84. # % guisection: Start
  85. # %end
  86. # %flag
  87. # % key: c
  88. # % description: Copy input cell values on output
  89. # % guisection: Path settings
  90. # %end
  91. # %flag
  92. # % key: a
  93. # % description: Accumulate input values along the path
  94. # % guisection: Path settings
  95. # %end
  96. # %flag
  97. # % key: n
  98. # % description: Count cell numbers along the path
  99. # % guisection: Path settings
  100. # %end
  101. # %flag
  102. # % key: d
  103. # % description: The input raster map is a cost surface (direction surface must also be specified)
  104. # % guisection: Cost surface
  105. # %end
  106. # %rules
  107. # % required: start_coordinates, start_points
  108. # %end
  109. import os
  110. import sys
  111. import atexit
  112. import grass.script as grass
  113. def cleanup():
  114. """Delete temporary direction map."""
  115. if tmp_maps:
  116. grass.run_command(
  117. "g.remove",
  118. flags="f",
  119. quiet=True,
  120. type="raster",
  121. name=tmp_maps,
  122. errors="ignore",
  123. )
  124. def main():
  125. valmap = options["input"]
  126. dirmap = options["direction"]
  127. rpathmap = options["output"]
  128. vpathmap = options["drain"]
  129. start_coords = options["start_coordinates"]
  130. start_pnts = options["start_points"]
  131. global tmp_maps
  132. tmp_maps = False
  133. atexit.register(cleanup)
  134. if not dirmap:
  135. # get directions with r.fill.dir, without sink filling
  136. dirmap = "%s_tmp_%d" % (valmap, os.getpid())
  137. fill_map = "%s_fill_%d" % (valmap, os.getpid())
  138. area_map = "%s_area_%d" % (valmap, os.getpid())
  139. tmp_maps = dirmap + "," + fill_map + "," + area_map
  140. grass.run_command(
  141. "r.fill.dir",
  142. input=valmap,
  143. output=fill_map,
  144. direction=dirmap,
  145. areas=area_map,
  146. flags="f",
  147. format="grass",
  148. )
  149. # create args for r.path
  150. kwargs = {}
  151. kwargs["input"] = dirmap
  152. if flags["c"] or flags["a"]:
  153. kwargs["values"] = valmap
  154. kwargs["format"] = "degree"
  155. if start_coords:
  156. kwargs["start_coordinates"] = start_coords
  157. if start_pnts:
  158. kwargs["start_points"] = start_pnts
  159. if rpathmap:
  160. kwargs["raster_path"] = rpathmap
  161. if vpathmap:
  162. kwargs["vector_path"] = vpathmap
  163. pathflags = ""
  164. if flags["c"]:
  165. pathflags += "c"
  166. if flags["a"]:
  167. pathflags += "a"
  168. if flags["n"]:
  169. pathflags += "n"
  170. grass.run_command("r.path", flags=pathflags, **kwargs)
  171. return 0
  172. if __name__ == "__main__":
  173. options, flags = grass.parser()
  174. sys.exit(main())