瀏覽代碼

r.blend: Quote map names in r.mapcalc expressions. (#277)

* r.blend: Quote map names in r.mapcalc expressions.
Avoids failure if map / mapset name is also a valid mathematical expression.

* r.grow: Quote map names in mapcalc expression

* testsuite: r.grow test case for mapcalc map quoting.
Based on PR #347 and PR #277

Co-authored-by: Māris Nartišs <maris.nartiss@lu.lv>
Māris Nartišs 5 年之前
父節點
當前提交
02bff13532
共有 3 個文件被更改,包括 70 次插入8 次删除
  1. 5 5
      scripts/r.blend/r.blend.py
  2. 3 3
      scripts/r.grow/r.grow.py
  3. 62 0
      scripts/r.grow/testsuite/test_r_grow_quoting.py

+ 5 - 5
scripts/r.blend/r.blend.py

@@ -70,11 +70,11 @@ def main():
 
     gscript.message(_("Calculating the three component maps..."))
 
-    template = string.Template("$$output.$ch = "
-                               "if(isnull($$first), $ch#$$second, "
-                               "if(isnull($$second), $ch#$$first, "
-                               "$$frac1 * $ch#$$first + "
-                               "$$frac2 * $ch#$$second))")
+    template = string.Template('$$output.$ch = '
+                               'if(isnull("$$first"), $ch#"$$second", '
+                               'if(isnull("$$second"), $ch#"$$first", '
+                               '$$frac1 * $ch#"$$first" + '
+                               '$$frac2 * $ch#"$$second"))')
     cmd = [template.substitute(ch=ch) for ch in ['r', 'g', 'b']]
     cmd = ';'.join(cmd)
 

+ 3 - 3
scripts/r.grow/r.grow.py

@@ -97,12 +97,12 @@ def main():
 
     if new == '' and shrink == False:
         temp_val = "r.grow.tmp.%s.val" % tmp
-        new = temp_val
+        new = '"%s"' % temp_val
     else:
         temp_val = None
 
     if old == '':
-        old = input
+        old = '"%s"' % input
 
     if not mapunits:
         kv = grass.region()
@@ -135,7 +135,7 @@ def main():
             grass.fatal(_("Growing failed. Removing temporary maps."))
 
         grass.mapcalc(
-            "$output = if(!isnull($input),$old,if($dist < $radius,$new,null()))",
+            '$output = if(!isnull("$input"),$old,if($dist < $radius,$new,null()))',
             output=output, input=input, radius=radius,
             old=old, new=new, dist=temp_dist)
     else:

+ 62 - 0
scripts/r.grow/testsuite/test_r_grow_quoting.py

@@ -0,0 +1,62 @@
+"""
+Created on Mon 17 Feb 2020 02:27:26 PM UTC
+
+@author: Markus Neteler, Maris Nartiss upon https://github.com/OSGeo/grass/pull/277
+"""
+
+import os
+from grass.gunittest.case import TestCase
+from grass.gunittest.main import test
+from grass.gunittest.gmodules import SimpleModule
+from grass.script.core import run_command
+from grass.gunittest.utils import silent_rmtree
+
+
+class TestRGrow(TestCase):
+    """Test r.grow script"""
+
+    map1 = 'elevation'
+    temp1 = 'grown'
+    mapsets_to_remove = []
+    # mapset with a name is also a valid mathematical expression
+    mapset_name = "1234-56-78"
+    gisenv = SimpleModule('g.gisenv', get='MAPSET')
+    TestCase.runModule(gisenv, expecting_stdout=True)
+    old_mapset = gisenv.outputs.stdout.strip()
+
+    @classmethod
+    def setUpClass(cls):
+        """Create maps in a small region."""
+        # create a mapset with a name is also a valid mathematical expression
+        cls.runModule("g.mapset", flags="c", mapset=cls.mapset_name)
+        cls.mapsets_to_remove.append(cls.mapset_name)
+        run_command('g.copy', raster=cls.map1 + '@PERMANENT,' + cls.map1)
+        cls.runModule('g.region', raster=cls.map1, flags='p')
+
+    @classmethod
+    def tearDownClass(cls):
+        """Remove temporary data"""
+        gisenv = SimpleModule('g.gisenv', get='GISDBASE')
+        cls.runModule(gisenv, expecting_stdout=True)
+        gisdbase = gisenv.outputs.stdout.strip()
+        gisenv = SimpleModule('g.gisenv', get='LOCATION_NAME')
+        cls.runModule(gisenv, expecting_stdout=True)
+        location = gisenv.outputs.stdout.strip()
+        cls.runModule('g.remove', flags='f', type='raster',
+                      name=(cls.temp1, ))
+        cls.runModule("g.mapset", mapset=cls.old_mapset)
+        for mapset_name in cls.mapsets_to_remove:
+            mapset_path = os.path.join(gisdbase, location, mapset_name)
+            silent_rmtree(mapset_path)
+
+    def test_grow(self):
+        """grows test with special mapset name"""
+
+        # should not lead to syntax error, unexpected INTEGER, expecting VARNAME or NAME
+        module = SimpleModule('r.grow', input=self.map1 + '@' + self.mapset_name,
+                              output=self.temp1)
+        self.assertModule(module)
+
+
+if __name__ == '__main__':
+    test()