[GRASS-SVN] r56919 - grass-addons/grass7/imagery/i.histo.match

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jun 25 07:17:08 PDT 2013


Author: mlennert
Date: 2013-06-25 07:17:08 -0700 (Tue, 25 Jun 2013)
New Revision: 56919

Modified:
   grass-addons/grass7/imagery/i.histo.match/i.histo.match.py
Log:
quote table names to allow map names with dots + simplify SQL


Modified: grass-addons/grass7/imagery/i.histo.match/i.histo.match.py
===================================================================
--- grass-addons/grass7/imagery/i.histo.match/i.histo.match.py	2013-06-25 13:57:36 UTC (rev 56918)
+++ grass-addons/grass7/imagery/i.histo.match/i.histo.match.py	2013-06-25 14:17:08 UTC (rev 56919)
@@ -81,10 +81,10 @@
     for i in images:
         iname = i.split('@')[0]
         # drop table if exist
-        query_drop = "DROP TABLE if exists t%s" % iname
+        query_drop = "DROP TABLE if exists \"t%s\"" % iname
         curs.execute(query_drop)
         # create table
-        query_create = "CREATE TABLE t%s (grey_value integer,pixel_frequency " % iname
+        query_create = "CREATE TABLE \"t%s\" (grey_value integer,pixel_frequency " % iname
         query_create += "integer, cumulative_histogram integer, cdf real)"
         curs.execute(query_create)
         # set the region on the raster
@@ -101,12 +101,12 @@
             try:
                 val = int(stats_dict[str(n)])
                 cdf += val
-                insert = "INSERT INTO t%s VALUES (%i, %i, %i, 0.000000)" % (
+                insert = "INSERT INTO \"t%s\" VALUES (%i, %i, %i, 0.000000)" % (
                                                             iname, n, val, cdf)
                 curs.execute(insert)
             except:
                 cdf += 0
-                insert = "INSERT INTO t%s VALUES (%i, 0, %i, 0.000000)" % (
+                insert = "INSERT INTO \"t%s\" VALUES (%i, 0, %i, 0.000000)" % (
                                                             iname, n, cdf)
                 curs.execute(insert)
         db.commit()
@@ -115,14 +115,14 @@
         # for each number in the range
         for n in range(0, max_value):
             # select value for cumulative_histogram for the range number
-            select_ch = "SELECT cumulative_histogram FROM t%s WHERE " % iname
+            select_ch = "SELECT cumulative_histogram FROM \"t%s\" WHERE " % iname
             select_ch += "(grey_value=%i)" % n
             result = curs.execute(select_ch)
             val = result.fetchone()[0]
             # update cdf with new value
             if val != 0 and numPixel != 0:
                 update_cdf = round(float(val) / float(numPixel), 6)
-                update_cdf = "UPDATE t%s SET cdf=%s WHERE (grey_value=%i)" % (
+                update_cdf = "UPDATE \"t%s\" SET cdf=%s WHERE (grey_value=%i)" % (
                                                             iname, update_cdf,n)
                 curs.execute(update_cdf)
                 db.commit()
@@ -134,7 +134,7 @@
         # for each image
         for i in images:
             iname = i.split('@')[0]
-            pixel_freq = "SELECT pixel_frequency FROM t%s WHERE (grey_value=%i)" % (
+            pixel_freq = "SELECT pixel_frequency FROM \"t%s\" WHERE (grey_value=%i)" % (
                                                             iname, n)
             result = curs.execute(pixel_freq)
             val = result.fetchone()[0]
@@ -157,7 +157,7 @@
         for i in images:
             iname = i.split('@')[0]
             # select pixel frequency
-            pixel_freq = "SELECT pixel_frequency FROM t%s WHERE (grey_value=%i)" % (
+            pixel_freq = "SELECT pixel_frequency FROM \"t%s\" WHERE (grey_value=%i)" % (
                                                             iname, n)
             result = curs.execute(pixel_freq)
             val = result.fetchone()[0]
@@ -181,24 +181,19 @@
         outfile = open(grass.tempfile(), 'w')
         new_grey = 0
         for n in range(0, max_value):
-            select_min = "SELECT min(abs(a.cdf - b.cdf)) FROM t%s as a," % iname
-            select_min += " %s as b WHERE (a.grey_value=%i)" % (table_ave, n)
-            result_min = curs.execute(select_min)
-            min_abs = result_min.fetchone()[0]
-            select_cdf = "SELECT cdf FROM t%s WHERE grey_value=%i" % (iname, n)
-            result_cdf = curs.execute(select_cdf)
-            cdf = result_cdf.fetchone()[0]  
-            select_newgrey = "SELECT grey_value FROM %s WHERE " % table_ave \
-                + "cdf=(%s-%s) OR cdf=(%s+%s)" % (cdf,min_abs,cdf,min_abs)
+            select_newgrey = "SELECT b.grey_value FROM \"t%s\" as a, " % iname
+            select_newgrey += "%s as b WHERE a.grey_value=%i " % (table_ave, n) \
+                + "ORDER BY abs(a.cdf-b.cdf) LIMIT 1"
             # write line with old and new value
             try:
                 result_new = curs.execute(select_newgrey)
-                new_grey = result_new.fetchone()[0] 
+                new_grey = result_new.fetchone()[0]
                 out_line = "%d = %d\n" % (n, new_grey)
-                outfile.write(out_line)                
+                outfile.write(out_line)
             except:
                 out_line = "%d = %d\n" % (n, new_grey)
-                outfile.write(out_line)                 
+                outfile.write(out_line)
+
         outfile.close()
         outname = '%s.%s' % (iname, suffix)
         # check if a output map already exists



More information about the grass-commit mailing list