g.gui.tplot.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env python3
  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. #%flag
  29. #% key: h
  30. #% description: Set the header of CSV file, to be used with csv option
  31. #%end
  32. #%option G_OPT_STVDS_INPUTS
  33. #% key: stvds
  34. #% required: no
  35. #%end
  36. #%option G_OPT_STRDS_INPUTS
  37. #% key: strds
  38. #% required: no
  39. #%end
  40. #%option G_OPT_M_COORDS
  41. #% required: no
  42. #%end
  43. # TODO use option G_OPT_V_CATS
  44. #%option
  45. #% key: cats
  46. #% label: Categories of vectores features
  47. #% description: To use only with stvds
  48. #% required: no
  49. #%end
  50. #%option
  51. #% key: attr
  52. #% label: Name of attribute
  53. #% description: Name of attribute which represent data for plotting
  54. #% required: no
  55. #%end
  56. #%option G_OPT_F_OUTPUT
  57. #% required: no
  58. #% label: Name for output graphical file
  59. #% description: Full path for output file containing the plot, ddd extension to specify format (.png, .pdf, .svg)
  60. #%end
  61. #%option G_OPT_F_OUTPUT
  62. #% key: csv
  63. #% required: no
  64. #% label: Name for output CSV file
  65. #% description: Full path for the CSV file containing the plotted data
  66. #%end
  67. #%option
  68. #% key: title
  69. #% label: Title for plot
  70. #% description: The title for the output plot
  71. #% required: no
  72. #%end
  73. #%option
  74. #% key: xlabel
  75. #% label: Label for x axis
  76. #% description: The x axis label for the output plot
  77. #% required: no
  78. #%end
  79. #%option
  80. #% key: ylabel
  81. #% label: Label for y axis
  82. #% description: The y axis label for the output plot
  83. #% required: no
  84. #%end
  85. #%option
  86. #% key: size
  87. #% type: string
  88. #% label: The size for output image
  89. #% description: It works only with output parameter
  90. #% required: no
  91. #%end
  92. import grass.script as gscript
  93. def main():
  94. options, flags = gscript.parser()
  95. import wx
  96. from grass.script.setup import set_gui_path
  97. set_gui_path()
  98. from core.giface import StandaloneGrassInterface
  99. try:
  100. from tplot.frame import TplotFrame
  101. except ImportError as e:
  102. gscript.fatal(e.message)
  103. rasters = None
  104. if options['strds']:
  105. rasters = options['strds'].strip().split(',')
  106. coords = None
  107. if options['coordinates']:
  108. coords = options['coordinates'].strip().split(',')
  109. cats = None
  110. if options['cats']:
  111. cats = options['cats']
  112. output = options['output']
  113. vectors = None
  114. attr = None
  115. if options['stvds']:
  116. vectors = options['stvds'].strip().split(',')
  117. if not options['attr']:
  118. gscript.fatal(_("With stvds you have to set 'attr' option"))
  119. else:
  120. attr = options['attr']
  121. if coords and cats:
  122. gscript.fatal(_("With stvds it is not possible to use 'coordinates' "
  123. "and 'cats' options together"))
  124. elif not coords and not cats:
  125. gscript.warning(_("With stvds you have to use 'coordinates' or "
  126. "'cats' option"))
  127. title = None
  128. if options['title']:
  129. title = options['title']
  130. xlabel = None
  131. if options['xlabel']:
  132. xlabel = options['xlabel']
  133. ylabel = None
  134. if options['ylabel']:
  135. ylabel = options['ylabel']
  136. csvfile = None
  137. if options['csv']:
  138. csvfile = options['csv']
  139. app = wx.App()
  140. frame = TplotFrame(parent=None, giface=StandaloneGrassInterface())
  141. frame.SetDatasets(rasters, vectors, coords, cats, attr, title, xlabel,
  142. ylabel, csvfile, flags['h'], gscript .overwrite)
  143. if output:
  144. frame.OnRedraw()
  145. if options['size']:
  146. sizes = options['size'].strip().split(',')
  147. sizes = [int(s) for s in sizes]
  148. frame.canvas.SetSize(sizes)
  149. frame.canvas.figure.savefig(output)
  150. else:
  151. frame.Show()
  152. app.MainLoop()
  153. if __name__ == '__main__':
  154. main()