v.db.update.py 3.3 KB

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