v.db.addcolumn.py 3.0 KB

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