g.gui.tplot.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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: title
  59. #% label: Title for plot
  60. #% description: The title for the output plot
  61. #% required: no
  62. #%end
  63. #%option
  64. #% key: xlabel
  65. #% label: Label for x axis
  66. #% description: The x axis label for the output plot
  67. #% required: no
  68. #%end
  69. #%option
  70. #% key: ylabel
  71. #% label: Label for y axis
  72. #% description: The y axis label for the output plot
  73. #% required: no
  74. #%end
  75. #%option
  76. #% key: size
  77. #% type: string
  78. #% label: The size for output image
  79. #% description: It works only with output parameter
  80. #% required: no
  81. #%end
  82. import grass.script as gscript
  83. def main():
  84. options, flags = gscript.parser()
  85. import wx
  86. from grass.script.setup import set_gui_path
  87. set_gui_path()
  88. from core.utils import _
  89. from core.giface import StandaloneGrassInterface
  90. try:
  91. from tplot.frame import TplotFrame
  92. except ImportError as e:
  93. gscript.fatal(e.message)
  94. rasters = None
  95. if options['strds']:
  96. rasters = options['strds'].strip().split(',')
  97. coords = None
  98. if options['coordinates']:
  99. coords = options['coordinates'].strip().split(',')
  100. cats = None
  101. if options['cats']:
  102. cats = options['cats']
  103. output = options['output']
  104. vectors = None
  105. attr = None
  106. if options['stvds']:
  107. vectors = options['stvds'].strip().split(',')
  108. if not options['attr']:
  109. gscript.fatal(_("With stvds you have to set 'attr' option"))
  110. else:
  111. attr = options['attr']
  112. if coords and cats:
  113. gscript.fatal(_("With stvds it is not possible to use 'coordinates' "
  114. "and 'cats' options together"))
  115. elif not coords and not cats:
  116. gscript.warning(_("With stvds you have to use 'coordinates' or "
  117. "'cats' option"))
  118. title = None
  119. if options['title']:
  120. title = options['title']
  121. xlabel = None
  122. if options['xlabel']:
  123. xlabel = options['xlabel']
  124. ylabel = None
  125. if options['ylabel']:
  126. ylabel = options['ylabel']
  127. app = wx.App()
  128. frame = TplotFrame(parent=None, giface=StandaloneGrassInterface())
  129. frame.SetDatasets(rasters, vectors, coords, cats, attr, title, xlabel,
  130. ylabel)
  131. if output:
  132. frame.OnRedraw()
  133. if options['size']:
  134. sizes = options['size'].strip().split(',')
  135. sizes = [int(s) for s in sizes]
  136. frame.canvas.SetSize(sizes)
  137. frame.canvas.figure.savefig(output)
  138. else:
  139. frame.Show()
  140. app.MainLoop()
  141. if __name__ == '__main__':
  142. main()