raster.py 2.2 KB

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