raster.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. @package grass.script.raster
  3. @brief GRASS Python scripting module
  4. Raster related functions to be used in Python scripts.
  5. Usage:
  6. @code
  7. from grass.script import core, raster as grass
  8. grass.parser()
  9. grass.raster_history(map)
  10. ...
  11. @endcode
  12. (C) 2008-2009 by the GRASS Development Team
  13. This program is free software under the GNU General Public
  14. License (>=v2). Read the file COPYING that comes with GRASS
  15. for details.
  16. @author Glynn Clements
  17. @author Martin Landa <landa.martin gmail.com>
  18. """
  19. import os
  20. import string
  21. from core import *
  22. # add raster history
  23. def raster_history(map):
  24. """Set the command history for a raster map to the command used to
  25. invoke the script (interface to `r.support').
  26. @return True on success
  27. @return False on failure
  28. """
  29. current_mapset = gisenv()['MAPSET']
  30. if find_file(name = map)['mapset'] == current_mapset:
  31. run_command('r.support', map = map, history = os.environ['CMDLINE'])
  32. return True
  33. warning("Unable to write history for <%s>. Raster map <%s> not found in current mapset." % (map, map))
  34. return False
  35. # run "r.info -rgstmpud ..." and parse output
  36. def raster_info(map):
  37. """Return information about a raster map (interface to `r.info')."""
  38. s = read_command('r.info', flags = 'rgstmpud', map = map)
  39. kv = parse_key_val(s)
  40. for k in ['min', 'max', 'north', 'south', 'east', 'west']:
  41. kv[k] = float(kv[k])
  42. for k in ['nsres', 'ewres']:
  43. kv[k] = float_or_dms(kv[k])
  44. return kv
  45. # interface to r.mapcalc
  46. def mapcalc(exp, **kwargs):
  47. t = string.Template(exp)
  48. e = t.substitute(**kwargs)
  49. if run_command('r.mapcalc', expression = e) != 0:
  50. fatal("An error occurred while running r.mapcalc")