mapdisplay.py 17 KB

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