v.db.addcolumn.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python3
  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. from grass.script.utils import encode
  41. def main():
  42. map = options['map']
  43. layer = options['layer']
  44. columns = options['columns']
  45. columns = [col.strip() for col in columns.split(',')]
  46. # does map exist in CURRENT mapset?
  47. mapset = grass.gisenv()['MAPSET']
  48. exists = bool(grass.find_file(map, element='vector', mapset=mapset)['file'])
  49. if not exists:
  50. grass.fatal(_("Vector map <%s> not found in current mapset") % map)
  51. try:
  52. f = grass.vector_db(map)[int(layer)]
  53. except KeyError:
  54. grass.fatal(
  55. _("There is no table connected to this map. Run v.db.connect or v.db.addtable first."))
  56. table = f['table']
  57. database = f['database']
  58. driver = f['driver']
  59. column_existing = grass.vector_columns(map, int(layer)).keys()
  60. for col in columns:
  61. if not col:
  62. grass.fatal(_("There is an empty column. Did you leave a trailing comma?"))
  63. col_name = col.split(' ')[0].strip()
  64. if col_name in column_existing:
  65. grass.error(_("Column <%s> is already in the table. Skipping.") % col_name)
  66. continue
  67. grass.verbose(_("Adding column <%s> to the table") % col_name)
  68. p = grass.feed_command('db.execute', input='-',
  69. database=database, driver=driver)
  70. res = "ALTER TABLE {} ADD COLUMN {}".format(table, col)
  71. p.stdin.write(encode(res))
  72. grass.debug(res)
  73. p.stdin.close()
  74. if p.wait() != 0:
  75. grass.fatal(_("Unable to add column <%s>.") % col)
  76. # write cmd history:
  77. grass.vector_history(map)
  78. if __name__ == "__main__":
  79. options, flags = grass.parser()
  80. main()