raster3d.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """!@package grass.script.raster3d
  2. @brief GRASS Python scripting module (raster3d functions)
  3. Raster3d related functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. from grass.script import raster3d as grass
  7. grass.raster3d_info(map)
  8. ...
  9. @endcode
  10. (C) 2008-2009 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Glynn Clements
  15. @author Martin Landa <landa.martin gmail.com>
  16. @author Soeren Gebbert <soeren.gebbert gmail.com>
  17. """
  18. import os
  19. import string
  20. from core import *
  21. from utils import float_or_dms, parse_key_val
  22. # add raster history
  23. # run "r3.info -rstgip ..." and parse output
  24. def raster3d_info(map):
  25. """!Return information about a raster3d map (interface to
  26. `r3.info'). Example:
  27. \code
  28. >>> grass.raster3d_info('volume')
  29. {'tiledimz': 1, 'tbres': 1.0, 'tiledimx': 27, 'tiledimy': 16, 'north': 60.490001999999997, 'tilenumy': 1, 'tilenumz': 1,
  30. 'min': 1.0, 'datatype': '"DCELL"', 'max': 1.0, 'top': 0.5, 'bottom': -0.5, 'west': -3.2200000000000002, 'tilenumx': 1,
  31. 'ewres': 0.98222219, 'east': 23.299999, 'nsres': 0.99937511999999995, 'Timestamp': '"none"', 'south': 44.5}
  32. \endcode
  33. @param map map name
  34. @return parsed raster3d info
  35. """
  36. def float_or_null(s):
  37. if s == 'NULL':
  38. return None
  39. else:
  40. return float(s)
  41. s = read_command('r3.info', flags='rg', map=map)
  42. kv = parse_key_val(s)
  43. for k in ['min', 'max']:
  44. kv[k] = float_or_null(kv[k])
  45. for k in ['north', 'south', 'east', 'west', 'top', 'bottom']:
  46. kv[k] = float(kv[k])
  47. for k in ['nsres', 'ewres', 'tbres']:
  48. kv[k] = float_or_dms(kv[k])
  49. for k in ['rows', 'cols', 'depths']:
  50. kv[k] = int(kv[k])
  51. for k in ['tilenumx', 'tilenumy', 'tilenumz']:
  52. kv[k] = int(kv[k])
  53. for k in ['tiledimx', 'tiledimy', 'tiledimz']:
  54. kv[k] = int(kv[k])
  55. return kv
  56. # interface to r3.mapcalc
  57. def mapcalc3d(exp, quiet = False, verbose = False, overwrite = False, **kwargs):
  58. """!Interface to r3.mapcalc.
  59. @param exp expression
  60. @param quiet True to run quietly (<tt>--q</tt>)
  61. @param verbose True to run verbosely (<tt>--v</tt>)
  62. @param overwrite True to enable overwriting the output (<tt>--o</tt>)
  63. @param kwargs
  64. """
  65. t = string.Template(exp)
  66. e = t.substitute(**kwargs)
  67. if run_command('r3.mapcalc', expression = e,
  68. quiet = quiet,
  69. verbose = verbose,
  70. overwrite = overwrite) != 0:
  71. fatal(_("An error occurred while running r3.mapcalc"))