r.out.xyz.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.out.xyz
  5. # AUTHOR: M. Hamish Bowman, Dept. Marine Science, Otago Univeristy,
  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. #% keywords: raster
  21. #% keywords: export
  22. #% keywords: ASCII
  23. #% keywords: 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. import sys
  35. from grass.script import core as grass
  36. from grass.exceptions import CalledModuleError
  37. def main():
  38. # if no output filename, output to stdout
  39. output = options['output']
  40. sep = options['separator']
  41. parameters = dict(flags="1gn",
  42. input=options['input'])
  43. if output:
  44. parameters.update(output=output)
  45. # windows don't like pipe so we don't include it in the command explicitly
  46. if sep != '|':
  47. parameters.update(separator=sep)
  48. ret = 0
  49. try:
  50. grass.run_command("r.stats", **parameters)
  51. except CalledModuleError:
  52. ret = 1
  53. sys.exit(ret)
  54. if __name__ == "__main__":
  55. options, flags = grass.parser()
  56. main()