ip2i_mapdisplay.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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::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=_("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. 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. - 'gcpdisp' - GCP Manager, Display
  252. - 'gcpman' - GCP Manager, points management
  253. - 'nviz' - 3D view mode
  254. """
  255. # default toolbar
  256. if name == "map":
  257. self.toolbars["map"] = MapToolbar(self, self._toolSwitcher)
  258. self._mgr.AddPane(
  259. self.toolbars["map"],
  260. wx.aui.AuiPaneInfo()
  261. .Name("maptoolbar")
  262. .Caption(_("Map Toolbar"))
  263. .ToolbarPane()
  264. .Top()
  265. .LeftDockable(False)
  266. .RightDockable(False)
  267. .BottomDockable(False)
  268. .TopDockable(True)
  269. .CloseButton(False)
  270. .Layer(2)
  271. .BestSize((self.toolbars["map"].GetSize())),
  272. )
  273. # GCP display
  274. elif name == "gcpdisp":
  275. self.toolbars["gcpdisp"] = GCPDisplayToolbar(self, self._toolSwitcher)
  276. self._mgr.AddPane(
  277. self.toolbars["gcpdisp"],
  278. wx.aui.AuiPaneInfo()
  279. .Name("gcpdisplaytoolbar")
  280. .Caption(_("GCP Display toolbar"))
  281. .ToolbarPane()
  282. .Top()
  283. .LeftDockable(False)
  284. .RightDockable(False)
  285. .BottomDockable(False)
  286. .TopDockable(True)
  287. .CloseButton(False)
  288. .Layer(2),
  289. )
  290. if self.show_target is False:
  291. self.toolbars["gcpdisp"].Enable("zoommenu", enable=False)
  292. self.toolbars["gcpman"] = GCPManToolbar(self)
  293. self._mgr.AddPane(
  294. self.toolbars["gcpman"],
  295. wx.aui.AuiPaneInfo()
  296. .Name("gcpmanagertoolbar")
  297. .Caption(_("GCP Manager toolbar"))
  298. .ToolbarPane()
  299. .Top()
  300. .Row(1)
  301. .LeftDockable(False)
  302. .RightDockable(False)
  303. .BottomDockable(False)
  304. .TopDockable(True)
  305. .CloseButton(False)
  306. .Layer(2),
  307. )
  308. self._mgr.Update()
  309. def OnUpdateProgress(self, event):
  310. """
  311. Update progress bar info
  312. """
  313. self.GetProgressBar().UpdateProgress(event.layer, event.map)
  314. event.Skip()
  315. def OnFocus(self, event):
  316. """
  317. Change choicebook page to match display.
  318. Or set display for georectifying
  319. """
  320. # was in if layer manager but considering the state it was executed
  321. # always, moreover, there is no layer manager dependent code
  322. # in GCP Management, set focus to current MapWindow for mouse actions
  323. self.OnPointer(event)
  324. self.MapWindow.SetFocus()
  325. event.Skip()
  326. def OnDraw(self, event):
  327. """Re-display current map composition"""
  328. self.MapWindow.UpdateMap(render=False)
  329. def OnRender(self, event):
  330. """Re-render map composition (each map layer)"""
  331. # FIXME: remove qlayer code or use RemoveQueryLayer() now in mapdisp.frame
  332. # delete tmp map layers (queries)
  333. qlayer = self.Map.GetListOfLayers(name=globalvar.QUERYLAYER)
  334. for layer in qlayer:
  335. self.Map.DeleteLayer(layer)
  336. self.SrcMapWindow.UpdateMap(render=True)
  337. if self.show_target:
  338. self.TgtMapWindow.UpdateMap(render=True)
  339. # update statusbar
  340. self.StatusbarUpdate()
  341. def OnPointer(self, event):
  342. """Pointer button clicked"""
  343. self.SrcMapWindow.SetModePointer()
  344. self.TgtMapWindow.SetModePointer()
  345. # change the default cursor
  346. self.SrcMapWindow.SetNamedCursor("cross")
  347. self.TgtMapWindow.SetNamedCursor("cross")
  348. def OnZoomIn(self, event):
  349. """Zoom in the map."""
  350. self.SrcMapWindow.SetModeZoomIn()
  351. self.TgtMapWindow.SetModeZoomIn()
  352. def OnZoomOut(self, event):
  353. """Zoom out the map."""
  354. self.SrcMapWindow.SetModeZoomOut()
  355. self.TgtMapWindow.SetModeZoomOut()
  356. def OnPan(self, event):
  357. """Panning, set mouse to drag"""
  358. self.SrcMapWindow.SetModePan()
  359. self.TgtMapWindow.SetModePan()
  360. def OnErase(self, event):
  361. """
  362. Erase the canvas
  363. """
  364. self.MapWindow.EraseMap()
  365. if self.MapWindow == self.SrcMapWindow:
  366. win = self.TgtMapWindow
  367. elif self.MapWindow == self.TgtMapWindow:
  368. win = self.SrcMapWindow
  369. win.EraseMap()
  370. def SaveToFile(self, event):
  371. """Save map to image"""
  372. img = self.MapWindow.img
  373. if not img:
  374. GMessage(
  375. parent=self,
  376. message=_("Nothing to render (empty map). Operation canceled."),
  377. )
  378. return
  379. filetype, ltype = GetImageHandlers(img)
  380. # get size
  381. dlg = ImageSizeDialog(self)
  382. dlg.CentreOnParent()
  383. if dlg.ShowModal() != wx.ID_OK:
  384. dlg.Destroy()
  385. return
  386. width, height = dlg.GetValues()
  387. dlg.Destroy()
  388. # get filename
  389. dlg = wx.FileDialog(
  390. parent=self,
  391. message=_(
  392. "Choose a file name to save the image " "(no need to add extension)"
  393. ),
  394. wildcard=filetype,
  395. style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT,
  396. )
  397. if dlg.ShowModal() == wx.ID_OK:
  398. path = dlg.GetPath()
  399. if not path:
  400. dlg.Destroy()
  401. return
  402. base, ext = os.path.splitext(path)
  403. fileType = ltype[dlg.GetFilterIndex()]["type"]
  404. extType = ltype[dlg.GetFilterIndex()]["ext"]
  405. if ext != extType:
  406. path = base + "." + extType
  407. self.MapWindow.SaveToFile(path, fileType, width, height)
  408. dlg.Destroy()
  409. def PrintMenu(self, event):
  410. """
  411. Print options and output menu for map display
  412. """
  413. point = wx.GetMousePosition()
  414. printmenu = Menu()
  415. # Add items to the menu
  416. setup = wx.MenuItem(printmenu, wx.ID_ANY, _("Page setup"))
  417. printmenu.AppendItem(setup)
  418. self.Bind(wx.EVT_MENU, self.printopt.OnPageSetup, setup)
  419. preview = wx.MenuItem(printmenu, wx.ID_ANY, _("Print preview"))
  420. printmenu.AppendItem(preview)
  421. self.Bind(wx.EVT_MENU, self.printopt.OnPrintPreview, preview)
  422. doprint = wx.MenuItem(printmenu, wx.ID_ANY, _("Print display"))
  423. printmenu.AppendItem(doprint)
  424. self.Bind(wx.EVT_MENU, self.printopt.OnDoPrint, doprint)
  425. # Popup the menu. If an item is selected then its handler
  426. # will be called before PopupMenu returns.
  427. self.PopupMenu(printmenu)
  428. printmenu.Destroy()
  429. def OnZoomToRaster(self, event):
  430. """
  431. Set display extents to match selected raster map (ignore NULLs)
  432. """
  433. self.MapWindow.ZoomToMap(ignoreNulls=True)
  434. def OnZoomToSaved(self, event):
  435. """Set display geometry to match extents in
  436. saved region file
  437. """
  438. self.MapWindow.SetRegion(zoomOnly=True)
  439. def OnDisplayToWind(self, event):
  440. """Set computational region (WIND file) to match display
  441. extents
  442. """
  443. self.MapWindow.DisplayToWind()
  444. def SaveDisplayRegion(self, event):
  445. """Save display extents to named region file."""
  446. self.MapWindow.SaveDisplayRegion()
  447. def OnZoomMenu(self, event):
  448. """Popup Zoom menu"""
  449. point = wx.GetMousePosition()
  450. zoommenu = Menu()
  451. # Add items to the menu
  452. zoomwind = wx.MenuItem(
  453. zoommenu, wx.ID_ANY, _("Zoom to computational region (set with g.region)")
  454. )
  455. zoommenu.AppendItem(zoomwind)
  456. self.Bind(wx.EVT_MENU, self.OnZoomToWind, zoomwind)
  457. zoomdefault = wx.MenuItem(zoommenu, wx.ID_ANY, _("Zoom to default region"))
  458. zoommenu.AppendItem(zoomdefault)
  459. self.Bind(wx.EVT_MENU, self.OnZoomToDefault, zoomdefault)
  460. zoomsaved = wx.MenuItem(zoommenu, wx.ID_ANY, _("Zoom to saved region"))
  461. zoommenu.AppendItem(zoomsaved)
  462. self.Bind(wx.EVT_MENU, self.OnZoomToSaved, zoomsaved)
  463. savewind = wx.MenuItem(
  464. zoommenu, wx.ID_ANY, _("Set computational region from display")
  465. )
  466. zoommenu.AppendItem(savewind)
  467. self.Bind(wx.EVT_MENU, self.OnDisplayToWind, savewind)
  468. savezoom = wx.MenuItem(
  469. zoommenu, wx.ID_ANY, _("Save display geometry to named region")
  470. )
  471. zoommenu.AppendItem(savezoom)
  472. self.Bind(wx.EVT_MENU, self.SaveDisplayRegion, savezoom)
  473. # Popup the menu. If an item is selected then its handler
  474. # will be called before PopupMenu returns.
  475. self.PopupMenu(zoommenu)
  476. zoommenu.Destroy()
  477. def IsStandalone(self):
  478. """Check if Map display is standalone"""
  479. # we do not know and we do not care, so always False
  480. return True
  481. def GetLayerManager(self):
  482. """Get reference to Layer Manager
  483. :return: always None
  484. """
  485. return None
  486. def GetSrcWindow(self):
  487. return self.SrcMapWindow
  488. def GetTgtWindow(self):
  489. return self.TgtMapWindow
  490. def GetShowTarget(self):
  491. return self.show_target
  492. def GetMapToolbar(self):
  493. """Returns toolbar with zooming tools"""
  494. return self.toolbars["gcpdisp"]
  495. def _setActiveMapWindow(self, mapWindow):
  496. if not self.MapWindow == mapWindow:
  497. self.MapWindow = mapWindow
  498. self.Map = mapWindow.Map
  499. self.UpdateActive(mapWindow)
  500. # needed for wingrass
  501. self.SetFocus()