g.gui.tplot.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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(
  128. _(
  129. "With stvds it is not possible to use 'coordinates' "
  130. "and 'cats' options together"
  131. )
  132. )
  133. elif not coords and not cats:
  134. gscript.warning(
  135. _("With stvds you have to use 'coordinates' or " "'cats' option")
  136. )
  137. title = None
  138. if options["title"]:
  139. title = options["title"]
  140. xlabel = None
  141. if options["xlabel"]:
  142. xlabel = options["xlabel"]
  143. ylabel = None
  144. if options["ylabel"]:
  145. ylabel = options["ylabel"]
  146. csvfile = None
  147. if options["csv"]:
  148. csvfile = options["csv"]
  149. app = wx.App()
  150. frame = TplotFrame(
  151. parent=None,
  152. giface=StandaloneGrassInterface(),
  153. title=_("Temporal Plot Tool - GRASS GIS"),
  154. )
  155. if flags["l"]:
  156. frame.linRegRaster.SetValue(state=True)
  157. frame.linRegVector.SetValue(state=True)
  158. frame.SetDatasets(
  159. rasters,
  160. vectors,
  161. coords,
  162. cats,
  163. attr,
  164. title,
  165. xlabel,
  166. ylabel,
  167. csvfile,
  168. flags["h"],
  169. gscript.overwrite,
  170. )
  171. if output:
  172. frame.OnRedraw()
  173. if options["size"]:
  174. sizes = options["size"].strip().split(",")
  175. sizes = [int(s) for s in sizes]
  176. frame.canvas.SetSize(sizes)
  177. frame.canvas.figure.savefig(output)
  178. else:
  179. frame.Show()
  180. app.MainLoop()
  181. if __name__ == "__main__":
  182. main()