[GRASS-SVN] r65445 - grass-addons/grass7/raster/r.sample.category

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jun 11 20:31:56 PDT 2015


Author: annakrat
Date: 2015-06-11 20:31:56 -0700 (Thu, 11 Jun 2015)
New Revision: 65445

Modified:
   grass-addons/grass7/raster/r.sample.category/r.sample.category.html
   grass-addons/grass7/raster/r.sample.category/r.sample.category.py
Log:
r.sample.category: add option to for different numbers of points for different categories, make module less verbose

Modified: grass-addons/grass7/raster/r.sample.category/r.sample.category.html
===================================================================
--- grass-addons/grass7/raster/r.sample.category/r.sample.category.html	2015-06-12 01:45:46 UTC (rev 65444)
+++ grass-addons/grass7/raster/r.sample.category/r.sample.category.html	2015-06-12 03:31:56 UTC (rev 65445)
@@ -5,12 +5,18 @@
 Each category (class) in a raster map will contain specified number
 of random points.
 
+<p>Different number of points can be specified for different categories.
+For example, if there are categories 1, 4, 7 in the input raster map,
+and npoints=100,200,300, 100 points will be generated in category 1,
+200 points in category 4 and 300 points in category 7.
+If only one number is specified, it will be used for every category.
 
 <h2>NOTES</h2>
 
 Mask (<em><a href="r.mask.html">r.mask</a></em>) to create points in areas
 with each category, thus mask cannot be active when the module is used.
 
+<p>Categories are identified based on current computational region.
 
 <h2>EXAMPLE</h2>
 
@@ -103,7 +109,8 @@
 <a href="r.random.html">r.random</a>,
 <a href="r.random.cells.html">r.random.cells</a>,
 <a href="v.random.html">v.random</a>,
-<a href="v.what.rast.html">v.what.rast</a>
+<a href="v.what.rast.html">v.what.rast</a>,
+<a href="r.describe.html">r.describe</a>
 </em>
 
 

Modified: grass-addons/grass7/raster/r.sample.category/r.sample.category.py
===================================================================
--- grass-addons/grass7/raster/r.sample.category/r.sample.category.py	2015-06-12 01:45:46 UTC (rev 65444)
+++ grass-addons/grass7/raster/r.sample.category/r.sample.category.py	2015-06-12 03:31:56 UTC (rev 65445)
@@ -36,9 +36,11 @@
 #% required: no
 #%end
 #%option
-#% description: Number of sampling points per category in the input map
+#% label: Number of sampling points per category in the input map
+#% description: You can provide multiple numbers, one for each category in input raster (sorted ascending)
 #% key: npoints
 #% required: yes
+#% multiple: yes
 #% type: integer
 #%end
 
@@ -49,7 +51,6 @@
 # TODO: specify number of points and distribute them uniformly
 # TODO: specify number of points and distribute them according to histogram
 # TODO: ensure/check minimum and maximum number of of points when doing histogram
-# TODO: be less verbose
 # TODO: create function to check for mask
 # TODO: move escape and mask functions to library
 
@@ -99,7 +100,7 @@
         sampled_rasters = options['sampled'].split(',')
     else:
         sampled_rasters = []
-    npoints_0 = int(options['npoints'])
+    npoints = [int(num) for num in options['npoints'].split(',')]
 
     if gscript.find_file(name='MASK', element='cell', mapset=gscript.gisenv()['MAPSET'])['name']:
         gscript.fatal(_("MASK is active. Please remove it before proceeding."))
@@ -112,31 +113,40 @@
     TMP.append(points_nocats)
 
     # input must be CELL
-    rdescribe = gscript.read_command('r.describe', map=input_raster, flags='d1')
+    rdescribe = gscript.read_command('r.describe', map=input_raster, flags='d1', quiet=True)
     categories = []
     for line in rdescribe.splitlines():
         try:
             categories.append(int(line))
         except ValueError:
             pass
+    if len(npoints) == 1:
+        npoints = npoints * len(categories)
+    else:
+        if len(categories) != len(npoints):
+            gscript.fatal(_("Number of categories in raster does not match the number of provided sampling points numbers."))
 
     vectors = []
-    for cat in categories:
+    for i, cat in enumerate(categories):
+        # skip generating points if none are required
+        if npoints[i] == 0:
+            continue
+        gscript.info(_("Selecting {n} sampling locations at category {cat}...").format(n=npoints[i], cat=cat))
         # change mask to sample zeroes and then change again to sample ones
         # overwrite mask for an easy loop
-        gscript.run_command('r.mask', raster=input_raster, maskcats=cat, overwrite=True)
+        gscript.run_command('r.mask', raster=input_raster, maskcats=cat, overwrite=True, quiet=True)
         vector = temp_name + str(cat)
         vectors.append(vector)
-        gscript.run_command('r.random', input=input_raster, npoints=npoints_0, vector=vector)
+        gscript.run_command('r.random', input=input_raster, npoints=npoints[i], vector=vector, quiet=True)
         TMP.append(vector)
 
-    gscript.run_command('r.mask', flags='r')
+    gscript.run_command('r.mask', flags='r', quiet=True)
 
-    gscript.run_command('v.patch', input=vectors, output=points)
+    gscript.run_command('v.patch', input=vectors, output=points, quiet=True)
     # remove and add gain cats so that they are unique
-    gscript.run_command('v.category', input=points, option='del', cat=-1, output=points_nocats)
+    gscript.run_command('v.category', input=points, option='del', cat=-1, output=points_nocats, quiet=True)
     # overwrite to reuse the map
-    gscript.run_command('v.category', input=points_nocats, option='add', output=points, overwrite=True)
+    gscript.run_command('v.category', input=points_nocats, option='add', output=points, overwrite=True, quiet=True)
 
     columns = []
     column_names = []
@@ -146,9 +156,10 @@
         column_names.append(column)
         # TODO: column type according to map type
         columns.append("{column} double precision".format(column=column))
-    gscript.run_command('v.db.addtable', map=points, columns=','.join(columns))
+    gscript.run_command('v.db.addtable', map=points, columns=','.join(columns), quiet=True)
     for raster, column in zip(sampled_rasters, column_names):
-        gscript.run_command('v.what.rast', map=points, type='point', raster=raster, column=column)
+        gscript.info(_("Sampling raster map %s...") % raster)
+        gscript.run_command('v.what.rast', map=points, type='point', raster=raster, column=column, quiet=True)
 
 
 if __name__ == '__main__':



More information about the grass-commit mailing list