[GRASS-SVN] r73376 - grass-addons/grass7/imagery/i.segment.stats

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Sep 20 05:06:45 PDT 2018


Author: mlennert
Date: 2018-09-20 05:06:45 -0700 (Thu, 20 Sep 2018)
New Revision: 73376

Modified:
   grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py
Log:
i.segment.stats: better handling of temp attribute table to avoid conflict with existing table

Modified: grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py
===================================================================
--- grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py	2018-09-19 14:33:13 UTC (rev 73375)
+++ grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py	2018-09-20 12:06:45 UTC (rev 73376)
@@ -105,6 +105,11 @@
         if gscript.find_file(temporary_vect, element='vector')['name']:
             gscript.run_command('g.remove', flags='f', type_='vector',
                     name=temporary_vect, quiet=True)
+        if gscript.db_table_exist(temporary_vect):
+            gscript.run_command('db.execute', 
+                                sql='DROP TABLE %s' % temporary_vect,
+                                quiet=True)
+
     if insert_sql:
         os.remove(insert_sql)
 
@@ -247,13 +252,18 @@
                             output=temporary_vect,
                             type_='area',
                             flags='vt',
-			    overwrite=True,
-			    quiet=True)
+                            overwrite=True,
+                            quiet=True)
 
         insert_sql = gscript.tempfile()
         fsql = open(insert_sql, 'w')
         fsql.write('BEGIN TRANSACTION;\n')
-        create_statement = 'CREATE TABLE ' + vectormap + ' (cat int, '
+        if gscript.db_table_exist(temporary_vect):
+            if gscript.overwrite():
+                fsql.write('DROP TABLE %s;' % temporary_vect)
+            else:
+                gscript.fatal(_("Table %s already exists. Use --o to overwrite" % temporary_vect))
+        create_statement = 'CREATE TABLE ' + temporary_vect + ' (cat int, '
         for header in output_header[1:-1]:
             create_statement += header +  ' double precision, '
         create_statement += output_header[-1] + ' double precision);\n'
@@ -260,7 +270,7 @@
         fsql.write(create_statement)
         for key in output_dict:
 		if len(output_dict[key]) + 1  == len(output_header):
-                    sql = "INSERT INTO " + vectormap + " VALUES (" + key+","+",".join(output_dict[key])+");\n"
+                    sql = "INSERT INTO %s VALUES (%s, %s);\n" % (temporary_vect, key, ",".join(output_dict[key]))
                     sql = sql.replace('inf', 'NULL')
                     fsql.write(sql)
 		else:
@@ -270,9 +280,9 @@
         fsql.write('END TRANSACTION;')
         fsql.close()
 
-        gscript.run_command('g.copy', vector=temporary_vect+','+vectormap, quiet=True)
         gscript.run_command('db.execute', input=insert_sql, quiet=True)
-        gscript.run_command('v.db.connect', map_=vectormap, table=vectormap, quiet=True)
+        gscript.run_command('v.db.connect', map_=temporary_vect, table=temporary_vect, quiet=True)
+        gscript.run_command('g.copy', vector="%s,%s" % (temporary_vect, vectormap), quiet=True)
 
     if error_objects:
         object_string = ', '.join(error_objects[:100])



More information about the grass-commit mailing list