raster3d.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. def raster3d_info(map):
  19. """Return information about a raster3d map (interface to `r3.info`).
  20. Example:
  21. >>> mapcalc3d('volume = row() + col() + depth()')
  22. >>> raster3d_info('volume') # doctest: +ELLIPSIS
  23. {'vertical_units': '"units"', 'tbres': 1.0, ... 'south': 185000.0}
  24. >>> run_command('g.remove', flags='f', type='rast3d', pattern='volume')
  25. 0
  26. :param str map: map name
  27. :return: parsed raster3d info
  28. """
  29. def float_or_null(s):
  30. if s == 'NULL':
  31. return None
  32. else:
  33. return float(s)
  34. s = read_command('r3.info', flags='rg', map=map)
  35. kv = parse_key_val(s)
  36. for k in ['min', 'max']:
  37. kv[k] = float_or_null(kv[k])
  38. for k in ['north', 'south', 'east', 'west', 'top', 'bottom']:
  39. kv[k] = float(kv[k])
  40. for k in ['nsres', 'ewres', 'tbres']:
  41. kv[k] = float_or_dms(kv[k])
  42. for k in ['rows', 'cols', 'depths']:
  43. kv[k] = int(kv[k])
  44. for k in ['tilenumx', 'tilenumy', 'tilenumz']:
  45. kv[k] = int(kv[k])
  46. for k in ['tiledimx', 'tiledimy', 'tiledimz']:
  47. kv[k] = int(kv[k])
  48. return kv
  49. def mapcalc3d(exp, quiet=False, verbose=False, overwrite=False, **kwargs):
  50. """Interface to r3.mapcalc.
  51. :param str exp: expression
  52. :param bool quiet: True to run quietly (<tt>--q</tt>)
  53. :param bool verbose: True to run verbosely (<tt>--v</tt>)
  54. :param bool overwrite: True to enable overwriting the output (<tt>--o</tt>)
  55. :param kwargs:
  56. """
  57. t = string.Template(exp)
  58. e = t.substitute(**kwargs)
  59. if run_command('r3.mapcalc', expression=e,
  60. quiet=quiet,
  61. verbose=verbose,
  62. overwrite=overwrite) != 0:
  63. fatal(_("An error occurred while running r3.mapcalc"))