g.gui.tplot.py 5.0 KB

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