[GRASS-SVN] r64595 - in grass-addons/grass7/raster: . r.viewshed.cva
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Feb 12 13:45:26 PST 2015
Author: isaacullah
Date: 2015-02-12 13:45:26 -0800 (Thu, 12 Feb 2015)
New Revision: 64595
Added:
grass-addons/grass7/raster/r.viewshed.cva/
grass-addons/grass7/raster/r.viewshed.cva/Makefile
grass-addons/grass7/raster/r.viewshed.cva/description.html
grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva
Log:
Adding a GRASS7 version of my r.viewshed.cva addon module. This module undertakes a cumulative viewshed analysis using r.viewshed and series of input points from a vector map. This version updates variable names and module interface to the GRASS7 standards. It also accounts for the inclusion of r.viewshed as a standard module in GRASS7, and offers an improvment in the handling of input points over the GRASS64 version of the script. Point locations are now determined internally (via r.out.xyz), so the user does not need to upload them to the vector's database. Use of r.out.xyz also allows the set of points to be constrained to the current region settings, avoiding errors raised by r.viewshed when attempting to call a viewpoint that is off map. The module description html file is updated to reflect these improvements.
Added: grass-addons/grass7/raster/r.viewshed.cva/Makefile
===================================================================
--- grass-addons/grass7/raster/r.viewshed.cva/Makefile (rev 0)
+++ grass-addons/grass7/raster/r.viewshed.cva/Makefile 2015-02-12 21:45:26 UTC (rev 64595)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.viewshed.cva
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script
Added: grass-addons/grass7/raster/r.viewshed.cva/description.html
===================================================================
--- grass-addons/grass7/raster/r.viewshed.cva/description.html (rev 0)
+++ grass-addons/grass7/raster/r.viewshed.cva/description.html 2015-02-12 21:45:26 UTC (rev 64595)
@@ -0,0 +1,76 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.viewshed.cva</em> is a module that allows for the
+construction of "Cumulative Viewshed", or "visualscape" maps from a
+series of input points stored in a vector points map. The routine is
+a python wrapper script that iterative loops through each input
+point, calculating a viewshed map, and then creates an output map
+that is coded by the number of input locations that can "see" each
+cell. <em>r.viewshed.cva</em> uses the GRASS module <em>r.viewshed</em>
+for the viewshed analysis. <it>r.viewshed</it> is very fast,
+thus allowing for a cumulative viewshed analysis to run in a
+reasonable amount of time. The final cumulative viewshed map is
+computed using the "count" method of <it>r.series</it>, rather than
+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)
+
+<h3>Options and flags:</h3>
+
+<it>r.viewshed.cva</it> requires an input elevation map and an input
+vector points map.
+There is currently only one native flag for <it>r.viewshed.cva</it>
+(-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<it> r.viewshed</it> help page for more
+information on these).
+
+<h2>NOTES</h2>
+
+The input vector points map can be manually digitized (with <it>
+v.digit</it>) over topographic or cultural features, or can be
+created as a series of random points (with <it>r.random</it> or <it>
+v.random</it>). Note that using the flag -k allows you to keep any
+interim viewshed maps created during the analysis, and these resultant
+viewshed maps will be named according to the cat number of the original
+input points. This is also useful for simple creating a large number
+of individual viewsheds from points in a vector file.
+
+<p>
+
+<h2>EXAMPLES</h2>
+Undertake a cumulative viewshed analysis from a digitized vector points map of prominent peaks in a region:<br>
+
+<div class="code"><pre>
+g.region rast=elevation_10m_dem at PERMANENT -p
+[use v.digit to digitize points]
+r.viewshed.cva.py elev=elevation10m_demPERMANENT output=peaks_CVA_map \
+ vect=prominent_peaks_points at PERMANENT x_column=x y_column=y \
+ name_column=cat obs_elev=0.0 tgt_elev=1.75 max_dist=-1 mem=1500
+</pre></div>
+
+
+<br>Undertake a cumulative viewshed analysis from a 10% sample of landscape locations in a region:<br>
+
+<div class="code"><pre>
+g.region rast=elevation_10m_dem at PERMANENT
+r.random input=elevation10m_demPERMANENT n=10% vector_output=rand_points_10p
+r.viewshed.cva.py elev=elevation10m_demPERMANENT output=peaks_CVA_map \
+ vect=rand_points_10p at PERMANENT x_column=x y_column=y \
+ name_column=cat obs_elev=0.0 tgt_elev=1.75 max_dist=-1 mem=1500
+</pre></div>
+
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.viewshed.html">r.viewshed</a>
+</em>
+
+
+<h2>AUTHOR</h2>
+
+Isaac Ullah
+
+<p>
+<i>Last changed: $Date: 2015-02-12 14:34:55 -0700 (Thurs, Feb. 12, 2015) $</i>
Added: grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva
===================================================================
--- grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva (rev 0)
+++ grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva 2015-02-12 21:45:26 UTC (rev 64595)
@@ -0,0 +1,183 @@
+#!/usr/bin/python
+#
+############################################################################
+#
+# MODULE: r.viewshed.cva.py
+# AUTHOR(S): Isaac Ullah
+# 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
+#% key: elev
+#% type: string
+#% gisprompt: new,cell,raster
+#% description: Input elevation map (DEM)
+#% required : yes
+#%END
+
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new,cell,raster
+#% description: Output CVA raster
+#% required : yes
+#%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
+#% description: Height of observation points off the ground
+#%answer: 0.0
+#% required : yes
+#%END
+
+#%option
+#% key: target_elevation
+#% type: string
+#% description: Height of target areas off the ground
+#%answer: 1.75
+#% required : yes
+#%END
+
+#%option
+#% key: max_distance
+#% type: string
+#% description: Maximum viewing distance (-1 = infinity)
+#%answer: -1
+#% required : yes
+#%END
+
+#%option
+#% key: memory
+#% type: string
+#% description: Amount of memory to use (in MB)
+#%answer: 1500
+#% required : yes
+#%END
+
+#%option
+#% key: refraction_coeff
+#% type: string
+#% description: Refraction coefficient (with flag -r)
+#%answer: 0.14286
+#% 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
+
+#%flag
+#% key: c
+#% description: -c Consider the curvature of the earth (current ellipsoid)
+#%END
+
+#%flag
+#% key: r
+#% description: -r Consider the effect of atmospheric refraction
+#%END
+
+#%flag
+#% key: b
+#% description: -b Output format is {0 (invisible) 1 (visible)}
+#%END
+
+#%flag
+#% key: e
+#% description: -e 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["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"]
+ out = options["output"]
+ #assemble flag string
+ if flags['r'] is True:
+ f1 = "r"
+ 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')
+ masterlist = []
+ for line in f.readlines():
+ masterlist.append(line.strip('\n').split(','))
+ f.close() #close the file
+ #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]
+ 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)
+ #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")
+ #Clean up temporary maps, if requested
+ if os.getenv('GIS_FLAG_k') == '1':
+ 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)
Property changes on: grass-addons/grass7/raster/r.viewshed.cva/r.viewshed.cva
___________________________________________________________________
Added: svn:executable
+ *
More information about the grass-commit
mailing list