v.db.join.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.db.join
  5. # AUTHOR(S): Markus Neteler
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: Join a table to a map table
  8. # COPYRIGHT: (C) 2007-2009 by Markus Neteler and the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (>=v2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. #
  14. #############################################################################
  15. #%module
  16. #% description: Allows to join a table to a vector map table.
  17. #% keywords: vector
  18. #% keywords: attribute table
  19. #% keywords: database
  20. #%end
  21. #%option G_OPT_V_MAP
  22. #% description: Vector map to which to join other table
  23. #% guidependency: layer,column
  24. #%end
  25. #%option G_OPT_V_FIELD
  26. #% description: Layer where to join
  27. #% guidependency: column
  28. #%end
  29. #%option G_OPT_DB_COLUMN
  30. #% description: Identifier column (e.g.: cat) in the vector table to be used for join
  31. #% required : yes
  32. #%end
  33. #%option G_OPT_DB_TABLE
  34. #% key: otable
  35. #% description: Other table name
  36. #% required: yes
  37. #% guidependency: ocolumn
  38. #%end
  39. #%option G_OPT_DB_COLUMN
  40. #% key: ocolumn
  41. #% description: Identifier column (e.g.: id) in the other table used for join
  42. #% required: yes
  43. #%end
  44. import sys
  45. import os
  46. import string
  47. import grass.script as grass
  48. def main():
  49. map = options['map']
  50. layer = options['layer']
  51. column = options['column']
  52. otable = options['otable']
  53. ocolumn = options['ocolumn']
  54. f = grass.vector_layer_db(map, layer)
  55. maptable = f['table']
  56. database = f['database']
  57. driver = f['driver']
  58. if driver == 'dbf':
  59. grass.fatal(_("JOIN is not supported for tables stored in DBF format"))
  60. if not maptable:
  61. grass.fatal(_("There is no table connected to this map. Unable to join any column."))
  62. if not grass.vector_columns(map, layer).has_key(column):
  63. grass.fatal(_("Column <%s> not found in table <%s> at layer <%s>") % (column, map, layer))
  64. all_cols_ot = grass.db_describe(otable, driver = driver, database = database)['cols']
  65. all_cols_tt = grass.vector_columns(map, int(layer)).keys()
  66. select = "SELECT $colname FROM $otable WHERE $otable.$ocolumn=$table.$column"
  67. template = string.Template("UPDATE $table SET $colname=(%s);" % select)
  68. for col in all_cols_ot:
  69. # Skip the vector column which is used for join
  70. colname = col[0]
  71. if colname == column:
  72. continue
  73. # Sqlite 3 does not support the precision number any more
  74. if len(col) > 2 and driver != "sqlite":
  75. coltype = "%s(%s)" % (col[1], col[2])
  76. else:
  77. coltype = "%s" % col[1]
  78. colspec = "%s %s" % (colname, coltype)
  79. # Add only the new column to the table
  80. if colname not in all_cols_tt:
  81. if grass.run_command('v.db.addcolumn', map = map, columns = colspec, layer = layer) != 0:
  82. grass.fatal(_("Error creating column <%s>") % colname)
  83. stmt = template.substitute(table = maptable, column = column,
  84. otable = otable, ocolumn = ocolumn,
  85. colname = colname)
  86. grass.verbose(_("Updating column <%s> of vector map <%s>...") % (colname, map))
  87. if grass.write_command('db.execute', stdin = stmt, input = '-', database = database, driver = driver) != 0:
  88. grass.fatal(_("Error filling column <%s>") % colname)
  89. # write cmd history:
  90. grass.vector_history(map)
  91. if __name__ == "__main__":
  92. options, flags = grass.parser()
  93. main()