[GRASS-SVN] r71314 - in grass/trunk/scripts/v.rast.stats: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jul 24 23:12:37 PDT 2017


Author: lucadelu
Date: 2017-07-24 23:12:37 -0700 (Mon, 24 Jul 2017)
New Revision: 71314

Modified:
   grass/trunk/scripts/v.rast.stats/testsuite/test_v_rast_stats.py
   grass/trunk/scripts/v.rast.stats/v.rast.stats.py
Log:
v.rast.stats.py: added flag -d; added tests for this new function

Modified: grass/trunk/scripts/v.rast.stats/testsuite/test_v_rast_stats.py
===================================================================
--- grass/trunk/scripts/v.rast.stats/testsuite/test_v_rast_stats.py	2017-07-25 05:38:25 UTC (rev 71313)
+++ grass/trunk/scripts/v.rast.stats/testsuite/test_v_rast_stats.py	2017-07-25 06:12:37 UTC (rev 71314)
@@ -4,6 +4,8 @@
 """
 from grass.gunittest.case import TestCase
 from grass.gunittest.gmodules import SimpleModule
+from grass.pygrass.vector import VectorTopo
+from grass.pygrass.vector.geometry import Line
 
 
 class TestRastStats(TestCase):
@@ -23,6 +25,7 @@
         self.runModule("g.remove", flags='f', type="raster", name="map_a")
         self.runModule("g.remove", flags='f', type="raster", name="map_b")
         self.runModule("g.remove", flags='f', type="raster", name="zone_map")
+        self.runModule("g.remove", flags='f', type="raster", name="test_line")
 
     def setUp(self):
         """Create input data
@@ -34,6 +37,16 @@
                        overwrite=True)
         self.runModule("r.to.vect", input="zone_map", output="zone_map",
                        type="area", overwrite=True)
+        cols = [(u'cat', 'INTEGER PRIMARY KEY'), (u'name', 'VARCHAR(20)')]
+        vt = VectorTopo('test_line')
+        vt.open('w', tab_cols=cols)
+        line1 = Line([(1, 1), (2, 1), (2, 2)])
+        line2 = Line([(10, 20), (15, 22), (20, 32), (30, 40)])
+        vt.write(line1, ('first',))
+        vt.write(line2, ('second',))
+        vt.table.conn.commit()
+        vt.close()
+        
 
     def test_1(self):
         # Output of v.rast.stats
@@ -51,6 +64,35 @@
         self.assertLooksLike(univar_string, v_db_select.outputs.stdout)
 
 
+    def test_line_d(self):
+        output_str = """cat|name|a_median|a_number|a_range
+1|first|192|3|1
+2|second|181|41|6
+"""
+        self.assertModule("v.rast.stats", map="test_line", raster="map_a",
+                          method=["median", "number", "range"], flags="dc",
+                          column_prefix="a")
+        v_db_select = SimpleModule("v.db.select", map="test_line")
+        
+        self.runModule(v_db_select)
+        self.assertLooksLike(output_str, v_db_select.outputs.stdout)
+
+
+    def test_line(self):
+        output_str = """cat|name|a_median|a_number|a_range
+1|first|192|5|2
+2|second|181|27|5
+
+"""
+        self.assertModule("v.rast.stats", map="test_line", raster="map_a",
+                          method=["median", "number", "range"], flags="c",
+                          column_prefix="a")
+        v_db_select = SimpleModule("v.db.select", map="test_line")
+        
+        self.runModule(v_db_select)
+        self.assertLooksLike(output_str, v_db_select.outputs.stdout)
+
+
 class TestRastStatsFails(TestCase):
 
     def test_error_handling_a(self):

Modified: grass/trunk/scripts/v.rast.stats/v.rast.stats.py
===================================================================
--- grass/trunk/scripts/v.rast.stats/v.rast.stats.py	2017-07-25 05:38:25 UTC (rev 71313)
+++ grass/trunk/scripts/v.rast.stats/v.rast.stats.py	2017-07-25 06:12:37 UTC (rev 71314)
@@ -31,6 +31,11 @@
 #% key: c
 #% description: Continue if upload column(s) already exist
 #%end
+#%flag
+#% key: d
+#% label: Create densified lines (default: thin lines)
+#% description: All cells touched by the line will be set, not only those on the render path
+#%end
 #%option G_OPT_V_MAP
 #%end
 #%option G_OPT_V_FIELD
@@ -131,8 +136,14 @@
 
     grass.message(_("Preprocessing input data..."))
     try:
-        grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
-                          use='cat', quiet=True)
+        nlines = grass.vector_info_topo(vector)['lines']
+        # Create densified lines rather than thin lines
+        if flags['d'] and nlines > 0:
+            grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
+                              use='cat', flags='d', quiet=True)
+        else:
+            grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
+                              use='cat', quiet=True)
     except CalledModuleError:
         grass.fatal(_("An error occurred while converting vector to raster"))
 



More information about the grass-commit mailing list