g.gui.tplot.py 4.7 KB

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