v.db.update.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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-2014 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. #% keyword: vector
  20. #% keyword: attribute table
  21. #% keyword: database
  22. #% keyword: attribute update
  23. #% keyword: type casting
  24. #%end
  25. #%option G_OPT_V_MAP
  26. #%end
  27. #%option G_OPT_V_FIELD
  28. #% required: yes
  29. #%end
  30. #%option G_OPT_DB_COLUMN
  31. #% key: column
  32. #% description: Name of attribute column to update
  33. #% required: yes
  34. #%end
  35. #%option
  36. #% key: value
  37. #% type: string
  38. #% description: Literal value to update the column with
  39. #% required: no
  40. #%end
  41. #%option G_OPT_DB_COLUMN
  42. #% key: query_column
  43. #% description: Name of other attribute column to query, can be combination of columns (e.g. co1+col2)
  44. #%end
  45. #%option G_OPT_DB_WHERE
  46. #%end
  47. #%option G_OPT_F_INPUT
  48. #% key: sqliteextra
  49. #% description: Name of SQLite extension file for extra functions (SQLite backend only)
  50. #% gisprompt: old,bin,file
  51. #% required: no
  52. #%end
  53. import sys
  54. import os
  55. import grass.script as grass
  56. def main():
  57. vector = options['map']
  58. layer = options['layer']
  59. column = options['column']
  60. value = options['value']
  61. qcolumn = options['query_column']
  62. where = options['where']
  63. sqlitefile = options['sqliteextra']
  64. mapset = grass.gisenv()['MAPSET']
  65. # does map exist in CURRENT mapset?
  66. if not grass.find_file(vector, element='vector', mapset=mapset)['file']:
  67. grass.fatal(_("Vector map <%s> not found in current mapset") % vector)
  68. try:
  69. f = grass.vector_db(vector)[int(layer)]
  70. except KeyError:
  71. grass.fatal(
  72. _('There is no table connected to this map. Run v.db.connect or v.db.addtable first.'))
  73. table = f['table']
  74. database = f['database']
  75. driver = f['driver']
  76. # check for SQLite backend for extra functions
  77. if sqlitefile and driver != "sqlite":
  78. grass.fatal(_("Use of libsqlitefunctions only with SQLite backend"))
  79. if driver == "sqlite" and sqlitefile:
  80. if not os.access(sqlitefile, os.R_OK):
  81. grass.fatal(_("File <%s> not found") % sqlitefile)
  82. # checking column types
  83. try:
  84. coltype = grass.vector_columns(vector, layer)[column]['type']
  85. except KeyError:
  86. grass.fatal(_('Column <%s> not found') % column)
  87. if qcolumn:
  88. if value:
  89. grass.fatal(_('<value> and <qcolumn> are mutually exclusive'))
  90. # special case: we copy from another column
  91. value = qcolumn
  92. else:
  93. if not value:
  94. grass.fatal(_('Either <value> or <qcolumn> must be given'))
  95. # we insert a value
  96. if coltype.upper() not in ["INTEGER", "DOUBLE PRECISION"]:
  97. value = "'%s'" % value
  98. cmd = "UPDATE %s SET %s=%s" % (table, column, value)
  99. if where:
  100. cmd += " WHERE " + where
  101. # SQLite: preload extra functions from extension lib if provided by user
  102. if sqlitefile:
  103. sqliteload = "SELECT load_extension('%s');\n" % sqlitefile
  104. cmd = sqliteload + cmd
  105. grass.verbose("SQL: \"%s\"" % cmd)
  106. grass.write_command('db.execute', input='-', database=database, driver=driver, stdin=cmd)
  107. # write cmd history:
  108. grass.vector_history(vector)
  109. return 0
  110. if __name__ == "__main__":
  111. options, flags = grass.parser()
  112. sys.exit(main())