1
"""!@package grass.script.raster
3
@brief GRASS Python scripting module (raster functions)
5
Raster related functions to be used in Python scripts.
10
from grass.script import raster as grass
12
grass.raster_history(map)
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
21
@author Glynn Clements
22
@author Martin Landa <landa.martin gmail.com>
32
gettext.install('grasslibs', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
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').
42
@return True on success
43
@return False on failure
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'])
50
warning(_("Unable to write history for <%(map)s>. "
51
"Raster map <%(map)s> not found in current mapset." % { 'map' : map, 'map' : map}))
54
# run "r.info -rgstmpud ..." and parse output
57
"""!Return information about a raster map (interface to
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}
71
@return parsed raster info
80
s = read_command('r.info', flags = 'rgstmpud', map = map)
82
for k in ['min', 'max']:
83
kv[k] = float_or_null(kv[k])
84
for k in ['north', 'south', 'east', 'west']:
86
for k in ['nsres', 'ewres']:
87
kv[k] = float_or_dms(kv[k])
90
# interface to r.mapcalc
92
def mapcalc(exp, quiet = False, verbose = False, overwrite = False, **kwargs):
93
"""!Interface to r.mapcalc.
98
t = string.Template(exp)
99
e = t.substitute(**kwargs)
101
env = os.environ.copy()
103
env['GRASS_VERBOSE'] = '0'
105
env['GRASS_VERBOSE'] = '3'
107
env['GRASS_OVERWRITE'] = '1'
109
if write_command('r.mapcalc', stdin = e, env = env) != 0:
110
fatal(_("An error occurred while running r.mapcalc"))
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.
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.
123
>>> # wait for them to finish:
128
@param exp expression
133
t = string.Template(exp)
134
e = t.substitute(**kwargs)
136
env = os.environ.copy()
138
env['GRASS_VERBOSE'] = '0'
140
env['GRASS_VERBOSE'] = '3'
142
env['GRASS_OVERWRITE'] = '1'
145
p = feed_command('r.mapcalc', env = env, **kwargs)