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