[GRASS-SVN] r64754 - grass-addons/grass7/raster/r.viewshed.cva

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Feb 26 10:30:35 PST 2015


Author: isaacullah
Date: 2015-02-26 10:30:35 -0800 (Thu, 26 Feb 2015)
New Revision: 64754

Modified:
   grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva.html
   grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva.py
Log:
Fixed g.parser code so that it reflects the new G_OPT_R_INPUT standard, with grass.overwrite() enabled. Merged proposed code and style changes by Ana Petrasova, and added back the 'name_col' option so that interim viewshed maps opted to be kept with -k flag can be named from a column in the input vector's database. Also fixed html man page to replace all <it> tags with <em>.

Modified: grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva.html
===================================================================
--- grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva.html	2015-02-26 17:15:09 UTC (rev 64753)
+++ grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva.html	2015-02-26 18:30:35 UTC (rev 64754)
@@ -14,16 +14,23 @@
 with mapcalc, as it better handles the null values in the individual 
 constituent viewshed maps (and allows for interim viewshed maps to 
 be coded in any way)
+<p>
 
 <h3>Options and flags:</h3>
 
-<em>r.viewshed.cva</em> requires an input elevation map and an input 
-vector points map.
-There is currently only one native flag for <em>r.viewshed.cva</em> 
-(-k), which allows you to keep the interim viewshed maps made for 
-each input point. All other flags and options are inherited from 
-r.viewshed (see the<em> r.viewshed</em> help page for more 
+<em>r.viewshed.cva</em> requires an input elevation map, <b>input</b>, 
+and an input vector points map, <b>vector</b>. There is currently only
+ one native flag for <em>r.viewshed.cva</em>, <b>-k</b>, which allows
+ you to keep the interim viewshed maps made for each input point. 
+Optionally, option <b>name_col</b> can be used with <b>-k</b> to specify 
+the suffix of the kept viewshed maps by a particular column in the 
+input vector sites' database. If no value is specified for <b>name_col</b>, 
+then the cat value will be used.
+<p>
+All other flags and options are inherited from 
+<em>r.viewshed<em> (see the <a href="r.viewshed.html">r.viewshed</a> help page for more 
 information on these).
+<p>
 
 <h2>NOTES</h2>
 
@@ -73,4 +80,4 @@
 Isaac Ullah
 
 <p>
-<i>Last changed: $Date$</i>
+<em>Last changed: $Date$</em>

Modified: grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva.py
===================================================================
--- grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva.py	2015-02-26 17:15:09 UTC (rev 64753)
+++ grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva.py	2015-02-26 18:30:35 UTC (rev 64754)
@@ -1,9 +1,9 @@
-#!/usr/bin/env python
+#!/usr/bin/python
 #
 ############################################################################
 #
 # MODULE:       	r.viewshed.cva.py
-# AUTHOR(S):	Isaac Ullah
+# 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
@@ -14,99 +14,96 @@
 #############################################################################
 
 
-#%Module
+#%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
+#%end
 
-#%option
-#% key: elev
-#% type: string
-#% gisprompt: new,cell,raster
+#%option G_OPT_R_INPUT
 #% description: Input elevation map (DEM)
-#% required : yes
 #%END
 
-#%option
+#%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
-#% type: string
-#% gisprompt: new,cell,raster
-#% description: Output CVA raster
-#% required : yes
-#%END
+#% description: Output cumulative viewshed raster
+#%end
 
 #%option
-#% key: vect
-#% type: string
-#% gisprompt: new,vector,vector
-#% description: Name of input vector points map containg the set of sites for this analysis.
-#% required : yes
-#%END
-
-#%option
 #% key: observer_elevation
-#% type: string
+#% type: double
 #% description: Height of observation points off the ground
-#%answer: 0.0
-#% required : yes
-#%END
+#%answer: 1.75
+#% required : no
+#%end
 
 #%option
 #% key: target_elevation
-#% type: string
+#% type: double
 #% description: Height of target areas off the ground
 #%answer: 1.75
-#% required : yes
-#%END
+#% required : no
+#%end
 
 #%option
 #% key: max_distance
-#% type: string
-#% description: Maximum viewing distance (-1 = infinity)
+#% type: double
+#% description: Maximum visibility radius. By default infinity (-1)
 #%answer: -1
-#% required : yes
-#%END
+#% required : no
+#%end
 
 #%option
 #% key: memory
-#% type: string
+#% type: integer
 #% description: Amount of memory to use (in MB)
-#%answer: 1500
-#% required : yes
-#%END
+#%answer: 500
+#% required : no
+#%end
 
 #%option
 #% key: refraction_coeff
-#% type: string
+#% type: double
 #% description: Refraction coefficient (with flag -r)
 #%answer: 0.14286
+#% options: 0.0-1.0
 #% required : no
-#%END
+#%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: -k 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)
-#%END
+#% 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: -c Consider the curvature of the earth (current ellipsoid)
-#%END
+#% description: Consider the curvature of the earth (current ellipsoid)
+#%end
 
 #%flag
 #% key: r
-#% description: -r Consider the effect of atmospheric refraction
-#%END
+#% description: Consider the effect of atmospheric refraction
+#%end
 
 #%flag
 #% key: b
-#% description: -b   Output format is {0 (invisible) 1 (visible)}
-#%END
+#% description: Output format is {0 (invisible) 1 (visible)}
+#%end
 
 #%flag
 #% key: e
-#% description:  -e   Output format is invisible = NULL, else current elev - viewpoint_elev
-#%END
+#% description:  Output format is invisible = NULL, else current elev - viewpoint_elev
+#%end
 
 
 
@@ -120,60 +117,52 @@
 #main block of code starts here
 def main():
     #bring in input variables
-    elev = options["elev"]
-    vect = options["vect"]
-    observer_elevation =options["observer_elevation"]
-    target_elevation = options['target_elevation']
-    max_distance = options["max_distance"]
-    memory = options["memory"]
-    refraction_coeff = options["refraction_coeff"]
+    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
-    if flags['r'] is True:
-        f1 = "r"
+    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:
-        f1 = ""
-    if flags['c'] is True:
-        f2 = "c"
-    else:
-        f2 = ""
-    if flags['b'] is True:
-        f3 = "b"
-    else:
-        f3 = ""
-    if flags['e'] is True:
-        f4 = "e"
-    else:
-        f4 = ""
-    flagstring = f1 + f2 + f3 +f4
-    #make a tempfile, and write out the coords from the vector map.
-    tmp1 = grass.tempfile()
-    grass.run_command("v.out.ascii", flags = 'r', input = vect, type = "point", output = tmp1, format = "point", separator = ",")
-    # 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 temp file back in, and parse it up.
-    f = open(tmp1, 'r')
+        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 f.readlines():
-        masterlist.append(line.strip('\n').split(','))
-    f.close() #close the file
+    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:
-        grass.message('Calculating viewshed for location %s,%s (point name = %s)\n' % (site[0], site[1], site[2]))
-        tempry = "vshed_%s" % site[2]
+        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], observer_elevation = observer_elevation, target_elevation = target_elevation, max_distance = max_distance, memory = memory,  refraction_coeff = refraction_coeff)
+        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.mapcalc("${output}=${command_string}", quiet = "True", output = out, command_string = ("+").join(vshed_list))
-    grass.run_command("r.series", quiet = "True", overwrite = grass.overwrite(), input = (",").join(vshed_list), output = out, method = "count")
+    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 os.getenv('GIS_FLAG_k') == '1':
-        grass.message("Temporary viewshed maps will not removed")
+    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))
+        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.



More information about the grass-commit mailing list