v.db.addcolumn.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: v.db.addcolumnumn
  6. # AUTHOR(S): Moritz Lennert
  7. # Converted to Python by Glynn Clements
  8. # PURPOSE: interface to db.execute to add a column to the attribute table
  9. # connected to a given vector map
  10. # COPYRIGHT: (C) 2005 by the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General Public
  13. # License (>=v2). Read the file COPYING that comes with GRASS
  14. # for details.
  15. #
  16. #############################################################################
  17. #%Module
  18. #% description: Adds one or more columns to the attribute table connected to a given vector map.
  19. #% keywords: vector
  20. #% keywords: database
  21. #% keywords: attribute table
  22. #%End
  23. #%option
  24. #% key: map
  25. #% type: string
  26. #% gisprompt: old,vector,vector
  27. #% key_desc : name
  28. #% description: Name of vector map for which to edit attribute table
  29. #% required : yes
  30. #%end
  31. #%option
  32. #% key: layer
  33. #% type: integer
  34. #% gisprompt: old_layer,layer,layer
  35. #% label: Layer number where to add column(s)
  36. #% description: A single vector map can be connected to multiple database tables. This number determines which table to use.
  37. #% answer: 1
  38. #% required : no
  39. #%end
  40. #%option
  41. #% key: columns
  42. #% type: string
  43. #% label: Name and type of the new column(s) ('name type [,name type, ...]')
  44. #% description: Data types depend on database backend, but all support VARCHAR(), INT, DOUBLE PRECISION and DATE
  45. #% required : yes
  46. #%end
  47. import sys
  48. import os
  49. import grass.script as grass
  50. def main():
  51. map = options['map']
  52. layer = options['layer']
  53. columns = options['columns']
  54. columns = [col.strip() for col in columns.split(',')]
  55. # does map exist in CURRENT mapset?
  56. mapset = grass.gisenv()['MAPSET']
  57. exists = bool(grass.find_file(map, element = 'vector', mapset = mapset)['file'])
  58. if not exists:
  59. grass.fatal(_("Vector map <%s> not found in current mapset") % map)
  60. try:
  61. f = grass.vector_db(map)[int(layer)]
  62. except KeyError:
  63. grass.fatal(_("There is no table connected to this map. Run v.db.connect or v.db.addtable first."))
  64. table = f['table']
  65. database = f['database']
  66. driver = f['driver']
  67. column_existing = grass.vector_columns(map, int(layer)).keys()
  68. for col in columns:
  69. if not col:
  70. grass.fatal(_("There is an empty column. Did you leave a trailing comma?"))
  71. col_name = col.split(' ')[0].strip()
  72. if col_name in column_existing:
  73. grass.error(_("Column <%s> is already in the table. Skipping.") % col_name)
  74. continue
  75. grass.verbose(_("Adding column <%s> to the table") % col_name)
  76. p = grass.feed_command('db.execute', input = '-', database = database, driver = driver)
  77. p.stdin.write("ALTER TABLE %s ADD COLUMN %s" % (table, col))
  78. grass.debug("ALTER TABLE %s ADD COLUMN %s" % (table, col))
  79. p.stdin.close()
  80. if p.wait() != 0:
  81. grass.fatal(_("Unable to add column <%s>.") % col)
  82. # write cmd history:
  83. grass.vector_history(map)
  84. if __name__ == "__main__":
  85. options, flags = grass.parser()
  86. main()