[GRASS-SVN] r33560 - in grass/trunk: lib/python scripts/r.in.aster scripts/v.db.addcol scripts/v.db.join scripts/v.dissolve scripts/v.in.e00 scripts/v.in.geonames scripts/v.in.gns

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 26 17:13:14 EDT 2008


Author: glynn
Date: 2008-09-26 17:13:14 -0400 (Fri, 26 Sep 2008)
New Revision: 33560

Added:
   grass/trunk/scripts/v.db.addcol/v.db.addcol.py
   grass/trunk/scripts/v.db.join/v.db.join.py
Modified:
   grass/trunk/lib/python/grass.py
   grass/trunk/scripts/r.in.aster/r.in.aster.py
   grass/trunk/scripts/v.dissolve/v.dissolve.py
   grass/trunk/scripts/v.in.e00/v.in.e00.py
   grass/trunk/scripts/v.in.geonames/v.in.geonames.py
   grass/trunk/scripts/v.in.gns/v.in.gns.py
Log:
Convert v.db.addcol, v.db.join to Python
Make grass.parser() record original command line (to simplify v.support calls)


Modified: grass/trunk/lib/python/grass.py
===================================================================
--- grass/trunk/lib/python/grass.py	2008-09-26 15:55:45 UTC (rev 33559)
+++ grass/trunk/lib/python/grass.py	2008-09-26 21:13:14 UTC (rev 33560)
@@ -110,6 +110,10 @@
     if len(sys.argv) > 1 and sys.argv[1] == "@ARGS_PARSED@":
 	return _parse_env()
 
+    cmdline = [basename(sys.argv[0])]
+    cmdline += ['"' + arg + '"' for arg in sys.argv[1:]]
+    os.environ['CMDLINE'] = ' '.join(cmdline)
+
     argv = sys.argv[:]
     name = argv[0]
     if not os.path.isabs(name):

Modified: grass/trunk/scripts/r.in.aster/r.in.aster.py
===================================================================
--- grass/trunk/scripts/r.in.aster/r.in.aster.py	2008-09-26 15:55:45 UTC (rev 33559)
+++ grass/trunk/scripts/r.in.aster/r.in.aster.py	2008-09-26 21:13:14 UTC (rev 33560)
@@ -164,9 +164,6 @@
         srcfile=options['input']
         import_aster(proj, srcfile, tempfile, "DEM")
 
-    # write cmd history: Not sure how to replicate this in Python yet
-    #r.support "$GIS_OPT_OUTPUT" history="${CMDLINE}"
-
     #cleanup
     message("Cleaning up ...")
     grass.try_remove(tempfile)
@@ -196,7 +193,9 @@
     outfile = options['output'].strip()+'.'+band
     grass.run_command("r.in.gdal", overwrite = flags['o'], input = tempfile, output = outfile)
 
-    return
+    # write cmd history
+    grass.run_command('r.support', map = outfile, history = os.environ['CMDLINE'])
+    #r.support "$GIS_OPT_OUTPUT" history="${CMDLINE}"
 
 if __name__ == "__main__":
     options, flags = grass.parser()

Added: grass/trunk/scripts/v.db.addcol/v.db.addcol.py
===================================================================
--- grass/trunk/scripts/v.db.addcol/v.db.addcol.py	                        (rev 0)
+++ grass/trunk/scripts/v.db.addcol/v.db.addcol.py	2008-09-26 21:13:14 UTC (rev 33560)
@@ -0,0 +1,91 @@
+#!/usr/bin/env python
+#
+############################################################################
+#
+# MODULE:       v.db.addcolumn
+# AUTHOR(S):   	Moritz Lennert 
+#               Converted to Python by Glynn Clements
+# PURPOSE:      interface to db.execute to add a column to the attribute table
+#               connected to a given vector map
+# COPYRIGHT:    (C) 2005 by the GRASS Development Team
+#
+#               This program is free software under the GNU General Public
+#               License (>=v2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+#############################################################################
+
+
+#%Module
+#%  description: Adds one or more columns to the attribute table connected to a given vector map.
+#%  keywords: vector, database, attribute table
+#%End
+
+#%option
+#% key: map
+#% type: string
+#% gisprompt: old,vector,vector
+#% key_desc : name
+#% description: Vector map for which to edit attribute table
+#% required : yes
+#%end
+
+#%option
+#% key: layer
+#% type: integer
+#% description: Layer where to add column
+#% answer: 1
+#% required : no
+#%end
+
+#%option
+#% key: columns
+#% type: string
+#% description: Name and type of the new column(s) ('name type [,name type, ...]' - types depend on database backend, but all support VARCHAR(), INT, DOUBLE PRECISION and DATE)
+#% required : yes
+#%end
+
+import sys
+import os
+import grass
+import subprocess
+
+def main():
+    map = options['map']
+    layer = options['layer']
+    columns = options['columns']
+    columns = [col.strip() for col in columns.split(',')]
+
+    # does map exist in CURRENT mapset?
+    mapset = grass.gisenv()['MAPSET']
+    exists = bool(grass.find_file(map, element = 'vector', mapset = mapset)['file'])
+
+    if not exists:
+	grass.fatal("Vector map <%s> not found in current mapset" % map)
+
+    s = grass.read_command('v.db.connect', flags = 'g', map = map, layer = layer)
+    if not s:
+	grass.fatal("There is no table connected to this map. Run v.db.connect or v.db.addtable first.")
+    f = s.split()
+    table = f[1]
+    database = f[3]
+    driver = f[4]
+
+    colnum = len(columns)
+
+    for col in columns:
+	if not col:
+	    grass.fatal("There is an empty column. Did you leave a trailing comma?")
+
+	p = grass.start_command('db.execute', database = database, driver = driver, stdin = subprocess.PIPE)
+	p.stdin.write("ALTER TABLE %s ADD COLUMN %s" % (table, col))
+	p.stdin.close()
+	if p.wait() != 0:
+	    grass.fatal("Unable to add column <%s>." % col)
+
+    # write cmd history:
+    grass.run_command('v.support', map = map, cmdhist = os.environ['CMDLINE'])
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()


Property changes on: grass/trunk/scripts/v.db.addcol/v.db.addcol.py
___________________________________________________________________
Name: svn:executable
   + *

Added: grass/trunk/scripts/v.db.join/v.db.join.py
===================================================================
--- grass/trunk/scripts/v.db.join/v.db.join.py	                        (rev 0)
+++ grass/trunk/scripts/v.db.join/v.db.join.py	2008-09-26 21:13:14 UTC (rev 33560)
@@ -0,0 +1,126 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE:       v.db.join
+# AUTHOR(S):    Markus Neteler
+#               Converted to Python by Glynn Clements
+# PURPOSE:      Join a table to a map table
+# COPYRIGHT:    (C) 2007 by Markus Neteler and the GRASS Development Team
+#
+#               This program is free software under the GNU General Public
+#               License (>=v2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+#############################################################################
+
+#%Module
+#% description: Allows to join a table to a vector map table.
+#% keywords: vector, database, attribute table
+#%End
+
+#%option
+#% key: map
+#% type: string
+#% key_desc : name
+#% gisprompt: old,vector,vector
+#% description: Vector map to which to join other table
+#% required : yes
+#%end
+
+#%option
+#% key: layer
+#% type: integer
+#% description: Layer where to join
+#% answer: 1
+#% required : no
+#%end
+
+#%option
+#% key: column
+#% type: string
+#% description: Join column in map table
+#% required : yes
+#%end
+
+#%option
+#% key: otable
+#% type: string
+#% description: Other table name
+#% required : yes
+#%end
+
+#%option
+#% key: ocolumn
+#% type: string
+#% description: Join column in other table
+#% required : yes
+#%end
+
+import sys
+import os
+import subprocess
+import grass
+
+def main():
+    map = options['map']
+    layer = options['layer']
+    column = options['column']
+    otable = options['otable']
+    ocolumn = options['ocolumn']
+
+    s = grass.read_command('v.db.connect', flags = 'g', map = map, layer = layer)
+    f = s.split()
+    maptable = f[1]
+    database = f[3]
+    driver = f[4]
+
+    if driver == 'dbf':
+	grass.fatal("JOIN is not supported for tables stored in DBF format.")
+
+    if not maptable:
+	grass.fatal("There is no table connected to this map. Cannot join any column.")
+
+    found = False
+    s = grass.read_command('v.info', flags = 'c', map = map, layer = layer, quiet = True)
+    for line in s.splitlines():
+	f = line.split('|')
+	if len(f) > 1 and f[1] == column:
+	    found = True
+    if not found:
+	grass.fatal("Column <%> not found in table <%s> at layer <%s>" % (column, map, layer))
+
+    s = grass.read_command('db.describe', flags = 'c', driver = driver,
+			   database = database, table = otable)
+    cols = [l.split(':') for l in s.splitlines() if l.startswith('Column ')]
+
+    select = "SELECT $colname FROM $otable WHERE $otable.$ocolumn=$table.$column"
+    template = Template("UPDATE $table SET $colname=(%s);" % select)
+
+    for col in cols:
+	colname = col[0]
+	if len(col) > 2:
+	    coltype = "%s(%s)" % col[1:3]
+	else:
+	    coltype = "%s" % col[1]
+	colspec = "%s %s" % (colname, coltype)
+	if grass.run_command('v.db.addcol', map = map, col = colspec) != 0:
+	    grass.fatal("Error creating column <%s>." % colname)
+
+	stmt = template.substitute(table = maptable, column = column,
+				   otable = otable, ocolumn = ocolumn,
+				   colname = colname)
+
+	p = grass.start_command('db.execute', stdin = subprocess.PIPE)
+	p.stdin.write(stmt)
+	p.stdin.close()
+	p.wait()
+	if p.returncode != 0:
+	    grass.fatal("Error filling column <%s>." % colname)
+
+    # write cmd history:
+    grass.run_command('v.support', map = map, cmdhist = cmdline)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()

Modified: grass/trunk/scripts/v.dissolve/v.dissolve.py
===================================================================
--- grass/trunk/scripts/v.dissolve/v.dissolve.py	2008-09-26 15:55:45 UTC (rev 33559)
+++ grass/trunk/scripts/v.dissolve/v.dissolve.py	2008-09-26 21:13:14 UTC (rev 33560)
@@ -66,15 +66,6 @@
     layer = options['layer']
     column = options['column']
 
-    # save command line
-    cmdline = grass.basename(sys.argv[0])
-    cmdline += ' input=' + input
-    cmdline += ' output=' + output
-    if layer:
-	cmdline += ' layer=' + layer
-    if column:
-	cmdline += ' column=' + column
-
     #### setup temporary file
     tmp = str(os.getpid())
 
@@ -110,7 +101,7 @@
 			  output = output, type = 'area', layer = layer)
 
     # write cmd history:
-    grass.run_command('v.support', map = output, cmdhist = cmdline)
+    grass.run_command('v.support', map = output, cmdhist = os.environ['CMDLINE'])
 
 if __name__ == "__main__":
     options, flags = grass.parser()

Modified: grass/trunk/scripts/v.in.e00/v.in.e00.py
===================================================================
--- grass/trunk/scripts/v.in.e00/v.in.e00.py	2008-09-26 15:55:45 UTC (rev 33559)
+++ grass/trunk/scripts/v.in.e00/v.in.e00.py	2008-09-26 21:13:14 UTC (rev 33560)
@@ -63,13 +63,6 @@
     type = options['type']
     vect = options['vect']
 
-    # save command line
-    cmdline = grass.basename(sys.argv[0])
-    cmdline += ' file=' + filename
-    cmdline += ' type=' + type
-    if vect:
-	cmdline += ' vect=' + vect
-
     e00tmp = str(os.getpid())
 
     #### check for avcimport
@@ -175,7 +168,7 @@
     grass.message("Done.")
 
     # write cmd history:
-    grass.run_command('v.support', map = name, cmdhist = cmdline)
+    grass.run_command('v.support', map = name, cmdhist = os.environ['CMDLINE'])
 
 if __name__ == "__main__":
     options, flags = grass.parser()

Modified: grass/trunk/scripts/v.in.geonames/v.in.geonames.py
===================================================================
--- grass/trunk/scripts/v.in.geonames/v.in.geonames.py	2008-09-26 15:55:45 UTC (rev 33559)
+++ grass/trunk/scripts/v.in.geonames/v.in.geonames.py	2008-09-26 21:13:14 UTC (rev 33560)
@@ -51,12 +51,6 @@
     infile  = options['input']
     outfile = options['output']
     
-    # save command line
-    cmdline = grass.basename(sys.argv[0])
-    if infile:
-	cmdline += ' input=' + infile
-    if outfile:
-	cmdline += ' output=' + outfile
 
     #### setup temporary file
     tmpfile = grass.tempfile()
@@ -151,7 +145,7 @@
     grass.try_remove(tmpfile)
 
     # write cmd history:
-    grass.run_command('v.support', map = outfile, cmdhist = cmdline)
+    grass.run_command('v.support', map = outfile, cmdhist = os.environ['CMDLINE'])
 
 if __name__ == "__main__":
     options, flags = grass.parser()

Modified: grass/trunk/scripts/v.in.gns/v.in.gns.py
===================================================================
--- grass/trunk/scripts/v.in.gns/v.in.gns.py	2008-09-26 15:55:45 UTC (rev 33559)
+++ grass/trunk/scripts/v.in.gns/v.in.gns.py	2008-09-26 21:13:14 UTC (rev 33560)
@@ -51,13 +51,6 @@
     fileorig = options['file']
     filevect = options['vect']
     
-    # save command line
-    cmdline = grass.basename(sys.argv[0])
-    if fileorig:
-	cmdline += ' file=' + fileorig
-    if filevect:
-	cmdline += ' vect=' + filevect
-
     if not filevect:
 	filevect = grass.basename(fileorig, 'txt')
 
@@ -154,7 +147,7 @@
     grass.try_remove(tmpfile)
 
     # write cmd history:
-    grass.run_command('v.support', map = filevect, cmdhist = cmdline)
+    grass.run_command('v.support', map = filevect, cmdhist = os.environ['CMDLINE'])
 
 if __name__ == "__main__":
     options, flags = grass.parser()



More information about the grass-commit mailing list