v.db.join.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #%end
  34. #%option
  35. #% key: column
  36. #% type: string
  37. #% description: Join column in map table
  38. #% required : yes
  39. #%end
  40. #%option
  41. #% key: otable
  42. #% type: string
  43. #% description: Other table name
  44. #% required : yes
  45. #%end
  46. #%option
  47. #% key: ocolumn
  48. #% type: string
  49. #% description: Join column in other table
  50. #% required : yes
  51. #%end
  52. import sys
  53. import os
  54. import string
  55. import grass
  56. def main():
  57. map = options['map']
  58. layer = options['layer']
  59. column = options['column']
  60. otable = options['otable']
  61. ocolumn = options['ocolumn']
  62. s = grass.read_command('v.db.connect', flags = 'g', map = map, layer = layer)
  63. f = s.split()
  64. maptable = f[1]
  65. database = f[3]
  66. driver = f[4]
  67. if driver == 'dbf':
  68. grass.fatal("JOIN is not supported for tables stored in DBF format.")
  69. if not maptable:
  70. grass.fatal("There is no table connected to this map. Cannot join any column.")
  71. found = False
  72. s = grass.read_command('v.info', flags = 'c', map = map, layer = layer, quiet = True)
  73. for line in s.splitlines():
  74. f = line.split('|')
  75. if len(f) > 1 and f[1] == column:
  76. found = True
  77. if not found:
  78. grass.fatal("Column <%> not found in table <%s> at layer <%s>" % (column, map, layer))
  79. s = grass.read_command('db.describe', flags = 'c', driver = driver,
  80. database = database, table = otable)
  81. cols = [l.split(':') for l in s.splitlines() if l.startswith('Column ')]
  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[1].lstrip()
  86. if len(col) > 3:
  87. coltype = "%s(%s)" % (col[2], col[3])
  88. else:
  89. coltype = "%s" % col[2]
  90. colspec = "%s %s" % (colname, coltype)
  91. if grass.run_command('v.db.addcol', map = map, col = colspec) != 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) != 0:
  97. grass.fatal("Error filling column <%s>." % colname)
  98. # write cmd history:
  99. grass.run_command('v.support', map = map, cmdhist = os.environ['CMDLINE'])
  100. if __name__ == "__main__":
  101. options, flags = grass.parser()
  102. main()