r.drain.py 4.5 KB

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