grasspythonlib.dox 5.7 KB

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