mapdisplay.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. """!
  2. @package gcp.mapdisplay
  3. @brief Display to manage ground control points with two toolbars, one
  4. for various display management functions, one for manipulating GCPs.
  5. Classes:
  6. - mapdisplay::MapFrame
  7. (C) 2006-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 Markus Metz
  11. """
  12. import os
  13. import math
  14. import platform
  15. from core import globalvar
  16. import wx
  17. import wx.aui
  18. from core.render import EVT_UPDATE_PRGBAR
  19. from mapdisp.toolbars import MapToolbar
  20. from gcp.toolbars import GCPDisplayToolbar, GCPManToolbar
  21. from mapdisp.gprint import PrintOptions
  22. from core.gcmd import GMessage
  23. from gui_core.dialogs import GetImageHandlers, ImageSizeDialog
  24. from gui_core.mapdisp import SingleMapFrame
  25. from core.settings import UserSettings
  26. from mapdisp.mapwindow import BufferedWindow
  27. import mapdisp.statusbar as sb
  28. # for standalone app
  29. cmdfilename = None
  30. class MapFrame(SingleMapFrame):
  31. """!Main frame for map display window. Drawing takes place in
  32. child double buffered drawing window.
  33. """
  34. def __init__(self, parent=None, title=_("GRASS GIS Manage Ground Control Points"),
  35. toolbars=["gcpdisp"], tree=None, notebook=None, lmgr=None,
  36. page=None, Map=None, auimgr=None, name = 'GCPMapWindow', **kwargs):
  37. """!Main map display window with toolbars, statusbar and
  38. DrawWindow
  39. @param toolbars array of activated toolbars, e.g. ['map', 'digit']
  40. @param tree reference to layer tree
  41. @param notebook control book ID in Layer Manager
  42. @param lmgr Layer Manager
  43. @param page notebook page with layer tree
  44. @param Map instance of render.Map
  45. @param auimgs AUI manager
  46. @param kwargs wx.Frame attribures
  47. """
  48. SingleMapFrame.__init__(self, parent = parent, title = title,
  49. Map = Map, auimgr = auimgr, name = name, **kwargs)
  50. self._layerManager = lmgr # Layer Manager object
  51. self.tree = tree # Layer Manager layer tree object
  52. self.page = page # Notebook page holding the layer tree
  53. self.layerbook = notebook # Layer Manager layer tree notebook
  54. #
  55. # Add toolbars
  56. #
  57. for toolb in toolbars:
  58. self.AddToolbar(toolb)
  59. self.activemap = self.toolbars['gcpdisp'].togglemap
  60. self.activemap.SetSelection(0)
  61. self.SrcMap = self.grwiz.SrcMap # instance of render.Map
  62. self.TgtMap = self.grwiz.TgtMap # instance of render.Map
  63. self._mgr.SetDockSizeConstraint(0.5, 0.5)
  64. #
  65. # Add statusbar
  66. #
  67. # items for choice
  68. self.statusbarItems = [sb.SbCoordinates,
  69. sb.SbRegionExtent,
  70. sb.SbCompRegionExtent,
  71. sb.SbShowRegion,
  72. sb.SbResolution,
  73. sb.SbDisplayGeometry,
  74. sb.SbMapScale,
  75. sb.SbProjection,
  76. sb.SbGoToGCP,
  77. sb.SbRMSError]
  78. # create statusbar and its manager
  79. statusbar = self.CreateStatusBar(number = 4, style = 0)
  80. statusbar.SetStatusWidths([-5, -2, -1, -1])
  81. self.statusbarManager = sb.SbManager(mapframe = self, statusbar = statusbar)
  82. # fill statusbar manager
  83. self.statusbarManager.AddStatusbarItemsByClass(self.statusbarItems, mapframe = self, statusbar = statusbar)
  84. self.statusbarManager.AddStatusbarItem(sb.SbMask(self, statusbar = statusbar, position = 2))
  85. self.statusbarManager.AddStatusbarItem(sb.SbRender(self, statusbar = statusbar, position = 3))
  86. self.statusbarManager.SetMode(8) # goto GCP
  87. self.statusbarManager.Update()
  88. #
  89. # Init map display (buffered DC & set default cursor)
  90. #
  91. self.grwiz.SwitchEnv('source')
  92. self.SrcMapWindow = BufferedWindow(self, id=wx.ID_ANY,
  93. Map=self.SrcMap, tree=self.tree, lmgr=self._layerManager)
  94. self.grwiz.SwitchEnv('target')
  95. self.TgtMapWindow = BufferedWindow(self, id=wx.ID_ANY,
  96. Map=self.TgtMap, tree=self.tree, lmgr=self._layerManager)
  97. self.MapWindow = self.SrcMapWindow
  98. self.Map = self.SrcMap
  99. self.SrcMapWindow.SetCursor(self.cursors["cross"])
  100. self.TgtMapWindow.SetCursor(self.cursors["cross"])
  101. #
  102. # initialize region values
  103. #
  104. self._initMap(Map = self.SrcMap)
  105. self._initMap(Map = self.TgtMap)
  106. #
  107. # Bind various events
  108. #
  109. self.Bind(EVT_UPDATE_PRGBAR, self.OnUpdateProgress)
  110. self.activemap.Bind(wx.EVT_CHOICE, self.OnUpdateActive)
  111. #
  112. # Update fancy gui style
  113. #
  114. # AuiManager wants a CentrePane, workaround to get two equally sized windows
  115. self.list = self.CreateGCPList()
  116. #self.SrcMapWindow.SetSize((300, 300))
  117. #self.TgtMapWindow.SetSize((300, 300))
  118. self.list.SetSize((100, 150))
  119. self._mgr.AddPane(self.list, wx.aui.AuiPaneInfo().
  120. Name("gcplist").Caption(_("GCP List")).LeftDockable(False).
  121. RightDockable(False).PinButton().FloatingSize((600,200)).
  122. CloseButton(False).DestroyOnClose(True).
  123. Top().Layer(1).MinSize((200,100)))
  124. self._mgr.AddPane(self.SrcMapWindow, wx.aui.AuiPaneInfo().
  125. Name("source").Caption(_("Source Display")).Dockable(False).
  126. CloseButton(False).DestroyOnClose(True).Floatable(False).
  127. Centre())
  128. self._mgr.AddPane(self.TgtMapWindow, wx.aui.AuiPaneInfo().
  129. Name("target").Caption(_("Target Display")).Dockable(False).
  130. CloseButton(False).DestroyOnClose(True).Floatable(False).
  131. Right().Layer(0))
  132. srcwidth, srcheight = self.SrcMapWindow.GetSize()
  133. tgtwidth, tgtheight = self.TgtMapWindow.GetSize()
  134. srcwidth = (srcwidth + tgtwidth) / 2
  135. self._mgr.GetPane("target").Hide()
  136. self._mgr.Update()
  137. self._mgr.GetPane("source").BestSize((srcwidth, srcheight))
  138. self._mgr.GetPane("target").BestSize((srcwidth, srcheight))
  139. if self.show_target:
  140. self._mgr.GetPane("target").Show()
  141. else:
  142. self.activemap.Enable(False)
  143. # needed by Mac OS, does not harm on Linux, breaks display on Windows
  144. if platform.system() != 'Windows':
  145. self._mgr.Update()
  146. #
  147. # Init print module and classes
  148. #
  149. self.printopt = PrintOptions(self, self.MapWindow)
  150. #
  151. # Initialization of digitization tool
  152. #
  153. self.digit = None
  154. # set active map
  155. self.MapWindow = self.SrcMapWindow
  156. self.Map = self.SrcMap
  157. # do not init zoom history here, that happens when zooming to map(s)
  158. #
  159. # Re-use dialogs
  160. #
  161. self.dialogs = {}
  162. self.dialogs['attributes'] = None
  163. self.dialogs['category'] = None
  164. self.dialogs['barscale'] = None
  165. self.dialogs['legend'] = None
  166. self.decorationDialog = None # decoration/overlays
  167. def AddToolbar(self, name):
  168. """!Add defined toolbar to the window
  169. Currently known toolbars are:
  170. - 'map' - basic map toolbar
  171. - 'vdigit' - vector digitizer
  172. - 'gcpdisp' - GCP Manager, Display
  173. - 'gcpman' - GCP Manager, points management
  174. - 'nviz' - 3D view mode
  175. """
  176. # default toolbar
  177. if name == "map":
  178. self.toolbars['map'] = MapToolbar(self, self.Map)
  179. self._mgr.AddPane(self.toolbars['map'],
  180. wx.aui.AuiPaneInfo().
  181. Name("maptoolbar").Caption(_("Map Toolbar")).
  182. ToolbarPane().Top().
  183. LeftDockable(False).RightDockable(False).
  184. BottomDockable(False).TopDockable(True).
  185. CloseButton(False).Layer(2).
  186. BestSize((self.toolbars['map'].GetSize())))
  187. # GCP display
  188. elif name == "gcpdisp":
  189. self.toolbars['gcpdisp'] = GCPDisplayToolbar(self)
  190. self._mgr.AddPane(self.toolbars['gcpdisp'],
  191. wx.aui.AuiPaneInfo().
  192. Name("gcpdisplaytoolbar").Caption(_("GCP Display toolbar")).
  193. ToolbarPane().Top().
  194. LeftDockable(False).RightDockable(False).
  195. BottomDockable(False).TopDockable(True).
  196. CloseButton(False).Layer(2))
  197. if self.show_target == False:
  198. self.toolbars['gcpdisp'].Enable('zoommenu', enable = False)
  199. self.toolbars['gcpman'] = GCPManToolbar(self)
  200. self._mgr.AddPane(self.toolbars['gcpman'],
  201. wx.aui.AuiPaneInfo().
  202. Name("gcpmanagertoolbar").Caption(_("GCP Manager toolbar")).
  203. ToolbarPane().Top().Row(1).
  204. LeftDockable(False).RightDockable(False).
  205. BottomDockable(False).TopDockable(True).
  206. CloseButton(False).Layer(2))
  207. self._mgr.Update()
  208. def OnUpdateProgress(self, event):
  209. """
  210. Update progress bar info
  211. """
  212. self.GetProgressBar().SetValue(event.value)
  213. event.Skip()
  214. def OnFocus(self, event):
  215. """
  216. Change choicebook page to match display.
  217. Or set display for georectifying
  218. """
  219. if self._layerManager and \
  220. self._layerManager.gcpmanagement:
  221. # in GCP Management, set focus to current MapWindow for mouse actions
  222. self.OnPointer(event)
  223. self.MapWindow.SetFocus()
  224. else:
  225. # change bookcontrol page to page associated with display
  226. # GCP Manager: use bookcontrol?
  227. if self.page:
  228. pgnum = self.layerbook.GetPageIndex(self.page)
  229. if pgnum > -1:
  230. self.layerbook.SetSelection(pgnum)
  231. event.Skip()
  232. def OnDraw(self, event):
  233. """!Re-display current map composition
  234. """
  235. self.MapWindow.UpdateMap(render = False)
  236. def OnRender(self, event):
  237. """!Re-render map composition (each map layer)
  238. """
  239. # delete tmp map layers (queries)
  240. qlayer = self.Map.GetListOfLayers(l_name=globalvar.QUERYLAYER)
  241. for layer in qlayer:
  242. self.Map.DeleteLayer(layer)
  243. self.SrcMapWindow.UpdateMap(render=True)
  244. if self.show_target:
  245. self.TgtMapWindow.UpdateMap(render=True)
  246. # update statusbar
  247. self.StatusbarUpdate()
  248. def OnPointer(self, event):
  249. """!Pointer button clicked
  250. """
  251. # change the cursor
  252. self.SrcMapWindow.SetCursor(self.cursors["cross"])
  253. self.SrcMapWindow.mouse['use'] = "pointer"
  254. self.SrcMapWindow.mouse['box'] = "point"
  255. self.TgtMapWindow.SetCursor(self.cursors["cross"])
  256. self.TgtMapWindow.mouse['use'] = "pointer"
  257. self.TgtMapWindow.mouse['box'] = "point"
  258. def OnZoomIn(self, event):
  259. """
  260. Zoom in the map.
  261. Set mouse cursor, zoombox attributes, and zoom direction
  262. """
  263. if self.GetToolbar('map'):
  264. self.toolbars['map'].OnTool(event)
  265. self.toolbars['map'].action['desc'] = ''
  266. self.MapWindow.mouse['use'] = "zoom"
  267. self.MapWindow.mouse['box'] = "box"
  268. self.MapWindow.zoomtype = 1
  269. self.MapWindow.pen = wx.Pen(colour='Red', width=2, style=wx.SHORT_DASH)
  270. # change the cursor
  271. self.MapWindow.SetCursor(self.cursors["cross"])
  272. if self.MapWindow == self.SrcMapWindow:
  273. win = self.TgtMapWindow
  274. elif self.MapWindow == self.TgtMapWindow:
  275. win = self.SrcMapWindow
  276. win.mouse['use'] = "zoom"
  277. win.mouse['box'] = "box"
  278. win.zoomtype = 1
  279. win.pen = wx.Pen(colour='Red', width=2, style=wx.SHORT_DASH)
  280. # change the cursor
  281. win.SetCursor(self.cursors["cross"])
  282. def OnZoomOut(self, event):
  283. """
  284. Zoom out the map.
  285. Set mouse cursor, zoombox attributes, and zoom direction
  286. """
  287. if self.GetToolbar('map'):
  288. self.toolbars['map'].OnTool(event)
  289. self.toolbars['map'].action['desc'] = ''
  290. self.MapWindow.mouse['use'] = "zoom"
  291. self.MapWindow.mouse['box'] = "box"
  292. self.MapWindow.zoomtype = -1
  293. self.MapWindow.pen = wx.Pen(colour='Red', width=2, style=wx.SHORT_DASH)
  294. # change the cursor
  295. self.MapWindow.SetCursor(self.cursors["cross"])
  296. if self.MapWindow == self.SrcMapWindow:
  297. win = self.TgtMapWindow
  298. elif self.MapWindow == self.TgtMapWindow:
  299. win = self.SrcMapWindow
  300. win.mouse['use'] = "zoom"
  301. win.mouse['box'] = "box"
  302. win.zoomtype = -1
  303. win.pen = wx.Pen(colour='Red', width=2, style=wx.SHORT_DASH)
  304. # change the cursor
  305. win.SetCursor(self.cursors["cross"])
  306. def OnPan(self, event):
  307. """
  308. Panning, set mouse to drag
  309. """
  310. if self.GetToolbar('map'):
  311. self.toolbars['map'].OnTool(event)
  312. self.toolbars['map'].action['desc'] = ''
  313. self.MapWindow.mouse['use'] = "pan"
  314. self.MapWindow.mouse['box'] = "pan"
  315. self.MapWindow.zoomtype = 0
  316. # change the cursor
  317. self.MapWindow.SetCursor(self.cursors["hand"])
  318. if self.MapWindow == self.SrcMapWindow:
  319. win = self.TgtMapWindow
  320. elif self.MapWindow == self.TgtMapWindow:
  321. win = self.SrcMapWindow
  322. win.mouse['use'] = "pan"
  323. win.mouse['box'] = "pan"
  324. win.zoomtype = 0
  325. # change the cursor
  326. win.SetCursor(self.cursors["hand"])
  327. def OnErase(self, event):
  328. """
  329. Erase the canvas
  330. """
  331. self.MapWindow.EraseMap()
  332. if self.MapWindow == self.SrcMapWindow:
  333. win = self.TgtMapWindow
  334. elif self.MapWindow == self.TgtMapWindow:
  335. win = self.SrcMapWindow
  336. win.EraseMap()
  337. def OnZoomRegion(self, event):
  338. """
  339. Zoom to region
  340. """
  341. self.Map.getRegion()
  342. self.Map.getResolution()
  343. self.UpdateMap()
  344. # event.Skip()
  345. def OnAlignRegion(self, event):
  346. """
  347. Align region
  348. """
  349. if not self.Map.alignRegion:
  350. self.Map.alignRegion = True
  351. else:
  352. self.Map.alignRegion = False
  353. # event.Skip()
  354. def SaveToFile(self, event):
  355. """!Save map to image
  356. """
  357. img = self.MapWindow.img
  358. if not img:
  359. GMessage(parent = self,
  360. message = _("Nothing to render (empty map). Operation canceled."))
  361. return
  362. filetype, ltype = GetImageHandlers(img)
  363. # get size
  364. dlg = ImageSizeDialog(self)
  365. dlg.CentreOnParent()
  366. if dlg.ShowModal() != wx.ID_OK:
  367. dlg.Destroy()
  368. return
  369. width, height = dlg.GetValues()
  370. dlg.Destroy()
  371. # get filename
  372. dlg = wx.FileDialog(parent = self,
  373. message = _("Choose a file name to save the image "
  374. "(no need to add extension)"),
  375. wildcard = filetype,
  376. style=wx.SAVE | wx.FD_OVERWRITE_PROMPT)
  377. if dlg.ShowModal() == wx.ID_OK:
  378. path = dlg.GetPath()
  379. if not path:
  380. dlg.Destroy()
  381. return
  382. base, ext = os.path.splitext(path)
  383. fileType = ltype[dlg.GetFilterIndex()]['type']
  384. extType = ltype[dlg.GetFilterIndex()]['ext']
  385. if ext != extType:
  386. path = base + '.' + extType
  387. self.MapWindow.SaveToFile(path, fileType,
  388. width, height)
  389. dlg.Destroy()
  390. def PrintMenu(self, event):
  391. """
  392. Print options and output menu for map display
  393. """
  394. point = wx.GetMousePosition()
  395. printmenu = wx.Menu()
  396. # Add items to the menu
  397. setup = wx.MenuItem(printmenu, wx.ID_ANY, _('Page setup'))
  398. printmenu.AppendItem(setup)
  399. self.Bind(wx.EVT_MENU, self.printopt.OnPageSetup, setup)
  400. preview = wx.MenuItem(printmenu, wx.ID_ANY, _('Print preview'))
  401. printmenu.AppendItem(preview)
  402. self.Bind(wx.EVT_MENU, self.printopt.OnPrintPreview, preview)
  403. doprint = wx.MenuItem(printmenu, wx.ID_ANY, _('Print display'))
  404. printmenu.AppendItem(doprint)
  405. self.Bind(wx.EVT_MENU, self.printopt.OnDoPrint, doprint)
  406. # Popup the menu. If an item is selected then its handler
  407. # will be called before PopupMenu returns.
  408. self.PopupMenu(printmenu)
  409. printmenu.Destroy()
  410. def FormatDist(self, dist):
  411. """!Format length numbers and units in a nice way,
  412. as a function of length. From code by Hamish Bowman
  413. Grass Development Team 2006"""
  414. mapunits = self.Map.projinfo['units']
  415. if mapunits == 'metres': mapunits = 'meters'
  416. outunits = mapunits
  417. dist = float(dist)
  418. divisor = 1.0
  419. # figure out which units to use
  420. if mapunits == 'meters':
  421. if dist > 2500.0:
  422. outunits = 'km'
  423. divisor = 1000.0
  424. else: outunits = 'm'
  425. elif mapunits == 'feet':
  426. # nano-bug: we match any "feet", but US Survey feet is really
  427. # 5279.9894 per statute mile, or 10.6' per 1000 miles. As >1000
  428. # miles the tick markers are rounded to the nearest 10th of a
  429. # mile (528'), the difference in foot flavours is ignored.
  430. if dist > 5280.0:
  431. outunits = 'miles'
  432. divisor = 5280.0
  433. else:
  434. outunits = 'ft'
  435. elif 'degree' in mapunits:
  436. if dist < 1:
  437. outunits = 'min'
  438. divisor = (1/60.0)
  439. else:
  440. outunits = 'deg'
  441. # format numbers in a nice way
  442. if (dist/divisor) >= 2500.0:
  443. outdist = round(dist/divisor)
  444. elif (dist/divisor) >= 1000.0:
  445. outdist = round(dist/divisor,1)
  446. elif (dist/divisor) > 0.0:
  447. outdist = round(dist/divisor,int(math.ceil(3-math.log10(dist/divisor))))
  448. else:
  449. outdist = float(dist/divisor)
  450. return (outdist, outunits)
  451. def OnZoomToRaster(self, event):
  452. """!
  453. Set display extents to match selected raster map (ignore NULLs)
  454. """
  455. self.MapWindow.ZoomToMap(ignoreNulls = True)
  456. def OnZoomToSaved(self, event):
  457. """!Set display geometry to match extents in
  458. saved region file
  459. """
  460. self.MapWindow.ZoomToSaved()
  461. def OnDisplayToWind(self, event):
  462. """!Set computational region (WIND file) to match display
  463. extents
  464. """
  465. self.MapWindow.DisplayToWind()
  466. def SaveDisplayRegion(self, event):
  467. """!Save display extents to named region file.
  468. """
  469. self.MapWindow.SaveDisplayRegion()
  470. def OnZoomMenu(self, event):
  471. """!Popup Zoom menu
  472. """
  473. point = wx.GetMousePosition()
  474. zoommenu = wx.Menu()
  475. # Add items to the menu
  476. zoomwind = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to computational region (set with g.region)'))
  477. zoommenu.AppendItem(zoomwind)
  478. self.Bind(wx.EVT_MENU, self.OnZoomToWind, zoomwind)
  479. zoomdefault = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to default region'))
  480. zoommenu.AppendItem(zoomdefault)
  481. self.Bind(wx.EVT_MENU, self.OnZoomToDefault, zoomdefault)
  482. zoomsaved = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to saved region'))
  483. zoommenu.AppendItem(zoomsaved)
  484. self.Bind(wx.EVT_MENU, self.OnZoomToSaved, zoomsaved)
  485. savewind = wx.MenuItem(zoommenu, wx.ID_ANY, _('Set computational region from display'))
  486. zoommenu.AppendItem(savewind)
  487. self.Bind(wx.EVT_MENU, self.OnDisplayToWind, savewind)
  488. savezoom = wx.MenuItem(zoommenu, wx.ID_ANY, _('Save display geometry to named region'))
  489. zoommenu.AppendItem(savezoom)
  490. self.Bind(wx.EVT_MENU, self.SaveDisplayRegion, savezoom)
  491. # Popup the menu. If an item is selected then its handler
  492. # will be called before PopupMenu returns.
  493. self.PopupMenu(zoommenu)
  494. zoommenu.Destroy()
  495. def IsStandalone(self):
  496. """!Check if Map display is standalone"""
  497. if self._layerManager:
  498. return False
  499. return True
  500. def GetLayerManager(self):
  501. """!Get reference to Layer Manager
  502. @return window reference
  503. @return None (if standalone)
  504. """
  505. return self._layerManager
  506. def GetSrcWindow(self):
  507. return self.SrcMapWindow
  508. def GetTgtWindow(self):
  509. return self.TgtMapWindow
  510. def GetShowTarget(self):
  511. return self.show_target
  512. def GetMapToolbar(self):
  513. """!Returns toolbar with zooming tools"""
  514. return self.toolbars['gcpdisp']