v.db.addcolumn.py 2.6 KB

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