raster3d.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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-2016 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,
  52. seed=None, env=None, **kwargs):
  53. """Interface to r3.mapcalc.
  54. :param str exp: expression
  55. :param bool quiet: True to run quietly (<tt>--q</tt>)
  56. :param bool verbose: True to run verbosely (<tt>--v</tt>)
  57. :param bool overwrite: True to enable overwriting the output (<tt>--o</tt>)
  58. :param seed: an integer used to seed the random-number generator for the
  59. rand() function, or 'auto' to generate a random seed
  60. :param dict env: dictionary of environment variables for child process
  61. :param kwargs:
  62. """
  63. if seed == 'auto':
  64. seed = hash((os.getpid(), time.time())) % (2**32)
  65. t = string.Template(exp)
  66. e = t.substitute(**kwargs)
  67. try:
  68. write_command('r3.mapcalc', file='-', stdin=e, env=env, seed=seed,
  69. quiet=quiet, verbose=verbose, overwrite=overwrite)
  70. except CalledModuleError:
  71. fatal(_("An error occurred while running r3.mapcalc"
  72. " with expression: %s") % e)