123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979 |
- """!
- @package wxplot.py
- Iinteractive plotting using PyPlot (wx.lib.plot.py).
- Classes:
- - AbstractPlotFrame
- - HistFrame
- - ProfileFrame
- (C) 2011 by the GRASS Development Team
- This program is free software under the GNU General Public License
- (>=v2). Read the file COPYING that comes with GRASS for details.
- @author Michael Barton, Arizona State University
- """
- import os
- import sys
- import math
- import wx
- import wx.lib.colourselect as csel
- import globalvar
- import gcmd
- from render import Map
- from toolbars import Histogram2Toolbar
- from toolbars import ProfileToolbar
- from preferences import globalSettings as UserSettings
- import wxplot_dialogs as dialogs
- from grass.script import core as grass
- from grass.script import raster as raster
- try:
- import numpy
- import wx.lib.plot as plot
- except ImportError:
- msg = _("This module requires the NumPy module, which could not be "
- "imported. It probably is not installed (it's not part of the "
- "standard Python distribution). See the Numeric Python site "
- "(http://numpy.scipy.org) for information on downloading source or "
- "binaries.")
- print >> sys.stderr, "histogram2.py: " + msg
- class AbstractPlotFrame(wx.Frame):
- """!Abstract PyPlot display frame class"""
- def __init__(self, parent = None, id = wx.ID_ANY, title='', size = (700, 300),
- style = wx.DEFAULT_FRAME_STYLE, rasterList = [], **kwargs):
- wx.Frame.__init__(self, parent, id, title, size = size, style = style, **kwargs)
-
- self.parent = parent # MapFrame
- self.mapwin = self.parent.MapWindow
- self.Map = Map() # instance of render.Map to be associated with display
- self.rasterList = rasterList #list of rasters to plot
- self.raster = {} # dictionary of raster maps and their plotting parameters
- self.plottype = ''
-
- self.pstyledict = { 'solid' : wx.SOLID,
- 'dot' : wx.DOT,
- 'long-dash' : wx.LONG_DASH,
- 'short-dash' : wx.SHORT_DASH,
- 'dot-dash' : wx.DOT_DASH }
- self.ptfilldict = { 'transparent' : wx.TRANSPARENT,
- 'solid' : wx.SOLID }
- #
- # Icon
- #
- self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
-
- #
- # Add statusbar
- #
- self.statusbar = self.CreateStatusBar(number = 2, style = 0)
- self.statusbar.SetStatusWidths([-2, -1])
- #
- # Define canvas and settings
- #
- #
- self.client = plot.PlotCanvas(self)
- #define the function for drawing pointLabels
- self.client.SetPointLabelFunc(self.DrawPointLabel)
- # Create mouse event for showing cursor coords in status bar
- self.client.canvas.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
- # Show closest point when enabled
- self.client.canvas.Bind(wx.EVT_MOTION, self.OnMotion)
- self.plotlist = [] # list of things to plot
- self.plot = None # plot draw object
- self.ptitle = "" # title of window
- self.xlabel = "" # default X-axis label
- self.ylabel = "" # default Y-axis label
- #
- # Bind various events
- #
- self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
-
- self.CentreOnScreen()
-
- self._createColorDict()
- def _createColorDict(self):
- """!Create color dictionary to return wx.Color tuples
- for assigning colors to images in imagery groups"""
-
- self.colorDict = {}
- for clr in grass.named_colors.iterkeys():
- if clr == 'white' or clr == 'black': continue
- r = grass.named_colors[clr][0] * 255
- g = grass.named_colors[clr][1] * 255
- b = grass.named_colors[clr][2] * 255
- self.colorDict[clr] = (r,g,b,255)
- def InitPlotOpts(self, plottype):
- """!Initialize options for entire plot
- """
-
- self.plottype = plottype # histogram, profile, or scatter
- self.properties = {} # plot properties
- self.properties['font'] = {}
- self.properties['font']['prop'] = UserSettings.Get(group = self.plottype, key = 'font')
- self.properties['font']['wxfont'] = wx.Font(11, wx.FONTFAMILY_SWISS,
- wx.FONTSTYLE_NORMAL,
- wx.FONTWEIGHT_NORMAL)
-
- if self.plottype != 'histogram':
- self.properties['marker'] = UserSettings.Get(group = self.plottype, key = 'marker')
- # changing color string to tuple for markers/points
- colstr = str(self.properties['marker']['color'])
- self.properties['marker']['color'] = tuple(int(colval) for colval in colstr.strip('()').split(','))
- self.properties['grid'] = UserSettings.Get(group = self.plottype, key = 'grid')
- colstr = str(self.properties['grid']['color']) # changing color string to tuple
- self.properties['grid']['color'] = tuple(int(colval) for colval in colstr.strip('()').split(','))
-
- self.properties['x-axis'] = {}
- self.properties['x-axis']['prop'] = UserSettings.Get(group = self.plottype, key = 'x-axis')
- self.properties['x-axis']['axis'] = None
- self.properties['y-axis'] = {}
- self.properties['y-axis']['prop'] = UserSettings.Get(group = self.plottype, key = 'y-axis')
- self.properties['y-axis']['axis'] = None
-
- self.properties['legend'] = UserSettings.Get(group = self.plottype, key = 'legend')
- self.zoom = False # zooming disabled
- self.drag = False # draging disabled
- self.client.SetShowScrollbars(True) # vertical and horizontal scrollbars
- # x and y axis set to normal (non-log)
- self.client.setLogScale((False, False))
- if self.properties['x-axis']['prop']['type']:
- self.client.SetXSpec(self.properties['x-axis']['prop']['type'])
- else:
- self.client.SetXSpec('auto')
-
- if self.properties['y-axis']['prop']['type']:
- self.client.SetYSpec(self.properties['y-axis']['prop']['type'])
- else:
- self.client.SetYSpec('auto')
-
- def InitRasterOpts(self, rasterList):
- """!Initialize or update raster dictionary for plotting
- """
- rdict = {} # initialize a dictionary
- for r in rasterList:
- idx = rasterList.index(r)
-
- try:
- ret = raster.raster_info(r)
- except:
- continue
- # if r.info cannot parse map, skip it
- # self.raster[r] = UserSettings.Get(group = 'plot', key = 'raster') # some default settings
- rdict[r] = {} # initialize sub-dictionaries for each raster in the list
- if ret['units'] == '(none)' or ret['units'] == '' or ret['units'] == None:
- rdict[r]['units'] = ''
- else:
- self.raster[r]['units'] = ret['units']
- rdict[r]['plegend'] = r.split('@')[0]
- rdict[r]['datalist'] = [] # list of cell value,frequency pairs for plotting histogram
- rdict[r]['pline'] = None
- rdict[r]['datatype'] = ret['datatype']
- rdict[r]['pwidth'] = 1
- rdict[r]['pstyle'] = 'solid'
-
- if idx <= len(self.colorList):
- rdict[r]['pcolor'] = self.colorDict[self.colorList[idx]]
- else:
- r = randint(0, 255)
- b = randint(0, 255)
- g = randint(0, 255)
- rdict[r]['pcolor'] = ((r,g,b,255))
-
-
- return rdict
-
- def SetGraphStyle(self):
- """!Set plot and text options
- """
- self.client.SetFont(self.properties['font']['wxfont'])
- self.client.SetFontSizeTitle(self.properties['font']['prop']['titleSize'])
- self.client.SetFontSizeAxis(self.properties['font']['prop']['axisSize'])
- self.client.SetEnableZoom(self.zoom)
- self.client.SetEnableDrag(self.drag)
-
- #
- # axis settings
- #
- if self.properties['x-axis']['prop']['type'] == 'custom':
- self.client.SetXSpec('min')
- else:
- self.client.SetXSpec(self.properties['x-axis']['prop']['type'])
- if self.properties['y-axis']['prop']['type'] == 'custom':
- self.client.SetYSpec('min')
- else:
- self.client.SetYSpec(self.properties['y-axis']['prop']['type'])
- if self.properties['x-axis']['prop']['type'] == 'custom' and \
- self.properties['x-axis']['prop']['min'] < self.properties['x-axis']['prop']['max']:
- self.properties['x-axis']['axis'] = (self.properties['x-axis']['prop']['min'],
- self.properties['x-axis']['prop']['max'])
- else:
- self.properties['x-axis']['axis'] = None
- if self.properties['y-axis']['prop']['type'] == 'custom' and \
- self.properties['y-axis']['prop']['min'] < self.properties['y-axis']['prop']['max']:
- self.properties['y-axis']['axis'] = (self.properties['y-axis']['prop']['min'],
- self.properties['y-axis']['prop']['max'])
- else:
- self.properties['y-axis']['axis'] = None
- self.client.SetEnableGrid(self.properties['grid']['enabled'])
-
- self.client.SetGridColour(wx.Color(self.properties['grid']['color'][0],
- self.properties['grid']['color'][1],
- self.properties['grid']['color'][2],
- 255))
- self.client.SetFontSizeLegend(self.properties['font']['prop']['legendSize'])
- self.client.SetEnableLegend(self.properties['legend']['enabled'])
- if self.properties['x-axis']['prop']['log'] == True:
- self.properties['x-axis']['axis'] = None
- self.client.SetXSpec('min')
- if self.properties['y-axis']['prop']['log'] == True:
- self.properties['y-axis']['axis'] = None
- self.client.SetYSpec('min')
-
- self.client.setLogScale((self.properties['x-axis']['prop']['log'],
- self.properties['y-axis']['prop']['log']))
- def DrawPlot(self, plotlist):
- """!Draw line and point plot from list plot elements.
- """
- self.plot = plot.PlotGraphics(plotlist,
- self.ptitle,
- self.xlabel,
- self.ylabel)
- if self.properties['x-axis']['prop']['type'] == 'custom':
- self.client.SetXSpec('min')
- else:
- self.client.SetXSpec(self.properties['x-axis']['prop']['type'])
- if self.properties['y-axis']['prop']['type'] == 'custom':
- self.client.SetYSpec('min')
- else:
- self.client.SetYSpec(self.properties['y-axis']['prop']['type'])
- self.client.Draw(self.plot, self.properties['x-axis']['axis'],
- self.properties['y-axis']['axis'])
-
- def DrawPointLabel(self, dc, mDataDict):
- """!This is the fuction that defines how the pointLabels are
- plotted dc - DC that will be passed mDataDict - Dictionary
- of data that you want to use for the pointLabel
- As an example I have decided I want a box at the curve
- point with some text information about the curve plotted
- below. Any wxDC method can be used.
- """
- dc.SetPen(wx.Pen(wx.BLACK))
- dc.SetBrush(wx.Brush( wx.BLACK, wx.SOLID ) )
- sx, sy = mDataDict["scaledXY"] #scaled x,y of closest point
- dc.DrawRectangle( sx-5,sy-5, 10, 10) #10by10 square centered on point
- px,py = mDataDict["pointXY"]
- cNum = mDataDict["curveNum"]
- pntIn = mDataDict["pIndex"]
- legend = mDataDict["legend"]
- #make a string to display
- s = "Crv# %i, '%s', Pt. (%.2f,%.2f), PtInd %i" %(cNum, legend, px, py, pntIn)
- dc.DrawText(s, sx , sy+1)
- def OnZoom(self, event):
- """!Enable zooming and disable dragging
- """
- self.zoom = True
- self.drag = False
- self.client.SetEnableZoom(self.zoom)
- self.client.SetEnableDrag(self.drag)
- def OnDrag(self, event):
- """!Enable dragging and disable zooming
- """
- self.zoom = False
- self.drag = True
- self.client.SetEnableDrag(self.drag)
- self.client.SetEnableZoom(self.zoom)
- def OnRedraw(self, event):
- """!Redraw the plot window. Unzoom to original size
- """
- self.client.Reset()
- self.client.Redraw()
-
- def OnErase(self, event):
- """!Erase the plot window
- """
- self.client.Clear()
- self.mapwin.ClearLines(self.mapwin.pdc)
- self.mapwin.ClearLines(self.mapwin.pdcTmp)
- self.mapwin.polycoords = []
- self.mapwin.Refresh()
- def SaveToFile(self, event):
- """!Save plot to graphics file
- """
- self.client.SaveFile()
- def OnMouseLeftDown(self,event):
- self.SetStatusText(_("Left Mouse Down at Point: (%.4f, %.4f)") % \
- self.client._getXY(event))
- event.Skip() # allows plotCanvas OnMouseLeftDown to be called
- def OnMotion(self, event):
- """!Indicate when mouse is outside the plot area
- """
- if self.client.OnLeave(event): print 'out of area'
- #show closest point (when enbled)
- if self.client.GetEnablePointLabel() == True:
- #make up dict with info for the pointLabel
- #I've decided to mark the closest point on the closest curve
- dlst = self.client.GetClosetPoint( self.client._getXY(event), pointScaled = True)
- if dlst != []: #returns [] if none
- curveNum, legend, pIndex, pointXY, scaledXY, distance = dlst
- #make up dictionary to pass to my user function (see DrawPointLabel)
- mDataDict = {"curveNum":curveNum, "legend":legend, "pIndex":pIndex,\
- "pointXY":pointXY, "scaledXY":scaledXY}
- #pass dict to update the pointLabel
- self.client.UpdatePointLabel(mDataDict)
- event.Skip() #go to next handler
-
-
- def PlotOptionsMenu(self, event):
- """!Popup menu for plot and text options
- """
- point = wx.GetMousePosition()
- popt = wx.Menu()
- # Add items to the menu
- settext = wx.MenuItem(popt, wx.ID_ANY, _('Text settings'))
- popt.AppendItem(settext)
- self.Bind(wx.EVT_MENU, self.PlotText, settext)
- setgrid = wx.MenuItem(popt, wx.ID_ANY, _('Plot settings'))
- popt.AppendItem(setgrid)
- self.Bind(wx.EVT_MENU, self.PlotOptions, setgrid)
- # Popup the menu. If an item is selected then its handler
- # will be called before PopupMenu returns.
- self.PopupMenu(popt)
- popt.Destroy()
- def NotFunctional(self):
- """!Creates a 'not functional' message dialog
- """
- dlg = wx.MessageDialog(parent = self,
- message = _('This feature is not yet functional'),
- caption = _('Under Construction'),
- style = wx.OK | wx.ICON_INFORMATION)
- dlg.ShowModal()
- dlg.Destroy()
- def OnPlotText(self, dlg):
- """!Custom text settings for histogram plot.
- """
- self.ptitle = dlg.ptitle
- self.xlabel = dlg.xlabel
- self.ylabel = dlg.ylabel
- dlg.UpdateSettings()
- self.client.SetFont(self.properties['font']['wxfont'])
- self.client.SetFontSizeTitle(self.properties['font']['prop']['titleSize'])
- self.client.SetFontSizeAxis(self.properties['font']['prop']['axisSize'])
- if self.plot:
- self.plot.setTitle(dlg.ptitle)
- self.plot.setXLabel(dlg.xlabel)
- self.plot.setYLabel(dlg.ylabel)
-
- self.OnRedraw(event = None)
-
- def PlotText(self, event):
- """!Set custom text values for profile title and axis labels.
- """
- dlg = dialogs.TextDialog(parent = self, id = wx.ID_ANY,
- plottype = self.plottype,
- title = _('Histogram text settings'))
- if dlg.ShowModal() == wx.ID_OK:
- self.OnPlotText(dlg)
- dlg.Destroy()
- def PlotOptions(self, event):
- """!Set various profile options, including: line width, color,
- style; marker size, color, fill, and style; grid and legend
- options. Calls OptDialog class.
- """
- dlg = dialogs.OptDialog(parent = self, id = wx.ID_ANY,
- plottype = self.plottype,
- title = _('Plot settings'))
- btnval = dlg.ShowModal()
- if btnval == wx.ID_SAVE:
- dlg.UpdateSettings()
- self.SetGraphStyle()
- dlg.Destroy()
- elif btnval == wx.ID_CANCEL:
- dlg.Destroy()
- def PrintMenu(self, event):
- """!Print options and output menu
- """
- point = wx.GetMousePosition()
- printmenu = wx.Menu()
- for title, handler in ((_("Page setup"), self.OnPageSetup),
- (_("Print preview"), self.OnPrintPreview),
- (_("Print display"), self.OnDoPrint)):
- item = wx.MenuItem(printmenu, wx.ID_ANY, title)
- printmenu.AppendItem(item)
- self.Bind(wx.EVT_MENU, handler, item)
-
- # Popup the menu. If an item is selected then its handler
- # will be called before PopupMenu returns.
- self.PopupMenu(printmenu)
- printmenu.Destroy()
- def OnPageSetup(self, event):
- self.client.PageSetup()
- def OnPrintPreview(self, event):
- self.client.PrintPreview()
- def OnDoPrint(self, event):
- self.client.Printout()
- def OnQuit(self, event):
- self.Close(True)
- def OnCloseWindow(self, event):
- """!Close plot window and clean up
- """
- try:
- self.mapwin.ClearLines()
- self.mapwin.mouse['begin'] = self.mapwin.mouse['end'] = (0.0, 0.0)
- self.mapwin.mouse['use'] = 'pointer'
- self.mapwin.mouse['box'] = 'point'
- self.mapwin.polycoords = []
- self.mapwin.UpdateMap(render = False, renderVector = False)
- except:
- pass
-
- self.mapwin.SetCursor(self.Parent.cursors["default"])
- self.Destroy()
-
- class HistFrame(AbstractPlotFrame):
- def __init__(self, parent, id, pos, style, size,
- title = _("GRASS Histogramming Tool"), rasterList = []):
- """!Mainframe for displaying histogram of raster map. Uses wx.lib.plot.
- """
- AbstractPlotFrame.__init__(self, parent)
-
- self.toolbar = Histogram2Toolbar(parent = self)
- self.SetToolBar(self.toolbar)
- #
- # Init variables
- #
- self.rasterList = rasterList
- self.plottype = 'histogram'
- self.group = ''
- self.ptitle = _('Histogram of') # title of window
- self.xlabel = _("Raster cell values") # default X-axis label
- self.ylabel = _("Cell counts") # default Y-axis label
- self.maptype = 'raster' # default type of histogram to plot
- self.histtype = 'count'
- self.bins = 255
- self.colorList = ["blue", "green", "red", "yellow", "magenta", "cyan", \
- "aqua", "black", "grey", "orange", "brown", "purple", "violet", \
- "indigo"]
-
- if len(self.rasterList) > 0: # set raster name(s) from layer manager if a map is selected
- self.InitRasterOpts(self.rasterList)
- self._initOpts()
- def _initOpts(self):
- """!Initialize plot options
- """
- self.InitPlotOpts('histogram')
- def OnCreateHist(self, event):
- """!Main routine for creating a histogram. Uses r.stats to
- create a list of cell value and count/percent/area pairs. This is passed to
- plot to create a line graph of the histogram.
- """
- self.SetCursor(self.parent.cursors["default"])
- self.SetGraphStyle()
- self.SetupHistogram()
- p = self.CreatePlotList()
- self.DrawPlot(p)
- def OnSelectRaster(self, event):
- """!Select raster map(s) to profile
- """
- dlg = dialogs.HistRasterDialog(parent = self)
- if dlg.ShowModal() == wx.ID_OK:
- self.rasterList = dlg.rasterList
- self.group = dlg.group
- self.bins = dlg.bins
- self.histtype = dlg.histtype
- self.maptype = dlg.maptype
- self.raster = self.InitRasterOpts(self.rasterList)
- # plot histogram
- if len(self.rasterList) > 0:
- self.OnCreateHist(event = None)
- self.SetupHistogram()
- p = self.CreatePlotList()
- self.DrawPlot(p)
- dlg.Destroy()
- def SetupHistogram(self):
- """!Build data list for ploting each raster
- """
- #
- # populate raster dictionary
- #
- if len(self.rasterList) == 0: return # nothing selected
-
- for r in self.rasterList:
- self.raster[r]['datalist'] = self.CreateDatalist(r)
-
- #
- # update title
- #
- if self.maptype == 'group':
- self.ptitle = _('Histogram of %s') % self.group.split('@')[0]
- else:
- self.ptitle = _('Histogram of %s') % self.rasterList[0].split('@')[0]
-
- #
- # set xlabel based on first raster map in list to be histogrammed
- #
- units = self.raster[self.rasterList[0]]['units']
- if units != '' and units != '(none)' and units != None:
- self.xlabel = _('Raster cell values %s') % units
- else:
- self.xlabel = _('Raster cell values')
- #
- # set ylabel from self.histtype
- #
- if self.histtype == 'count': self.ylabel = _('Cell counts')
- if self.histtype == 'percent': self.ylabel = _('Percent of total cells')
- if self.histtype == 'area': self.ylabel = _('Area')
- def CreateDatalist(self, raster):
- """!Build a list of cell value, frequency pairs for histogram
- frequency can be in cell counts, percents, or area
- """
- datalist = []
-
- if self.histtype == 'count': freqflag = 'cn'
- if self.histtype == 'percent': freqflag = 'pn'
- if self.histtype == 'area': freqflag = 'an'
-
- try:
- ret = gcmd.RunCommand("r.stats",
- parent = self,
- input = raster,
- flags = freqflag,
- nsteps = self.bins,
- fs = ',',
- quiet = True,
- read = True)
-
- if not ret:
- return datalist
-
- for line in ret.splitlines():
- cellval, histval = line.strip().split(',')
- histval = histval.strip()
- if self.raster[raster]['datatype'] != 'CELL':
- cellval = cellval.split('-')[0]
- if self.histtype == 'percent':
- histval = histval.rstrip('%')
-
- datalist.append((cellval,histval))
- return datalist
- except gcmd.GException, e:
- gcmd.GError(parent = self,
- message = e.value)
- return None
-
- def CreatePlotList(self):
- """!Make list of elements to plot
- """
-
- # graph the cell value, frequency pairs for the histogram
- self.plotlist = []
- for r in self.rasterList:
- if len(self.raster[r]['datalist']) > 0:
- col = wx.Color(self.raster[r]['pcolor'][0],
- self.raster[r]['pcolor'][1],
- self.raster[r]['pcolor'][2],
- 255)
- self.raster[r]['pline'] = plot.PolyLine(self.raster[r]['datalist'],
- colour = col,
- width = self.raster[r]['pwidth'],
- style = self.pstyledict[self.raster[r]['pstyle']],
- legend = self.raster[r]['plegend'])
- self.plotlist.append(self.raster[r]['pline'])
-
- if len(self.plotlist) > 0:
- return self.plotlist
- else:
- return None
- def Update(self):
- """!Update histogram after changing options
- """
- self.SetGraphStyle()
- p = self.CreatePlotList()
- self.DrawPlot(p)
-
-
- class ProfileFrame(AbstractPlotFrame):
- """!Mainframe for displaying profile of one or more raster maps. Uses wx.lib.plot.
- """
- def __init__(self, parent, id, pos, style, size,
- title = _("GRASS Profile Analysis Tool"), rasterList = []):
- AbstractPlotFrame.__init__(self, parent)
- self.toolbar = ProfileToolbar(parent = self)
- self.SetToolBar(self.toolbar)
- #
- # Init variables
- #
- self.rasterList = rasterList
- self.plottype = 'profile'
- self.coordstr = '' # string of coordinates for r.profile
- self.seglist = [] # segment endpoint list
- self.ppoints = '' # segment endpoints data
- self.transect_length = 0.0 # total transect length
- self.ptitle = _('Profile of') # title of window
- self.raster = {}
- self.colorList = ["blue", "red", "green", "yellow", "magenta", "cyan", \
- "aqua", "black", "grey", "orange", "brown", "purple", "violet", \
- "indigo"]
- if len(self.rasterList) > 0: # set raster name(s) from layer manager if a map is selected
- self.InitRasterOpts(self.rasterList)
-
-
- self._initOpts()
- # determine units (axis labels)
- if self.parent.Map.projinfo['units'] != '':
- self.xlabel = _('Distance (%s)') % self.parent.Map.projinfo['units']
- else:
- self.xlabel = _("Distance along transect")
- self.ylabel = _("Cell values")
-
- def _initOpts(self):
- """!Initialize plot options
- """
- self.InitPlotOpts('profile')
- def OnDrawTransect(self, event):
- """!Draws transect to profile in map display
- """
- self.mapwin.polycoords = []
- self.seglist = []
- self.mapwin.ClearLines(self.mapwin.pdc)
- self.ppoints = ''
- self.parent.SetFocus()
- self.parent.Raise()
-
- self.mapwin.mouse['use'] = 'profile'
- self.mapwin.mouse['box'] = 'line'
- self.mapwin.pen = wx.Pen(colour = 'Red', width = 2, style = wx.SHORT_DASH)
- self.mapwin.polypen = wx.Pen(colour = 'dark green', width = 2, style = wx.SHORT_DASH)
- self.mapwin.SetCursor(self.Parent.cursors["cross"])
- def OnSelectRaster(self, event):
- """!Select raster map(s) to profile
- """
- dlg = dialogs.ProfileRasterDialog(parent = self)
- if dlg.ShowModal() == wx.ID_OK:
- self.rasterList = dlg.rasterList
- self.raster = self.InitRasterOpts(self.rasterList)
-
- # plot profile
- if len(self.mapwin.polycoords) > 0 and len(self.rasterList) > 0:
- self.OnCreateProfile(event = None)
- dlg.Destroy()
- def SetupProfile(self):
- """!Create coordinate string for profiling. Create segment list for
- transect segment markers.
- """
- #
- # create list of coordinate points for r.profile
- #
-
- dist = 0
- cumdist = 0
- self.coordstr = ''
- lasteast = lastnorth = None
-
- if len(self.mapwin.polycoords) > 0:
- for point in self.mapwin.polycoords:
- # build string of coordinate points for r.profile
- if self.coordstr == '':
- self.coordstr = '%d,%d' % (point[0], point[1])
- else:
- self.coordstr = '%s,%d,%d' % (self.coordstr, point[0], point[1])
- if len(self.rasterList) == 0:
- return
- # title of window
- self.ptitle = _('Profile of')
- #
- # create list of coordinates for transect segment markers
- #
- if len(self.mapwin.polycoords) > 0:
- for point in self.mapwin.polycoords:
- # get value of raster cell at coordinate point
- ret = gcmd.RunCommand('r.what',
- parent = self,
- read = True,
- input = self.rasterList[0],
- east_north = '%d,%d' % (point[0],point[1]))
-
- val = ret.splitlines()[0].split('|')[3]
-
- # calculate distance between coordinate points
- if lasteast and lastnorth:
- dist = math.sqrt(math.pow((lasteast-point[0]),2) + math.pow((lastnorth-point[1]),2))
- cumdist += dist
-
- #store total transect length
- self.transect_length = cumdist
- # build a list of distance,value pairs for each segment of transect
- self.seglist.append((cumdist,val))
- lasteast = point[0]
- lastnorth = point[1]
- # delete first and last segment point
- try:
- self.seglist.pop(0)
- self.seglist.pop()
- except:
- pass
- #
- # create datalist for each raster map
- #
- for r in self.raster.iterkeys():
- self.raster[r]['datalist'] = []
- self.raster[r]['datalist'] = self.CreateDatalist(r, self.coordstr)
- # update title
- self.ptitle += ' %s ,' % r.split('@')[0]
- self.ptitle = self.ptitle.rstrip(',')
- #
- # set ylabel to match units if they exist
- #
- self.ylabel = ''
- i = 0
- for r in self.rasterList:
- if self.raster[r]['units'] != '':
- self.ylabel += '%s (%d),' % (r['units'], i)
- i += 1
-
- if self.ylabel == '':
- self.ylabel = _('Raster values')
- else:
- self.ylabel = self.ylabel.rstrip(',')
- def CreateDatalist(self, raster, coords):
- """!Build a list of distance, value pairs for points along transect using r.profile
- """
- datalist = []
-
- # keep total number of transect points to 500 or less to avoid
- # freezing with large, high resolution maps
- region = grass.region()
- curr_res = min(float(region['nsres']),float(region['ewres']))
- transect_rec = 0
- if self.transect_length / curr_res > 500:
- transect_res = self.transect_length / 500
- else: transect_res = curr_res
-
- ret = gcmd.RunCommand("r.profile",
- parent = self,
- input = raster,
- profile = coords,
- res = transect_res,
- null = "nan",
- quiet = True,
- read = True)
-
- if not ret:
- return []
-
- for line in ret.splitlines():
- dist, elev = line.strip().split(' ')
- if elev != 'nan':
- datalist.append((dist,elev))
- return datalist
- def OnCreateProfile(self, event):
- """!Main routine for creating a profile. Uses r.profile to
- create a list of distance,cell value pairs. This is passed to
- plot to create a line graph of the profile. If the profile
- transect is in multiple segments, these are drawn as
- points. Profile transect is drawn, using methods in mapdisp.py
- """
-
- if len(self.mapwin.polycoords) == 0 or len(self.rasterList) == 0:
- dlg = wx.MessageDialog(parent = self,
- message = _('You must draw a transect to profile in the map display window.'),
- caption = _('Nothing to profile'),
- style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
- dlg.ShowModal()
- dlg.Destroy()
- return
- self.mapwin.SetCursor(self.parent.cursors["default"])
- self.SetCursor(self.parent.cursors["default"])
- self.SetGraphStyle()
- self.SetupProfile()
- p = self.CreatePlotList()
- self.DrawPlot(p)
- # reset transect
- self.mapwin.mouse['begin'] = self.mapwin.mouse['end'] = (0.0,0.0)
- self.mapwin.mouse['use'] = 'pointer'
- self.mapwin.mouse['box'] = 'point'
- def CreatePlotList(self):
- """!Create a plot data list from transect datalist and
- transect segment endpoint coordinates.
- """
- # graph the distance, value pairs for the transect
- self.plotlist = []
- # Add segment marker points to plot data list
- if len(self.seglist) > 0 :
- self.ppoints = plot.PolyMarker(self.seglist,
- legend = ' ' + self.properties['marker']['legend'],
- colour = wx.Color(self.properties['marker']['color'][0],
- self.properties['marker']['color'][1],
- self.properties['marker']['color'][2],
- 255),
- size = self.properties['marker']['size'],
- fillstyle = self.ptfilldict[self.properties['marker']['fill']],
- marker = self.properties['marker']['type'])
- self.plotlist.append(self.ppoints)
- # Add profile distance/elevation pairs to plot data list for each raster profiled
- for r in self.rasterList:
- col = wx.Color(self.raster[r]['pcolor'][0],
- self.raster[r]['pcolor'][1],
- self.raster[r]['pcolor'][2],
- 255)
- self.raster[r]['pline'] = plot.PolyLine(self.raster[r]['datalist'],
- colour = col,
- width = self.raster[r]['pwidth'],
- style = self.pstyledict[self.raster[r]['pstyle']],
- legend = self.raster[r]['plegend'])
- self.plotlist.append(self.raster[r]['pline'])
- if len(self.plotlist) > 0:
- return self.plotlist
- else:
- return None
- def Update(self):
- """!Update profile after changing options
- """
- self.SetGraphStyle()
- p = self.CreatePlotList()
- self.DrawPlot(p)
- def SaveProfileToFile(self, event):
- """!Save r.profile data to a csv file
- """
- wildcard = _("Comma separated value (*.csv)|*.csv")
-
- dlg = wx.FileDialog(parent = self,
- message = _("Path and prefix (for raster name) to save profile values..."),
- defaultDir = os.getcwd(),
- defaultFile = "", wildcard = wildcard, style = wx.SAVE)
- if dlg.ShowModal() == wx.ID_OK:
- path = dlg.GetPath()
-
- for r in self.rasterList:
- pfile = path+'_'+str(r['name'])+'.csv'
- try:
- file = open(pfile, "w")
- except IOError:
- wx.MessageBox(parent = self,
- message = _("Unable to open file <%s> for writing.") % pfile,
- caption = _("Error"), style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
- return False
- for datapair in self.raster[r]['datalist']:
- file.write('%d,%d\n' % (float(datapair[0]),float(datapair[1])))
-
- file.close()
- dlg.Destroy()
|