base.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. """
  2. @package wxplot.base
  3. @brief Base classes for iinteractive plotting using PyPlot
  4. Classes:
  5. - base::PlotIcons
  6. - base::BasePlotFrame
  7. (C) 2011 by the GRASS Development Team
  8. This program is free software under the GNU General Public License
  9. (>=v2). Read the file COPYING that comes with GRASS for details.
  10. @author Michael Barton, Arizona State University
  11. """
  12. import os
  13. import sys
  14. import wx
  15. from core.globalvar import CheckWxVersion
  16. try:
  17. if CheckWxVersion(version=[3, 0, 0]):
  18. import gui_core.wxlibplot as plot
  19. else:
  20. import wx.lib.plot as plot
  21. except ImportError as e:
  22. print >> sys.stderr, e
  23. from core.globalvar import ICONDIR
  24. from core.settings import UserSettings
  25. from wxplot.dialogs import TextDialog, OptDialog
  26. from core.render import Map
  27. from icons.icon import MetaIcon
  28. from gui_core.toolbars import BaseIcons
  29. from core.utils import _
  30. import grass.script as grass
  31. PlotIcons = {
  32. 'draw' : MetaIcon(img = 'show',
  33. label = _('Draw/re-draw plot')),
  34. 'transect' : MetaIcon(img = 'layer-raster-profile',
  35. label = _('Draw transect in map display window to profile')),
  36. 'options' : MetaIcon(img = 'settings',
  37. label = _('Plot options')),
  38. 'statistics' : MetaIcon(img = 'stats',
  39. label = _('Plot statistics')),
  40. 'save' : MetaIcon(img = 'save',
  41. label = _('Save profile data to CSV file')),
  42. 'quit' : BaseIcons['quit'].SetLabel(_('Quit plot tool')),
  43. }
  44. class BasePlotFrame(wx.Frame):
  45. """Abstract PyPlot display frame class"""
  46. def __init__(self, parent=None, size=wx.Size(700, 400),
  47. style=wx.DEFAULT_FRAME_STYLE, rasterList=[], **kwargs):
  48. wx.Frame.__init__(self, parent, id=wx.ID_ANY, size = size, style = style, **kwargs)
  49. self.parent = parent # MapFrame for a plot type
  50. self.Map = Map() # instance of render.Map to be associated with display
  51. self.rasterList = rasterList #list of rasters to plot
  52. self.raster = {} # dictionary of raster maps and their plotting parameters
  53. self.plottype = ''
  54. self.linestyledict = { 'solid' : wx.SOLID,
  55. 'dot' : wx.DOT,
  56. 'long-dash' : wx.LONG_DASH,
  57. 'short-dash' : wx.SHORT_DASH,
  58. 'dot-dash' : wx.DOT_DASH }
  59. self.ptfilldict = { 'transparent' : wx.TRANSPARENT,
  60. 'solid' : wx.SOLID }
  61. #
  62. # Icon
  63. #
  64. self.SetIcon(wx.Icon(os.path.join(ICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
  65. #
  66. # Add statusbar
  67. #
  68. self.statusbar = self.CreateStatusBar(number = 2, style = 0)
  69. self.statusbar.SetStatusWidths([-2, -1])
  70. #
  71. # Define canvas and settings
  72. #
  73. #
  74. self.client = plot.PlotCanvas(self)
  75. #define the function for drawing pointLabels
  76. self.client.SetPointLabelFunc(self.DrawPointLabel)
  77. # Create mouse event for showing cursor coords in status bar
  78. self.client.canvas.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
  79. # Show closest point when enabled
  80. self.client.canvas.Bind(wx.EVT_MOTION, self.OnMotion)
  81. self.plotlist = [] # list of things to plot
  82. self.plot = None # plot draw object
  83. self.ptitle = "" # title of window
  84. self.xlabel = "" # default X-axis label
  85. self.ylabel = "" # default Y-axis label
  86. self.CentreOnScreen()
  87. self._createColorDict()
  88. def _createColorDict(self):
  89. """Create color dictionary to return wx.Colour tuples
  90. for assigning colors to images in imagery groups"""
  91. self.colorDict = {}
  92. for clr in grass.named_colors.iterkeys():
  93. if clr == 'white': continue
  94. r = grass.named_colors[clr][0] * 255
  95. g = grass.named_colors[clr][1] * 255
  96. b = grass.named_colors[clr][2] * 255
  97. self.colorDict[clr] = (r,g,b,255)
  98. def InitPlotOpts(self, plottype):
  99. """Initialize options for entire plot
  100. """
  101. self.plottype = plottype # histogram, profile, or scatter
  102. self.properties = {} # plot properties
  103. self.properties['font'] = {}
  104. self.properties['font']['prop'] = UserSettings.Get(group = self.plottype, key = 'font')
  105. self.properties['font']['wxfont'] = wx.Font(11, wx.FONTFAMILY_SWISS,
  106. wx.FONTSTYLE_NORMAL,
  107. wx.FONTWEIGHT_NORMAL)
  108. self.properties['raster'] = {}
  109. self.properties['raster'] = UserSettings.Get(group = self.plottype, key = 'raster')
  110. colstr = str(self.properties['raster']['pcolor'])
  111. self.properties['raster']['pcolor'] = tuple(int(colval) for colval in colstr.strip('()').split(','))
  112. if self.plottype == 'profile':
  113. self.properties['marker'] = UserSettings.Get(group = self.plottype, key = 'marker')
  114. # changing color string to tuple for markers/points
  115. colstr = str(self.properties['marker']['color'])
  116. self.properties['marker']['color'] = tuple(int(colval) for colval in colstr.strip('()').split(','))
  117. self.properties['grid'] = UserSettings.Get(group = self.plottype, key = 'grid')
  118. colstr = str(self.properties['grid']['color']) # changing color string to tuple
  119. self.properties['grid']['color'] = tuple(int(colval) for colval in colstr.strip('()').split(','))
  120. self.properties['x-axis'] = {}
  121. self.properties['x-axis']['prop'] = UserSettings.Get(group = self.plottype, key = 'x-axis')
  122. self.properties['x-axis']['axis'] = None
  123. self.properties['y-axis'] = {}
  124. self.properties['y-axis']['prop'] = UserSettings.Get(group = self.plottype, key = 'y-axis')
  125. self.properties['y-axis']['axis'] = None
  126. self.properties['legend'] = UserSettings.Get(group = self.plottype, key = 'legend')
  127. self.zoom = False # zooming disabled
  128. self.drag = False # draging disabled
  129. self.client.SetShowScrollbars(True) # vertical and horizontal scrollbars
  130. # x and y axis set to normal (non-log)
  131. self.client.setLogScale((False, False))
  132. if self.properties['x-axis']['prop']['type']:
  133. self.client.SetXSpec(self.properties['x-axis']['prop']['type'])
  134. else:
  135. self.client.SetXSpec('auto')
  136. if self.properties['y-axis']['prop']['type']:
  137. self.client.SetYSpec(self.properties['y-axis']['prop']['type'])
  138. else:
  139. self.client.SetYSpec('auto')
  140. def InitRasterOpts(self, rasterList, plottype):
  141. """Initialize or update raster dictionary for plotting
  142. """
  143. rdict = {} # initialize a dictionary
  144. self.properties['raster'] = UserSettings.Get(group = self.plottype, key = 'raster')
  145. for r in rasterList:
  146. idx = rasterList.index(r)
  147. try:
  148. ret = grass.raster_info(r)
  149. except:
  150. continue
  151. # if r.info cannot parse map, skip it
  152. self.raster[r] = self.properties['raster'] # some default settings
  153. rdict[r] = {} # initialize sub-dictionaries for each raster in the list
  154. rdict[r]['units'] = ''
  155. if ret['units'] not in ('(none)', '"none"', '', None):
  156. rdict[r]['units'] = ret['units']
  157. rdict[r]['plegend'] = r # use fully-qualified names
  158. rdict[r]['datalist'] = [] # list of cell value,frequency pairs for plotting histogram
  159. rdict[r]['pline'] = None
  160. rdict[r]['datatype'] = ret['datatype']
  161. #
  162. #initialize with saved values
  163. #
  164. if self.properties['raster']['pwidth'] != None:
  165. rdict[r]['pwidth'] = self.properties['raster']['pwidth']
  166. else:
  167. rdict[r]['pwidth'] = 1
  168. if self.properties['raster']['pstyle'] != None and \
  169. self.properties['raster']['pstyle'] != '':
  170. rdict[r]['pstyle'] = self.properties['raster']['pstyle']
  171. else:
  172. rdict[r]['pstyle'] = 'solid'
  173. if idx < len(self.colorList):
  174. if idx == 0:
  175. # use saved color for first plot
  176. if self.properties['raster']['pcolor'] != None:
  177. rdict[r]['pcolor'] = self.properties['raster']['pcolor']
  178. else:
  179. rdict[r]['pcolor'] = self.colorDict[self.colorList[idx]]
  180. else:
  181. rdict[r]['pcolor'] = self.colorDict[self.colorList[idx]]
  182. else:
  183. r = randint(0, 255)
  184. b = randint(0, 255)
  185. g = randint(0, 255)
  186. rdict[r]['pcolor'] = ((r,g,b,255))
  187. return rdict
  188. def InitRasterPairs(self, rasterList, plottype):
  189. """Initialize or update raster dictionary with raster pairs for
  190. bivariate scatterplots
  191. """
  192. if len(rasterList) == 0: return
  193. rdict = {} # initialize a dictionary
  194. for rpair in rasterList:
  195. idx = rasterList.index(rpair)
  196. try:
  197. ret0 = grass.raster_info(rpair[0])
  198. ret1 = grass.raster_info(rpair[1])
  199. except:
  200. continue
  201. # if r.info cannot parse map, skip it
  202. self.raster[rpair] = UserSettings.Get(group = plottype, key = 'rasters') # some default settings
  203. rdict[rpair] = {} # initialize sub-dictionaries for each raster in the list
  204. rdict[rpair][0] = {}
  205. rdict[rpair][1] = {}
  206. rdict[rpair][0]['units'] = ''
  207. rdict[rpair][1]['units'] = ''
  208. if ret0['units'] not in ('(none)', '"none"', '', None):
  209. rdict[rpair][0]['units'] = ret0['units']
  210. if ret1['units'] not in ('(none)', '"none"', '', None):
  211. rdict[rpair][1]['units'] = ret1['units']
  212. rdict[rpair]['plegend'] = rpair[0].split('@')[0] + ' vs ' + rpair[1].split('@')[0]
  213. rdict[rpair]['datalist'] = [] # list of cell value,frequency pairs for plotting histogram
  214. rdict[rpair][0]['datatype'] = ret0['datatype']
  215. rdict[rpair][1]['datatype'] = ret1['datatype']
  216. #
  217. #initialize with saved values
  218. #
  219. if self.properties['raster']['ptype'] != None and \
  220. self.properties['raster']['ptype'] != '':
  221. rdict[rpair]['ptype'] = self.properties['raster']['ptype']
  222. else:
  223. rdict[rpair]['ptype'] = 'dot'
  224. if self.properties['raster']['psize'] != None:
  225. rdict[rpair]['psize'] = self.properties['raster']['psize']
  226. else:
  227. rdict[rpair]['psize'] = 1
  228. if self.properties['raster']['pfill'] != None and \
  229. self.properties['raster']['pfill'] != '':
  230. rdict[rpair]['pfill'] = self.properties['raster']['pfill']
  231. else:
  232. rdict[rpair]['pfill'] = 'solid'
  233. if idx <= len(self.colorList):
  234. rdict[rpair]['pcolor'] = self.colorDict[self.colorList[idx]]
  235. else:
  236. r = randint(0, 255)
  237. b = randint(0, 255)
  238. g = randint(0, 255)
  239. rdict[rpair]['pcolor'] = ((r,g,b,255))
  240. return rdict
  241. def SetGraphStyle(self):
  242. """Set plot and text options
  243. """
  244. self.client.SetFont(self.properties['font']['wxfont'])
  245. self.client.SetFontSizeTitle(self.properties['font']['prop']['titleSize'])
  246. self.client.SetFontSizeAxis(self.properties['font']['prop']['axisSize'])
  247. self.client.SetEnableZoom(self.zoom)
  248. self.client.SetEnableDrag(self.drag)
  249. #
  250. # axis settings
  251. #
  252. if self.properties['x-axis']['prop']['type'] == 'custom':
  253. self.client.SetXSpec('min')
  254. else:
  255. self.client.SetXSpec(self.properties['x-axis']['prop']['type'])
  256. if self.properties['y-axis']['prop']['type'] == 'custom':
  257. self.client.SetYSpec('min')
  258. else:
  259. self.client.SetYSpec(self.properties['y-axis']['prop'])
  260. if self.properties['x-axis']['prop']['type'] == 'custom' and \
  261. self.properties['x-axis']['prop']['min'] < self.properties['x-axis']['prop']['max']:
  262. self.properties['x-axis']['axis'] = (self.properties['x-axis']['prop']['min'],
  263. self.properties['x-axis']['prop']['max'])
  264. else:
  265. self.properties['x-axis']['axis'] = None
  266. if self.properties['y-axis']['prop']['type'] == 'custom' and \
  267. self.properties['y-axis']['prop']['min'] < self.properties['y-axis']['prop']['max']:
  268. self.properties['y-axis']['axis'] = (self.properties['y-axis']['prop']['min'],
  269. self.properties['y-axis']['prop']['max'])
  270. else:
  271. self.properties['y-axis']['axis'] = None
  272. if self.properties['x-axis']['prop']['log'] == True:
  273. self.properties['x-axis']['axis'] = None
  274. self.client.SetXSpec('min')
  275. if self.properties['y-axis']['prop']['log'] == True:
  276. self.properties['y-axis']['axis'] = None
  277. self.client.SetYSpec('min')
  278. self.client.setLogScale((self.properties['x-axis']['prop']['log'],
  279. self.properties['y-axis']['prop']['log']))
  280. #
  281. # grid settings
  282. #
  283. self.client.SetEnableGrid(self.properties['grid']['enabled'])
  284. self.client.SetGridColour(wx.Colour(self.properties['grid']['color'][0],
  285. self.properties['grid']['color'][1],
  286. self.properties['grid']['color'][2],
  287. 255))
  288. #
  289. # legend settings
  290. #
  291. self.client.SetFontSizeLegend(self.properties['font']['prop']['legendSize'])
  292. self.client.SetEnableLegend(self.properties['legend']['enabled'])
  293. def DrawPlot(self, plotlist):
  294. """Draw line and point plot from list plot elements.
  295. """
  296. xlabel, ylabel = self._getPlotLabels()
  297. self.plot = plot.PlotGraphics(plotlist,
  298. self.ptitle,
  299. xlabel,
  300. ylabel)
  301. if self.properties['x-axis']['prop']['type'] == 'custom':
  302. self.client.SetXSpec('min')
  303. else:
  304. self.client.SetXSpec(self.properties['x-axis']['prop']['type'])
  305. if self.properties['y-axis']['prop']['type'] == 'custom':
  306. self.client.SetYSpec('min')
  307. else:
  308. self.client.SetYSpec(self.properties['y-axis']['prop']['type'])
  309. self.client.Draw(self.plot, self.properties['x-axis']['axis'],
  310. self.properties['y-axis']['axis'])
  311. def DrawPointLabel(self, dc, mDataDict):
  312. """This is the fuction that defines how the pointLabels are
  313. plotted dc - DC that will be passed mDataDict - Dictionary
  314. of data that you want to use for the pointLabel
  315. As an example I have decided I want a box at the curve
  316. point with some text information about the curve plotted
  317. below. Any wxDC method can be used.
  318. """
  319. dc.SetPen(wx.Pen(wx.BLACK))
  320. dc.SetBrush(wx.Brush( wx.BLACK, wx.SOLID ) )
  321. sx, sy = mDataDict["scaledXY"] #scaled x,y of closest point
  322. dc.DrawRectangle( sx-5,sy-5, 10, 10) #10by10 square centered on point
  323. px,py = mDataDict["pointXY"]
  324. cNum = mDataDict["curveNum"]
  325. pntIn = mDataDict["pIndex"]
  326. legend = mDataDict["legend"]
  327. #make a string to display
  328. s = "Crv# %i, '%s', Pt. (%.2f,%.2f), PtInd %i" %(cNum, legend, px, py, pntIn)
  329. dc.DrawText(s, sx , sy + 1)
  330. def OnZoom(self, event):
  331. """Enable zooming and disable dragging
  332. """
  333. self.zoom = True
  334. self.drag = False
  335. self.client.SetEnableZoom(self.zoom)
  336. self.client.SetEnableDrag(self.drag)
  337. def OnDrag(self, event):
  338. """Enable dragging and disable zooming
  339. """
  340. self.zoom = False
  341. self.drag = True
  342. self.client.SetEnableDrag(self.drag)
  343. self.client.SetEnableZoom(self.zoom)
  344. def OnRedraw(self, event):
  345. """Redraw the plot window. Unzoom to original size
  346. """
  347. self.UpdateLabels()
  348. self.client.Reset()
  349. self.client.Redraw()
  350. def OnErase(self, event):
  351. """Erase the plot window
  352. """
  353. self.client.Clear()
  354. def SaveToFile(self, event):
  355. """Save plot to graphics file
  356. """
  357. self.client.SaveFile()
  358. def OnMouseLeftDown(self,event):
  359. self.SetStatusText(_("Left Mouse Down at Point:") + \
  360. " (%.4f, %.4f)" % self.client._getXY(event))
  361. event.Skip() # allows plotCanvas OnMouseLeftDown to be called
  362. def OnMotion(self, event):
  363. """Indicate when mouse is outside the plot area
  364. """
  365. if self.client.GetEnablePointLabel() is True:
  366. #make up dict with info for the pointLabel
  367. #I've decided to mark the closest point on the closest curve
  368. dlst = self.client.GetClosestPoint(self.client._getXY(event), pointScaled=True)
  369. if dlst != []: #returns [] if none
  370. curveNum, legend, pIndex, pointXY, scaledXY, distance = dlst
  371. #make up dictionary to pass to my user function (see DrawPointLabel)
  372. mDataDict = {"curveNum":curveNum, "legend":legend, "pIndex":pIndex,\
  373. "pointXY":pointXY, "scaledXY":scaledXY}
  374. #pass dict to update the pointLabel
  375. self.client.UpdatePointLabel(mDataDict)
  376. event.Skip() #go to next handler
  377. def PlotOptionsMenu(self, event):
  378. """Popup menu for plot and text options
  379. """
  380. point = wx.GetMousePosition()
  381. popt = wx.Menu()
  382. # Add items to the menu
  383. settext = wx.MenuItem(popt, wx.ID_ANY, _('Text settings'))
  384. popt.AppendItem(settext)
  385. self.Bind(wx.EVT_MENU, self.PlotText, settext)
  386. setgrid = wx.MenuItem(popt, wx.ID_ANY, _('Plot settings'))
  387. popt.AppendItem(setgrid)
  388. self.Bind(wx.EVT_MENU, self.PlotOptions, setgrid)
  389. # Popup the menu. If an item is selected then its handler
  390. # will be called before PopupMenu returns.
  391. self.PopupMenu(popt)
  392. popt.Destroy()
  393. def NotFunctional(self):
  394. """Creates a 'not functional' message dialog
  395. """
  396. dlg = wx.MessageDialog(parent = self,
  397. message = _('This feature is not yet functional'),
  398. caption = _('Under Construction'),
  399. style = wx.OK | wx.ICON_INFORMATION)
  400. dlg.ShowModal()
  401. dlg.Destroy()
  402. def _getPlotLabels(self):
  403. def log(txt):
  404. return "log( " + txt + " )"
  405. x = self.xlabel
  406. if self.properties['x-axis']['prop']['log']:
  407. x = log(x)
  408. y = self.ylabel
  409. if self.properties['y-axis']['prop']['log']:
  410. y = log(y)
  411. return x, y
  412. def OnPlotText(self, dlg):
  413. """Custom text settings for histogram plot.
  414. """
  415. self.ptitle = dlg.ptitle
  416. self.xlabel = dlg.xlabel
  417. self.ylabel = dlg.ylabel
  418. if self.plot:
  419. self.plot.setTitle(dlg.ptitle)
  420. self.OnRedraw(event = None)
  421. def UpdateLabels(self):
  422. x, y = self._getPlotLabels()
  423. self.client.SetFont(self.properties['font']['wxfont'])
  424. self.client.SetFontSizeTitle(self.properties['font']['prop']['titleSize'])
  425. self.client.SetFontSizeAxis(self.properties['font']['prop']['axisSize'])
  426. if self.plot:
  427. self.plot.setXLabel(x)
  428. self.plot.setYLabel(y)
  429. def PlotText(self, event):
  430. """Set custom text values for profile title and axis labels.
  431. """
  432. dlg = TextDialog(parent = self, id = wx.ID_ANY,
  433. plottype = self.plottype,
  434. title = _('Text settings'))
  435. btnval = dlg.ShowModal()
  436. if btnval == wx.ID_SAVE or btnval == wx.ID_OK or btnval == wx.ID_CANCEL:
  437. dlg.Destroy()
  438. def PlotOptions(self, event):
  439. """Set various profile options, including: line width, color,
  440. style; marker size, color, fill, and style; grid and legend
  441. options. Calls OptDialog class.
  442. """
  443. dlg = OptDialog(parent = self, id = wx.ID_ANY,
  444. plottype = self.plottype,
  445. title = _('Plot settings'))
  446. btnval = dlg.ShowModal()
  447. if btnval == wx.ID_SAVE or btnval == wx.ID_OK or btnval == wx.ID_CANCEL:
  448. dlg.Destroy()
  449. self.Update()
  450. def PrintMenu(self, event):
  451. """Print options and output menu
  452. """
  453. point = wx.GetMousePosition()
  454. printmenu = wx.Menu()
  455. for title, handler in ((_("Page setup"), self.OnPageSetup),
  456. (_("Print preview"), self.OnPrintPreview),
  457. (_("Print display"), self.OnDoPrint)):
  458. item = wx.MenuItem(printmenu, wx.ID_ANY, title)
  459. printmenu.AppendItem(item)
  460. self.Bind(wx.EVT_MENU, handler, item)
  461. # Popup the menu. If an item is selected then its handler
  462. # will be called before PopupMenu returns.
  463. self.PopupMenu(printmenu)
  464. printmenu.Destroy()
  465. def OnPageSetup(self, event):
  466. self.client.PageSetup()
  467. def OnPrintPreview(self, event):
  468. self.client.PrintPreview()
  469. def OnDoPrint(self, event):
  470. self.client.Printout()
  471. def OnQuit(self, event):
  472. self.Close(True)