ip2i_mapdisplay.py 18 KB

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