v.db.join.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. import 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_db(map, layer)
  67. if not f:
  68. grass.fatal("An error occured while running v.db.connect")
  69. maptable = f[1]
  70. database = f[3]
  71. driver = f[4]
  72. if driver == 'dbf':
  73. grass.fatal("JOIN is not supported for tables stored in DBF format.")
  74. if not maptable:
  75. grass.fatal("There is no table connected to this map. Cannot join any column.")
  76. if column not in [f[1] for f in grass.vector_columns(map, layer)]:
  77. grass.fatal("Column <%> not found in table <%s> at layer <%s>" % (column, map, layer))
  78. cols = grass.db_describe(otable, driver = driver, database = database)['cols']
  79. select = "SELECT $colname FROM $otable WHERE $otable.$ocolumn=$table.$column"
  80. template = string.Template("UPDATE $table SET $colname=(%s);" % select)
  81. for col in cols:
  82. colname = col[0]
  83. if len(col) > 2:
  84. coltype = "%s(%s)" % (col[1], col[2])
  85. else:
  86. coltype = "%s" % col[1]
  87. colspec = "%s %s" % (colname, coltype)
  88. if grass.run_command('v.db.addcol', map = map, columns = colspec, layer = layer) != 0:
  89. grass.fatal("Error creating column <%s>." % colname)
  90. stmt = template.substitute(table = maptable, column = column,
  91. otable = otable, ocolumn = ocolumn,
  92. colname = colname)
  93. if grass.write_command('db.execute', stdin = stmt, database = database, driver = driver) != 0:
  94. grass.fatal("Error filling column <%s>." % colname)
  95. # write cmd history:
  96. grass.vector_history(map)
  97. if __name__ == "__main__":
  98. options, flags = grass.parser()
  99. main()