[GRASS-SVN] r60800 - grass-addons/grass7/raster/r.random.weight
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jun 12 10:14:10 PDT 2014
Author: pvanbosgeo
Date: 2014-06-12 10:14:10 -0700 (Thu, 12 Jun 2014)
New Revision: 60800
Added:
grass-addons/grass7/raster/r.random.weight/r.random.weight
grass-addons/grass7/raster/r.random.weight/r.random.weight.html
Log:
Function to create weighted random sample points, whereby the change a location is included is proportional to the relative value of an user-defined weight layer
Added: grass-addons/grass7/raster/r.random.weight/r.random.weight
===================================================================
--- grass-addons/grass7/raster/r.random.weight/r.random.weight (rev 0)
+++ grass-addons/grass7/raster/r.random.weight/r.random.weight 2014-06-12 17:14:10 UTC (rev 60800)
@@ -0,0 +1,114 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+##############################################################################
+#
+# MODULE: r.rand.weight
+# AUTHOR(S): paulo van Breugel <paulo at ecodiv.org>
+# PURPOSE: Create a layer with weighted random sample
+# COPYRIGHT: (C) 2014 Paulo van Breugel
+# http://ecodiv.org
+# http://pvanb.wordpress.com/
+#
+# This program is free software under the GNU General Public
+# License (>=v2). Read the file COPYING that comes with GRASS
+# for details.
+##############################################################################
+
+#%module
+#% description: Weighted sample
+#% keywords: raster, sample
+#%end
+
+#%option
+#% key: weights
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: layer with weight
+#% key_desc: weight
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new,cell,raster
+#% description: output layer
+#% key_desc: output
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: start
+#% type: double
+#% description: minimum weight
+#% key_desc: start
+#% answer: 0
+#% required: yes
+#%end
+
+#%option
+#% key: end
+#% type: double
+#% description: maximum weight
+#% key_desc: end
+#% answer: 100
+#% required: yes
+#%end
+
+#%option
+#% key: subsample
+#% type: string
+#% description: subsample - not implemented yet
+#% key_desc: ss
+#% answer: 0
+#% required: no
+#%end
+
+# import libraries
+import os
+import sys
+import atexit
+import grass.script as grass
+
+
+def cleanup():
+ grass.run_command('g.remove',
+ rast = tmp_map, quiet = True)
+
+# main function
+def main():
+ global tmp_map
+
+ # check if GISBASE is set
+ if "GISBASE" not in os.environ:
+ # return an error advice
+ grass.fatal(_("You must be in GRASS GIS to run this program"))
+
+ # input raster map and parameters
+ minval = options['start']
+ maxval = options['end']
+ weight = options['weights']
+ outmap = options['output']
+ subsample = options['subsample']
+
+ # setup temporary files
+ tmp_map = 'r_w_rand_987654321'
+
+ grass.mapcalc("$tmp_map = rand(${minval},${maxval})",
+ minval = minval,
+ maxval = maxval,
+ tmp_map = tmp_map)
+
+ grass.mapcalc("${outmap} = if($tmp_map < ${weight},1,0)",
+ weight = weight,
+ outmap = outmap,
+ tmp_map = tmp_map)
+
+if __name__ == "__main__":
+ options, flags = grass.parser()
+ atexit.register(cleanup)
+ sys.exit(main())
+
Property changes on: grass-addons/grass7/raster/r.random.weight/r.random.weight
___________________________________________________________________
Added: svn:executable
+ *
Added: grass-addons/grass7/raster/r.random.weight/r.random.weight.html
===================================================================
--- grass-addons/grass7/raster/r.random.weight/r.random.weight.html (rev 0)
+++ grass-addons/grass7/raster/r.random.weight/r.random.weight.html 2014-06-12 17:14:10 UTC (rev 60800)
@@ -0,0 +1,13 @@
+
+<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 assigned a 1 (to get selected) depends on the weight (value) of that cell in the input weight layer.
+
+<h2>Note</h2>
+The values of the weight raster layer should be within the range defined by the start and end value. Currently the script does not check this (on the todo list)
+
+<h2>AUTHOR</h2>
+
+Paulo van Breugel <paulo at ecodiv.org>
+
+<i>Last changed: $Date$</i>
More information about the grass-commit
mailing list