GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
raster.py
Go to the documentation of this file.
1 """!@package grass.script.raster
2 
3 @brief GRASS Python scripting module (raster functions)
4 
5 Raster related functions to be used in Python scripts.
6 
7 Usage:
8 
9 @code
10 from grass.script import raster as grass
11 
12 grass.raster_history(map)
13 ...
14 @endcode
15 
16 (C) 2008-2009 by the GRASS Development Team
17 This program is free software under the GNU General Public
18 License (>=v2). Read the file COPYING that comes with GRASS
19 for details.
20 
21 @author Glynn Clements
22 @author Martin Landa <landa.martin gmail.com>
23 """
24 
25 import os
26 import string
27 
28 from core import *
29 
30 # i18N
31 import gettext
32 gettext.install('grasslibs', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
33 
34 # add raster history
35 
36 def raster_history(map):
37  """!Set the command history for a raster map to the command used to
38  invoke the script (interface to `r.support').
39 
40  @param map map name
41 
42  @return True on success
43  @return False on failure
44  """
45  current_mapset = gisenv()['MAPSET']
46  if find_file(name = map)['mapset'] == current_mapset:
47  run_command('r.support', map = map, history = os.environ['CMDLINE'])
48  return True
49 
50  warning(_("Unable to write history for <%(map)s>. "
51  "Raster map <%(map)s> not found in current mapset." % { 'map' : map, 'map' : map}))
52  return False
53 
54 # run "r.info -rgstmpud ..." and parse output
55 
56 def raster_info(map):
57  """!Return information about a raster map (interface to
58  `r.info'). Example:
59 
60  \code
61  >>> grass.raster_info('elevation')
62  {'north': 228500.0, 'timestamp': '"none"', 'min': 55.578792572021499,
63  'datatype': 'FCELL', 'max': 156.32986450195301, 'ewres': 10.0,
64  'vertical_datum': '', 'west': 630000.0, 'units': '',
65  'title': 'South-West Wake county: Elevation NED 10m (elev_ned10m)',
66  'east': 645000.0, 'nsres': 10.0, 'south': 215000.0}
67  \endcode
68 
69  @param map map name
70 
71  @return parsed raster info
72  """
73 
74  def float_or_null(s):
75  if s == 'NULL':
76  return None
77  else:
78  return float(s)
79 
80  s = read_command('r.info', flags = 'rgstmpud', map = map)
81  kv = parse_key_val(s)
82  for k in ['min', 'max']:
83  kv[k] = float_or_null(kv[k])
84  for k in ['north', 'south', 'east', 'west']:
85  kv[k] = float(kv[k])
86  for k in ['nsres', 'ewres']:
87  kv[k] = float_or_dms(kv[k])
88  return kv
89 
90 # interface to r.mapcalc
91 
92 def mapcalc(exp, quiet = False, verbose = False, overwrite = False, **kwargs):
93  """!Interface to r.mapcalc.
94 
95  @param exp expression
96  @param kwargs
97  """
98  t = string.Template(exp)
99  e = t.substitute(**kwargs)
100 
101  env = os.environ.copy()
102  if quiet:
103  env['GRASS_VERBOSE'] = '0'
104  if verbose:
105  env['GRASS_VERBOSE'] = '3'
106  if overwrite:
107  env['GRASS_OVERWRITE'] = '1'
108 
109  if write_command('r.mapcalc', stdin = e, env = env) != 0:
110  fatal(_("An error occurred while running r.mapcalc"))
111 
112 
113 def mapcalc_start(exp, quiet = False, verbose = False, overwrite = False, **kwargs):
114  """!Interface to r.mapcalc, doesn't wait for it to finish, returns Popen object.
115 
116  \code
117  >>> expr1 = '"%s" = "%s" * 10' % (output, input)
118  >>> expr2 = '...' # etc.
119  >>> # launch the jobs:
120  >>> p1 = grass.mapcalc_start(expr1)
121  >>> p2 = grass.mapcalc_start(expr2) # etc.
122  ...
123  >>> # wait for them to finish:
124  >>> p1.wait()
125  >>> p2.wait() # etc.
126  \endcode
127 
128  @param exp expression
129  @param kwargs
130 
131  @return Popen object
132  """
133  t = string.Template(exp)
134  e = t.substitute(**kwargs)
135 
136  env = os.environ.copy()
137  if quiet:
138  env['GRASS_VERBOSE'] = '0'
139  if verbose:
140  env['GRASS_VERBOSE'] = '3'
141  if overwrite:
142  env['GRASS_OVERWRITE'] = '1'
143 
144  stdin = e
145  p = feed_command('r.mapcalc', env = env, **kwargs)
146  p.stdin.write(stdin)
147  p.stdin.close()
148 
149  return p
def float_or_dms
Convert DMS to float.
Definition: core.py:1034
def feed_command
Passes all arguments to start_command(), but also adds &quot;stdin = PIPE&quot;.
Definition: core.py:217
def raster_history
Set the command history for a raster map to the command used to invoke the script (interface to `r...
Definition: raster.py:36
def fatal
Display an error message using g.message -e, then abort.
Definition: core.py:383
def parse_key_val
Parse a string into a dictionary, where entries are separated by newlines and the key and value are s...
Definition: core.py:505
def mapcalc_start
Interface to r.mapcalc, doesn&#39;t wait for it to finish, returns Popen object.
Definition: raster.py:113
def write_command
Passes all arguments to feed_command, with the string specified by the &#39;stdin&#39; argument fed to the pr...
Definition: core.py:282
def mapcalc
Interface to r.mapcalc.
Definition: raster.py:92
def warning
Display a warning message using g.message -w
Definition: core.py:363
def run_command
Passes all arguments to start_command(), then waits for the process to complete, returning its exit c...
Definition: core.py:179
def read_command
Passes all arguments to pipe_command, then waits for the process to complete, returning its stdout (i...
Definition: core.py:229
def gisenv
Returns the output from running g.gisenv (with no arguments), as a dictionary.
Definition: core.py:546
def raster_info
Return information about a raster map (interface to `r.info&#39;).
Definition: raster.py:56