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