-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr.viewshed.cva7
executable file
·175 lines (152 loc) · 5.73 KB
/
r.viewshed.cva7
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
#!/usr/bin/python
#
############################################################################
#
# MODULE: r.viewshed.cva.py
# AUTHOR(S): Isaac Ullah, additions by Anna Petrasova
# PURPOSE: Undertakes a "cumulative viewshed analysis" using a vector points map as input "viewing" locations, using r.viewshed to calculate the individual viewsheds.
# COPYRIGHT: (C) 2015 by Isaac Ullah
# REFERENCES: r.viewshed
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
#############################################################################
#%module
#% description: Undertakes a "cumulative viewshed analysis" using a vector points map as input "viewing" locations, using r.viewshed to calculate the individual viewsheds.
#%end
#%option G_OPT_R_INPUT
#% description: Input elevation map (DEM)
#%END
#%option G_OPT_V_INPUT
#% key: vector
#% description: Name of input vector points map containg the set of sites for this analysis.
#%end
#%option G_OPT_R_OUTPUT
#% key: output
#% description: Output cumulative viewshed raster
#%end
#%option
#% key: observer_elevation
#% type: double
#% description: Height of observation points off the ground
#%answer: 1.75
#% required : no
#%end
#%option
#% key: target_elevation
#% type: double
#% description: Height of target areas off the ground
#%answer: 1.75
#% required : no
#%end
#%option
#% key: max_distance
#% type: double
#% description: Maximum visibility radius. By default infinity (-1)
#%answer: -1
#% required : no
#%end
#%option
#% key: memory
#% type: integer
#% description: Amount of memory to use (in MB)
#%answer: 500
#% required : no
#%end
#%option
#% key: refraction_coeff
#% type: double
#% description: Refraction coefficient (with flag -r)
#%answer: 0.14286
#% options: 0.0-1.0
#% required : no
#%end
#%option G_OPT_DB_COLUMN
#% key: name_col
#% description: Database column for site names (with flag -k)
#% required : no
#%end
#%flag
#% key: k
#% description: Keep all interim viewshed maps produced by the routine (maps will be named "vshed_'name'", where 'name' is the value in "name_column" for each input point. If no value specified in "name_column", cat value will be used instead)
#%end
#%flag
#% key: c
#% description: Consider the curvature of the earth (current ellipsoid)
#%end
#%flag
#% key: r
#% description: Consider the effect of atmospheric refraction
#%end
#%flag
#% key: b
#% description: Output format is {0 (invisible) 1 (visible)}
#%end
#%flag
#% key: e
#% description: Output format is invisible = NULL, else current elev - viewpoint_elev
#%end
import sys
import os
grass_install_tree = os.getenv('GISBASE')
sys.path.append(grass_install_tree + os.sep + 'etc' + os.sep + 'python')
import grass.script as grass
#main block of code starts here
def main():
#bring in input variables
elev = options["input"]
vect = options["vector"]
viewshed_options = {}
for option in ('observer_elevation', 'target_elevation',
'max_distance', 'memory', 'refraction_coeff'):
viewshed_options[option] = options[option]
out = options["output"]
#assemble flag string
flagstring = ''
if flags['r']:
flagstring += 'r'
if flags['c']:
flagstring += 'c'
if flags['b']:
flagstring += 'b'
if flags['e']:
flagstring += 'e'
#get the coords from the vector map, and check if we want to name them
if flags['k'] and options["name_col"] is not '':
output_points = grass.read_command("v.out.ascii", flags='r', input=vect, type="point", format="point", separator=",", columns=options["name_col"]).strip()
# note that the "r" flag will constrain to points in the current geographic region.
else:
output_points = grass.read_command("v.out.ascii", flags='r', input=vect, type="point", format="point", separator=",").strip()
# note that the "r" flag will constrain to points in the current geographic region.
grass.message(_("Note that the routine is constrained to points in the current geographic region."))
#read the coordinates, and parse them up.
masterlist = []
for line in output_points.split(os.linesep):
masterlist.append(line.split(','))
#now, loop through the master list and run r.viewshed for each of the sites, and append the viewsheds to a list (so we can work with them later)
vshed_list = []
for site in masterlist:
if flags['k'] and options["name_col"] is not '':
ptname = site[3]
else:
ptname = site[2]
grass.verbose(_('Calculating viewshed for location %s,%s (point name = %s)') % (site[0], site[1], ptname))
tempry = "vshed_%s" % ptname
vshed_list.append(tempry)
grass.run_command("r.viewshed", quiet = True, overwrite=grass.overwrite(), flags=flagstring, input=elev, output=tempry, coordinates=site[0] + "," + site[1], **viewshed_options)
#now make a mapcalc statement to add all the viewsheds together to make the outout cumulative viewsheds map
grass.message(_("Calculating \"Cumulative Viewshed\" map"))
grass.run_command("r.series", quiet=True, overwrite=grass.overwrite(), input=(",").join(vshed_list), output=out, method="count")
#Clean up temporary maps, if requested
if flags['k']:
grass.message(_("Temporary viewshed maps will not removed"))
else:
grass.message(_("Removing temporary viewshed maps"))
grass.run_command("g.remove", quiet=True, flags='f', type='raster', name=(",").join(vshed_list))
return
# here is where the code in "main" actually gets executed. This way of programming is neccessary for the way g.parser needs to run.
if __name__ == "__main__":
options, flags = grass.parser()
main()
exit(0)