r.drain.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. try:
  117. grass.run_command("g.remove", flags='f', quiet=True, type='raster', name=tmp_maps)
  118. except:
  119. pass
  120. def main():
  121. valmap = options['input']
  122. dirmap = options['direction']
  123. rpathmap = options['output']
  124. vpathmap = options['drain']
  125. start_coords = options['start_coordinates']
  126. start_pnts = options['start_points']
  127. global tmp_maps
  128. tmp_maps = False
  129. atexit.register(cleanup)
  130. if not dirmap:
  131. # get directions with r.fill.dir, without sink filling
  132. dirmap = "%s_tmp_%d" % (valmap, os.getpid())
  133. fill_map = "%s_fill_%d" % (valmap, os.getpid())
  134. area_map = "%s_area_%d" % (valmap, os.getpid())
  135. tmp_maps = dirmap + ',' + fill_map + ',' + area_map
  136. grass.run_command("r.fill.dir", input=valmap, output=fill_map, direction=dirmap, areas=area_map, flags='f', format='grass')
  137. # create args for r.path
  138. kwargs = {}
  139. kwargs['input'] = dirmap
  140. if flags['c'] or flags['a']:
  141. kwargs['values'] = valmap
  142. kwargs['format'] = 'degree'
  143. if start_coords:
  144. kwargs['start_coordinates'] = start_coords
  145. if start_pnts:
  146. kwargs['start_points'] = start_pnts
  147. if rpathmap:
  148. kwargs['raster_path'] = rpathmap
  149. if vpathmap:
  150. kwargs['vector_path'] = vpathmap
  151. pathflags = ''
  152. if flags['c']:
  153. pathflags += 'c'
  154. if flags['a']:
  155. pathflags += 'a'
  156. if flags['n']:
  157. pathflags += 'n'
  158. grass.run_command("r.path", flags=pathflags, **kwargs)
  159. return 0
  160. if __name__ == "__main__":
  161. options, flags = grass.parser()
  162. sys.exit(main())