v.db.join.py 3.1 KB

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