raster.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """!@package grass.script.raster
  2. @brief GRASS Python scripting module (raster functions)
  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-2011 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 <%(map)s>. "
  33. "Raster map <%(map)s> not found in current mapset." % { 'map' : map, 'map' : map}))
  34. return False
  35. # run "r.info -gre ..." and parse output
  36. def raster_info(map):
  37. """!Return information about a raster map (interface to
  38. `r.info'). Example:
  39. \code
  40. >>> grass.raster_info('elevation')
  41. {'north': 228500.0, 'timestamp': '"none"', 'min': 55.578792572021499,
  42. 'datatype': 'FCELL', 'max': 156.32986450195301, 'ewres': 10.0,
  43. 'vertical_datum': '', 'west': 630000.0, 'units': '',
  44. 'title': 'South-West Wake county: Elevation NED 10m (elev_ned10m)',
  45. 'east': 645000.0, 'nsres': 10.0, 'south': 215000.0}
  46. \endcode
  47. @param map map name
  48. @return parsed raster info
  49. """
  50. def float_or_null(s):
  51. if s == 'NULL':
  52. return None
  53. else:
  54. return float(s)
  55. s = read_command('r.info', flags = 'gre', map = map)
  56. kv = parse_key_val(s)
  57. for k in ['min', 'max']:
  58. kv[k] = float_or_null(kv[k])
  59. for k in ['north', 'south', 'east', 'west']:
  60. kv[k] = float(kv[k])
  61. for k in ['nsres', 'ewres']:
  62. kv[k] = float_or_dms(kv[k])
  63. return kv
  64. # interface to r.mapcalc
  65. def mapcalc(exp, quiet = False, verbose = False, overwrite = False, **kwargs):
  66. """!Interface to r.mapcalc.
  67. @param exp expression
  68. @param quiet True to run quietly (<tt>--q</tt>)
  69. @param verbose True to run verbosely (<tt>--v</tt>)
  70. @param overwrite True to enable overwriting the output (<tt>--o</tt>)
  71. @param kwargs
  72. """
  73. t = string.Template(exp)
  74. e = t.substitute(**kwargs)
  75. if run_command('r.mapcalc', expression = e,
  76. quiet = quiet,
  77. verbose = verbose,
  78. overwrite = overwrite) != 0:
  79. fatal(_("An error occurred while running r.mapcalc"))
  80. def mapcalc_start(exp, quiet = False, verbose = False, overwrite = False, **kwargs):
  81. """!Interface to r.mapcalc, doesn't wait for it to finish, returns Popen object.
  82. \code
  83. >>> expr1 = '"%s" = "%s" * 10' % (output, input)
  84. >>> expr2 = '...' # etc.
  85. >>> # launch the jobs:
  86. >>> p1 = grass.mapcalc_start(expr1)
  87. >>> p2 = grass.mapcalc_start(expr2) # etc.
  88. ...
  89. >>> # wait for them to finish:
  90. >>> p1.wait()
  91. >>> p2.wait() # etc.
  92. \endcode
  93. @param exp expression
  94. @param quiet True to run quietly (<tt>--q</tt>)
  95. @param verbose True to run verbosely (<tt>--v</tt>)
  96. @param overwrite True to enable overwriting the output (<tt>--o</tt>)
  97. @param kwargs
  98. @return Popen object
  99. """
  100. t = string.Template(exp)
  101. e = t.substitute(**kwargs)
  102. return start_command('r.mapcalc', expression = e,
  103. quiet = quiet,
  104. verbose = verbose,
  105. overwrite = overwrite)