[GRASS-SVN] r68028 - in grass-addons/grass7: . db db/db.join

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Mar 9 02:08:11 PST 2016


Author: neteler
Date: 2016-03-09 02:08:11 -0800 (Wed, 09 Mar 2016)
New Revision: 68028

Added:
   grass-addons/grass7/db/
   grass-addons/grass7/db/db.join/
   grass-addons/grass7/db/db.join/Makefile
   grass-addons/grass7/db/db.join/db.join.html
   grass-addons/grass7/db/db.join/db.join.py
Log:
db.join addon: first attempt; derived from v.db.join

Added: grass-addons/grass7/db/db.join/Makefile
===================================================================
--- grass-addons/grass7/db/db.join/Makefile	                        (rev 0)
+++ grass-addons/grass7/db/db.join/Makefile	2016-03-09 10:08:11 UTC (rev 68028)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = db.join
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script


Property changes on: grass-addons/grass7/db/db.join/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass-addons/grass7/db/db.join/db.join.html
===================================================================
--- grass-addons/grass7/db/db.join/db.join.html	                        (rev 0)
+++ grass-addons/grass7/db/db.join/db.join.html	2016-03-09 10:08:11 UTC (rev 68028)
@@ -0,0 +1,47 @@
+<h2>DESCRIPTION</h2>
+
+<em>db.join</em> joins the content of one attribute table into another
+attribute table through common attributes.
+
+<h2>NOTES</h2>
+
+<em>db.join</em> is a front-end to <em>db.execute</em> to allow easier usage.
+
+The attribute table must be stored in a SQL database (SQLite, PostgreSQL,
+MySQL, ODBC, ...). The DBF backend is not supported. Tables can be
+imported with <em>db.in.ogr</em>.
+
+<h2>EXAMPLES</h2>
+
+
+<div class="code"><pre>
+# join soils_legend into mysoils attribute table
+db.join mysoils col=label otable=soils_legend ocol=shortname
+
+# verification of join
+db.select mysoils
+cat|label|id|shortname|longname
+1|Aab|||
+2|Ba|2|Ba|Barnum silt loam
+3|Bb|3|Bb|Barnum silt loam, channeled
+4|BcB|4|BcB|Boneek silt loam, 2 to 6
+5|BcC|5|BcC|Boneek silt loam, 6 to 9
+...
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="db.execute.html">db.execute</a>,
+<a href="db.in.ogr.html">db.in.ogr</a>,
+<a href="db.select.html">db.select</a>,
+<a href="v.db.join.html">v.db.join</a>,
+<a href="v.db.update.html">v.db.update</a><br>
+<a href="sql.html">GRASS SQL interface</a>
+</em>
+
+<h2>AUTHOR</h2>
+
+Markus Neteler
+
+<p><i>Last changed: $Date$</i>


Property changes on: grass-addons/grass7/db/db.join/db.join.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass-addons/grass7/db/db.join/db.join.py
===================================================================
--- grass-addons/grass7/db/db.join/db.join.py	                        (rev 0)
+++ grass-addons/grass7/db/db.join/db.join.py	2016-03-09 10:08:11 UTC (rev 68028)
@@ -0,0 +1,170 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE:       db.join
+# AUTHOR(S):    Markus Neteler
+#               Derived from v.db.join
+# PURPOSE:      Join a table to another table
+# COPYRIGHT:    (C) 2016 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: Joins a database table to another database table.
+#% keyword: database
+#% keyword: attribute table
+#%end
+
+#%option G_OPT_DB_TABLE
+#% description: Table to which to join other table
+#% required : yes
+#%end
+
+#%option G_OPT_DB_COLUMN
+#% description: Identifier column (e.g.: cat) in the table to be used for join
+#% required : yes
+#%end
+
+#%option G_OPT_DB_TABLE
+#% key: other_table
+#% description: Other table name
+#% required: yes
+#% guidependency: ocolumn,scolumns
+#%end
+
+#%option G_OPT_DB_DATABASE
+#%end
+
+#%option G_OPT_DB_DRIVER
+#% options: dbf,odbc,ogr,sqlite,pg
+#%end
+
+#%option G_OPT_DB_COLUMN
+#% key: other_column
+#% description: Identifier column (e.g.: id) in the other table used for join
+#% required: yes
+#%end
+
+#%option G_OPT_DB_COLUMN
+#% key: subset_columns
+#% multiple: yes
+#% required: no
+#% description: Subset of columns from the other table
+#%end
+
+import sys
+import string
+import grass.script as grass
+from grass.exceptions import CalledModuleError
+
+
+def main():
+    table = options['table']
+    column = options['column']
+    otable = options['other_table']
+    ocolumn = options['other_column']
+    if options['subset_columns']:
+        scolumns = options['subset_columns'].split(',')
+    else:
+        scolumns = None
+
+    database = options['database']
+    driver = options['driver']
+
+    # this error handling is completely different among th db.* scripts - FIX
+    if not database:
+        database = None
+    if not driver:
+        driver = None
+
+    if driver == 'dbf':
+        grass.fatal(_("JOIN is not supported for tables stored in DBF format"))
+
+    # describe input table
+    all_cols_tt = grass.db_describe(table, driver=driver, database=database)['cols']
+    if not all_cols_tt:
+        grass.fatal(_("Unable to describe table <%s>") % table)
+    found = False
+
+    # check if column is in input table
+    if column not in [col[0] for col in all_cols_tt]:
+        grass.fatal(_("Column <%s> not found in table <%s>") % (column, table))
+
+    # describe other table
+    all_cols_ot = grass.db_describe(otable, driver=driver, database=database)['cols']
+
+    # check if ocolumn is in other table
+    if ocolumn not in [ocol[0] for ocol in all_cols_ot]:
+        grass.fatal(_("Column <%s> not found in table <%s>") % (ocolumn, otable))
+
+    # determine columns subset from other table
+    if not scolumns:
+        # select all columns from other table
+        cols_to_add = all_cols_ot
+    else:
+        cols_to_add = []
+        # check if scolumns exists in the other table
+        for scol in scolumns:
+            found = False
+            for col_ot in all_cols_ot:
+                if scol == col_ot[0]:
+                    found = True
+                    cols_to_add.append(col_ot)
+                    break
+            if not found:
+                grass.warning(_("Column <%s> not found in table <%s>") % (scol, otable))
+
+    select = "SELECT $colname FROM $otable WHERE $otable.$ocolumn=$table.$column"
+    template = string.Template("UPDATE $table SET $colname=(%s);" % select)
+
+    for col in cols_to_add:
+        # skip the vector column which is used for join
+        colname = col[0]
+        if colname == column:
+            continue
+
+        use_len = False
+        if len(col) > 2:
+            use_len = True
+            # Sqlite 3 does not support the precision number any more
+            if driver == "sqlite":
+                use_len = False
+            # MySQL - expect format DOUBLE PRECISION(M,D), see #2792
+            elif driver == "mysql" and col[1] == 'DOUBLE PRECISION':
+                use_len = False
+
+        if use_len:
+            coltype = "%s(%s)" % (col[1], col[2])
+        else:
+            coltype = "%s" % col[1]
+
+        colspec = "%s %s" % (colname, coltype)
+
+        # add only the new column to the table
+        if colname not in all_cols_tt:
+            try:
+                grass.run_command('db.addcolumn', table=table,
+                                  columns=colspec)
+            except CalledModuleError:
+                grass.fatal(_("Error creating column <%s>") % colname)
+
+        stmt = template.substitute(table=table, column=column,
+                                   otable=otable, ocolumn=ocolumn,
+                                   colname=colname)
+        grass.debug(stmt, 1)
+        grass.verbose(_("Updating column <%s> of table <%s>...") % (colname, table))
+        try:
+            grass.write_command('db.execute', stdin=stmt, input='-',
+                                database=database, driver=driver)
+        except CalledModuleError:
+            grass.fatal(_("Error filling column <%s>") % colname)
+
+    return 0
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())


Property changes on: grass-addons/grass7/db/db.join/db.join.py
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native



More information about the grass-commit mailing list