mapdisplay.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 platform
  14. from core import globalvar
  15. import wx
  16. import wx.aui
  17. from mapdisp.toolbars import MapToolbar
  18. from gcp.toolbars import GCPDisplayToolbar, GCPManToolbar
  19. from mapdisp.gprint import PrintOptions
  20. from core.gcmd import GMessage
  21. from gui_core.dialogs import GetImageHandlers, ImageSizeDialog
  22. from gui_core.mapdisp import SingleMapFrame
  23. from gui_core.wrap import Menu
  24. from mapwin.buffered import BufferedMapWindow
  25. from mapwin.base import MapWindowProperties
  26. import mapdisp.statusbar as sb
  27. import gcp.statusbar as sbgcp
  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__(
  35. self,
  36. parent,
  37. giface,
  38. title=_("Manage Ground Control Points"),
  39. toolbars=["gcpdisp"],
  40. Map=None,
  41. auimgr=None,
  42. name="GCPMapWindow",
  43. **kwargs,
  44. ):
  45. """Main map display window with toolbars, statusbar and
  46. DrawWindow
  47. :param giface: GRASS interface instance
  48. :param title: window title
  49. :param toolbars: array of activated toolbars, e.g. ['map', 'digit']
  50. :param map: instance of render.Map
  51. :param auimgs: AUI manager
  52. :param kwargs: wx.Frame attribures
  53. """
  54. SingleMapFrame.__init__(
  55. self,
  56. parent=parent,
  57. giface=giface,
  58. title=title,
  59. Map=Map,
  60. auimgr=auimgr,
  61. name=name,
  62. **kwargs,
  63. )
  64. self._giface = giface
  65. # properties are shared in other objects, so defining here
  66. self.mapWindowProperties = MapWindowProperties()
  67. self.mapWindowProperties.setValuesFromUserSettings()
  68. self.mapWindowProperties.alignExtent = True
  69. #
  70. # Add toolbars
  71. #
  72. for toolb in toolbars:
  73. self.AddToolbar(toolb)
  74. self.activemap = self.toolbars["gcpdisp"].togglemap
  75. self.activemap.SetSelection(0)
  76. self.SrcMap = self.grwiz.SrcMap # instance of render.Map
  77. self.TgtMap = self.grwiz.TgtMap # instance of render.Map
  78. self._mgr.SetDockSizeConstraint(0.5, 0.5)
  79. #
  80. # Add statusbar
  81. #
  82. # items for choice
  83. self.statusbarItems = [
  84. sb.SbCoordinates,
  85. sb.SbRegionExtent,
  86. sb.SbCompRegionExtent,
  87. sb.SbShowRegion,
  88. sb.SbResolution,
  89. sb.SbDisplayGeometry,
  90. sb.SbMapScale,
  91. sb.SbProjection,
  92. sbgcp.SbGoToGCP,
  93. sbgcp.SbRMSError,
  94. ]
  95. # create statusbar and its manager
  96. statusbar = self.CreateStatusBar(number=4, style=0)
  97. statusbar.SetStatusWidths([-5, -2, -1, -1])
  98. self.statusbarManager = sb.SbManager(mapframe=self, statusbar=statusbar)
  99. # fill statusbar manager
  100. self.statusbarManager.AddStatusbarItemsByClass(
  101. self.statusbarItems, mapframe=self, statusbar=statusbar
  102. )
  103. self.statusbarManager.AddStatusbarItem(
  104. sb.SbMask(self, statusbar=statusbar, position=2)
  105. )
  106. self.statusbarManager.AddStatusbarItem(
  107. sb.SbRender(self, statusbar=statusbar, position=3)
  108. )
  109. self.statusbarManager.SetMode(8) # goto GCP
  110. #
  111. # Init map display (buffered DC & set default cursor)
  112. #
  113. self.grwiz.SwitchEnv("source")
  114. self.SrcMapWindow = BufferedMapWindow(
  115. parent=self,
  116. giface=self._giface,
  117. id=wx.ID_ANY,
  118. properties=self.mapWindowProperties,
  119. Map=self.SrcMap,
  120. )
  121. self.grwiz.SwitchEnv("target")
  122. self.TgtMapWindow = BufferedMapWindow(
  123. parent=self,
  124. giface=self._giface,
  125. id=wx.ID_ANY,
  126. properties=self.mapWindowProperties,
  127. Map=self.TgtMap,
  128. )
  129. self.MapWindow = self.SrcMapWindow
  130. self.Map = self.SrcMap
  131. self._setUpMapWindow(self.SrcMapWindow)
  132. self._setUpMapWindow(self.TgtMapWindow)
  133. self.SrcMapWindow.SetNamedCursor("cross")
  134. self.TgtMapWindow.SetNamedCursor("cross")
  135. # used to switch current map (combo box in toolbar)
  136. self.SrcMapWindow.mouseEntered.connect(
  137. lambda: self._setActiveMapWindow(self.SrcMapWindow)
  138. )
  139. self.TgtMapWindow.mouseEntered.connect(
  140. lambda: self._setActiveMapWindow(self.TgtMapWindow)
  141. )
  142. #
  143. # initialize region values
  144. #
  145. self._initMap(Map=self.SrcMap)
  146. self._initMap(Map=self.TgtMap)
  147. self.GetMapToolbar().SelectDefault()
  148. #
  149. # Bind various events
  150. #
  151. self.activemap.Bind(wx.EVT_CHOICE, self.OnUpdateActive)
  152. self.Bind(wx.EVT_SIZE, self.OnSize)
  153. #
  154. # Update fancy gui style
  155. #
  156. # AuiManager wants a CentrePane, workaround to get two equally sized
  157. # windows
  158. self.list = self.CreateGCPList()
  159. # self.SrcMapWindow.SetSize((300, 300))
  160. # self.TgtMapWindow.SetSize((300, 300))
  161. self.list.SetSize((100, 150))
  162. self._mgr.AddPane(
  163. self.list,
  164. wx.aui.AuiPaneInfo()
  165. .Name("gcplist")
  166. .Caption(_("GCP List"))
  167. .LeftDockable(False)
  168. .RightDockable(False)
  169. .PinButton()
  170. .FloatingSize((600, 200))
  171. .CloseButton(False)
  172. .DestroyOnClose(True)
  173. .Top()
  174. .Layer(1)
  175. .MinSize((200, 100)),
  176. )
  177. self._mgr.AddPane(
  178. self.SrcMapWindow,
  179. wx.aui.AuiPaneInfo()
  180. .Name("source")
  181. .Caption(_("Source Display"))
  182. .Dockable(False)
  183. .CloseButton(False)
  184. .DestroyOnClose(True)
  185. .Floatable(False)
  186. .Centre(),
  187. )
  188. self._mgr.AddPane(
  189. self.TgtMapWindow,
  190. wx.aui.AuiPaneInfo()
  191. .Name("target")
  192. .Caption(_("Target Display"))
  193. .Dockable(False)
  194. .CloseButton(False)
  195. .DestroyOnClose(True)
  196. .Floatable(False)
  197. .Right()
  198. .Layer(0),
  199. )
  200. srcwidth, srcheight = self.SrcMapWindow.GetSize()
  201. tgtwidth, tgtheight = self.TgtMapWindow.GetSize()
  202. srcwidth = (srcwidth + tgtwidth) / 2
  203. self._mgr.GetPane("target").Hide()
  204. self._mgr.Update()
  205. self._mgr.GetPane("source").BestSize((srcwidth, srcheight))
  206. self._mgr.GetPane("target").BestSize((srcwidth, srcheight))
  207. if self.show_target:
  208. self._mgr.GetPane("target").Show()
  209. else:
  210. self.activemap.Enable(False)
  211. # needed by Mac OS, does not harm on Linux, breaks display on Windows
  212. if platform.system() != "Windows":
  213. self._mgr.Update()
  214. #
  215. # Init print module and classes
  216. #
  217. self.printopt = PrintOptions(self, self.MapWindow)
  218. #
  219. # Initialization of digitization tool
  220. #
  221. self.digit = None
  222. # set active map
  223. self.MapWindow = self.SrcMapWindow
  224. self.Map = self.SrcMap
  225. # do not init zoom history here, that happens when zooming to map(s)
  226. #
  227. # Re-use dialogs
  228. #
  229. self.dialogs = {}
  230. self.dialogs["attributes"] = None
  231. self.dialogs["category"] = None
  232. self.dialogs["barscale"] = None
  233. self.dialogs["legend"] = None
  234. self.decorationDialog = None # decoration/overlays
  235. # doing nice things in statusbar when other things are ready
  236. self.statusbarManager.Update()
  237. def _setUpMapWindow(self, mapWindow):
  238. # TODO: almost the same implementation as for MapFrameBase (only names differ)
  239. # enable or disable zoom history tool
  240. mapWindow.zoomHistoryAvailable.connect(
  241. lambda: self.GetMapToolbar().Enable("zoomback", enable=True)
  242. )
  243. mapWindow.zoomHistoryUnavailable.connect(
  244. lambda: self.GetMapToolbar().Enable("zoomback", enable=False)
  245. )
  246. mapWindow.mouseMoving.connect(self.CoordinatesChanged)
  247. def AddToolbar(self, name):
  248. """Add defined toolbar to the window
  249. Currently known toolbars are:
  250. - 'map' - basic map toolbar
  251. - 'vdigit' - vector digitizer
  252. - 'gcpdisp' - GCP Manager, Display
  253. - 'gcpman' - GCP Manager, points management
  254. - 'nviz' - 3D view mode
  255. """
  256. # default toolbar
  257. if name == "map":
  258. self.toolbars["map"] = MapToolbar(self, self._toolSwitcher)
  259. self._mgr.AddPane(
  260. self.toolbars["map"],
  261. wx.aui.AuiPaneInfo()
  262. .Name("maptoolbar")
  263. .Caption(_("Map Toolbar"))
  264. .ToolbarPane()
  265. .Top()
  266. .LeftDockable(False)
  267. .RightDockable(False)
  268. .BottomDockable(False)
  269. .TopDockable(True)
  270. .CloseButton(False)
  271. .Layer(2)
  272. .BestSize((self.toolbars["map"].GetSize())),
  273. )
  274. # GCP display
  275. elif name == "gcpdisp":
  276. self.toolbars["gcpdisp"] = GCPDisplayToolbar(self, self._toolSwitcher)
  277. self._mgr.AddPane(
  278. self.toolbars["gcpdisp"],
  279. wx.aui.AuiPaneInfo()
  280. .Name("gcpdisplaytoolbar")
  281. .Caption(_("GCP Display toolbar"))
  282. .ToolbarPane()
  283. .Top()
  284. .LeftDockable(False)
  285. .RightDockable(False)
  286. .BottomDockable(False)
  287. .TopDockable(True)
  288. .CloseButton(False)
  289. .Layer(2),
  290. )
  291. if not self.show_target:
  292. self.toolbars["gcpdisp"].Enable("zoommenu", enable=False)
  293. self.toolbars["gcpman"] = GCPManToolbar(self)
  294. self._mgr.AddPane(
  295. self.toolbars["gcpman"],
  296. wx.aui.AuiPaneInfo()
  297. .Name("gcpmanagertoolbar")
  298. .Caption(_("GCP Manager toolbar"))
  299. .ToolbarPane()
  300. .Top()
  301. .Row(1)
  302. .LeftDockable(False)
  303. .RightDockable(False)
  304. .BottomDockable(False)
  305. .TopDockable(True)
  306. .CloseButton(False)
  307. .Layer(2),
  308. )
  309. self._mgr.Update()
  310. def OnUpdateProgress(self, event):
  311. """
  312. Update progress bar info
  313. """
  314. self.GetProgressBar().UpdateProgress(event.layer, event.map)
  315. event.Skip()
  316. def OnFocus(self, event):
  317. """
  318. Change choicebook page to match display.
  319. Or set display for georectifying
  320. """
  321. # was in if layer manager but considering the state it was executed
  322. # always, moreover, there is no layer manager dependent code
  323. # in GCP Management, set focus to current MapWindow for mouse actions
  324. self.OnPointer(event)
  325. self.MapWindow.SetFocus()
  326. event.Skip()
  327. def OnDraw(self, event):
  328. """Re-display current map composition"""
  329. self.MapWindow.UpdateMap(render=False)
  330. def OnRender(self, event):
  331. """Re-render map composition (each map layer)"""
  332. # FIXME: remove qlayer code or use RemoveQueryLayer() now in mapdisp.frame
  333. # delete tmp map layers (queries)
  334. qlayer = self.Map.GetListOfLayers(name=globalvar.QUERYLAYER)
  335. for layer in qlayer:
  336. self.Map.DeleteLayer(layer)
  337. self.SrcMapWindow.UpdateMap(render=True)
  338. if self.show_target:
  339. self.TgtMapWindow.UpdateMap(render=True)
  340. # update statusbar
  341. self.StatusbarUpdate()
  342. def OnPointer(self, event):
  343. """Pointer button clicked"""
  344. self.SrcMapWindow.SetModePointer()
  345. self.TgtMapWindow.SetModePointer()
  346. # change the default cursor
  347. self.SrcMapWindow.SetNamedCursor("cross")
  348. self.TgtMapWindow.SetNamedCursor("cross")
  349. def OnZoomIn(self, event):
  350. """Zoom in the map."""
  351. self.SrcMapWindow.SetModeZoomIn()
  352. self.TgtMapWindow.SetModeZoomIn()
  353. def OnZoomOut(self, event):
  354. """Zoom out the map."""
  355. self.SrcMapWindow.SetModeZoomOut()
  356. self.TgtMapWindow.SetModeZoomOut()
  357. def OnPan(self, event):
  358. """Panning, set mouse to drag"""
  359. self.SrcMapWindow.SetModePan()
  360. self.TgtMapWindow.SetModePan()
  361. def OnErase(self, event):
  362. """
  363. Erase the canvas
  364. """
  365. self.MapWindow.EraseMap()
  366. if self.MapWindow == self.SrcMapWindow:
  367. win = self.TgtMapWindow
  368. elif self.MapWindow == self.TgtMapWindow:
  369. win = self.SrcMapWindow
  370. win.EraseMap()
  371. def SaveToFile(self, event):
  372. """Save map to image"""
  373. img = self.MapWindow.img
  374. if not img:
  375. GMessage(
  376. parent=self,
  377. message=_("Nothing to render (empty map). Operation canceled."),
  378. )
  379. return
  380. filetype, ltype = GetImageHandlers(img)
  381. # get size
  382. dlg = ImageSizeDialog(self)
  383. dlg.CentreOnParent()
  384. if dlg.ShowModal() != wx.ID_OK:
  385. dlg.Destroy()
  386. return
  387. width, height = dlg.GetValues()
  388. dlg.Destroy()
  389. # get filename
  390. dlg = wx.FileDialog(
  391. parent=self,
  392. message=_(
  393. "Choose a file name to save the image " "(no need to add extension)"
  394. ),
  395. wildcard=filetype,
  396. style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT,
  397. )
  398. if dlg.ShowModal() == wx.ID_OK:
  399. path = dlg.GetPath()
  400. if not path:
  401. dlg.Destroy()
  402. return
  403. base, ext = os.path.splitext(path)
  404. fileType = ltype[dlg.GetFilterIndex()]["type"]
  405. extType = ltype[dlg.GetFilterIndex()]["ext"]
  406. if ext != extType:
  407. path = base + "." + extType
  408. self.MapWindow.SaveToFile(path, fileType, width, height)
  409. dlg.Destroy()
  410. def PrintMenu(self, event):
  411. """
  412. Print options and output menu for map display
  413. """
  414. point = wx.GetMousePosition()
  415. printmenu = Menu()
  416. # Add items to the menu
  417. setup = wx.MenuItem(printmenu, wx.ID_ANY, _("Page setup"))
  418. printmenu.AppendItem(setup)
  419. self.Bind(wx.EVT_MENU, self.printopt.OnPageSetup, setup)
  420. preview = wx.MenuItem(printmenu, wx.ID_ANY, _("Print preview"))
  421. printmenu.AppendItem(preview)
  422. self.Bind(wx.EVT_MENU, self.printopt.OnPrintPreview, preview)
  423. doprint = wx.MenuItem(printmenu, wx.ID_ANY, _("Print display"))
  424. printmenu.AppendItem(doprint)
  425. self.Bind(wx.EVT_MENU, self.printopt.OnDoPrint, doprint)
  426. # Popup the menu. If an item is selected then its handler
  427. # will be called before PopupMenu returns.
  428. self.PopupMenu(printmenu)
  429. printmenu.Destroy()
  430. def OnZoomToRaster(self, event):
  431. """
  432. Set display extents to match selected raster map (ignore NULLs)
  433. """
  434. self.MapWindow.ZoomToMap(ignoreNulls=True)
  435. def OnZoomToSaved(self, event):
  436. """Set display geometry to match extents in
  437. saved region file
  438. """
  439. self.MapWindow.SetRegion(zoomOnly=True)
  440. def OnDisplayToWind(self, event):
  441. """Set computational region (WIND file) to match display
  442. extents
  443. """
  444. self.MapWindow.DisplayToWind()
  445. def SaveDisplayRegion(self, event):
  446. """Save display extents to named region file."""
  447. self.MapWindow.SaveDisplayRegion()
  448. def OnZoomMenu(self, event):
  449. """Popup Zoom menu"""
  450. point = wx.GetMousePosition()
  451. zoommenu = Menu()
  452. # Add items to the menu
  453. zoomwind = wx.MenuItem(
  454. zoommenu, wx.ID_ANY, _("Zoom to computational region (set with g.region)")
  455. )
  456. zoommenu.AppendItem(zoomwind)
  457. self.Bind(wx.EVT_MENU, self.OnZoomToWind, zoomwind)
  458. zoomdefault = wx.MenuItem(zoommenu, wx.ID_ANY, _("Zoom to default region"))
  459. zoommenu.AppendItem(zoomdefault)
  460. self.Bind(wx.EVT_MENU, self.OnZoomToDefault, zoomdefault)
  461. zoomsaved = wx.MenuItem(zoommenu, wx.ID_ANY, _("Zoom to saved region"))
  462. zoommenu.AppendItem(zoomsaved)
  463. self.Bind(wx.EVT_MENU, self.OnZoomToSaved, zoomsaved)
  464. savewind = wx.MenuItem(
  465. zoommenu, wx.ID_ANY, _("Set computational region from display")
  466. )
  467. zoommenu.AppendItem(savewind)
  468. self.Bind(wx.EVT_MENU, self.OnDisplayToWind, savewind)
  469. savezoom = wx.MenuItem(
  470. zoommenu, wx.ID_ANY, _("Save display geometry to named region")
  471. )
  472. zoommenu.AppendItem(savezoom)
  473. self.Bind(wx.EVT_MENU, self.SaveDisplayRegion, savezoom)
  474. # Popup the menu. If an item is selected then its handler
  475. # will be called before PopupMenu returns.
  476. self.PopupMenu(zoommenu)
  477. zoommenu.Destroy()
  478. def IsStandalone(self):
  479. """Check if Map display is standalone"""
  480. # we do not know and we do not care, so always False
  481. return True
  482. def GetLayerManager(self):
  483. """Get reference to Layer Manager
  484. :return: always None
  485. """
  486. return None
  487. def GetSrcWindow(self):
  488. return self.SrcMapWindow
  489. def GetTgtWindow(self):
  490. return self.TgtMapWindow
  491. def GetShowTarget(self):
  492. return self.show_target
  493. def GetMapToolbar(self):
  494. """Returns toolbar with zooming tools"""
  495. return self.toolbars["gcpdisp"]
  496. def _setActiveMapWindow(self, mapWindow):
  497. if not self.MapWindow == mapWindow:
  498. self.MapWindow = mapWindow
  499. self.Map = mapWindow.Map
  500. self.UpdateActive(mapWindow)
  501. # needed for wingrass
  502. self.SetFocus()