raster3d.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """
  2. Raster3d related functions to be used in Python scripts.
  3. Usage:
  4. ::
  5. from grass.script import raster3d as grass
  6. grass.raster3d_info(map)
  7. (C) 2008-2009 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. .. sectionauthor:: Glynn Clements
  12. .. sectionauthor:: Martin Landa <landa.martin gmail.com>
  13. .. sectionauthor:: Soeren Gebbert <soeren.gebbert gmail.com>
  14. """
  15. import string
  16. from core import *
  17. from utils import float_or_dms, parse_key_val
  18. from grass.exceptions import CalledModuleError
  19. def raster3d_info(map):
  20. """Return information about a raster3d map (interface to `r3.info`).
  21. Example:
  22. >>> mapcalc3d('volume = row() + col() + depth()')
  23. >>> raster3d_info('volume') # doctest: +ELLIPSIS
  24. {'vertical_units': '"units"', 'tbres': 1.0, ... 'south': 185000.0}
  25. >>> run_command('g.remove', flags='f', type='rast3d', name='volume')
  26. 0
  27. :param str map: map name
  28. :return: parsed raster3d info
  29. """
  30. def float_or_null(s):
  31. if s == 'NULL':
  32. return None
  33. else:
  34. return float(s)
  35. s = read_command('r3.info', flags='rg', map=map)
  36. kv = parse_key_val(s)
  37. for k in ['min', 'max']:
  38. kv[k] = float_or_null(kv[k])
  39. for k in ['north', 'south', 'east', 'west', 'top', 'bottom']:
  40. kv[k] = float(kv[k])
  41. for k in ['nsres', 'ewres', 'tbres']:
  42. kv[k] = float_or_dms(kv[k])
  43. for k in ['rows', 'cols', 'depths']:
  44. kv[k] = int(kv[k])
  45. for k in ['tilenumx', 'tilenumy', 'tilenumz']:
  46. kv[k] = int(kv[k])
  47. for k in ['tiledimx', 'tiledimy', 'tiledimz']:
  48. kv[k] = int(kv[k])
  49. return kv
  50. def mapcalc3d(exp, quiet=False, verbose=False, overwrite=False, **kwargs):
  51. """Interface to r3.mapcalc.
  52. :param str exp: expression
  53. :param bool quiet: True to run quietly (<tt>--q</tt>)
  54. :param bool verbose: True to run verbosely (<tt>--v</tt>)
  55. :param bool overwrite: True to enable overwriting the output (<tt>--o</tt>)
  56. :param kwargs:
  57. """
  58. t = string.Template(exp)
  59. e = t.substitute(**kwargs)
  60. try:
  61. run_command('r3.mapcalc', expression=e,
  62. quiet=quiet,
  63. verbose=verbose,
  64. overwrite=overwrite)
  65. except CalledModuleError:
  66. fatal(_("An error occurred while running r3.mapcalc"))