raster3d.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. # add raster history
  22. # run "r3.info -rstgip ..." and parse output
  23. def raster3d_info(map):
  24. """!Return information about a raster3d map (interface to
  25. `r3.info'). Example:
  26. \code
  27. >>> grass.raster3d_info('volume')
  28. {'tiledimz': 1, 'tbres': 1.0, 'tiledimx': 27, 'tiledimy': 16, 'north': 60.490001999999997, 'tilenumy': 1, 'tilenumz': 1,
  29. 'min': 1.0, 'datatype': '"DCELL"', 'max': 1.0, 'top': 0.5, 'bottom': -0.5, 'west': -3.2200000000000002, 'tilenumx': 1,
  30. 'ewres': 0.98222219, 'east': 23.299999, 'nsres': 0.99937511999999995, 'Timestamp': '"none"', 'south': 44.5}
  31. \endcode
  32. @param map map name
  33. @return parsed raster3d info
  34. """
  35. def float_or_null(s):
  36. if s == 'NULL':
  37. return None
  38. else:
  39. return float(s)
  40. s = read_command('r3.info', flags='rg', map=map)
  41. kv = parse_key_val(s)
  42. for k in ['min', 'max']:
  43. kv[k] = float_or_null(kv[k])
  44. for k in ['north', 'south', 'east', 'west', 'top', 'bottom']:
  45. kv[k] = float(kv[k])
  46. for k in ['nsres', 'ewres', 'tbres']:
  47. kv[k] = float_or_dms(kv[k])
  48. for k in ['tilenumx', 'tilenumy', 'tilenumz']:
  49. kv[k] = int(kv[k])
  50. for k in ['tiledimx', 'tiledimy', 'tiledimz']:
  51. kv[k] = int(kv[k])
  52. return kv
  53. # interface to r3.mapcalc
  54. def mapcalc3d(exp, quiet = False, verbose = False, overwrite = False, **kwargs):
  55. """!Interface to r3.mapcalc.
  56. @param exp expression
  57. @param quiet True to run quietly (<tt>--q</tt>)
  58. @param verbose True to run verbosely (<tt>--v</tt>)
  59. @param overwrite True to enable overwriting the output (<tt>--o</tt>)
  60. @param kwargs
  61. """
  62. t = string.Template(exp)
  63. e = t.substitute(**kwargs)
  64. if run_command('r3.mapcalc', expression = e,
  65. quiet = quiet,
  66. verbose = verbose,
  67. overwrite = overwrite) != 0:
  68. fatal(_("An error occurred while running r3.mapcalc"))