v.db.renamecolumn.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.db.renamecolumn
  5. # AUTHOR(S): Markus Neteler
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: interface to db.execute to drop a column from the
  8. # attribute table connected to a given vector map
  9. # - Based on v.db.dropcolumn
  10. # - with special trick for SQLite and DBF (here the new col is
  11. # added/values copied/old col deleted)
  12. # COPYRIGHT: (C) 2007 by the GRASS Development Team
  13. #
  14. # This program is free software under the GNU General Public
  15. # License (>=v2). Read the file COPYING that comes with GRASS
  16. # for details.
  17. #
  18. # TODO: MySQL untested
  19. #############################################################################
  20. #%Module
  21. #% description: Renames a column in the attribute table connected to a given vector map.
  22. #% keywords: vector
  23. #% keywords: database
  24. #% keywords: attribute table
  25. #%End
  26. #%option
  27. #% key: map
  28. #% type: string
  29. #% gisprompt: old,vector,vector
  30. #% key_desc : name
  31. #% description: Vector map for which to rename attribute column
  32. #% required : yes
  33. #%end
  34. #%option
  35. #% key: layer
  36. #% gisprompt: old_layer,layer,layer
  37. #% description: Layer where to rename column
  38. #% answer: 1
  39. #% required : no
  40. #%end
  41. #%option
  42. #% key: column
  43. #% type: string
  44. #% description: Old and new name of the column (old,new)
  45. #% required : yes
  46. #% multiple: no
  47. #% key_desc: oldcol,newcol
  48. #%end
  49. import sys
  50. import os
  51. import grass.script as grass
  52. def main():
  53. map = options['map']
  54. layer = options['layer']
  55. column = options['column']
  56. mapset = grass.gisenv()['MAPSET']
  57. if not grass.find_file(map, element = 'vector', mapset = mapset):
  58. grass.fatal(_("Vector map <%s> not found in current mapset") % map)
  59. f = grass.vector_layer_db(map, layer)
  60. table = f['table']
  61. keycol = f['key']
  62. database = f['database']
  63. driver = f['driver']
  64. if not table:
  65. grass.fatal(_("There is no table connected to the input vector map. Cannot rename any column"))
  66. cols = column.split(',')
  67. oldcol = cols[0]
  68. newcol = cols[1]
  69. if driver == "dbf":
  70. if len(newcol) > 10:
  71. grass.fatal(_("Column name <%s> too long. The DBF driver supports column names not longer than 10 characters") % newcol)
  72. if oldcol == keycol:
  73. grass.fatal(_("Cannot rename column <%s> as it is needed to keep table <%s> connected to the input vector map") % (oldcol, table))
  74. # describe old col
  75. oldcoltype = None
  76. for f in grass.db_describe(table)['cols']:
  77. if f[0] != oldcol:
  78. continue
  79. oldcoltype = f[1]
  80. oldcollength = f[2]
  81. # old col there?
  82. if not oldcol:
  83. grass.fatal(_("Column <%s> not found in table <%s>") % (coldcol, table))
  84. # some tricks
  85. if driver in ['sqlite', 'dbf']:
  86. if oldcoltype.upper() == "CHARACTER":
  87. colspec = "%s varchar(%s)" % (newcol, oldcollength)
  88. else:
  89. colspec = "%s %s" % (newcol, oldcoltype)
  90. grass.run_command('v.db.addcolumn', map = map, layer = layer, column = colspec)
  91. sql = "UPDATE %s SET %s=%s" % (table, newcol, oldcol)
  92. grass.write_command('db.execute', input = '-', database = database, driver = driver, stdin = sql)
  93. grass.run_command('v.db.dropcolumn', map = map, layer = layer, column = oldcol)
  94. else:
  95. sql = "ALTER TABLE %s RENAME %s TO %s" % (table, oldcol, newcol)
  96. grass.write_command('db.execute', input = '-', database = database, driver = driver, stdin = sql)
  97. # write cmd history:
  98. grass.vector_history(map)
  99. if __name__ == "__main__":
  100. options, flags = grass.parser()
  101. main()