v.db.addcolumn.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #% keyword: vector
  20. #% keyword: attribute table
  21. #% keyword: database
  22. #%end
  23. #%option G_OPT_V_MAP
  24. #%end
  25. #%option G_OPT_V_FIELD
  26. #% label: Layer number where to add column(s)
  27. #%end
  28. #%option
  29. #% key: columns
  30. #% type: string
  31. #% label: Name and type of the new column(s) ('name type [,name type, ...]')
  32. #% description: Data types depend on database backend, but all support VARCHAR(), INT, DOUBLE PRECISION and DATE
  33. #% required: yes
  34. #%end
  35. import sys
  36. import os
  37. import grass.script as grass
  38. def main():
  39. map = options['map']
  40. layer = options['layer']
  41. columns = options['columns']
  42. columns = [col.strip() for col in columns.split(',')]
  43. # does map exist in CURRENT mapset?
  44. mapset = grass.gisenv()['MAPSET']
  45. exists = bool(grass.find_file(map, element='vector', mapset=mapset)['file'])
  46. if not exists:
  47. grass.fatal(_("Vector map <%s> not found in current mapset") % map)
  48. try:
  49. f = grass.vector_db(map)[int(layer)]
  50. except KeyError:
  51. grass.fatal(
  52. _("There is no table connected to this map. Run v.db.connect or v.db.addtable first."))
  53. table = f['table']
  54. database = f['database']
  55. driver = f['driver']
  56. column_existing = grass.vector_columns(map, int(layer)).keys()
  57. for col in columns:
  58. if not col:
  59. grass.fatal(_("There is an empty column. Did you leave a trailing comma?"))
  60. col_name = col.split(' ')[0].strip()
  61. if col_name in column_existing:
  62. grass.error(_("Column <%s> is already in the table. Skipping.") % col_name)
  63. continue
  64. grass.verbose(_("Adding column <%s> to the table") % col_name)
  65. p = grass.feed_command('db.execute', input='-', database=database, driver=driver)
  66. p.stdin.write("ALTER TABLE %s ADD COLUMN %s" % (table, col))
  67. grass.debug("ALTER TABLE %s ADD COLUMN %s" % (table, col))
  68. p.stdin.close()
  69. if p.wait() != 0:
  70. grass.fatal(_("Unable to add column <%s>.") % col)
  71. # write cmd history:
  72. grass.vector_history(map)
  73. if __name__ == "__main__":
  74. options, flags = grass.parser()
  75. main()