v.db.join.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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: database
  19. #% keywords: attribute table
  20. #%End
  21. #%option
  22. #% key: map
  23. #% type: string
  24. #% key_desc : name
  25. #% gisprompt: old,vector,vector
  26. #% description: Vector map to which to join other table
  27. #% required : yes
  28. #% guidependency: layer,column
  29. #%end
  30. #%option
  31. #% key: layer
  32. #% type: integer
  33. #% description: Layer where to join
  34. #% answer: 1
  35. #% required : no
  36. #% gisprompt: old_layer,layer,layer
  37. #% guidependency: column
  38. #%end
  39. #%option
  40. #% key: column
  41. #% type: string
  42. #% description: Join column in map table
  43. #% required : yes
  44. #% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
  45. #%end
  46. #%option
  47. #% key: otable
  48. #% type: string
  49. #% description: Other table name
  50. #% required : yes
  51. #% gisprompt: old_dbtable,dbtable,dbtable
  52. #% guidependency: ocolumn
  53. #%end
  54. #%option
  55. #% key: ocolumn
  56. #% type: string
  57. #% description: Join column in other table
  58. #% required : yes
  59. #% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
  60. #%end
  61. import sys
  62. import os
  63. import string
  64. import grass.script as grass
  65. def main():
  66. map = options['map']
  67. layer = options['layer']
  68. column = options['column']
  69. otable = options['otable']
  70. ocolumn = options['ocolumn']
  71. f = grass.vector_layer_db(map, layer)
  72. maptable = f['table']
  73. database = f['database']
  74. driver = f['driver']
  75. if driver == 'dbf':
  76. grass.fatal(_("JOIN is not supported for tables stored in DBF format."))
  77. if not maptable:
  78. grass.fatal(_("There is no table connected to this map. Cannot join any column."))
  79. if not grass.vector_columns(map, layer).has_key(column):
  80. grass.fatal(_("Column <%> not found in table <%s> at layer <%s>") % (column, map, layer))
  81. cols = grass.db_describe(otable, driver = driver, database = database)['cols']
  82. select = "SELECT $colname FROM $otable WHERE $otable.$ocolumn=$table.$column"
  83. template = string.Template("UPDATE $table SET $colname=(%s);" % select)
  84. for col in cols:
  85. colname = col[0]
  86. if len(col) > 2:
  87. coltype = "%s(%s)" % (col[1], col[2])
  88. else:
  89. coltype = "%s" % col[1]
  90. colspec = "%s %s" % (colname, coltype)
  91. if grass.run_command('v.db.addcolumn', map = map, columns = colspec, layer = layer) != 0:
  92. grass.fatal(_("Error creating column <%s>.") % colname)
  93. stmt = template.substitute(table = maptable, column = column,
  94. otable = otable, ocolumn = ocolumn,
  95. colname = colname)
  96. if grass.write_command('db.execute', stdin = stmt, input = '-', database = database, driver = driver) != 0:
  97. grass.fatal(_("Error filling column <%s>.") % colname)
  98. # write cmd history:
  99. grass.vector_history(map)
  100. if __name__ == "__main__":
  101. options, flags = grass.parser()
  102. main()