-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
r.grow.py
executable file
·166 lines (141 loc) · 4.18 KB
/
r.grow.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
#!/usr/bin/env python
#
############################################################################
#
# MODULE: r.grow
# AUTHOR(S): Glynn Clements
# PURPOSE: Fast replacement for r.grow using r.grow.distance
#
# COPYRIGHT: (C) 2008 by Glynn Clements
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
#############################################################################
#%Module
#% description: Generates a raster map layer with contiguous areas grown by one cell.
#% keyword: raster
#% keyword: distance
#% keyword: proximity
#%end
#%flag
#% key: m
#% description: Radius is in map units rather than cells
#%end
#%option G_OPT_R_INPUT
#%end
#%option G_OPT_R_OUTPUT
#%end
#%option
#% key: radius
#% type: double
#% required: no
#% multiple: no
#% description: Radius of buffer in raster cells
#% answer: 1.01
#%end
#%option
#% key: metric
#% type: string
#% required: no
#% multiple: no
#% options: euclidean,maximum,manhattan
#% description: Metric
#% answer: euclidean
#%end
#%option
#% key: old
#% type: integer
#% required: no
#% multiple: no
#% description: Value to write for input cells which are non-NULL (-1 => NULL)
#%end
#%option
#% key: new
#% type: integer
#% required: no
#% multiple: no
#% description: Value to write for "grown" cells
#%end
import os
import atexit
import math
import grass.script as grass
from grass.exceptions import CalledModuleError
# i18N
import gettext
gettext.install('grassmods', os.path.join(os.getenv("GISBASE"), 'locale'))
# what to do in case of user break:
def cleanup():
for map in [temp_dist, temp_val]:
if map:
grass.run_command('g.remove', flags='fb', quiet=True,
type='rast', name=map)
def main():
global temp_dist, temp_val
input = options['input']
radius = float(options['radius'])
metric = options['metric']
old = options['old']
new = options['new']
mapunits = flags['m']
tmp = str(os.getpid())
temp_dist = "r.grow.tmp.%s.dist" % tmp
shrink = False
if radius < 0.0:
shrink = True
radius = -radius
if new == '' and shrink == False:
temp_val = "r.grow.tmp.%s.val" % tmp
new = temp_val
else:
temp_val = None
if old == '':
old = input
if not mapunits:
kv = grass.region()
scale = math.sqrt(float(kv['nsres']) * float(kv['ewres']))
radius *= scale
if metric == 'euclidean':
metric = 'squared'
radius = radius * radius
# check if input file exists
if not grass.find_file(input)['file']:
grass.fatal(_("Raster map <%s> not found") % input)
# Workaround for r.mapcalc bug #3475
# Mapcalc will fail if output is a fully qualified map name
out_name = options['output'].split('@')
if len(out_name) == 2:
if out_name[1] != grass.gisenv()['MAPSET']:
grass.fatal(_("Output can be written only to the current mapset"))
output = out_name[0]
else:
output = out_name[0]
if shrink is False:
try:
grass.run_command('r.grow.distance', input=input, metric=metric,
distance=temp_dist, value=temp_val)
except CalledModuleError:
grass.fatal(_("Growing failed. Removing temporary maps."))
grass.mapcalc(
"$output = if(!isnull($input),$old,if($dist < $radius,$new,null()))",
output=output, input=input, radius=radius,
old=old, new=new, dist=temp_dist)
else:
# shrink
try:
grass.run_command('r.grow.distance', input=input, metric=metric,
distance=temp_dist, value=temp_val, flags='n')
except CalledModuleError:
grass.fatal(_("Shrinking failed. Removing temporary maps."))
grass.mapcalc(
"$output = if($dist < $radius,null(),$old)",
output=output, radius=radius, old=old, dist=temp_dist)
grass.run_command('r.colors', map=output, raster=input)
# write cmd history:
grass.raster_history(output)
if __name__ == "__main__":
options, flags = grass.parser()
atexit.register(cleanup)
main()