raster3d.py 2.4 KB

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