grasspythonlib.dox 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*! \page pythonlib GRASS 7 Python library
  2. by GRASS Development Team
  3. http://grass.osgeo.org
  4. See code in: grass.py
  5. \section scripting GRASS scripting tasks for Python provided by "grass.py".
  6. Usage: "import grass"
  7. \section make_command
  8. \code
  9. def make_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, **options):
  10. \endcode
  11. Return a list of strings suitable for use as the args parameter to
  12. Popen() or call(). Example:
  13. \code
  14. >>> grass.make_command("g.message", flags = 'w', message = 'this is a warning')
  15. ['g.message', '-w', 'message=this is a warning']
  16. \endcode
  17. \section start_command
  18. \code
  19. def start_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, **kwargs):
  20. \endcode
  21. Returns a Popen object with the command created by make_command.
  22. Accepts any of the arguments which Popen() accepts apart from "args"
  23. and "shell". Example:
  24. \code
  25. >>> p = grass.start_command("g.gisenv", stdout = subprocess.PIPE)
  26. >>> print p
  27. <subprocess.Popen object at 0xb7c12f6c>
  28. >>> print p.communicate()[0]
  29. GISDBASE='/opt/grass-data';
  30. LOCATION_NAME='spearfish57';
  31. MAPSET='glynn';
  32. GRASS_DB_ENCODING='ascii';
  33. GRASS_GUI='text';
  34. MONITOR='x0';
  35. \endcode
  36. \section pipe_command
  37. \code
  38. def pipe_command(*args, **kwargs):
  39. \endcode
  40. Passes all arguments to start_command, but also adds
  41. "stdout = subprocess.PIPE". Returns the Popen object. Example:
  42. \code
  43. >>> p = grass.pipe_command("g.gisenv")
  44. >>> print p
  45. <subprocess.Popen object at 0xb7c12f6c>
  46. >>> print p.communicate()[0]
  47. GISDBASE='/opt/grass-data';
  48. LOCATION_NAME='spearfish57';
  49. MAPSET='glynn';
  50. GRASS_DB_ENCODING='ascii';
  51. GRASS_GUI='text';
  52. MONITOR='x0';
  53. \endcode
  54. \section run_command
  55. \code
  56. def run_command(*args, **kwargs):
  57. \endcode
  58. Passes all arguments to start_command, then waits for the process to
  59. complete, returning its exit code. Similar to subprocess.call(), but
  60. with the make_command() interface.
  61. \section read_command
  62. \code
  63. def read_command(*args, **kwargs):
  64. \endcode
  65. Passes all arguments to start_command, then waits for the process to
  66. complete, returning its stdout (i.e. similar to shell "backticks").
  67. \section messages
  68. \code
  69. def message(msg, flag = None):
  70. def debug(msg):
  71. def verbose(msg):
  72. def info(msg):
  73. def warning(msg):
  74. def error(msg):
  75. \endcode
  76. These all run g.message, differing only in which flag (if any) is used.
  77. \section fatal
  78. \code
  79. def fatal(msg):
  80. \endcode
  81. Like error(), but also calls sys.exit(1).
  82. \section parser
  83. \code
  84. def parser():
  85. \endcode
  86. Interface to g.parser, intended to be run from the top-level, e.g.:
  87. \code
  88. if __name__ == "__main__":
  89. options, flags = grass.parser()
  90. main()
  91. \endcode
  92. Thereafter, the global variables "options" and "flags" will be
  93. dictionaries containing option/flag values, keyed by lower-case
  94. option/flag names. The values in "options" are strings, those in
  95. "flags" are Python booleans.
  96. \section tempfile
  97. \code
  98. def tempfile():
  99. \endcode
  100. Returns the name of a temporary file, created with g.tempfile.
  101. \section gisenv
  102. \code
  103. def gisenv():
  104. \endcode
  105. Returns the output from running g.gisenv (with no arguments), as a
  106. dictionary. Example:
  107. \code
  108. >>> env = grass.gisenv()
  109. >>> print env['GISDBASE']
  110. /opt/grass-data
  111. \endcode
  112. \section region
  113. \code
  114. def region():
  115. \endcode
  116. Returns the output from running "g.region -g", as a dictionary.
  117. Example:
  118. \code
  119. >>> region = grass.region()
  120. >>> [region[key] for key in "nsew"]
  121. ['4928000', '4914020', '609000', '590010']
  122. >>> (region['nsres'], region['ewres'])
  123. ('30', '30')
  124. \endcode
  125. \section use_temp_region
  126. \code
  127. def use_temp_region():
  128. \endcode
  129. Copies the current region to a temporary region with "g.region save=",
  130. then sets WIND_OVERRIDE to refer to that region. Installs an atexit
  131. handler to delete the temporary region upon termination.
  132. \section del_temp_region
  133. \code
  134. def del_temp_region():
  135. \endcode
  136. Unsets WIND_OVERRIDE and removes any region named by it.
  137. \section find_file
  138. \code
  139. def find_file(name, element = 'cell'):
  140. \endcode
  141. Returns the output from running g.findfile as a dictionary. Example:
  142. \code
  143. >>> result = grass.find_file('fields', element = 'vector')
  144. >>> print result['fullname']
  145. fields@PERMANENT
  146. >>> print result['file']
  147. /opt/grass-data/spearfish60/PERMANENT/vector/fields
  148. \endcode
  149. \section list_grouped
  150. \code
  151. def list_grouped(type):
  152. \endcode
  153. Returns the output from running g.list, as a dictionary where the keys
  154. are mapset names and the values are lists of maps in that mapset.
  155. Example:
  156. \code
  157. >>> grass.list_grouped('rast')['PERMANENT']
  158. ['aspect', 'erosion1', 'quads', 'soils', 'strm.dist', ...
  159. \endcode
  160. \section list_pairs
  161. \code
  162. def list_pairs(type):
  163. Returns the output from running g.list, as a list of (map, mapset)
  164. pairs. Example:
  165. \code
  166. >>> grass.list_pairs('rast')
  167. [('aspect', 'PERMANENT'), ('erosion1', 'PERMANENT'), ('quads', 'PERMANENT'), ...
  168. \endcode
  169. \section list_strings
  170. \code
  171. def list_strings(type):
  172. \endcode
  173. Returns the output from running g.list, as a list of qualified names.
  174. Example:
  175. \code
  176. >>> grass.list_strings('rast')
  177. ['aspect@PERMANENT', 'erosion1@PERMANENT', 'quads@PERMANENT', 'soils@PERMANENT', ...
  178. \endcode
  179. \section parse_color
  180. \code
  181. def parse_color(val, dflt = None):
  182. \endcode
  183. Parses the string "val" as a GRASS colour, which can be either one of
  184. the named colours or an R:G:B tuple e.g. 255:255:255. Returns an
  185. (r,g,b) triple whose components are floating point values between 0
  186. and 1. Example:
  187. \code
  188. >>> grass.parse_color("red")
  189. (1.0, 0.0, 0.0)
  190. >>> grass.parse_color("255:0:0")
  191. (1.0, 0.0, 0.0)
  192. \endcode
  193. */