r.out.xyz.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  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: output
  23. # % keyword: ASCII
  24. # % keyword: conversion
  25. # %end
  26. # %option G_OPT_R_INPUTS
  27. # % multiple: yes
  28. # %end
  29. # %option G_OPT_F_OUTPUT
  30. # % description: Name for output file (if omitted or "-" output to stdout)
  31. # % required: no
  32. # %end
  33. # %option G_OPT_F_SEP
  34. # %end
  35. # %flag
  36. # % key: i
  37. # % description: Include no data values
  38. # %end
  39. import sys
  40. from grass.script import core as grass
  41. from grass.exceptions import CalledModuleError
  42. def main():
  43. # if no output filename, output to stdout
  44. output = options["output"]
  45. donodata = flags["i"]
  46. if donodata:
  47. statsflags = "1g"
  48. else:
  49. statsflags = "1gn"
  50. parameters = dict(
  51. flags=statsflags, input=options["input"], separator=options["separator"]
  52. )
  53. if output:
  54. parameters.update(output=output)
  55. ret = 0
  56. try:
  57. grass.run_command("r.stats", **parameters)
  58. except CalledModuleError:
  59. ret = 1
  60. sys.exit(ret)
  61. if __name__ == "__main__":
  62. options, flags = grass.parser()
  63. main()