r.out.xyz.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.out.xyz
  5. # AUTHOR: M. Hamish Bowman, Dept. Marine Science, Otago University,
  6. # New Zealand
  7. # Converted to Python by Glynn Clements
  8. # PURPOSE: Export a raster map as x,y,z values based on cell centers
  9. # This is a simple wrapper script for "r.stats -1ng"
  10. #
  11. # COPYRIGHT: (c) 2006 Hamish Bowman, and the GRASS Development Team
  12. # (c) 2008 Glynn Clements, and the GRASS Development Team
  13. # This program is free software under the GNU General Public
  14. # License (>=v2). Read the file COPYING that comes with GRASS
  15. # for details.
  16. #
  17. #############################################################################
  18. #%module
  19. #% description: Exports a raster map to a text file as x,y,z values based on cell centers.
  20. #% keyword: raster
  21. #% keyword: export
  22. #% keyword: ASCII
  23. #% keyword: conversion
  24. #%end
  25. #%option G_OPT_R_INPUTS
  26. #% multiple: yes
  27. #%end
  28. #%option G_OPT_F_OUTPUT
  29. #% description: Name for output file (if omitted or "-" output to stdout)
  30. #% required: no
  31. #%end
  32. #%option G_OPT_F_SEP
  33. #%end
  34. #%flag
  35. #% key: i
  36. #% description: Include no data values
  37. #%end
  38. import sys
  39. from grass.script import core as grass
  40. from grass.exceptions import CalledModuleError
  41. def main():
  42. # if no output filename, output to stdout
  43. output = options['output']
  44. donodata = flags['i']
  45. if donodata:
  46. statsflags="1g"
  47. else:
  48. statsflags="1gn"
  49. parameters = dict(flags=statsflags,
  50. input=options['input'],
  51. separator=options['separator'])
  52. if output:
  53. parameters.update(output=output)
  54. ret = 0
  55. try:
  56. grass.run_command("r.stats", **parameters)
  57. except CalledModuleError:
  58. ret = 1
  59. sys.exit(ret)
  60. if __name__ == "__main__":
  61. options, flags = grass.parser()
  62. main()