[GRASS-SVN] r64029 - grass-addons/grass7/raster/r.random.weight

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jan 9 06:02:28 PST 2015


Author: pvanbosgeo
Date: 2015-01-09 06:02:28 -0800 (Fri, 09 Jan 2015)
New Revision: 64029

Modified:
   grass-addons/grass7/raster/r.random.weight/r.random.weight.html
   grass-addons/grass7/raster/r.random.weight/r.random.weight.py
Log:
bugfix

Modified: grass-addons/grass7/raster/r.random.weight/r.random.weight.html
===================================================================
--- grass-addons/grass7/raster/r.random.weight/r.random.weight.html	2015-01-09 13:59:12 UTC (rev 64028)
+++ grass-addons/grass7/raster/r.random.weight/r.random.weight.html	2015-01-09 14:02:28 UTC (rev 64029)
@@ -1,9 +1,9 @@
 
 <h2>DESCRIPTION</h2>
 
-<em>r.rand.weight</em> generates a binary raster layer with randomly 
-assigned values of 1 or 0, weighted by raster cell values of an 
-user-defined raster layer. I.e., the change for a raster cell to get 
+<em>r.rand.weight</em> generates a binary raster layer with a random
+selection of raster cells which are assigned 1. The other cells are 
+assigned NULL (or optionally 0). The change for a raster cell to get 
 assigned a 1 (to get selected) depends on the weight (value) of that 
 cell in the input weight layer.
 
@@ -28,5 +28,4 @@
 
 Paulo van Breugel, paulo at ecodiv.org
 
-<i>Last changed: $Date$
-</i> 
+<i>Last changed: $Date$</i> 

Modified: grass-addons/grass7/raster/r.random.weight/r.random.weight.py
===================================================================
--- grass-addons/grass7/raster/r.random.weight/r.random.weight.py	2015-01-09 13:59:12 UTC (rev 64028)
+++ grass-addons/grass7/raster/r.random.weight/r.random.weight.py	2015-01-09 14:02:28 UTC (rev 64029)
@@ -72,9 +72,17 @@
 #% guisection: Sample options
 #%end
 
+#%flag
+#% key: n
+#% description: set non-selected values to 0 (default to NULL)
+#%end
+
+
 # import libraries
 import os
 import sys
+import uuid
+import string
 import atexit
 import grass.script as grass
 
@@ -102,6 +110,7 @@
     outmap = options['output']
     subsample = options['subsample']
     seed = options['seed']
+    flag_n = flags['n']
        
     # setup temporary files and seed
     tmp_map = "r_w_rand_" + str(uuid.uuid4())
@@ -120,17 +129,24 @@
             tmp_map = tmp_map)
     else:        
         grass.mapcalc("$tmp_map = rand(float(${minval}),float(${maxval}))", 
-            seed=1,
+            seed=int(seed),
             minval = minval, 
             maxval = maxval,
             tmp_map = tmp_map)
     clean_rast.add(tmp_map) 
 
-    grass.mapcalc("${outmap} = if($tmp_map <= ${weight},1,0)",
-        weight = weight,
-        outmap = outmap,
-        tmp_map = tmp_map)
-        
+    if flag_n:
+        grass.mapcalc("${outmap} = if($tmp_map <= ${weight},1,0)",
+            weight = weight,
+            outmap = outmap,
+            tmp_map = tmp_map)
+    else:
+        grass.mapcalc("${outmap} = if($tmp_map <= ${weight},1,null())",
+            weight = weight,
+            outmap = outmap,
+            tmp_map = tmp_map)
+    
+    grass.run_command("g.remove", quiet=True, flags="f", type="raster", name=tmp_map)        
     if not subsample == '': 
         grass.run_command('r.null',
             map = outmap, 
@@ -140,9 +156,10 @@
             n = subsample,
             raster_output = outmap,
             overwrite=True)
-        grass.run_command('r.null',
-            map = outmap, 
-            null = 0)
+        if flag_n:
+            grass.run_command('r.null',
+                map = outmap, 
+                null = 0)
 
     print("------------------")
     print("Ready!")



More information about the grass-commit mailing list