raster3d.py 2.9 KB

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