g.gui.tplot.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: g.gui.tplot.py
  5. # AUTHOR(S): Luca Delucchi
  6. # PURPOSE: Temporal Plot Tool is a wxGUI component (based on matplotlib)
  7. # the user to see in a plot the values of one or more temporal
  8. # datasets for a queried point defined by a coordinate pair.
  9. # COPYRIGHT: (C) 2014-2015 by Luca Delucchi, and the GRASS Development Team
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. ############################################################################
  22. #%module
  23. #% description: Plots the values of temporal datasets.
  24. #% keywords: general
  25. #% keywords: GUI
  26. #% keywords: temporal
  27. #%end
  28. #%option G_OPT_STVDS_INPUTS
  29. #% key: stvds
  30. #% required: no
  31. #%end
  32. #%option G_OPT_STRDS_INPUTS
  33. #% key: strds
  34. #% required: no
  35. #%end
  36. #%option G_OPT_M_COORDS
  37. #% required: no
  38. #%end
  39. #TODO use option G_OPT_V_CATS
  40. #%option
  41. #% key: cats
  42. #% label: Categories of vectores features
  43. #% description: To use only with stvds
  44. #% required: no
  45. #%end
  46. #%option
  47. #% key: attr
  48. #% label: Name of attribute
  49. #% description: Name of attribute which represent data for plotting
  50. #% required: no
  51. #%end
  52. #%option G_OPT_F_OUTPUT
  53. #% required: no
  54. #% label: Name for output file
  55. #% description: Add extension to specify format (.png, .pdf, .svg)
  56. #%end
  57. #%option
  58. #% key: size
  59. #% type: string
  60. #% label: The size for output image
  61. #% description: It works only with output parameter
  62. #% required: no
  63. #%end
  64. import grass.script as gscript
  65. def main():
  66. options, flags = gscript.parser()
  67. import wx
  68. from core.utils import _
  69. from core.giface import StandaloneGrassInterface
  70. try:
  71. from tplot.frame import TplotFrame
  72. except ImportError as e:
  73. gscript.fatal(e.message)
  74. rasters = None
  75. if options['strds']:
  76. rasters = options['strds'].strip().split(',')
  77. coords = None
  78. if options['coordinates']:
  79. coords = options['coordinates'].strip().split(',')
  80. cats = None
  81. if options['cats']:
  82. cats = options['cats']
  83. output = options['output']
  84. vectors = None
  85. attr = None
  86. if options['stvds']:
  87. vectors = options['stvds'].strip().split(',')
  88. if not options['attr']:
  89. gscript.fatal(_("With stvds you have to set 'attr' option"))
  90. else:
  91. attr = options['attr']
  92. if coords and cats:
  93. gscript.fatal(_("With stvds it is not possible to use 'coordinates' "
  94. "and 'cats' options together"))
  95. elif not coords and not cats:
  96. gscript.warning(_("With stvds you have to use 'coordinates' or "
  97. "'cats' option"))
  98. app = wx.App()
  99. frame = TplotFrame(parent=None, giface=StandaloneGrassInterface())
  100. frame.SetDatasets(rasters, vectors, coords, cats, attr)
  101. if output:
  102. frame.OnRedraw()
  103. if options['size']:
  104. sizes = options['size'].strip().split(',')
  105. sizes = [int(s) for s in sizes]
  106. frame.canvas.SetSize(sizes)
  107. if output.split('.')[-1].lower() == 'png':
  108. frame.canvas.print_png(output)
  109. if output.split('.')[-1].lower() in ['jpg', 'jpeg']:
  110. frame.canvas.print_jpg(output)
  111. if output.split('.')[-1].lower() in ['tif', 'tiff']:
  112. frame.canvas.print_tif(output)
  113. else:
  114. frame.Show()
  115. app.MainLoop()
  116. if __name__ == '__main__':
  117. main()