vector.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """
  2. @package grass.script.vector
  3. @brief GRASS Python scripting module
  4. Vector related functions to be used in Python scripts.
  5. Usage:
  6. @code
  7. from grass.script import core, vector as grass
  8. grass.parser()
  9. grass.vector_db(map)
  10. ...
  11. @endcode
  12. (C) 2008-2009 by the GRASS Development Team
  13. This program is free software under the GNU General Public
  14. License (>=v2). Read the file COPYING that comes with GRASS
  15. for details.
  16. @author Glynn Clements
  17. @author Martin Landa <landa.martin gmail.com>
  18. """
  19. import os
  20. from core import *
  21. # run "v.db.connect -g ..." and parse output
  22. def vector_db(map, **args):
  23. """Return the database connection details for a vector map
  24. (interface to `v.db.connect -g').
  25. @param map vector map
  26. @param args
  27. @return dictionary { layer : { 'layer', 'table, 'database', 'driver', 'key' }
  28. """
  29. s = read_command('v.db.connect', flags = 'g', map = map, fs = ';', **args)
  30. result = {}
  31. for l in s.splitlines():
  32. f = l.split(';')
  33. if len(f) != 5:
  34. continue
  35. if '/' in f[0]:
  36. f1 = f[0].split('/')
  37. layer = f1[0]
  38. name = f1[1]
  39. else:
  40. layer = f[0]
  41. name = ''
  42. result[int(layer)] = {
  43. 'layer' : layer,
  44. 'name' : name,
  45. 'table' : f[1],
  46. 'key' : f[2],
  47. 'database' : f[3],
  48. 'driver' : f[4] }
  49. return result
  50. def vector_layer_db(map, layer):
  51. """Return the database connection details for a vector map layer.
  52. If db connection for given layer is not defined, fatal() is called.
  53. @param map map name
  54. @param layer layer number
  55. @return parsed output
  56. """
  57. try:
  58. f = vector_db(map)[int(layer)]
  59. except KeyError:
  60. grass.fatal("Database connection not defined for layer %s" % layer)
  61. return f
  62. # run "v.info -c ..." and parse output
  63. def vector_columns(map, layer = None, **args):
  64. """Return a dictionary of the columns for the database table connected to
  65. a vector map (interface to `v.info -c').
  66. @param map map name
  67. @param layer layer number (None for all layers)
  68. @param args
  69. @return parsed output
  70. """
  71. s = read_command('v.info', flags = 'c', map = map, layer = layer, quiet = True, **args)
  72. result = {}
  73. for line in s.splitlines():
  74. f = line.split('|')
  75. if len(f) == 2:
  76. result[f[1]] = f[0]
  77. return result
  78. # add vector history
  79. def vector_history(map):
  80. """Set the command history for a vector map to the command used to
  81. invoke the script (interface to `v.support').
  82. @param map mapname
  83. @return v.support output
  84. """
  85. run_command('v.support', map = map, cmdhist = os.environ['CMDLINE'])
  86. # run "v.info -t" and parse output
  87. def vector_info_topo(map):
  88. """Return information about a vector map (interface to `v.info -t').
  89. @param map map name
  90. @return parsed output
  91. """
  92. s = read_command('v.info', flags = 't', map = map)
  93. return parse_key_val(s, val_type = int)