v.db.addcolumn.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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: attribute table
  21. #% keywords: 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(_("There is no table connected to this map. Run v.db.connect or v.db.addtable first."))
  52. table = f['table']
  53. database = f['database']
  54. driver = f['driver']
  55. column_existing = grass.vector_columns(map, int(layer)).keys()
  56. for col in columns:
  57. if not col:
  58. grass.fatal(_("There is an empty column. Did you leave a trailing comma?"))
  59. col_name = col.split(' ')[0].strip()
  60. if col_name in column_existing:
  61. grass.error(_("Column <%s> is already in the table. Skipping.") % col_name)
  62. continue
  63. grass.verbose(_("Adding column <%s> to the table") % col_name)
  64. p = grass.feed_command('db.execute', input = '-', database = database, driver = driver)
  65. p.stdin.write("ALTER TABLE %s ADD COLUMN %s" % (table, col))
  66. grass.debug("ALTER TABLE %s ADD COLUMN %s" % (table, col))
  67. p.stdin.close()
  68. if p.wait() != 0:
  69. grass.fatal(_("Unable to add column <%s>.") % col)
  70. # write cmd history:
  71. grass.vector_history(map)
  72. if __name__ == "__main__":
  73. options, flags = grass.parser()
  74. main()