Hi dev team,

I have been trying to run a few t.rast.mapcalc operations and encountered the substring dataset name problem described here: http://trac.osgeo.org/grass/ticket/2735

To my understanding this has never actually been fixed, so I tried to come up with a fool-proof solution. To prevent successive substitution of substrings, the dataset names in the expression are first filled with random string identifiers in descending string length order that can later be "formated" with exp.format(). See diff attached. This might be useful for other t.* modules that use the exp.replace() approach.

Since I havent got the latest trunk compiled, I wasnt able to test it in grass, but the concept should work. Would be great if someone with more t.* experience than me could test it.

Best regards,

Michel

Index: lib/python/temporal/mapcalc.py
===================================================================
--- lib/python/temporal/mapcalc.py	(revision 71405)
+++ lib/python/temporal/mapcalc.py	(working copy)
@@ -11,6 +11,8 @@
 # i18N
 import gettext
 import copy
+import string
+import random
 from datetime import datetime
 from multiprocessing import Process
 import grass.script as gscript
@@ -196,6 +198,18 @@
         proc_list = []
         proc_count = 0
 
+        # Remove spaces and new lines
+        exp = expression.replace(" ", "")
+        # sort input dataset names by decending length
+        inpr = inputs
+        inpr.sort(lambda x,y: cmp(len(y), len(x)))
+
+        # assign tmp variables in order
+        inputmp = {}
+        for i,n in inpr:
+            inputmp[n] = ''.join(random.choice(string.ascii_letters) for _ in range(12))
+            exp = exp.replace(n, '{%s}' % inputmp[n])
+        
         # For all samples
         for i in range(num):
 
@@ -205,19 +219,19 @@
             # Create the r.mapcalc statement for the current time step
             map_name = "{base}_{suffix}".format(base=base,
                                                 suffix=gscript.get_num_suffix(count, num))
-            # Remove spaces and new lines
-            expr = expression.replace(" ", "")
 
             # Check that all maps are in the sample
             valid_maps = True
             # Replace all dataset names with their map names of the
             # current time step
+            fmts = {}
             for j in range(len(map_matrix)):
                 if map_matrix[j][i] is None:
                     valid_maps = False
                     break
-                # Substitute the dataset name with the map name
-                expr = expr.replace(id_list[j], map_matrix[j][i])
+                fmts[inputmp[id_list[j]]] = map_matrix[j][i]
+            # Substitute the dataset name with the map name
+            expr = exp.formate(**fmts)
 
             # Proceed with the next sample
             if not valid_maps:
_______________________________________________
grass-dev mailing list
[email protected]
https://lists.osgeo.org/mailman/listinfo/grass-dev

Reply via email to