mapdisp.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. """!
  2. @package gui_core.mapdisp
  3. @brief Base classes for Map display window
  4. Classes:
  5. - mapdisp::MapFrameBase
  6. (C) 2009-2011 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Martin Landa <landa.martin gmail.com>
  10. @author Michael Barton <michael.barton@asu.edu>
  11. """
  12. import os
  13. import wx
  14. from core import globalvar
  15. from core.debug import Debug
  16. from grass.script import core as grass
  17. class MapFrameBase(wx.Frame):
  18. """!Base class for map display window
  19. Derived class must use statusbarManager or override
  20. GetProperty, SetProperty and HasProperty methods.
  21. If derived class enables and disables auto-rendering,
  22. it should override IsAutoRendered method.
  23. """
  24. def __init__(self, parent = None, id = wx.ID_ANY, title = None,
  25. style = wx.DEFAULT_FRAME_STYLE, toolbars = None,
  26. Map = None, auimgr = None, name = None, **kwargs):
  27. """!
  28. @param toolbars array of activated toolbars, e.g. ['map', 'digit']
  29. @param Map instance of render.Map
  30. @param auimgs AUI manager
  31. @param name frame name
  32. @param kwargs wx.Frame attributes
  33. """
  34. self.Map = Map # instance of render.Map
  35. self.parent = parent
  36. wx.Frame.__init__(self, parent, id, title, style = style, name = name, **kwargs)
  37. # available cursors
  38. self.cursors = {
  39. # default: cross
  40. # "default" : wx.StockCursor(wx.CURSOR_DEFAULT),
  41. "default" : wx.StockCursor(wx.CURSOR_ARROW),
  42. "cross" : wx.StockCursor(wx.CURSOR_CROSS),
  43. "hand" : wx.StockCursor(wx.CURSOR_HAND),
  44. "pencil" : wx.StockCursor(wx.CURSOR_PENCIL),
  45. "sizenwse": wx.StockCursor(wx.CURSOR_SIZENWSE)
  46. }
  47. #
  48. # set the size & system icon
  49. #
  50. self.SetClientSize(self.GetSize())
  51. self.iconsize = (16, 16)
  52. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass_map.ico'), wx.BITMAP_TYPE_ICO))
  53. # toolbars
  54. self.toolbars = {}
  55. #
  56. # Fancy gui
  57. #
  58. self._mgr = wx.aui.AuiManager(self)
  59. def _initMap(self, map):
  60. """!Initialize map display, set dimensions and map region
  61. """
  62. if not grass.find_program('g.region', ['--help']):
  63. sys.exit(_("GRASS module '%s' not found. Unable to start map "
  64. "display window.") % 'g.region')
  65. self.width, self.height = self.GetClientSize()
  66. Debug.msg(2, "MapFrame._initMap():")
  67. map.ChangeMapSize(self.GetClientSize())
  68. map.region = map.GetRegion() # g.region -upgc
  69. # self.Map.SetRegion() # adjust region to match display window
  70. def SetProperty(self, name, value):
  71. """!Sets property"""
  72. self.statusbarManager.SetProperty(name, value)
  73. def GetProperty(self, name):
  74. """!Returns property"""
  75. return self.statusbarManager.GetProperty(name)
  76. def HasProperty(self, name):
  77. """!Checks whether object has property"""
  78. return self.statusbarManager.HasProperty(name)
  79. def GetPPM(self):
  80. """! Get pixel per meter
  81. @todo now computed every time, is it necessary?
  82. @todo enable user to specify ppm (and store it in UserSettings)
  83. """
  84. # TODO: need to be fixed...
  85. ### screen X region problem
  86. ### user should specify ppm
  87. dc = wx.ScreenDC()
  88. dpSizePx = wx.DisplaySize() # display size in pixels
  89. dpSizeMM = wx.DisplaySizeMM() # display size in mm (system)
  90. dpSizeIn = (dpSizeMM[0] / 25.4, dpSizeMM[1] / 25.4) # inches
  91. sysPpi = dc.GetPPI()
  92. comPpi = (dpSizePx[0] / dpSizeIn[0],
  93. dpSizePx[1] / dpSizeIn[1])
  94. ppi = comPpi # pixel per inch
  95. ppm = ((ppi[0] / 2.54) * 100, # pixel per meter
  96. (ppi[1] / 2.54) * 100)
  97. Debug.msg(4, "MapFrameBase.GetPPM(): size: px=%d,%d mm=%f,%f "
  98. "in=%f,%f ppi: sys=%d,%d com=%d,%d; ppm=%f,%f" % \
  99. (dpSizePx[0], dpSizePx[1], dpSizeMM[0], dpSizeMM[1],
  100. dpSizeIn[0], dpSizeIn[1],
  101. sysPpi[0], sysPpi[1], comPpi[0], comPpi[1],
  102. ppm[0], ppm[1]))
  103. return ppm
  104. def SetMapScale(self, value, map = None):
  105. """! Set current map scale
  106. @param value scale value (n if scale is 1:n)
  107. @param map Map instance (if none self.Map is used)
  108. """
  109. if not map:
  110. map = self.Map
  111. region = self.Map.region
  112. dEW = value * (region['cols'] / self.GetPPM()[0])
  113. dNS = value * (region['rows'] / self.GetPPM()[1])
  114. region['n'] = region['center_northing'] + dNS / 2.
  115. region['s'] = region['center_northing'] - dNS / 2.
  116. region['w'] = region['center_easting'] - dEW / 2.
  117. region['e'] = region['center_easting'] + dEW / 2.
  118. # add to zoom history
  119. self.GetWindow().ZoomHistory(region['n'], region['s'],
  120. region['e'], region['w'])
  121. def GetMapScale(self, map = None):
  122. """! Get current map scale
  123. @param map Map instance (if none self.Map is used)
  124. """
  125. if not map:
  126. map = self.Map
  127. region = map.region
  128. ppm = self.GetPPM()
  129. heightCm = region['rows'] / ppm[1] * 100
  130. widthCm = region['cols'] / ppm[0] * 100
  131. Debug.msg(4, "MapFrame.GetMapScale(): width_cm=%f, height_cm=%f" %
  132. (widthCm, heightCm))
  133. xscale = (region['e'] - region['w']) / (region['cols'] / ppm[0])
  134. yscale = (region['n'] - region['s']) / (region['rows'] / ppm[1])
  135. scale = (xscale + yscale) / 2.
  136. Debug.msg(3, "MapFrame.GetMapScale(): xscale=%f, yscale=%f -> scale=%f" % \
  137. (xscale, yscale, scale))
  138. return scale
  139. def GetProgressBar(self):
  140. """!Returns progress bar
  141. Progress bar can be used by other classes.
  142. """
  143. return self.statusbarManager.GetProgressBar()
  144. def GetMap(self):
  145. """!Returns current Map instance
  146. """
  147. return self.Map
  148. def GetWindow(self):
  149. """!Get map window"""
  150. return self.MapWindow
  151. def GetMapToolbar(self):
  152. """!Returns toolbar with zooming tools"""
  153. raise NotImplementedError()
  154. def GetToolbar(self, name):
  155. """!Returns toolbar if exists else None.
  156. Toolbars dictionary contains currently used toolbars only.
  157. """
  158. if name in self.toolbars:
  159. return self.toolbars[name]
  160. return None
  161. def StatusbarUpdate(self):
  162. """!Update statusbar content"""
  163. self.statusbarManager.Update()
  164. def IsAutoRendered(self):
  165. """!Check if auto-rendering is enabled"""
  166. return self.GetProperty('render')
  167. def CoordinatesChanged(self):
  168. """!Shows current coordinates on statusbar.
  169. Used in BufferedWindow to report change of map coordinates (under mouse cursor).
  170. """
  171. self.statusbarManager.ShowItem('coordinates')
  172. def StatusbarReposition(self):
  173. """!Reposition items in statusbar"""
  174. self.statusbarManager.Reposition()
  175. def StatusbarEnableLongHelp(self, enable = True):
  176. """!Enable/disable toolbars long help"""
  177. for toolbar in self.toolbars.itervalues():
  178. toolbar.EnableLongHelp(enable)
  179. def IsStandalone(self):
  180. """!Check if Map display is standalone"""
  181. raise NotImplementedError("IsStandalone")
  182. def OnRender(self, event):
  183. """!Re-render map composition (each map layer)
  184. """
  185. raise NotImplementedError("OnRender")
  186. def OnDraw(self, event):
  187. """!Re-display current map composition
  188. """
  189. self.MapWindow.UpdateMap(render = False)
  190. def OnErase(self, event):
  191. """!Erase the canvas
  192. """
  193. self.MapWindow.EraseMap()
  194. def OnZoomIn(self, event):
  195. """!Zoom in the map.
  196. Set mouse cursor, zoombox attributes, and zoom direction
  197. """
  198. toolbar = self.GetMapToolbar()
  199. self._switchTool(toolbar, event)
  200. win = self.GetWindow()
  201. self._prepareZoom(mapWindow = win, zoomType = 1)
  202. def OnZoomOut(self, event):
  203. """!Zoom out the map.
  204. Set mouse cursor, zoombox attributes, and zoom direction
  205. """
  206. toolbar = self.GetMapToolbar()
  207. self._switchTool(toolbar, event)
  208. win = self.GetWindow()
  209. self._prepareZoom(mapWindow = win, zoomType = -1)
  210. def _prepareZoom(self, mapWindow, zoomType):
  211. """!Prepares MapWindow for zoom, toggles toolbar
  212. @param mapWindow MapWindow to prepare
  213. @param zoomType 1 for zoom in, -1 for zoom out
  214. """
  215. mapWindow.mouse['use'] = "zoom"
  216. mapWindow.mouse['box'] = "box"
  217. mapWindow.zoomtype = zoomType
  218. mapWindow.pen = wx.Pen(colour = 'Red', width = 2, style = wx.SHORT_DASH)
  219. # change the cursor
  220. mapWindow.SetCursor(self.cursors["cross"])
  221. def _switchTool(self, toolbar, event):
  222. """!Helper function to switch tools"""
  223. if toolbar:
  224. toolbar.OnTool(event)
  225. toolbar.action['desc'] = ''
  226. def OnPan(self, event):
  227. """!Panning, set mouse to drag
  228. """
  229. toolbar = self.GetMapToolbar()
  230. self._switchTool(toolbar, event)
  231. win = self.GetWindow()
  232. self._preparePan(mapWindow = win)
  233. def _preparePan(self, mapWindow):
  234. """!Prepares MapWindow for pan, toggles toolbar
  235. @param mapWindow MapWindow to prepare
  236. """
  237. mapWindow.mouse['use'] = "pan"
  238. mapWindow.mouse['box'] = "pan"
  239. mapWindow.zoomtype = 0
  240. # change the cursor
  241. mapWindow.SetCursor(self.cursors["hand"])
  242. def OnZoomBack(self, event):
  243. """!Zoom last (previously stored position)
  244. """
  245. self.MapWindow.ZoomBack()
  246. def OnZoomToMap(self, event):
  247. """!
  248. Set display extents to match selected raster (including NULLs)
  249. or vector map.
  250. """
  251. self.MapWindow.ZoomToMap(layers = self.Map.GetListOfLayers())
  252. def OnZoomToWind(self, event):
  253. """!Set display geometry to match computational region
  254. settings (set with g.region)
  255. """
  256. self.MapWindow.ZoomToWind()
  257. def OnZoomToDefault(self, event):
  258. """!Set display geometry to match default region settings
  259. """
  260. self.MapWindow.ZoomToDefault()