GRASS Programmer's Manual  6.4.2(2012)
raster.py
Go to the documentation of this file.
00001 """!@package grass.script.raster
00002 
00003 @brief GRASS Python scripting module (raster functions)
00004 
00005 Raster related functions to be used in Python scripts.
00006 
00007 Usage:
00008 
00009 @code
00010 from grass.script import raster as grass
00011 
00012 grass.raster_history(map)
00013 ...
00014 @endcode
00015 
00016 (C) 2008-2009 by the GRASS Development Team
00017 This program is free software under the GNU General Public
00018 License (>=v2). Read the file COPYING that comes with GRASS
00019 for details.
00020 
00021 @author Glynn Clements
00022 @author Martin Landa <landa.martin gmail.com>
00023 """
00024 
00025 import os
00026 import string
00027 
00028 from core import *
00029 
00030 # i18N
00031 import gettext
00032 gettext.install('grasslibs', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
00033 
00034 # add raster history
00035 
00036 def raster_history(map):
00037     """!Set the command history for a raster map to the command used to
00038     invoke the script (interface to `r.support').
00039 
00040     @param map map name
00041 
00042     @return True on success
00043     @return False on failure
00044     """
00045     current_mapset = gisenv()['MAPSET']
00046     if find_file(name = map)['mapset'] == current_mapset:
00047         run_command('r.support', map = map, history = os.environ['CMDLINE'])
00048         return True
00049     
00050     warning(_("Unable to write history for <%(map)s>. "
00051               "Raster map <%(map)s> not found in current mapset." % { 'map' : map, 'map' : map}))
00052     return False
00053 
00054 # run "r.info -rgstmpud ..." and parse output
00055 
00056 def raster_info(map):
00057     """!Return information about a raster map (interface to
00058     `r.info'). Example:
00059 
00060     \code
00061     >>> grass.raster_info('elevation')
00062     {'north': 228500.0, 'timestamp': '"none"', 'min': 55.578792572021499,
00063     'datatype': 'FCELL', 'max': 156.32986450195301, 'ewres': 10.0,
00064     'vertical_datum': '', 'west': 630000.0, 'units': '',
00065     'title': 'South-West Wake county: Elevation NED 10m (elev_ned10m)',
00066     'east': 645000.0, 'nsres': 10.0, 'south': 215000.0}
00067     \endcode
00068 
00069     @param map map name
00070     
00071     @return parsed raster info
00072     """
00073 
00074     def float_or_null(s):
00075         if s == 'NULL':
00076             return None
00077         else:
00078             return float(s)
00079 
00080     s = read_command('r.info', flags = 'rgstmpud', map = map)
00081     kv = parse_key_val(s)
00082     for k in ['min', 'max']:
00083         kv[k] = float_or_null(kv[k])
00084     for k in ['north', 'south', 'east', 'west']:
00085         kv[k] = float(kv[k])
00086     for k in ['nsres', 'ewres']:
00087         kv[k] = float_or_dms(kv[k])
00088     return kv
00089 
00090 # interface to r.mapcalc
00091 
00092 def mapcalc(exp, quiet = False, verbose = False, overwrite = False, **kwargs):
00093     """!Interface to r.mapcalc.
00094 
00095     @param exp expression
00096     @param kwargs
00097     """
00098     t = string.Template(exp)
00099     e = t.substitute(**kwargs)
00100 
00101     env = os.environ.copy()
00102     if quiet:
00103         env['GRASS_VERBOSE'] = '0'
00104     if verbose:
00105         env['GRASS_VERBOSE'] = '3'
00106     if overwrite:
00107         env['GRASS_OVERWRITE'] = '1'
00108 
00109     if write_command('r.mapcalc', stdin = e, env = env) != 0:
00110         fatal(_("An error occurred while running r.mapcalc"))
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines