raster3d.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 os
  17. import time
  18. import string
  19. from .core import read_command, write_command, fatal
  20. from .utils import float_or_dms, parse_key_val
  21. from grass.exceptions import CalledModuleError
  22. def raster3d_info(map, env=None):
  23. """Return information about a raster3d map (interface to `r3.info`).
  24. Example:
  25. >>> mapcalc3d('volume = row() + col() + depth()')
  26. >>> raster3d_info('volume') # doctest: +ELLIPSIS
  27. {'vertical_units': '"units"', 'tbres': 1.0, ... 'south': 185000.0}
  28. >>> run_command('g.remove', flags='f', type='raster_3d', name='volume')
  29. 0
  30. :param str map: map name
  31. :param env: environment
  32. :return: parsed raster3d info
  33. """
  34. def float_or_null(s):
  35. if s == "NULL":
  36. return None
  37. else:
  38. return float(s)
  39. s = read_command("r3.info", flags="rg", map=map, env=env)
  40. kv = parse_key_val(s)
  41. for k in ["min", "max"]:
  42. kv[k] = float_or_null(kv[k])
  43. for k in ["north", "south", "east", "west", "top", "bottom"]:
  44. kv[k] = float(kv[k])
  45. for k in ["nsres", "ewres", "tbres"]:
  46. kv[k] = float_or_dms(kv[k])
  47. for k in ["rows", "cols", "depths"]:
  48. kv[k] = int(kv[k])
  49. for k in ["tilenumx", "tilenumy", "tilenumz"]:
  50. kv[k] = int(kv[k])
  51. for k in ["tiledimx", "tiledimy", "tiledimz"]:
  52. kv[k] = int(kv[k])
  53. return kv
  54. def mapcalc3d(
  55. exp, quiet=False, verbose=False, overwrite=False, seed=None, env=None, **kwargs
  56. ):
  57. """Interface to r3.mapcalc.
  58. :param str exp: expression
  59. :param bool quiet: True to run quietly (<tt>--q</tt>)
  60. :param bool verbose: True to run verbosely (<tt>--v</tt>)
  61. :param bool overwrite: True to enable overwriting the output (<tt>--o</tt>)
  62. :param seed: an integer used to seed the random-number generator for the
  63. rand() function, or 'auto' to generate a random seed
  64. :param dict env: dictionary of environment variables for child process
  65. :param kwargs:
  66. """
  67. if seed == "auto":
  68. seed = hash((os.getpid(), time.time())) % (2**32)
  69. t = string.Template(exp)
  70. e = t.substitute(**kwargs)
  71. try:
  72. write_command(
  73. "r3.mapcalc",
  74. file="-",
  75. stdin=e,
  76. env=env,
  77. seed=seed,
  78. quiet=quiet,
  79. verbose=verbose,
  80. overwrite=overwrite,
  81. )
  82. except CalledModuleError:
  83. fatal(
  84. _("An error occurred while running r3.mapcalc" " with expression: %s") % e
  85. )