mapdisp.py 11 KB

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