v.db.update.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: v.db.update
  6. # AUTHOR(S): Moritz Lennert
  7. # Extensions by Markus Neteler
  8. # Converted to Python by Glynn Clements
  9. # PURPOSE: Interface to db.execute to update a column in the attribute table connected to a given map
  10. # COPYRIGHT: (C) 2005,2007-2008,2011 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: Updates a column in the attribute table connected to a 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. #% required: yes
  27. #%end
  28. #%option G_OPT_DB_COLUMN
  29. #% key: column
  30. #% description: Name of attribute column to update
  31. #% required: yes
  32. #%end
  33. #%option
  34. #% key: value
  35. #% type: string
  36. #% description: Literal value to update the column with
  37. #% required: no
  38. #%end
  39. #%option G_OPT_DB_COLUMN
  40. #% key: qcolumn
  41. #% description: Name of other attribute column to query, can be combination of columns (e.g. co1+col2)
  42. #%end
  43. #%option G_OPT_DB_WHERE
  44. #%end
  45. import sys
  46. import os
  47. import grass.script as grass
  48. def main():
  49. vector = options['map']
  50. layer = options['layer']
  51. column = options['column']
  52. value = options['value']
  53. qcolumn = options['qcolumn']
  54. where = options['where']
  55. mapset = grass.gisenv()['MAPSET']
  56. # does map exist in CURRENT mapset?
  57. if not grass.find_file(vector, element = 'vector', mapset = mapset)['file']:
  58. grass.fatal(_("Vector map <%s> not found in current mapset") % vector)
  59. try:
  60. f = grass.vector_db(vector)[int(layer)]
  61. except KeyError:
  62. grass.fatal(_('There is no table connected to this map. Run v.db.connect or v.db.addtable first.'))
  63. table = f['table']
  64. database = f['database']
  65. driver = f['driver']
  66. # checking column types
  67. try:
  68. coltype = grass.vector_columns(vector, layer)[column]['type']
  69. except KeyError:
  70. grass.fatal(_('Column <%s> not found') % column)
  71. if qcolumn:
  72. if value:
  73. grass.fatal(_('<value> and <qcolumn> are mutually exclusive'))
  74. # special case: we copy from another column
  75. value = qcolumn
  76. else:
  77. if not value:
  78. grass.fatal(_('Either <value> or <qcolumn> must be given'))
  79. # we insert a value
  80. if coltype.upper() not in ["INTEGER", "DOUBLE PRECISION"]:
  81. value = "'%s'" % value
  82. cmd = "UPDATE %s SET %s=%s" % (table, column, value)
  83. if where:
  84. cmd += " WHERE " + where
  85. grass.verbose("SQL: \"%s\"" % cmd)
  86. grass.write_command('db.execute', input = '-', database = database, driver = driver, stdin = cmd)
  87. # write cmd history:
  88. grass.vector_history(vector)
  89. return 0
  90. if __name__ == "__main__":
  91. options, flags = grass.parser()
  92. sys.exit(main())