[GRASS-SVN] r68819 - grass-addons/grass7/raster/r.vector.ruggedness

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jun 30 13:48:09 PDT 2016


Author: spawley
Date: 2016-06-30 13:48:09 -0700 (Thu, 30 Jun 2016)
New Revision: 68819

Modified:
   grass-addons/grass7/raster/r.vector.ruggedness/r.vector.ruggedness.py
Log:
Processing efficiency improved for r.vector.ruggedness, reducing the number of separate processing steps, and also running several of the processes in parallel (with an option to disable). 

Modified: grass-addons/grass7/raster/r.vector.ruggedness/r.vector.ruggedness.py
===================================================================
--- grass-addons/grass7/raster/r.vector.ruggedness/r.vector.ruggedness.py	2016-06-30 19:20:45 UTC (rev 68818)
+++ grass-addons/grass7/raster/r.vector.ruggedness/r.vector.ruggedness.py	2016-06-30 20:48:09 UTC (rev 68819)
@@ -4,19 +4,19 @@
 #
 # MODULE:       Vector Ruggedness Measure
 #
-# AUTHOR(S):    Adaption of original Sappington et al. (2007) script
-#               by Steven Pawley
+# AUTHOR(S):   Adaption of original Sappington et al. (2007) script
+#                       by Steven Pawley
 #
-# PURPOSE:      This tool measures terrain ruggedness by calculating the vector
-#               ruggedness measure described in Sappington, J.M., K.M. Longshore,
-#               and D.B. Thomson. 2007. Quantifiying Landscape Ruggedness for
-#               Animal Habitat Anaysis: A case Study Using Bighorn Sheep in
-#               the Mojave Desert.
-#               Journal of Wildlife Management. 71(5): 1419 -1426.
+# PURPOSE:     This tool measures terrain ruggedness by calculating the vector
+#                       ruggedness measure described in Sappington, J.M., K.M. Longshore,
+#                       and D.B. Thomson. 2007. Quantifiying Landscape Ruggedness for
+#                       Animal Habitat Anaysis: A case Study Using Bighorn Sheep in
+#                       the Mojave Desert.
+#                       Journal of Wildlife Management. 71(5): 1419 -1426.
 #
-# COPYRIGHT:    (C) 2015 Steven Pawley, and by the GRASS Development Team
+# COPYRIGHT: (C) 2016 Steven Pawley, and by the GRASS Development Team
 #
-# DATE:         21 April 2015
+# DATE:            30 June 2016
 #
 ##############################################################################
 
@@ -28,6 +28,8 @@
 import random
 import string
 import atexit
+import copy
+from grass.pygrass.modules import Module, ParallelModuleQueue
 
 #%option G_OPT_R_INPUTS
 #% key: elevation
@@ -47,6 +49,12 @@
 #% label: Vector Ruggedness Index Output
 #%end
 
+#%flag
+#% key: p
+#% label: Do not run in parallel
+#% guisection: Optional
+#%end
+
 tmp_rast = []
 
 def cleanup():
@@ -58,18 +66,18 @@
     dem = options['elevation']
     neighborhood_size = options['size']
     OutRaster = options['output']
+    notparallel = flags['p']
 
     # Internal raster map names
-    SlopeRaster = 'tmp_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
-    AspectRaster = 'tmp_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
-    xyRaster = 'tmp_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
-    zRaster = 'tmp_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
-    xRaster = 'tmp_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
-    yRaster = 'tmp_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
-    xSumRaster = 'tmp_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
-    ySumRaster = 'tmp_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
-    zSumRaster = 'tmp_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
-    ResultRaster = 'tmp_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
+    SlopeRaster = 'tmpSlope_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
+    AspectRaster = 'tmpAspect_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
+    xyRaster = 'tmpxyRaster_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
+    zRaster = 'tmpzRaster_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
+    xRaster = 'tmpxRaster_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
+    yRaster = 'tmpyRaster_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
+    xSumRaster = 'tmpxSumRaster_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
+    ySumRaster = 'tmpySumRaster_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
+    zSumRaster = 'tmpzSumRaster_' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
 
     tmp_rast.append(SlopeRaster)
     tmp_rast.append(AspectRaster)
@@ -80,7 +88,6 @@
     tmp_rast.append(xSumRaster)
     tmp_rast.append(ySumRaster)
     tmp_rast.append(zSumRaster)
-    tmp_rast.append(ResultRaster)
 
     # Create Slope and Aspect rasters
     grass.message("Calculating slope and aspect...")
@@ -97,28 +104,87 @@
     # Calculate x y and z rasters
     # Note - GRASS sin/cos functions differ from ArcGIS which expects input grid in radians, whereas GRASS functions expect degrees
     # No need to convert slope and aspect to radians as in the original ArcGIS script
-    grass.message("Calculating x, y, and z rasters...")
-    grass.mapcalc('{x} = sin({a})'.format(x=xyRaster, a=SlopeRaster))
-    grass.mapcalc('{x} = cos({a})'.format(x=zRaster, a=SlopeRaster))
-    grass.mapcalc('{x} = sin({a}) * {b}'.format(x=xRaster, a=AspectRaster, b=xyRaster))
-    grass.mapcalc('{x} = cos({a}) * {b}'.format(x=yRaster, a=AspectRaster, b=xyRaster))
 
+    if notparallel == False:
+        # parallel version
+        grass.message("Calculating x, y, and z rasters...")
+
+        # calculate xy and z rasters using two parallel processes
+        mapcalc_list = []
+        mapcalc = Module("r.mapcalc", run_=False)
+        queue = ParallelModuleQueue(nprocs=2)
+
+        mapcalc1 = copy.deepcopy(mapcalc)
+        mapcalc_list.append(mapcalc1)
+        m = mapcalc1(expression='{x} = sin({a})'.format(x=xyRaster, a=SlopeRaster))
+        queue.put(m)
+
+        mapcalc2 = copy.deepcopy(mapcalc)
+        mapcalc_list.append(mapcalc2)
+        m = mapcalc2(expression='{x} = cos({a})'.format(x=zRaster, a=SlopeRaster))
+        queue.put(m)
+
+        queue.wait()
+
+        # calculate x and y rasters using two parallel processes
+        mapcalc_list = []
+        mapcalc = Module("r.mapcalc", run_=False)
+        queue = ParallelModuleQueue(nprocs=2)
+
+        mapcalc1 = copy.deepcopy(mapcalc)
+        mapcalc_list.append(mapcalc1)
+        m = mapcalc1(expression='{x} = sin({a}) * {b}'.format(x=xRaster, a=AspectRaster, b=xyRaster))
+        queue.put(m)
+
+        mapcalc2 = copy.deepcopy(mapcalc)
+        mapcalc_list.append(mapcalc2)
+        m = mapcalc2(expression='{x} = cos({a}) * {b}'.format(x=yRaster, a=AspectRaster, b=xyRaster))
+        queue.put(m)
+
+        queue.wait()
+    else:
+        grass.mapcalc('{x} = sin({a})'.format(x=xyRaster, a=SlopeRaster))
+        grass.mapcalc('{x} = cos({a})'.format(x=zRaster, a=SlopeRaster))
+        grass.mapcalc('{x} = sin({a}) * {b}'.format(x=xRaster, a=AspectRaster, b=xyRaster))
+        grass.mapcalc('{x} = cos({a}) * {b}'.format(x=yRaster, a=AspectRaster, b=xyRaster))
+
     # Calculate sums of x, y, and z rasters for selected neighborhood size
-    grass.message("Calculating sums of x, y, and z rasters in selected neighborhood...")
-    grass.run_command("r.neighbors", input = xRaster, output = xSumRaster, method = "average", size = neighborhood_size)
-    grass.run_command("r.neighbors", input = yRaster, output = ySumRaster, method = "average", size = neighborhood_size)
-    grass.run_command("r.neighbors", input = zRaster, output = zSumRaster, method = "average", size = neighborhood_size)
 
-    # Calculate the resultant vector
+    if notparallel == False:
+        # parallel version using three parallel processes
+        grass.message("Calculating sums of x, y, and z rasters in selected neighborhood...")
+
+        n_list = []
+        neighbors = Module("r.neighbors", overwrite=True, run_=False)
+        queue = ParallelModuleQueue(nprocs=3)
+
+        n1 = copy.deepcopy(neighbors)
+        n_list.append(n1)
+        n = n1(input = xRaster, output = xSumRaster, method = "average", size = neighborhood_size)
+        queue.put(n1)
+
+        n2 = copy.deepcopy(neighbors)
+        n_list.append(n2)
+        n = n2(input = yRaster, output = ySumRaster, method = "average", size = neighborhood_size)
+        queue.put(n2)
+
+        n3 = copy.deepcopy(neighbors)
+        n_list.append(n3)
+        n = n3(input = zRaster, output = zSumRaster, method = "average", size = neighborhood_size)
+        queue.put(n3)
+
+        queue.wait()
+    else:
+        grass.run_command("r.neighbors", input = xRaster, output = xSumRaster, method = "average", size = neighborhood_size)
+        grass.run_command("r.neighbors", input = yRaster, output = ySumRaster, method = "average", size = neighborhood_size)
+        grass.run_command("r.neighbors", input = zRaster, output = zSumRaster, method = "average", size = neighborhood_size)
+
+    # Calculate the resultant vector and final ruggedness raster
     # Modified from the original script to multiple each SumRaster by the n neighborhood cells to get the sum
-    grass.message("Calculating the resultant vector...")
+    grass.message("Calculating the final ruggedness raster...")
     maxValue = int(neighborhood_size) * int(neighborhood_size)
-    grass.mapcalc('{x} = sqrt(({a}*{d})^2 + ({b}*{d})^2 + ({c}*{d})^2)'.format(x=ResultRaster, a=xSumRaster, b=ySumRaster, c=zSumRaster, d=maxValue))
+    grass.mapcalc('{x} = 1-( (sqrt(({a}*{d})^2 + ({b}*{d})^2 + ({c}*{d})^2) / {e}))'.format(x=OutRaster, a=xSumRaster, b=ySumRaster, c=zSumRaster, d=maxValue, e=maxValue))
 
-    # Calculate the Ruggedness raster
-    grass.message("Calculating the final ruggedness raster...")
-    grass.mapcalc('{x} = 1-({a} / {b})'.format(x=OutRaster, a=ResultRaster, b=maxValue))
-
     # Set the default color table
     grass.run_command("r.colors", flags = 'e', map = OutRaster, color = "ryb")
 



More information about the grass-commit mailing list