raster3d.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, env=None):
  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. :param env: environment
  30. :return: parsed raster3d info
  31. """
  32. def float_or_null(s):
  33. if s == 'NULL':
  34. return None
  35. else:
  36. return float(s)
  37. s = read_command('r3.info', flags='rg', map=map, env=env)
  38. kv = parse_key_val(s)
  39. for k in ['min', 'max']:
  40. kv[k] = float_or_null(kv[k])
  41. for k in ['north', 'south', 'east', 'west', 'top', 'bottom']:
  42. kv[k] = float(kv[k])
  43. for k in ['nsres', 'ewres', 'tbres']:
  44. kv[k] = float_or_dms(kv[k])
  45. for k in ['rows', 'cols', 'depths']:
  46. kv[k] = int(kv[k])
  47. for k in ['tilenumx', 'tilenumy', 'tilenumz']:
  48. kv[k] = int(kv[k])
  49. for k in ['tiledimx', 'tiledimy', 'tiledimz']:
  50. kv[k] = int(kv[k])
  51. return kv
  52. def mapcalc3d(exp, quiet=False, verbose=False, overwrite=False,
  53. seed=None, env=None, **kwargs):
  54. """Interface to r3.mapcalc.
  55. :param str exp: expression
  56. :param bool quiet: True to run quietly (<tt>--q</tt>)
  57. :param bool verbose: True to run verbosely (<tt>--v</tt>)
  58. :param bool overwrite: True to enable overwriting the output (<tt>--o</tt>)
  59. :param seed: an integer used to seed the random-number generator for the
  60. rand() function, or 'auto' to generate a random seed
  61. :param dict env: dictionary of environment variables for child process
  62. :param kwargs:
  63. """
  64. if seed == 'auto':
  65. seed = hash((os.getpid(), time.time())) % (2**32)
  66. t = string.Template(exp)
  67. e = t.substitute(**kwargs)
  68. try:
  69. write_command('r3.mapcalc', file='-', stdin=e, env=env, seed=seed,
  70. quiet=quiet, verbose=verbose, overwrite=overwrite)
  71. except CalledModuleError:
  72. fatal(_("An error occurred while running r3.mapcalc"
  73. " with expression: %s") % e)