v.db.update.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 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: Allows to update a column in the attribute table connected to a 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. #% description: Vector map to edit the attribute table for
  28. #% required : yes
  29. #%end
  30. #%option
  31. #% key: layer
  32. #% type: integer
  33. #% gisprompt: old_layer,layer,layer
  34. #% description: Layer to which the table to be changed is connected
  35. #% answer: 1
  36. #% required : no
  37. #%end
  38. #%option
  39. #% key: column
  40. #% type: string
  41. #% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
  42. #% description: Column to update
  43. #% required : yes
  44. #%end
  45. #%option
  46. #% key: value
  47. #% type: string
  48. #% description: Value to update the column with, can be (combination of) other column(s)
  49. #% required : no
  50. #%end
  51. #%option
  52. #% key: qcolumn
  53. #% type: string
  54. #% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
  55. #% description: Column to query
  56. #% required : no
  57. #%end
  58. #%option
  59. #% key: where
  60. #% type: string
  61. #% description: WHERE conditions for update, without 'where' keyword (e.g. cat=1 or col1/col2>1)
  62. #% required : no
  63. #%end
  64. import sys
  65. import os
  66. import grass.script as grass
  67. def main():
  68. map = options['map']
  69. layer = options['layer']
  70. column = options['column']
  71. value = options['value']
  72. qcolumn = options['qcolumn']
  73. where = options['where']
  74. mapset = grass.gisenv()['MAPSET']
  75. # does map exist in CURRENT mapset?
  76. if not grass.find_file(map, element = 'vector', mapset = mapset)['file']:
  77. grass.fatal(_("Vector map '$GIS_OPT_MAP' not found in current mapset"))
  78. try:
  79. f = grass.vector_db(map)[int(layer)]
  80. except KeyError:
  81. grass.fatal(_('There is no table connected to this map. Run v.db.connect or v.db.addtable first.'))
  82. table = f['table']
  83. database = f['database']
  84. driver = f['driver']
  85. # checking column types
  86. try:
  87. coltype = grass.vector_columns(map, layer)[column]['type']
  88. except KeyError:
  89. grass.fatal(_('Column <%s> not found') % column)
  90. if qcolumn:
  91. if value:
  92. grass.fatal(_('value= and qcolumn= are mutually exclusive'))
  93. # special case: we copy from another column
  94. value = qcolumn
  95. else:
  96. if not value:
  97. grass.fatal(_('Either value= or qcolumn= must be given'))
  98. # we insert a value
  99. if coltype.upper() not in ["INTEGER", "DOUBLE PRECISION"]:
  100. value = "'%s'" % value
  101. cmd = "UPDATE %s SET %s=%s" % (table, column, value)
  102. if where:
  103. cmd += " WHERE " + where
  104. grass.verbose("SQL: \"%s\"" % cmd)
  105. grass.write_command('db.execute', input = '-', database = database, driver = driver, stdin = cmd)
  106. # write cmd history:
  107. grass.vector_history(map)
  108. if __name__ == "__main__":
  109. options, flags = grass.parser()
  110. main()