manager.py 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839
  1. """!
  2. @package gcp.manager
  3. @brief Georectification module for GRASS GIS. Includes ground control
  4. point management and interactive point and click GCP creation
  5. Classes:
  6. - manager::GCPWizard
  7. - manager::LocationPage
  8. - manager::GroupPage
  9. - manager::DispMapPage
  10. - manager::GCP
  11. - manager::GCPList
  12. - manager::VectGroup
  13. - manager::EditGCP
  14. - manager::GrSettingsDialog
  15. (C) 2006-2012 by the GRASS Development Team
  16. This program is free software under the GNU General Public License
  17. (>=v2). Read the file COPYING that comes with GRASS for details.
  18. @author Original author Michael Barton
  19. @author Original version improved by Martin Landa <landa.martin gmail.com>
  20. @author Rewritten by Markus Metz redesign georectfier -> GCP Manage
  21. @author Support for GraphicsSet added by Stepan Turek <stepan.turek seznam.cz> (2012)
  22. """
  23. import os
  24. import sys
  25. import shutil
  26. import time
  27. from copy import copy
  28. import wx
  29. from wx.lib.mixins.listctrl import CheckListCtrlMixin, ColumnSorterMixin, ListCtrlAutoWidthMixin
  30. import wx.lib.colourselect as csel
  31. import wx.wizard as wiz
  32. import grass.script as grass
  33. from core import globalvar
  34. from core import utils
  35. from core.render import Map
  36. from core.utils import _
  37. from gui_core.gselect import Select, LocationSelect, MapsetSelect
  38. from gui_core.dialogs import GroupDialog
  39. from core.gcmd import RunCommand, GMessage, GError, GWarning
  40. from core.settings import UserSettings
  41. from gcp.mapdisplay import MapFrame
  42. from location_wizard.wizard import TitledPage as TitledPage
  43. #
  44. # global variables
  45. #
  46. global src_map
  47. global tgt_map
  48. global maptype
  49. src_map = ''
  50. tgt_map = { 'raster' : '',
  51. 'vector' : '' }
  52. maptype = 'raster'
  53. def getSmallUpArrowImage():
  54. stream = open(os.path.join(globalvar.ETCIMGDIR, 'small_up_arrow.png'), 'rb')
  55. try:
  56. img = wx.ImageFromStream(stream)
  57. finally:
  58. stream.close()
  59. return img
  60. def getSmallDnArrowImage():
  61. stream = open(os.path.join(globalvar.ETCIMGDIR, 'small_down_arrow.png'), 'rb')
  62. try:
  63. img = wx.ImageFromStream(stream)
  64. finally:
  65. stream.close()
  66. stream.close()
  67. return img
  68. class GCPWizard(object):
  69. """
  70. Start wizard here and finish wizard here
  71. """
  72. def __init__(self, parent, giface):
  73. self.parent = parent # GMFrame
  74. self._giface = giface
  75. #
  76. # get environmental variables
  77. #
  78. self.grassdatabase = grass.gisenv()['GISDBASE']
  79. #
  80. # read original environment settings
  81. #
  82. self.target_gisrc = os.environ['GISRC']
  83. self.gisrc_dict = {}
  84. try:
  85. f = open(self.target_gisrc, 'r')
  86. for line in f.readlines():
  87. line = line.replace('\n', '').strip()
  88. if len(line) < 1:
  89. continue
  90. key, value = line.split(':', 1)
  91. self.gisrc_dict[key.strip()] = value.strip()
  92. finally:
  93. f.close()
  94. self.currentlocation = self.gisrc_dict['LOCATION_NAME']
  95. self.currentmapset = self.gisrc_dict['MAPSET']
  96. # location for xy map to georectify
  97. self.newlocation = ''
  98. # mapset for xy map to georectify
  99. self.newmapset = ''
  100. global maptype
  101. global src_map
  102. global tgt_map
  103. #src_map = ''
  104. #tgt_map = ''
  105. maptype = 'raster'
  106. # GISRC file for source location/mapset of map(s) to georectify
  107. self.source_gisrc = ''
  108. self.src_maps = []
  109. #
  110. # define wizard pages
  111. #
  112. self.wizard = wiz.Wizard(parent=parent, id=wx.ID_ANY, title=_("Setup for georectification"))
  113. self.startpage = LocationPage(self.wizard, self)
  114. self.grouppage = GroupPage(self.wizard, self)
  115. self.mappage = DispMapPage(self.wizard, self)
  116. #
  117. # set the initial order of the pages
  118. #
  119. self.startpage.SetNext(self.grouppage)
  120. self.grouppage.SetPrev(self.startpage)
  121. self.grouppage.SetNext(self.mappage)
  122. self.mappage.SetPrev(self.grouppage)
  123. #
  124. # do pages layout
  125. #
  126. self.startpage.DoLayout()
  127. self.grouppage.DoLayout()
  128. self.mappage.DoLayout()
  129. self.wizard.FitToPage(self.startpage)
  130. # self.Bind(wx.EVT_CLOSE, self.Cleanup)
  131. # self.parent.Bind(wx.EVT_ACTIVATE, self.OnGLMFocus)
  132. success = False
  133. #
  134. # run wizard
  135. #
  136. if self.wizard.RunWizard(self.startpage):
  137. success = self.OnWizFinished()
  138. if success == False:
  139. GMessage(parent = self.parent,
  140. message = _("Georectifying setup canceled."))
  141. self.Cleanup()
  142. else:
  143. GMessage(parent = self.parent,
  144. message = _("Georectifying setup canceled."))
  145. self.Cleanup()
  146. #
  147. # start GCP display
  148. #
  149. if success != False:
  150. # instance of render.Map to be associated with display
  151. self.SwitchEnv('source')
  152. self.SrcMap = Map(gisrc=self.source_gisrc)
  153. self.SwitchEnv('target')
  154. self.TgtMap = Map(gisrc=self.target_gisrc)
  155. self.Map = self.SrcMap
  156. #
  157. # add layer to source map
  158. #
  159. if maptype == 'raster':
  160. rendertype = 'raster'
  161. cmdlist = ['d.rast', 'map=%s' % src_map]
  162. else: # -> vector layer
  163. rendertype = 'vector'
  164. cmdlist = ['d.vect', 'map=%s' % src_map]
  165. self.SwitchEnv('source')
  166. name, found = utils.GetLayerNameFromCmd(cmdlist)
  167. self.SrcMap.AddLayer(ltype=rendertype, command=cmdlist, active=True,
  168. name=name, hidden=False, opacity=1.0, render=False)
  169. self.SwitchEnv('target')
  170. if tgt_map['raster']:
  171. #
  172. # add raster layer to target map
  173. #
  174. rendertype = 'raster'
  175. cmdlist = ['d.rast', 'map=%s' % tgt_map['raster']]
  176. name, found = utils.GetLayerNameFromCmd(cmdlist)
  177. self.TgtMap.AddLayer(ltype=rendertype, command=cmdlist, active=True,
  178. name=name, hidden=False, opacity=1.0, render=False)
  179. if tgt_map['vector']:
  180. #
  181. # add raster layer to target map
  182. #
  183. rendertype = 'vector'
  184. cmdlist = ['d.vect', 'map=%s' % tgt_map['vector']]
  185. name, found = utils.GetLayerNameFromCmd(cmdlist)
  186. self.TgtMap.AddLayer(ltype=rendertype, command=cmdlist, active=True,
  187. name=name, hidden=False, opacity=1.0, render=False)
  188. #
  189. # start GCP Manager
  190. #
  191. self.gcpmgr = GCP(self.parent, giface = self._giface,
  192. grwiz=self, size=globalvar.MAP_WINDOW_SIZE,
  193. toolbars=["gcpdisp"],
  194. Map=self.SrcMap, lmgr=self.parent)
  195. # load GCPs
  196. self.gcpmgr.InitMapDisplay()
  197. self.gcpmgr.CenterOnScreen()
  198. self.gcpmgr.Show()
  199. # need to update AUI here for wingrass
  200. self.gcpmgr._mgr.Update()
  201. else:
  202. self.Cleanup()
  203. def SetSrcEnv(self, location, mapset):
  204. """!Create environment to use for location and mapset
  205. that are the source of the file(s) to georectify
  206. @param location source location
  207. @param mapset source mapset
  208. @return False on error
  209. @return True on success
  210. """
  211. self.newlocation = location
  212. self.newmapset = mapset
  213. # check to see if we are georectifying map in current working location/mapset
  214. if self.newlocation == self.currentlocation and self.newmapset == self.currentmapset:
  215. return False
  216. self.gisrc_dict['LOCATION_NAME'] = location
  217. self.gisrc_dict['MAPSET'] = mapset
  218. self.source_gisrc = utils.GetTempfile()
  219. try:
  220. f = open(self.source_gisrc, mode='w')
  221. for line in self.gisrc_dict.items():
  222. f.write(line[0] + ": " + line[1] + "\n")
  223. finally:
  224. f.close()
  225. return True
  226. def SwitchEnv(self, grc):
  227. """
  228. Switches between original working location/mapset and
  229. location/mapset that is source of file(s) to georectify
  230. """
  231. # check to see if we are georectifying map in current working location/mapset
  232. if self.newlocation == self.currentlocation and self.newmapset == self.currentmapset:
  233. return False
  234. if grc == 'target':
  235. os.environ['GISRC'] = str(self.target_gisrc)
  236. elif grc == 'source':
  237. os.environ['GISRC'] = str(self.source_gisrc)
  238. return True
  239. def OnWizFinished(self):
  240. # self.Cleanup()
  241. return True
  242. def OnGLMFocus(self, event):
  243. """!Layer Manager focus"""
  244. # self.SwitchEnv('target')
  245. event.Skip()
  246. def Cleanup(self):
  247. """!Return to current location and mapset"""
  248. self.SwitchEnv('target')
  249. self.parent.gcpmanagement = None
  250. self.wizard.Destroy()
  251. class LocationPage(TitledPage):
  252. """
  253. Set map type (raster or vector) to georectify and
  254. select location/mapset of map(s) to georectify.
  255. """
  256. def __init__(self, wizard, parent):
  257. TitledPage.__init__(self, wizard, _("Select map type and location/mapset"))
  258. self.parent = parent
  259. self.grassdatabase = self.parent.grassdatabase
  260. self.xylocation = ''
  261. self.xymapset = ''
  262. #
  263. # layout
  264. #
  265. # map type
  266. self.rb_maptype = wx.RadioBox(parent=self, id=wx.ID_ANY,
  267. label=' %s ' % _("Map type to georectify"),
  268. choices=[_('raster'), _('vector')],
  269. majorDimension=wx.RA_SPECIFY_COLS)
  270. self.sizer.Add(item=self.rb_maptype,
  271. flag=wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, border=5,
  272. pos=(1, 1), span=(1, 2))
  273. # location
  274. self.sizer.Add(item=wx.StaticText(parent=self, id=wx.ID_ANY, label=_('Select source location:')),
  275. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  276. pos=(2, 1))
  277. self.cb_location = LocationSelect(parent = self, gisdbase = self.grassdatabase)
  278. self.sizer.Add(item=self.cb_location,
  279. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  280. pos=(2, 2))
  281. # mapset
  282. self.sizer.Add(item=wx.StaticText(parent=self, id=wx.ID_ANY, label=_('Select source mapset:')),
  283. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  284. pos=(3, 1))
  285. self.cb_mapset = MapsetSelect(parent = self, gisdbase = self.grassdatabase,
  286. setItems = False)
  287. self.sizer.Add(item=self.cb_mapset,
  288. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  289. pos=(3,2))
  290. self.sizer.AddGrowableCol(2)
  291. #
  292. # bindings
  293. #
  294. self.Bind(wx.EVT_RADIOBOX, self.OnMaptype, self.rb_maptype)
  295. self.Bind(wx.EVT_COMBOBOX, self.OnLocation, self.cb_location)
  296. self.Bind(wx.EVT_COMBOBOX, self.OnMapset, self.cb_mapset)
  297. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  298. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  299. # self.Bind(wx.EVT_CLOSE, self.parent.Cleanup)
  300. def OnMaptype(self,event):
  301. """!Change map type"""
  302. global maptype
  303. if event.GetInt() == 0:
  304. maptype = 'raster'
  305. else:
  306. maptype = 'vector'
  307. def OnLocation(self, event):
  308. """!Sets source location for map(s) to georectify"""
  309. self.xylocation = event.GetString()
  310. #create a list of valid mapsets
  311. tmplist = os.listdir(os.path.join(self.grassdatabase, self.xylocation))
  312. self.mapsetList = []
  313. for item in tmplist:
  314. if os.path.isdir(os.path.join(self.grassdatabase, self.xylocation, item)) and \
  315. os.path.exists(os.path.join(self.grassdatabase, self.xylocation, item, 'WIND')):
  316. if item != 'PERMANENT':
  317. self.mapsetList.append(item)
  318. self.xymapset = 'PERMANENT'
  319. utils.ListSortLower(self.mapsetList)
  320. self.mapsetList.insert(0, 'PERMANENT')
  321. self.cb_mapset.SetItems(self.mapsetList)
  322. self.cb_mapset.SetStringSelection(self.xymapset)
  323. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  324. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  325. def OnMapset(self, event):
  326. """!Sets source mapset for map(s) to georectify"""
  327. if self.xylocation == '':
  328. GMessage(_('You must select a valid location '
  329. 'before selecting a mapset'),
  330. parent = self)
  331. return
  332. self.xymapset = event.GetString()
  333. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  334. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  335. def OnPageChanging(self, event=None):
  336. if event.GetDirection() and \
  337. (self.xylocation == '' or self.xymapset == ''):
  338. GMessage(_('You must select a valid location '
  339. 'and mapset in order to continue'),
  340. parent = self)
  341. event.Veto()
  342. return
  343. self.parent.SetSrcEnv(self.xylocation, self.xymapset)
  344. def OnEnterPage(self, event=None):
  345. if self.xylocation == '' or self.xymapset == '':
  346. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  347. else:
  348. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  349. class GroupPage(TitledPage):
  350. """
  351. Set group to georectify. Create group if desired.
  352. """
  353. def __init__(self, wizard, parent):
  354. TitledPage.__init__(self, wizard, _("Select image/map group to georectify"))
  355. self.parent = parent
  356. self.grassdatabase = self.parent.grassdatabase
  357. self.groupList = []
  358. self.xylocation = ''
  359. self.xymapset = ''
  360. self.xygroup = ''
  361. # default extension
  362. self.extension = '_georect' + str(os.getpid())
  363. #
  364. # layout
  365. #
  366. # group
  367. self.sizer.Add(item=wx.StaticText(parent=self, id=wx.ID_ANY, label=_('Select group:')),
  368. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  369. pos=(1, 1))
  370. self.cb_group = wx.ComboBox(parent=self, id=wx.ID_ANY,
  371. choices=self.groupList, size=(350, -1),
  372. style=wx.CB_DROPDOWN | wx.CB_READONLY)
  373. self.sizer.Add(item=self.cb_group,
  374. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  375. pos=(1, 2))
  376. # create group
  377. self.sizer.Add(item=wx.StaticText(parent=self, id=wx.ID_ANY, label=_('Create group if none exists')),
  378. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  379. pos=(2, 1))
  380. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  381. self.btn_mkgroup = wx.Button(parent=self, id=wx.ID_ANY, label=_("Create/edit group..."))
  382. self.btn_vgroup = wx.Button(parent=self, id=wx.ID_ANY, label=_("Add vector map to group..."))
  383. btnSizer.Add(item=self.btn_mkgroup,
  384. flag=wx.RIGHT, border=5)
  385. btnSizer.Add(item=self.btn_vgroup,
  386. flag=wx.LEFT, border=5)
  387. self.sizer.Add(item=btnSizer,
  388. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  389. pos=(2, 2))
  390. # extension
  391. self.sizer.Add(item=wx.StaticText(parent=self, id=wx.ID_ANY, label=_('Extension for output maps:')),
  392. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  393. pos=(3, 1))
  394. self.ext_txt = wx.TextCtrl(parent=self, id=wx.ID_ANY, value="", size=(350,-1))
  395. self.ext_txt.SetValue(self.extension)
  396. self.sizer.Add(item=self.ext_txt,
  397. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  398. pos=(3, 2))
  399. self.sizer.AddGrowableCol(2)
  400. #
  401. # bindings
  402. #
  403. self.Bind(wx.EVT_COMBOBOX, self.OnGroup, self.cb_group)
  404. self.Bind(wx.EVT_TEXT, self.OnExtension, self.ext_txt)
  405. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  406. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  407. self.Bind(wx.EVT_CLOSE, self.parent.Cleanup)
  408. # hide vector group button by default
  409. self.btn_vgroup.Hide()
  410. def OnGroup(self, event):
  411. self.xygroup = event.GetString()
  412. def OnMkGroup(self, event):
  413. """!Create new group in source location/mapset"""
  414. dlg = GroupDialog(parent = self, defaultGroup = self.xygroup)
  415. dlg.ShowModal()
  416. gr = dlg.GetSelectedGroup()
  417. if gr in dlg.GetExistGroups():
  418. self.xygroup = gr
  419. else:
  420. gr = ''
  421. dlg.Destroy()
  422. self.OnEnterPage()
  423. self.Update()
  424. def OnVGroup(self, event):
  425. """!Add vector maps to group"""
  426. dlg = VectGroup(parent = self,
  427. id = wx.ID_ANY,
  428. grassdb = self.grassdatabase,
  429. location = self.xylocation,
  430. mapset = self.xymapset,
  431. group = self.xygroup)
  432. if dlg.ShowModal() != wx.ID_OK:
  433. return
  434. dlg.MakeVGroup()
  435. self.OnEnterPage()
  436. def OnExtension(self, event):
  437. self.extension = self.ext_txt.GetValue()
  438. def OnPageChanging(self, event=None):
  439. if event.GetDirection() and self.xygroup == '':
  440. GMessage(_('You must select a valid image/map '
  441. 'group in order to continue'),
  442. parent = self)
  443. event.Veto()
  444. return
  445. if event.GetDirection() and self.extension == '':
  446. GMessage(_('You must enter an map name '
  447. 'extension in order to continue'),
  448. parent = self)
  449. event.Veto()
  450. return
  451. def OnEnterPage(self, event=None):
  452. global maptype
  453. self.groupList = []
  454. self.xylocation = self.parent.gisrc_dict['LOCATION_NAME']
  455. self.xymapset = self.parent.gisrc_dict['MAPSET']
  456. # create a list of groups in selected mapset
  457. if os.path.isdir(os.path.join(self.grassdatabase,
  458. self.xylocation,
  459. self.xymapset,
  460. 'group')):
  461. tmplist = os.listdir(os.path.join(self.grassdatabase,
  462. self.xylocation,
  463. self.xymapset,
  464. 'group'))
  465. for item in tmplist:
  466. if os.path.isdir(os.path.join(self.grassdatabase,
  467. self.xylocation,
  468. self.xymapset,
  469. 'group',
  470. item)):
  471. self.groupList.append(item)
  472. if maptype == 'raster':
  473. self.btn_vgroup.Hide()
  474. self.Bind(wx.EVT_BUTTON, self.OnMkGroup, self.btn_mkgroup)
  475. elif maptype == 'vector':
  476. self.btn_vgroup.Show()
  477. self.Bind(wx.EVT_BUTTON, self.OnMkGroup, self.btn_mkgroup)
  478. self.Bind(wx.EVT_BUTTON, self.OnVGroup, self.btn_vgroup)
  479. utils.ListSortLower(self.groupList)
  480. self.cb_group.SetItems(self.groupList)
  481. if len(self.groupList) > 0:
  482. if self.xygroup and self.xygroup in self.groupList:
  483. self.cb_group.SetStringSelection(self.xygroup)
  484. else:
  485. self.cb_group.SetSelection(0)
  486. self.xygroup = self.groupList[0]
  487. if self.xygroup == '' or \
  488. self.extension == '':
  489. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  490. else:
  491. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  492. # switch to source
  493. self.parent.SwitchEnv('source')
  494. class DispMapPage(TitledPage):
  495. """
  496. Select ungeoreferenced map to display for interactively
  497. setting ground control points (GCPs).
  498. """
  499. def __init__(self, wizard, parent):
  500. TitledPage.__init__(self, wizard,
  501. _("Select maps to display for ground control point (GCP) creation"))
  502. self.parent = parent
  503. global maptype
  504. #
  505. # layout
  506. #
  507. self.sizer.Add(item=wx.StaticText(parent=self, id=wx.ID_ANY, label=_('Select source map to display:')),
  508. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  509. pos=(1, 1))
  510. self.srcselection = Select(self, id=wx.ID_ANY,
  511. size=globalvar.DIALOG_GSELECT_SIZE, type=maptype, updateOnPopup = False)
  512. self.sizer.Add(item=self.srcselection,
  513. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  514. pos=(1, 2))
  515. self.sizer.Add(item=wx.StaticText(parent=self, id=wx.ID_ANY, label=_('Select target raster map to display:')),
  516. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  517. pos=(2, 1))
  518. self.tgtrastselection = Select(self, id=wx.ID_ANY,
  519. size=globalvar.DIALOG_GSELECT_SIZE, type='raster', updateOnPopup = False)
  520. self.sizer.Add(item=self.tgtrastselection,
  521. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  522. pos=(2, 2))
  523. self.sizer.Add(item=wx.StaticText(parent=self, id=wx.ID_ANY, label=_('Select target vector map to display:')),
  524. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  525. pos=(3, 1))
  526. self.tgtvectselection = Select(self, id=wx.ID_ANY,
  527. size=globalvar.DIALOG_GSELECT_SIZE, type='vector', updateOnPopup = False)
  528. self.sizer.Add(item=self.tgtvectselection,
  529. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  530. pos=(3, 2))
  531. #
  532. # bindings
  533. #
  534. self.srcselection.Bind(wx.EVT_TEXT, self.OnSrcSelection)
  535. self.tgtrastselection.Bind(wx.EVT_TEXT, self.OnTgtRastSelection)
  536. self.tgtvectselection.Bind(wx.EVT_TEXT, self.OnTgtVectSelection)
  537. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  538. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  539. self.Bind(wx.EVT_CLOSE, self.parent.Cleanup)
  540. def OnSrcSelection(self, event):
  541. """!Source map to display selected"""
  542. global src_map
  543. global maptype
  544. src_map = self.srcselection.GetValue()
  545. if src_map == '':
  546. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  547. else:
  548. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  549. try:
  550. # set computational region to match selected map and zoom display to region
  551. if maptype == 'raster':
  552. p = RunCommand('g.region', 'rast=src_map')
  553. elif maptype == 'vector':
  554. p = RunCommand('g.region', 'vect=src_map')
  555. if p.returncode == 0:
  556. print 'returncode = ', str(p.returncode)
  557. self.parent.Map.region = self.parent.Map.GetRegion()
  558. except:
  559. pass
  560. def OnTgtRastSelection(self, event):
  561. """!Source map to display selected"""
  562. global tgt_map
  563. tgt_map['raster'] = self.tgtrastselection.GetValue()
  564. def OnTgtVectSelection(self,event):
  565. """!Source map to display selected"""
  566. global tgt_map
  567. tgt_map['vector'] = self.tgtvectselection.GetValue()
  568. def OnPageChanging(self, event=None):
  569. global src_map
  570. global tgt_map
  571. if event.GetDirection() and (src_map == ''):
  572. GMessage(_('You must select a source map '
  573. 'in order to continue'),
  574. parent = self)
  575. event.Veto()
  576. return
  577. self.parent.SwitchEnv('target')
  578. def OnEnterPage(self, event=None):
  579. global maptype
  580. global src_map
  581. global tgt_map
  582. self.srcselection.SetElementList(maptype)
  583. if maptype == 'raster':
  584. ret = RunCommand('i.group',
  585. parent = self,
  586. read = True,
  587. group = self.parent.grouppage.xygroup,
  588. flags = 'g')
  589. if ret:
  590. self.parent.src_maps = ret.splitlines()
  591. else:
  592. GError(parent = self,
  593. message = _('No maps in selected group <%s>.\n'
  594. 'Please edit group or select another group.') %
  595. self.parent.grouppage.xygroup)
  596. return
  597. elif maptype == 'vector':
  598. grassdatabase = self.parent.grassdatabase
  599. xylocation = self.parent.gisrc_dict['LOCATION_NAME']
  600. xymapset = self.parent.gisrc_dict['MAPSET']
  601. # make list of vectors to georectify from VREF
  602. vgrpfile = os.path.join(grassdatabase,
  603. xylocation,
  604. xymapset,
  605. 'group',
  606. self.parent.grouppage.xygroup,
  607. 'VREF')
  608. f = open(vgrpfile)
  609. try:
  610. for vect in f.readlines():
  611. vect = vect.strip('\n')
  612. if len(vect) < 1:
  613. continue
  614. self.parent.src_maps.append(vect)
  615. finally:
  616. f.close()
  617. if len(self.parent.src_maps) < 1:
  618. GError(parent = self,
  619. message = _('No maps in selected group <%s>.\n'
  620. 'Please edit group or select another group.') %
  621. self.parent.grouppage.xygroup)
  622. return
  623. # filter out all maps not in group
  624. self.srcselection.tcp.GetElementList(elements = self.parent.src_maps)
  625. src_map = self.parent.src_maps[0]
  626. self.srcselection.SetValue(src_map)
  627. self.parent.SwitchEnv('target')
  628. self.tgtrastselection.SetElementList('raster')
  629. self.tgtrastselection.GetElementList()
  630. self.tgtvectselection.SetElementList('vector')
  631. self.tgtvectselection.GetElementList()
  632. self.parent.SwitchEnv('source')
  633. if src_map == '':
  634. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  635. else:
  636. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  637. class GCP(MapFrame, ColumnSorterMixin):
  638. """!
  639. Manages ground control points for georectifying. Calculates RMS statistics.
  640. Calls i.rectify or v.rectify to georectify map.
  641. """
  642. def __init__(self, parent, giface, grwiz = None, id = wx.ID_ANY,
  643. title = _("Manage Ground Control Points"),
  644. size = (700, 300), toolbars = ["gcpdisp"], Map = None, lmgr = None):
  645. self.grwiz = grwiz # GR Wizard
  646. self._giface = giface
  647. if tgt_map['raster'] == '' and tgt_map['vector'] == '':
  648. self.show_target = False
  649. else:
  650. self.show_target = True
  651. #wx.Frame.__init__(self, parent, id, title, size = size, name = "GCPFrame")
  652. MapFrame.__init__(self, parent = parent, giface = self._giface, title = title, size = size,
  653. Map = Map, toolbars = toolbars, lmgr = lmgr, name = 'GCPMapWindow')
  654. #
  655. # init variables
  656. #
  657. self.parent = parent # GMFrame
  658. self.parent.gcpmanagement = self
  659. #
  660. # register data structures for drawing GCP's
  661. #
  662. self.pointsToDrawTgt = self.TgtMapWindow.RegisterGraphicsToDraw(graphicsType = "point", setStatusFunc = self.SetGCPSatus)
  663. self.pointsToDrawSrc = self.SrcMapWindow.RegisterGraphicsToDraw(graphicsType = "point", setStatusFunc = self.SetGCPSatus)
  664. # window resized
  665. self.resize = False
  666. self.grassdatabase = self.grwiz.grassdatabase
  667. self.currentlocation = self.grwiz.currentlocation
  668. self.currentmapset = self.grwiz.currentmapset
  669. self.newlocation = self.grwiz.newlocation
  670. self.newmapset = self.grwiz.newmapset
  671. self.xylocation = self.grwiz.gisrc_dict['LOCATION_NAME']
  672. self.xymapset = self.grwiz.gisrc_dict['MAPSET']
  673. self.xygroup = self.grwiz.grouppage.xygroup
  674. self.src_maps = self.grwiz.src_maps
  675. self.extension = self.grwiz.grouppage.extension
  676. self.outname = ''
  677. self.VectGRList = []
  678. self.file = {
  679. 'points' : os.path.join(self.grassdatabase,
  680. self.xylocation,
  681. self.xymapset,
  682. 'group',
  683. self.xygroup,
  684. 'POINTS'),
  685. 'points_bak' : os.path.join(self.grassdatabase,
  686. self.xylocation,
  687. self.xymapset,
  688. 'group',
  689. self.xygroup,
  690. 'POINTS_BAK'),
  691. 'rgrp' : os.path.join(self.grassdatabase,
  692. self.xylocation,
  693. self.xymapset,
  694. 'group',
  695. self.xygroup,
  696. 'REF'),
  697. 'vgrp' : os.path.join(self.grassdatabase,
  698. self.xylocation,
  699. self.xymapset,
  700. 'group',
  701. self.xygroup,
  702. 'VREF'),
  703. 'target' : os.path.join(self.grassdatabase,
  704. self.xylocation,
  705. self.xymapset,
  706. 'group',
  707. self.xygroup,
  708. 'TARGET'),
  709. }
  710. # make a backup of the current points file
  711. if os.path.exists(self.file['points']):
  712. shutil.copy(self.file['points'], self.file['points_bak'])
  713. # polynomial order transformation for georectification
  714. self.gr_order = 1
  715. # interpolation method for georectification
  716. self.gr_method = 'nearest'
  717. # region clipping for georectified map
  718. self.clip_to_region = False
  719. # number of GCPs selected to be used for georectification (checked)
  720. self.GCPcount = 0
  721. # forward RMS error
  722. self.fwd_rmserror = 0.0
  723. # backward RMS error
  724. self.bkw_rmserror = 0.0
  725. # list map coords and ID of map display they came from
  726. self.mapcoordlist = []
  727. self.mapcoordlist.append([ 0, # GCP number
  728. 0.0, # source east
  729. 0.0, # source north
  730. 0.0, # target east
  731. 0.0, # target north
  732. 0.0, # forward error
  733. 0.0 ] ) # backward error
  734. # init vars to highlight high RMS errors
  735. self.highest_only = True
  736. self.show_unused = True
  737. self.highest_key = -1
  738. self.rmsthresh = 0
  739. self.rmsmean = 0
  740. self.rmssd = 0
  741. self.SetTarget(self.xygroup, self.currentlocation, self.currentmapset)
  742. self.itemDataMap = None
  743. # images for column sorting
  744. # CheckListCtrlMixin must set an ImageList first
  745. self.il = self.list.GetImageList(wx.IMAGE_LIST_SMALL)
  746. SmallUpArrow = wx.BitmapFromImage(getSmallUpArrowImage())
  747. SmallDnArrow = wx.BitmapFromImage(getSmallDnArrowImage())
  748. self.sm_dn = self.il.Add(SmallDnArrow)
  749. self.sm_up = self.il.Add(SmallUpArrow)
  750. # set mouse characteristics
  751. self.mapwin = self.SrcMapWindow
  752. self.mapwin.mouse['box'] = 'point'
  753. self.mapwin.mouse["use"] == "pointer"
  754. self.mapwin.zoomtype = 0
  755. self.mapwin.pen = wx.Pen(colour='black', width=2, style=wx.SOLID)
  756. self.mapwin.SetCursor(self.cursors["cross"])
  757. self.mapwin = self.TgtMapWindow
  758. # set mouse characteristics
  759. self.mapwin.mouse['box'] = 'point'
  760. self.mapwin.mouse["use"] == "pointer"
  761. self.mapwin.zoomtype = 0
  762. self.mapwin.pen = wx.Pen(colour='black', width=2, style=wx.SOLID)
  763. self.mapwin.SetCursor(self.cursors["cross"])
  764. #
  765. # show new display & draw map
  766. #
  767. if self.show_target:
  768. self.MapWindow = self.TgtMapWindow
  769. self.Map = self.TgtMap
  770. self.OnZoomToMap(None)
  771. self.MapWindow = self.SrcMapWindow
  772. self.Map = self.SrcMap
  773. self.OnZoomToMap(None)
  774. #
  775. # bindings
  776. #
  777. self.Bind(wx.EVT_ACTIVATE, self.OnFocus)
  778. self.Bind(wx.EVT_SIZE, self.OnSize)
  779. self.Bind(wx.EVT_IDLE, self.OnIdle)
  780. self.Bind(wx.EVT_CLOSE, self.OnQuit)
  781. self.SetSettings()
  782. def __del__(self):
  783. """!Disable GCP manager mode"""
  784. self.parent.gcpmanagement = None
  785. def CreateGCPList(self):
  786. """!Create GCP List Control"""
  787. return GCPList(parent=self, gcp=self)
  788. # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
  789. def GetListCtrl(self):
  790. return self.list
  791. def GetMapCoordList(self):
  792. return self.mapcoordlist
  793. # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
  794. def GetSortImages(self):
  795. return (self.sm_dn, self.sm_up)
  796. def GetFwdError(self):
  797. return self.fwd_rmserror
  798. def GetBkwError(self):
  799. return self.bkw_rmserror
  800. def InitMapDisplay(self):
  801. self.list.LoadData()
  802. # initialize column sorter
  803. self.itemDataMap = self.mapcoordlist
  804. ncols = self.list.GetColumnCount()
  805. ColumnSorterMixin.__init__(self, ncols)
  806. # init to ascending sort on first click
  807. self._colSortFlag = [1] * ncols
  808. def SetTarget(self, tgroup, tlocation, tmapset):
  809. """
  810. Sets rectification target to current location and mapset
  811. """
  812. # check to see if we are georectifying map in current working location/mapset
  813. if self.newlocation == self.currentlocation and self.newmapset == self.currentmapset:
  814. RunCommand('i.target',
  815. parent = self,
  816. flags = 'c',
  817. group = tgroup)
  818. else:
  819. self.grwiz.SwitchEnv('source')
  820. RunCommand('i.target',
  821. parent = self,
  822. group = tgroup,
  823. location = tlocation,
  824. mapset = tmapset)
  825. self.grwiz.SwitchEnv('target')
  826. def AddGCP(self, event):
  827. """
  828. Appends an item to GCP list
  829. """
  830. keyval = self.list.AddGCPItem() + 1
  831. # source east, source north, target east, target north, forward error, backward error
  832. self.mapcoordlist.append([ keyval, # GCP number
  833. 0.0, # source east
  834. 0.0, # source north
  835. 0.0, # target east
  836. 0.0, # target north
  837. 0.0, # forward error
  838. 0.0 ] ) # backward error
  839. if self.statusbarManager.GetMode() == 8: # go to
  840. self.StatusbarUpdate()
  841. def DeleteGCP(self, event):
  842. """
  843. Deletes selected item in GCP list
  844. """
  845. minNumOfItems = self.OnGROrder(None)
  846. if self.list.GetItemCount() <= minNumOfItems:
  847. GMessage(parent = self,
  848. message=_("At least %d GCPs required. Operation canceled.") % minNumOfItems)
  849. return
  850. key = self.list.DeleteGCPItem()
  851. del self.mapcoordlist[key]
  852. # update key and GCP number
  853. for newkey in range(key, len(self.mapcoordlist)):
  854. index = self.list.FindItemData(-1, newkey + 1)
  855. self.mapcoordlist[newkey][0] = newkey
  856. self.list.SetStringItem(index, 0, str(newkey))
  857. self.list.SetItemData(index, newkey)
  858. # update selected
  859. if self.list.GetItemCount() > 0:
  860. if self.list.selected < self.list.GetItemCount():
  861. self.list.selectedkey = self.list.GetItemData(self.list.selected)
  862. else:
  863. self.list.selected = self.list.GetItemCount() - 1
  864. self.list.selectedkey = self.list.GetItemData(self.list.selected)
  865. self.list.SetItemState(self.list.selected,
  866. wx.LIST_STATE_SELECTED,
  867. wx.LIST_STATE_SELECTED)
  868. else:
  869. self.list.selected = wx.NOT_FOUND
  870. self.list.selectedkey = -1
  871. self.UpdateColours()
  872. if self.statusbarManager.GetMode() == 8: # go to
  873. self.StatusbarUpdate()
  874. if self.list.selectedkey > 0:
  875. self.statusbarManager.SetProperty('gotoGCP', self.list.selectedkey)
  876. def ClearGCP(self, event):
  877. """
  878. Clears all values in selected item of GCP list and unchecks it
  879. """
  880. index = self.list.GetSelected()
  881. key = self.list.GetItemData(index)
  882. for i in range(1, 5):
  883. self.list.SetStringItem(index, i, '0.0')
  884. self.list.SetStringItem(index, 5, '')
  885. self.list.SetStringItem(index, 6, '')
  886. self.list.CheckItem(index, False)
  887. # GCP number, source E, source N, target E, target N, fwd error, bkwd error
  888. self.mapcoordlist[key] = [key, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  889. def SetSettings(self):
  890. """!Sets settings for drawing of GCP's.
  891. """
  892. self.highest_only = UserSettings.Get(group='gcpman', key='rms', subkey='highestonly')
  893. self.show_unused = UserSettings.Get(group='gcpman', key='symbol', subkey='unused')
  894. colours = { "color" : "default",
  895. "hcolor" : "highest",
  896. "scolor" : "selected",
  897. "ucolor" : "unused" }
  898. wpx = UserSettings.Get(group = 'gcpman', key = 'symbol', subkey = 'width')
  899. for k, v in colours.iteritems():
  900. col = UserSettings.Get(group='gcpman', key='symbol', subkey= k)
  901. self.pointsToDrawSrc.GetPen(v).SetColour(wx.Colour(col[0], col[1], col[2], 255)) # TODO GetPen neni to spatne?
  902. self.pointsToDrawTgt.GetPen(v).SetColour(wx.Colour(col[0], col[1], col[2], 255))
  903. self.pointsToDrawSrc.GetPen(v).SetWidth(wpx)
  904. self.pointsToDrawTgt.GetPen(v).SetWidth(wpx)
  905. spx = UserSettings.Get(group = 'gcpman', key = 'symbol', subkey = 'size')
  906. self.pointsToDrawSrc.SetPropertyVal("size", int(spx))
  907. self.pointsToDrawTgt.SetPropertyVal("size", int(spx))
  908. font = self.GetFont()
  909. font.SetPointSize(int(spx) + 2)
  910. textProp = {}
  911. textProp['active'] = True
  912. textProp['font'] = font
  913. self.pointsToDrawSrc.SetPropertyVal("text", textProp)
  914. self.pointsToDrawTgt.SetPropertyVal("text", copy(textProp))
  915. def SetGCPSatus(self, item, itemIndex):
  916. """!Before GCP is drawn, decides it's colour and whether it
  917. will be drawed.
  918. """
  919. key = self.list.GetItemData(itemIndex)
  920. itemIndex += 1 # incremented because of itemDataMap (has one more item) - will be changed
  921. if not self.list.IsChecked(key - 1):
  922. wxPen = "unused"
  923. if not self.show_unused:
  924. item.SetPropertyVal('hide', True)
  925. else:
  926. item.SetPropertyVal('hide', False)
  927. else:
  928. item.SetPropertyVal('hide', False)
  929. if self.highest_only == True:
  930. if itemIndex == self.highest_key:
  931. wxPen = "highest"
  932. else:
  933. wxPen = "default"
  934. else:
  935. if (gcp[5] > self.rmsthresh):
  936. wxPen = "highest"
  937. else:
  938. wxPen = "default"
  939. if itemIndex == self.list.selectedkey:
  940. wxPen = "selected"
  941. item.SetPropertyVal('label', str(itemIndex))
  942. item.SetPropertyVal('penName', wxPen)
  943. def SetGCPData(self, coordtype, coord, mapdisp=None, confirm=False):
  944. """!Inserts coordinates from file, mouse click on map, or
  945. after editing into selected item of GCP list and checks it for
  946. use.
  947. """
  948. index = self.list.GetSelected()
  949. if index == wx.NOT_FOUND:
  950. return
  951. coord0 = coord[0]
  952. coord1 = coord[1]
  953. key = self.list.GetItemData(index)
  954. if confirm:
  955. if self.MapWindow == self.SrcMapWindow:
  956. currloc = _("source")
  957. else:
  958. currloc = _("target")
  959. ret = wx.MessageBox(parent = self,
  960. caption = _("Set GCP coordinates"),
  961. message = _('Set %(coor)s coordinates for GCP No. %(key)s? \n\n'
  962. 'East: %(coor0)s \n'
  963. 'North: %(coor1)s') % \
  964. { 'coor' : currloc,
  965. 'key' : str(key),
  966. 'coor0' : str(coord0),
  967. 'coor1' : str(coord1) },
  968. style = wx.ICON_QUESTION | wx.YES_NO | wx.CENTRE)
  969. # for wingrass
  970. if os.name == 'nt':
  971. self.MapWindow.SetFocus()
  972. if ret == wx.NO:
  973. return
  974. if coordtype == 'source':
  975. self.list.SetStringItem(index, 1, str(coord0))
  976. self.list.SetStringItem(index, 2, str(coord1))
  977. self.mapcoordlist[key][1] = coord[0]
  978. self.mapcoordlist[key][2] = coord[1]
  979. self.pointsToDrawSrc.GetItem(key - 1).SetCoords([coord0, coord1])
  980. elif coordtype == 'target':
  981. self.list.SetStringItem(index, 3, str(coord0))
  982. self.list.SetStringItem(index, 4, str(coord1))
  983. self.mapcoordlist[key][3] = coord[0]
  984. self.mapcoordlist[key][4] = coord[1]
  985. self.pointsToDrawTgt.GetItem(key - 1).SetCoords([coord0, coord1])
  986. self.list.SetStringItem(index, 5, '0')
  987. self.list.SetStringItem(index, 6, '0')
  988. self.mapcoordlist[key][5] = 0.0
  989. self.mapcoordlist[key][6] = 0.0
  990. # self.list.ResizeColumns()
  991. def SaveGCPs(self, event):
  992. """!Make a POINTS file or save GCP coordinates to existing
  993. POINTS file
  994. """
  995. self.GCPcount = 0
  996. try:
  997. f = open(self.file['points'], mode='w')
  998. # use os.linesep or '\n' here ???
  999. f.write('# Ground Control Points File\n')
  1000. f.write("# \n")
  1001. f.write("# target location: " + self.currentlocation + '\n')
  1002. f.write("# target mapset: " + self.currentmapset + '\n')
  1003. f.write("#\tsource\t\ttarget\t\tstatus\n")
  1004. f.write("#\teast\tnorth\teast\tnorth\t(1=ok, 0=ignore)\n")
  1005. f.write("#----------------------- ----------------------- ---------------\n")
  1006. for index in range(self.list.GetItemCount()):
  1007. if self.list.IsChecked(index) == True:
  1008. check = "1"
  1009. self.GCPcount += 1
  1010. else:
  1011. check = "0"
  1012. coord0 = self.list.GetItem(index, 1).GetText()
  1013. coord1 = self.list.GetItem(index, 2).GetText()
  1014. coord2 = self.list.GetItem(index, 3).GetText()
  1015. coord3 = self.list.GetItem(index, 4).GetText()
  1016. f.write(coord0 + ' ' + coord1 + ' ' + coord2 + ' ' + coord3 + ' ' + check + '\n')
  1017. except IOError, err:
  1018. GError(parent = self,
  1019. message="%s <%s>. %s%s" % (_("Writing POINTS file failed"),
  1020. self.file['points'], os.linesep, err))
  1021. return
  1022. f.close()
  1023. # if event != None save also to backup file
  1024. if event:
  1025. shutil.copy(self.file['points'], self.file['points_bak'])
  1026. self._giface.WriteLog(_('POINTS file saved for group <%s>') % self.xygroup)
  1027. #self.SetStatusText(_('POINTS file saved'))
  1028. def ReadGCPs(self):
  1029. """
  1030. Reads GCPs and georectified coordinates from POINTS file
  1031. """
  1032. self.GCPcount = 0
  1033. sourceMapWin = self.SrcMapWindow
  1034. targetMapWin = self.TgtMapWindow
  1035. if not sourceMapWin:
  1036. GError(parent = self,
  1037. message = "%s. %s%s" % (_("source mapwin not defined"),
  1038. os.linesep, err))
  1039. if not targetMapWin:
  1040. GError(parent = self,
  1041. message="%s. %s%s" % (_("target mapwin not defined"),
  1042. os.linesep, err))
  1043. try:
  1044. f = open(self.file['points'], 'r')
  1045. GCPcnt = 0
  1046. for line in f.readlines():
  1047. if line[0] == '#' or line =='':
  1048. continue
  1049. line = line.replace('\n', '').strip()
  1050. coords = map(float, line.split())
  1051. if coords[4] == 1:
  1052. check = True
  1053. self.GCPcount +=1
  1054. else:
  1055. check = False
  1056. self.AddGCP(event=None)
  1057. self.SetGCPData('source', (coords[0], coords[1]), sourceMapWin)
  1058. self.SetGCPData('target', (coords[2], coords[3]), targetMapWin)
  1059. index = self.list.GetSelected()
  1060. if index != wx.NOT_FOUND:
  1061. self.list.CheckItem(index, check)
  1062. GCPcnt += 1
  1063. except IOError, err:
  1064. GError(parent = self,
  1065. message = "%s <%s>. %s%s" % (_("Reading POINTS file failed"),
  1066. self.file['points'], os.linesep, err))
  1067. return
  1068. f.close()
  1069. if GCPcnt == 0:
  1070. # 3 gcp is minimum
  1071. for i in range(3):
  1072. self.AddGCP(None)
  1073. if self.CheckGCPcount():
  1074. # calculate RMS
  1075. self.RMSError(self.xygroup, self.gr_order)
  1076. def ReloadGCPs(self, event):
  1077. """!Reload data from file"""
  1078. # use backup
  1079. shutil.copy(self.file['points_bak'], self.file['points'])
  1080. # delete all items in mapcoordlist
  1081. self.mapcoordlist = []
  1082. self.mapcoordlist.append([ 0, # GCP number
  1083. 0.0, # source east
  1084. 0.0, # source north
  1085. 0.0, # target east
  1086. 0.0, # target north
  1087. 0.0, # forward error
  1088. 0.0 ] ) # backward error
  1089. self.list.LoadData()
  1090. self.itemDataMap = self.mapcoordlist
  1091. if self._col != -1:
  1092. self.list.ClearColumnImage(self._col)
  1093. self._colSortFlag = [1] * self.list.GetColumnCount()
  1094. # draw GCPs (source and target)
  1095. sourceMapWin = self.SrcMapWindow
  1096. sourceMapWin.UpdateMap(render=False, renderVector=False)
  1097. if self.show_target:
  1098. targetMapWin = self.TgtMapWindow
  1099. targetMapWin.UpdateMap(render=False, renderVector=False)
  1100. def OnFocus(self, event):
  1101. # self.grwiz.SwitchEnv('source')
  1102. pass
  1103. def OnRMS(self, event):
  1104. """
  1105. RMS button handler
  1106. """
  1107. self.RMSError(self.xygroup,self.gr_order)
  1108. sourceMapWin = self.SrcMapWindow
  1109. sourceMapWin.UpdateMap(render=False, renderVector=False)
  1110. if self.show_target:
  1111. targetMapWin = self.TgtMapWindow
  1112. targetMapWin.UpdateMap(render=False, renderVector=False)
  1113. def CheckGCPcount(self, msg=False):
  1114. """
  1115. Checks to make sure that the minimum number of GCPs have been defined and
  1116. are active for the selected transformation order
  1117. """
  1118. if (self.GCPcount < 3 and self.gr_order == 1) or \
  1119. (self.GCPcount < 6 and self.gr_order == 2) or \
  1120. (self.GCPcount < 10 and self.gr_order == 3):
  1121. if msg:
  1122. GWarning(parent = self,
  1123. message=_('Insufficient points defined and active (checked) '
  1124. 'for selected rectification method.\n'
  1125. '3+ points needed for 1st order,\n'
  1126. '6+ points for 2nd order, and\n'
  1127. '10+ points for 3rd order.'))
  1128. return False
  1129. else:
  1130. return True
  1131. def OnGeorect(self, event):
  1132. """
  1133. Georectifies map(s) in group using i.rectify or v.transform
  1134. """
  1135. global maptype
  1136. self.SaveGCPs(None)
  1137. if self.CheckGCPcount(msg=True) == False:
  1138. return
  1139. if maptype == 'raster':
  1140. self.grwiz.SwitchEnv('source')
  1141. if self.clip_to_region:
  1142. flags = "ac"
  1143. else:
  1144. flags = "a"
  1145. busy = wx.BusyInfo(message=_("Rectifying images, please wait..."),
  1146. parent=self)
  1147. wx.Yield()
  1148. ret, msg = RunCommand('i.rectify',
  1149. parent = self,
  1150. getErrorMsg = True,
  1151. quiet = True,
  1152. group = self.xygroup,
  1153. extension = self.extension,
  1154. order = self.gr_order,
  1155. method=self.gr_method,
  1156. flags = flags)
  1157. busy.Destroy()
  1158. # provide feedback on failure
  1159. if ret != 0:
  1160. print >> sys.stderr, msg
  1161. elif maptype == 'vector':
  1162. # loop through all vectors in VREF
  1163. self.grwiz.SwitchEnv('source')
  1164. # make list of vectors to georectify from VREF
  1165. f = open(self.file['vgrp'])
  1166. vectlist = []
  1167. try:
  1168. for vect in f.readlines():
  1169. vect = vect.strip('\n')
  1170. if len(vect) < 1:
  1171. continue
  1172. vectlist.append(vect)
  1173. finally:
  1174. f.close()
  1175. # georectify each vector in VREF using v.rectify
  1176. for vect in vectlist:
  1177. self.outname = str(vect.split('@')[0]) + self.extension
  1178. self._giface.WriteLog(text = _('Transforming <%s>...') % vect,
  1179. switchPage = True)
  1180. ret = msg = ''
  1181. busy = wx.BusyInfo(message=_("Rectifying vector map <%s>, please wait...") % vect,
  1182. parent=self)
  1183. wx.Yield()
  1184. ret, msg = RunCommand('v.rectify',
  1185. parent = self,
  1186. getErrorMsg = True,
  1187. quiet = True,
  1188. input = vect,
  1189. output = self.outname,
  1190. group = self.xygroup,
  1191. order = self.gr_order)
  1192. busy.Destroy()
  1193. # provide feedback on failure
  1194. if ret != 0:
  1195. print >> sys.stderr, msg
  1196. self.grwiz.SwitchEnv('target')
  1197. def OnGeorectDone(self, **kargs):
  1198. """!Print final message"""
  1199. global maptype
  1200. if maptype == 'raster':
  1201. return
  1202. returncode = kargs['returncode']
  1203. if returncode == 0:
  1204. self.VectGRList.append(self.outname)
  1205. print '*****vector list = ' + str(self.VectGRList)
  1206. else:
  1207. self._giface.WriteError(_('Georectification of vector map <%s> failed') %
  1208. self.outname)
  1209. def OnSettings(self, event):
  1210. """!GCP Manager settings"""
  1211. dlg = GrSettingsDialog(parent=self, giface=self._giface,
  1212. id=wx.ID_ANY, title=_('GCP Manager settings'))
  1213. if dlg.ShowModal() == wx.ID_OK:
  1214. pass
  1215. dlg.Destroy()
  1216. def UpdateColours(self, srcrender=False, srcrenderVector=False,
  1217. tgtrender=False, tgtrenderVector=False):
  1218. """!update colours"""
  1219. highest_fwd_err = 0.0
  1220. self.highest_key = 0
  1221. highest_idx = 0
  1222. for index in range(self.list.GetItemCount()):
  1223. if self.list.IsChecked(index):
  1224. key = self.list.GetItemData(index)
  1225. fwd_err = self.mapcoordlist[key][5]
  1226. if self.highest_only == True:
  1227. self.list.SetItemTextColour(index, wx.BLACK)
  1228. if highest_fwd_err < fwd_err:
  1229. highest_fwd_err = fwd_err
  1230. self.highest_key = key
  1231. highest_idx = index
  1232. elif self.rmsthresh > 0:
  1233. if (fwd_err > self.rmsthresh):
  1234. self.list.SetItemTextColour(index, wx.RED)
  1235. else:
  1236. self.list.SetItemTextColour(index, wx.BLACK)
  1237. else:
  1238. self.list.SetItemTextColour(index, wx.BLACK)
  1239. if self.highest_only and highest_fwd_err > 0.0:
  1240. self.list.SetItemTextColour(highest_idx, wx.RED)
  1241. sourceMapWin = self.SrcMapWindow
  1242. sourceMapWin.UpdateMap(render=srcrender, renderVector=srcrenderVector)
  1243. if self.show_target:
  1244. targetMapWin = self.TgtMapWindow
  1245. targetMapWin.UpdateMap(render=tgtrender, renderVector=tgtrenderVector)
  1246. def OnQuit(self, event):
  1247. """!Quit georectifier"""
  1248. ret = wx.MessageBox(parent=self,
  1249. caption=_("Quit GCP Manager"),
  1250. message=_('Save ground control points?'),
  1251. style=wx.ICON_QUESTION | wx.YES_NO | wx.CANCEL | wx.CENTRE)
  1252. if ret != wx.CANCEL:
  1253. if ret == wx.YES:
  1254. self.SaveGCPs(None)
  1255. elif ret == wx.NO:
  1256. # restore POINTS file from backup
  1257. if os.path.exists(self.file['points_bak']):
  1258. shutil.copy(self.file['points_bak'], self.file['points'])
  1259. if os.path.exists(self.file['points_bak']):
  1260. os.unlink(self.file['points_bak'])
  1261. self.SrcMap.Clean()
  1262. self.TgtMap.Clean()
  1263. self.grwiz.Cleanup()
  1264. self.Destroy()
  1265. #event.Skip()
  1266. def OnGROrder(self, event):
  1267. """
  1268. sets transformation order for georectifying
  1269. """
  1270. if event:
  1271. self.gr_order = event.GetInt() + 1
  1272. numOfItems = self.list.GetItemCount()
  1273. minNumOfItems = numOfItems
  1274. if self.gr_order == 1:
  1275. minNumOfItems = 3
  1276. # self.SetStatusText(_('Insufficient points, 3+ points needed for 1st order'))
  1277. elif self.gr_order == 2:
  1278. minNumOfItems = 6
  1279. diff = 6 - numOfItems
  1280. # self.SetStatusText(_('Insufficient points, 6+ points needed for 2nd order'))
  1281. elif self.gr_order == 3:
  1282. minNumOfItems = 10
  1283. # self.SetStatusText(_('Insufficient points, 10+ points needed for 3rd order'))
  1284. for i in range(minNumOfItems - numOfItems):
  1285. self.AddGCP(None)
  1286. return minNumOfItems
  1287. def RMSError(self, xygroup, order):
  1288. """
  1289. Uses m.transform to calculate forward and backward error for each used GCP
  1290. in POINTS file and insert error values into GCP list.
  1291. Calculates total forward and backward RMS error for all used points
  1292. """
  1293. # save GCPs to points file to make sure that all checked GCPs are used
  1294. self.SaveGCPs(None)
  1295. #self.SetStatusText('')
  1296. if self.CheckGCPcount(msg=True) == False:
  1297. return
  1298. # get list of forward and reverse rms error values for each point
  1299. self.grwiz.SwitchEnv('source')
  1300. ret = RunCommand('m.transform',
  1301. parent = self,
  1302. read = True,
  1303. group = xygroup,
  1304. order = order)
  1305. self.grwiz.SwitchEnv('target')
  1306. if ret:
  1307. errlist = ret.splitlines()
  1308. else:
  1309. GError(parent = self,
  1310. message=_('Could not calculate RMS Error.\n'
  1311. 'Possible error with m.transform.'))
  1312. return
  1313. # insert error values into GCP list for checked items
  1314. sdfactor = float(UserSettings.Get(group='gcpman', key='rms', subkey='sdfactor'))
  1315. GCPcount = 0
  1316. sumsq_fwd_err = 0.0
  1317. sumsq_bkw_err = 0.0
  1318. sum_fwd_err = 0.0
  1319. highest_fwd_err = 0.0
  1320. self.highest_key = 0
  1321. highest_idx = 0
  1322. for index in range(self.list.GetItemCount()):
  1323. key = self.list.GetItemData(index)
  1324. if self.list.IsChecked(index):
  1325. fwd_err, bkw_err = errlist[GCPcount].split()
  1326. self.list.SetStringItem(index, 5, fwd_err)
  1327. self.list.SetStringItem(index, 6, bkw_err)
  1328. self.mapcoordlist[key][5] = float(fwd_err)
  1329. self.mapcoordlist[key][6] = float(bkw_err)
  1330. self.list.SetItemTextColour(index, wx.BLACK)
  1331. if self.highest_only:
  1332. if highest_fwd_err < float(fwd_err):
  1333. highest_fwd_err = float(fwd_err)
  1334. self.highest_key = key
  1335. highest_idx = index
  1336. sumsq_fwd_err += float(fwd_err)**2
  1337. sumsq_bkw_err += float(bkw_err)**2
  1338. sum_fwd_err += float(fwd_err)
  1339. GCPcount += 1
  1340. else:
  1341. self.list.SetStringItem(index, 5, '')
  1342. self.list.SetStringItem(index, 6, '')
  1343. self.mapcoordlist[key][5] = 0.0
  1344. self.mapcoordlist[key][6] = 0.0
  1345. self.list.SetItemTextColour(index, wx.BLACK)
  1346. # SD
  1347. if GCPcount > 0:
  1348. sum_fwd_err /= GCPcount
  1349. self.rmsmean = sum_fwd_err /GCPcount
  1350. self.rmssd = (((sumsq_fwd_err/GCPcount) - self.rmsmean**2)**0.5)
  1351. self.rmsthresh = self.rmsmean + sdfactor * self.rmssd
  1352. else:
  1353. self.rmsthresh = 0
  1354. self.rmsmean = 0
  1355. self.rmssd = 0
  1356. if self.highest_only and highest_fwd_err > 0.0:
  1357. self.list.SetItemTextColour(highest_idx, wx.RED)
  1358. elif GCPcount > 0 and self.rmsthresh > 0 and not self.highest_only:
  1359. for index in range(self.list.GetItemCount()):
  1360. if self.list.IsChecked(index):
  1361. key = self.list.GetItemData(index)
  1362. if (self.mapcoordlist[key][5] > self.rmsthresh):
  1363. self.list.SetItemTextColour(index, wx.RED)
  1364. # calculate global RMS error (geometric mean)
  1365. self.fwd_rmserror = round((sumsq_fwd_err/GCPcount)**0.5,4)
  1366. self.bkw_rmserror = round((sumsq_bkw_err/GCPcount)**0.5,4)
  1367. self.list.ResizeColumns()
  1368. def GetNewExtent(self, region, map = None):
  1369. coord_file = utils.GetTempfile()
  1370. newreg = { 'n' : 0.0, 's' : 0.0, 'e' : 0.0, 'w' : 0.0,}
  1371. try:
  1372. f = open(coord_file, mode='w')
  1373. # NW corner
  1374. f.write(str(region['e']) + " " + str(region['n']) + "\n")
  1375. # NE corner
  1376. f.write(str(region['e']) + " " + str(region['s']) + "\n")
  1377. # SW corner
  1378. f.write(str(region['w']) + " " + str(region['n']) + "\n")
  1379. # SE corner
  1380. f.write(str(region['w']) + " " + str(region['s']) + "\n")
  1381. finally:
  1382. f.close()
  1383. # save GCPs to points file to make sure that all checked GCPs are used
  1384. self.SaveGCPs(None)
  1385. order = self.gr_order
  1386. self.gr_order = 1
  1387. if self.CheckGCPcount(msg=True) == False:
  1388. self.gr_order = order
  1389. return
  1390. self.gr_order = order
  1391. # get list of forward and reverse rms error values for each point
  1392. self.grwiz.SwitchEnv('source')
  1393. if map == 'source':
  1394. ret = RunCommand('m.transform',
  1395. parent = self,
  1396. read = True,
  1397. group = self.xygroup,
  1398. order = 1,
  1399. format = 'dst',
  1400. coords = coord_file)
  1401. elif map == 'target':
  1402. ret = RunCommand('m.transform',
  1403. parent = self,
  1404. read = True,
  1405. group = self.xygroup,
  1406. order = 1,
  1407. flags = 'r',
  1408. format = 'src',
  1409. coords = coord_file)
  1410. os.unlink(coord_file)
  1411. self.grwiz.SwitchEnv('target')
  1412. if ret:
  1413. errlist = ret.splitlines()
  1414. else:
  1415. GError(parent = self,
  1416. message=_('Could not calculate new extends.\n'
  1417. 'Possible error with m.transform.'))
  1418. return
  1419. # fist corner
  1420. e, n = errlist[0].split()
  1421. fe = float(e)
  1422. fn = float(n)
  1423. newreg['n'] = fn
  1424. newreg['s'] = fn
  1425. newreg['e'] = fe
  1426. newreg['w'] = fe
  1427. # other three corners
  1428. for i in range(1, 4):
  1429. e, n = errlist[i].split()
  1430. fe = float(e)
  1431. fn = float(n)
  1432. if fe < newreg['w']:
  1433. newreg['w'] = fe
  1434. if fe > newreg['e']:
  1435. newreg['e'] = fe
  1436. if fn < newreg['s']:
  1437. newreg['s'] = fn
  1438. if fn > newreg['n']:
  1439. newreg['n'] = fn
  1440. return newreg
  1441. def OnHelp(self, event):
  1442. """!Show GCP Manager manual page"""
  1443. self._giface.Help(entry = 'wxGUI.GCP_Manager')
  1444. def OnUpdateActive(self, event):
  1445. if self.activemap.GetSelection() == 0:
  1446. self.MapWindow = self.SrcMapWindow
  1447. self.Map = self.SrcMap
  1448. else:
  1449. self.MapWindow = self.TgtMapWindow
  1450. self.Map = self.TgtMap
  1451. self.UpdateActive(self.MapWindow)
  1452. # for wingrass
  1453. if os.name == 'nt':
  1454. self.MapWindow.SetFocus()
  1455. def UpdateActive(self, win):
  1456. # optionally disable tool zoomback tool
  1457. self.GetMapToolbar().Enable('zoomback', enable = (len(self.MapWindow.zoomhistory) > 1))
  1458. if self.activemap.GetSelection() != (win == self.TgtMapWindow):
  1459. self.activemap.SetSelection(win == self.TgtMapWindow)
  1460. self.StatusbarUpdate()
  1461. def AdjustMap(self, newreg):
  1462. """!Adjust map window to new extents
  1463. """
  1464. # adjust map window
  1465. self.Map.region['n'] = newreg['n']
  1466. self.Map.region['s'] = newreg['s']
  1467. self.Map.region['e'] = newreg['e']
  1468. self.Map.region['w'] = newreg['w']
  1469. self.MapWindow.ZoomHistory(self.Map.region['n'], self.Map.region['s'],
  1470. self.Map.region['e'], self.Map.region['w'])
  1471. # LL locations
  1472. if self.Map.projinfo['proj'] == 'll':
  1473. if newreg['n'] > 90.0:
  1474. newreg['n'] = 90.0
  1475. if newreg['s'] < -90.0:
  1476. newreg['s'] = -90.0
  1477. ce = newreg['w'] + (newreg['e'] - newreg['w']) / 2
  1478. cn = newreg['s'] + (newreg['n'] - newreg['s']) / 2
  1479. # calculate new center point and display resolution
  1480. self.Map.region['center_easting'] = ce
  1481. self.Map.region['center_northing'] = cn
  1482. self.Map.region["ewres"] = (newreg['e'] - newreg['w']) / self.Map.width
  1483. self.Map.region["nsres"] = (newreg['n'] - newreg['s']) / self.Map.height
  1484. self.Map.AlignExtentFromDisplay()
  1485. self.MapWindow.ZoomHistory(self.Map.region['n'], self.Map.region['s'],
  1486. self.Map.region['e'], self.Map.region['w'])
  1487. if self.MapWindow.redrawAll is False:
  1488. self.MapWindow.redrawAll = True
  1489. self.MapWindow.UpdateMap()
  1490. self.StatusbarUpdate()
  1491. def OnZoomToSource(self, event):
  1492. """!Set target map window to match extents of source map window
  1493. """
  1494. if not self.MapWindow == self.TgtMapWindow:
  1495. self.MapWindow = self.TgtMapWindow
  1496. self.Map = self.TgtMap
  1497. self.UpdateActive(self.TgtMapWindow)
  1498. # get new N, S, E, W for target
  1499. newreg = self.GetNewExtent(self.SrcMap.region, 'source')
  1500. if newreg:
  1501. self.AdjustMap(newreg)
  1502. def OnZoomToTarget(self, event):
  1503. """!Set source map window to match extents of target map window
  1504. """
  1505. if not self.MapWindow == self.SrcMapWindow:
  1506. self.MapWindow = self.SrcMapWindow
  1507. self.Map = self.SrcMap
  1508. self.UpdateActive(self.SrcMapWindow)
  1509. # get new N, S, E, W for target
  1510. newreg = self.GetNewExtent(self.TgtMap.region, 'target')
  1511. if newreg:
  1512. self.AdjustMap(newreg)
  1513. def OnZoomMenuGCP(self, event):
  1514. """!Popup Zoom menu
  1515. """
  1516. point = wx.GetMousePosition()
  1517. zoommenu = wx.Menu()
  1518. # Add items to the menu
  1519. zoomsource = wx.MenuItem(zoommenu, wx.ID_ANY, _('Adjust source display to target display'))
  1520. zoommenu.AppendItem(zoomsource)
  1521. self.Bind(wx.EVT_MENU, self.OnZoomToTarget, zoomsource)
  1522. zoomtarget = wx.MenuItem(zoommenu, wx.ID_ANY, _('Adjust target display to source display'))
  1523. zoommenu.AppendItem(zoomtarget)
  1524. self.Bind(wx.EVT_MENU, self.OnZoomToSource, zoomtarget)
  1525. # Popup the menu. If an item is selected then its handler
  1526. # will be called before PopupMenu returns.
  1527. self.PopupMenu(zoommenu)
  1528. zoommenu.Destroy()
  1529. def OnSize(self, event):
  1530. """!Adjust Map Windows after GCP Map Display has been resized
  1531. """
  1532. # re-render image on idle
  1533. self.resize = time.clock()
  1534. def OnIdle(self, event):
  1535. """!GCP Map Display resized, adjust Map Windows
  1536. """
  1537. if self.GetMapToolbar():
  1538. if self.resize and self.resize + 0.2 < time.clock():
  1539. srcwidth, srcheight = self.SrcMapWindow.GetSize()
  1540. tgtwidth, tgtheight = self.TgtMapWindow.GetSize()
  1541. srcwidth = (srcwidth + tgtwidth) / 2
  1542. if self.show_target:
  1543. self._mgr.GetPane("target").Hide()
  1544. self._mgr.Update()
  1545. self._mgr.GetPane("source").BestSize((srcwidth, srcheight))
  1546. self._mgr.GetPane("target").BestSize((srcwidth, tgtheight))
  1547. if self.show_target:
  1548. self._mgr.GetPane("target").Show()
  1549. self._mgr.Update()
  1550. self.resize = False
  1551. elif self.resize:
  1552. event.RequestMore()
  1553. pass
  1554. class GCPList(wx.ListCtrl,
  1555. CheckListCtrlMixin,
  1556. ListCtrlAutoWidthMixin):
  1557. def __init__(self, parent, gcp, id=wx.ID_ANY,
  1558. pos=wx.DefaultPosition, size=wx.DefaultSize,
  1559. style=wx.LC_REPORT | wx.SUNKEN_BORDER | wx.LC_HRULES |
  1560. wx.LC_SINGLE_SEL):
  1561. wx.ListCtrl.__init__(self, parent, id, pos, size, style)
  1562. self.gcp = gcp # GCP class
  1563. self.render = True
  1564. # Mixin settings
  1565. CheckListCtrlMixin.__init__(self)
  1566. ListCtrlAutoWidthMixin.__init__(self)
  1567. # TextEditMixin.__init__(self)
  1568. # tracks whether list items are checked or not
  1569. self.CheckList = []
  1570. self._Create()
  1571. self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  1572. self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)
  1573. self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick)
  1574. self.selected = wx.NOT_FOUND
  1575. self.selectedkey = -1
  1576. def _Create(self):
  1577. if 0:
  1578. # normal, simple columns
  1579. idx_col = 0
  1580. for col in (_('use'),
  1581. _('source E'),
  1582. _('source N'),
  1583. _('target E'),
  1584. _('target N'),
  1585. _('Forward error'),
  1586. _('Backward error')):
  1587. self.InsertColumn(idx_col, col)
  1588. idx_col += 1
  1589. else:
  1590. # the hard way: we want images on the column header
  1591. info = wx.ListItem()
  1592. info.SetMask(wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT)
  1593. info.SetImage(-1)
  1594. info.m_format = wx.LIST_FORMAT_LEFT
  1595. idx_col = 0
  1596. for lbl in (_('use'),
  1597. _('source E'),
  1598. _('source N'),
  1599. _('target E'),
  1600. _('target N'),
  1601. _('Forward error'),
  1602. _('Backward error')):
  1603. info.SetText(lbl)
  1604. self.InsertColumnInfo(idx_col, info)
  1605. idx_col += 1
  1606. def LoadData(self):
  1607. """!Load data into list"""
  1608. self.DeleteAllItems()
  1609. self.render = False
  1610. if os.path.isfile(self.gcp.file['points']):
  1611. self.gcp.ReadGCPs()
  1612. else:
  1613. # 3 gcp is minimum
  1614. for i in range(3):
  1615. self.gcp.AddGCP(None)
  1616. # select first point by default
  1617. self.selected = 0
  1618. self.selectedkey = self.GetItemData(self.selected)
  1619. self.SetItemState(self.selected,
  1620. wx.LIST_STATE_SELECTED,
  1621. wx.LIST_STATE_SELECTED)
  1622. self.ResizeColumns()
  1623. self.render = True
  1624. self.EnsureVisible(self.selected)
  1625. def OnCheckItem(self, index, flag):
  1626. """!Item is checked/unchecked"""
  1627. if self.render:
  1628. # redraw points
  1629. sourceMapWin = self.gcp.SrcMapWindow
  1630. sourceMapWin.UpdateMap(render=False, renderVector=False)
  1631. if self.gcp.show_target:
  1632. targetMapWin = self.gcp.TgtMapWindow
  1633. targetMapWin.UpdateMap(render=False, renderVector=False)
  1634. def AddGCPItem(self):
  1635. """
  1636. Appends an item to GCP list
  1637. """
  1638. self.selectedkey = self.GetItemCount() + 1
  1639. self.Append([str(self.selectedkey), # GCP number
  1640. '0.0', # source E
  1641. '0.0', # source N
  1642. '0.0', # target E
  1643. '0.0', # target N
  1644. '', # forward error
  1645. '']) # backward error
  1646. self.selected = self.GetItemCount() - 1
  1647. self.SetItemData(self.selected, self.selectedkey)
  1648. self.SetItemState(self.selected,
  1649. wx.LIST_STATE_SELECTED,
  1650. wx.LIST_STATE_SELECTED)
  1651. self.ResizeColumns()
  1652. self.gcp.pointsToDrawSrc.AddItem(coords = [0,0], label = str(self.selectedkey))
  1653. self.gcp.pointsToDrawTgt.AddItem(coords = [0,0], label = str(self.selectedkey))
  1654. self.EnsureVisible(self.selected)
  1655. return self.selected
  1656. def DeleteGCPItem(self):
  1657. """!Deletes selected item in GCP list.
  1658. """
  1659. if self.selected == wx.NOT_FOUND:
  1660. return
  1661. key = self.GetItemData(self.selected)
  1662. self.DeleteItem(self.selected)
  1663. if self.selected != wx.NOT_FOUND:
  1664. item = self.gcp.pointsToDrawSrc.GetItem(key - 1)
  1665. self.gcp.pointsToDrawSrc.DeleteItem(item)
  1666. item = self.gcp.pointsToDrawTgt.GetItem(key - 1)
  1667. self.gcp.pointsToDrawTgt.DeleteItem(item)
  1668. return key
  1669. def ResizeColumns(self):
  1670. """!Resize columns"""
  1671. minWidth = [90, 120]
  1672. for i in range(self.GetColumnCount()):
  1673. self.SetColumnWidth(i, wx.LIST_AUTOSIZE)
  1674. # first column is checkbox, don't set to minWidth
  1675. if i > 0 and self.GetColumnWidth(i) < minWidth[i > 4]:
  1676. self.SetColumnWidth(i, minWidth[i > 4])
  1677. self.SendSizeEvent()
  1678. def GetSelected(self):
  1679. """!Get index of selected item"""
  1680. return self.selected
  1681. def OnItemSelected(self, event):
  1682. """!Item selected
  1683. """
  1684. if self.render and self.selected != event.GetIndex():
  1685. self.selected = event.GetIndex()
  1686. self.selectedkey = self.GetItemData(self.selected)
  1687. sourceMapWin = self.gcp.SrcMapWindow
  1688. sourceMapWin.UpdateMap(render=False, renderVector=False)
  1689. if self.gcp.show_target:
  1690. targetMapWin = self.gcp.TgtMapWindow
  1691. targetMapWin.UpdateMap(render=False, renderVector=False)
  1692. event.Skip()
  1693. def OnItemActivated(self, event):
  1694. """
  1695. When item double clicked, open editor to update coordinate values
  1696. """
  1697. coords = []
  1698. index = event.GetIndex()
  1699. key = self.GetItemData(index)
  1700. changed = False
  1701. for i in range(1, 5):
  1702. coords.append(self.GetItem(index, i).GetText())
  1703. dlg = EditGCP(parent=self, id=wx.ID_ANY, data=coords, gcpno=key)
  1704. if dlg.ShowModal() == wx.ID_OK:
  1705. values = dlg.GetValues() # string
  1706. if len(values) == 0:
  1707. GError(parent = self,
  1708. message=_("Invalid coordinate value. Operation canceled."))
  1709. else:
  1710. for i in range(len(values)):
  1711. if values[i] != coords[i]:
  1712. self.SetStringItem(index, i + 1, values[i])
  1713. changed = True
  1714. if changed:
  1715. # reset RMS and update mapcoordlist
  1716. self.SetStringItem(index, 5, '')
  1717. self.SetStringItem(index, 6, '')
  1718. key = self.GetItemData(index)
  1719. self.gcp.mapcoordlist[key] = [key,
  1720. float(values[0]),
  1721. float(values[1]),
  1722. float(values[2]),
  1723. float(values[3]),
  1724. 0.0,
  1725. 0.0]
  1726. self.gcp.pointsToDrawSrc.GetItem(key - 1).SetCoords([float(values[0]),
  1727. float(values[1])])
  1728. self.gcp.pointsToDrawTgt.GetItem(key - 1).SetCoords([float(values[2]),
  1729. float(values[3])])
  1730. self.gcp.UpdateColours()
  1731. def OnColClick(self, event):
  1732. """!ListCtrl forgets selected item..."""
  1733. self.selected = self.FindItemData(-1, self.selectedkey)
  1734. self.SetItemState(self.selected,
  1735. wx.LIST_STATE_SELECTED,
  1736. wx.LIST_STATE_SELECTED)
  1737. event.Skip()
  1738. class VectGroup(wx.Dialog):
  1739. """!Dialog to create a vector group (VREF file) for georectifying
  1740. @todo Replace by g.group
  1741. """
  1742. def __init__(self, parent, id, grassdb, location, mapset, group,
  1743. style=wx.DEFAULT_DIALOG_STYLE):
  1744. wx.Dialog.__init__(self, parent, id, style=style,
  1745. title = _("Create vector map group"))
  1746. self.grassdatabase = grassdb
  1747. self.xylocation = location
  1748. self.xymapset = mapset
  1749. self.xygroup = group
  1750. #
  1751. # get list of valid vector directories
  1752. #
  1753. vectlist = os.listdir(os.path.join(self.grassdatabase,
  1754. self.xylocation,
  1755. self.xymapset,
  1756. 'vector'))
  1757. for dir in vectlist:
  1758. if not os.path.isfile(os.path.join(self.grassdatabase,
  1759. self.xylocation,
  1760. self.xymapset,
  1761. 'vector',
  1762. dir,
  1763. 'coor')):
  1764. vectlist.remove(dir)
  1765. utils.ListSortLower(vectlist)
  1766. # path to vref file
  1767. self.vgrpfile = os.path.join(self.grassdatabase,
  1768. self.xylocation,
  1769. self.xymapset,
  1770. 'group',
  1771. self.xygroup,
  1772. 'VREF')
  1773. #
  1774. # buttons
  1775. #
  1776. self.btnCancel = wx.Button(parent = self,
  1777. id = wx.ID_CANCEL)
  1778. self.btnOK = wx.Button(parent = self,
  1779. id = wx.ID_OK)
  1780. self.btnOK.SetDefault()
  1781. #
  1782. # list of vector maps
  1783. #
  1784. self.listMap = wx.CheckListBox(parent = self, id = wx.ID_ANY,
  1785. choices = vectlist)
  1786. if os.path.isfile(self.vgrpfile):
  1787. f = open(self.vgrpfile)
  1788. try:
  1789. checked = []
  1790. for line in f.readlines():
  1791. line = line.replace('\n', '')
  1792. if len(line) < 1:
  1793. continue
  1794. checked.append(line)
  1795. self.listMap.SetCheckedStrings(checked)
  1796. finally:
  1797. f.close()
  1798. line = wx.StaticLine(parent = self,
  1799. id = wx.ID_ANY, size = (20, -1),
  1800. style = wx.LI_HORIZONTAL)
  1801. #
  1802. # layout
  1803. #
  1804. sizer = wx.BoxSizer(wx.VERTICAL)
  1805. box = wx.BoxSizer(wx.HORIZONTAL)
  1806. box.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
  1807. label = _('Select vector map(s) to add to group:')),
  1808. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT | wx.LEFT,
  1809. border = 5)
  1810. box.Add(item = self.listMap,
  1811. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT | wx.LEFT,
  1812. border = 5)
  1813. sizer.Add(box, flag = wx.ALIGN_RIGHT | wx.ALL,
  1814. border = 3)
  1815. sizer.Add(item = line, proportion = 0,
  1816. flag = wx.GROW | wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT,
  1817. border = 5)
  1818. # buttons
  1819. btnSizer = wx.StdDialogButtonSizer()
  1820. btnSizer.AddButton(self.btnCancel)
  1821. btnSizer.AddButton(self.btnOK)
  1822. btnSizer.Realize()
  1823. sizer.Add(item = btnSizer, proportion = 0,
  1824. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER,
  1825. border = 5)
  1826. self.SetSizer(sizer)
  1827. sizer.Fit(self)
  1828. self.Layout()
  1829. def MakeVGroup(self):
  1830. """!Create VREF file"""
  1831. vgrouplist = []
  1832. for item in range(self.listMap.GetCount()):
  1833. if not self.listMap.IsChecked(item):
  1834. continue
  1835. vgrouplist.append(self.listMap.GetString(item) + '@' + self.xymapset)
  1836. f = open(self.vgrpfile, mode='w')
  1837. try:
  1838. for vect in vgrouplist:
  1839. f.write(vect + '\n')
  1840. finally:
  1841. f.close()
  1842. class EditGCP(wx.Dialog):
  1843. def __init__(self, parent, data, gcpno, id=wx.ID_ANY,
  1844. title=_("Edit GCP"),
  1845. style=wx.DEFAULT_DIALOG_STYLE):
  1846. """!Dialog for editing GPC and map coordinates in list control"""
  1847. wx.Dialog.__init__(self, parent, id, title=title, style=style)
  1848. panel = wx.Panel(parent=self)
  1849. sizer = wx.BoxSizer(wx.VERTICAL)
  1850. box = wx.StaticBox (parent=panel, id=wx.ID_ANY,
  1851. label=" %s %s " % (_("Ground Control Point No."), str(gcpno)))
  1852. boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  1853. # source coordinates
  1854. gridSizer = wx.GridBagSizer(vgap=5, hgap=5)
  1855. self.xcoord = wx.TextCtrl(parent=panel, id=wx.ID_ANY, size=(150, -1))
  1856. self.ycoord = wx.TextCtrl(parent=panel, id=wx.ID_ANY, size=(150, -1))
  1857. self.ecoord = wx.TextCtrl(parent=panel, id=wx.ID_ANY, size=(150, -1))
  1858. self.ncoord = wx.TextCtrl(parent=panel, id=wx.ID_ANY, size=(150, -1))
  1859. # swap source N, target E
  1860. tmp_coord = data[1]
  1861. data[1] = data[2]
  1862. data[2] = tmp_coord
  1863. row = 0
  1864. col = 0
  1865. idx = 0
  1866. for label, win in ((_("source E:"), self.xcoord),
  1867. (_("target E:"), self.ecoord),
  1868. (_("source N:"), self.ycoord),
  1869. (_("target N:"), self.ncoord)):
  1870. label = wx.StaticText(parent=panel, id=wx.ID_ANY,
  1871. label=label)
  1872. gridSizer.Add(item=label,
  1873. flag=wx.ALIGN_CENTER_VERTICAL,
  1874. pos=(row, col))
  1875. col += 1
  1876. win.SetValue(str(data[idx]))
  1877. gridSizer.Add(item=win,
  1878. pos=(row, col))
  1879. col += 1
  1880. idx += 1
  1881. if col > 3:
  1882. row += 1
  1883. col = 0
  1884. boxSizer.Add(item=gridSizer, proportion=1,
  1885. flag=wx.EXPAND | wx.ALL, border=5)
  1886. sizer.Add(item=boxSizer, proportion=1,
  1887. flag=wx.EXPAND | wx.ALL, border=5)
  1888. #
  1889. # buttons
  1890. #
  1891. self.btnCancel = wx.Button(panel, wx.ID_CANCEL)
  1892. self.btnOk = wx.Button(panel, wx.ID_OK)
  1893. self.btnOk.SetDefault()
  1894. btnSizer = wx.StdDialogButtonSizer()
  1895. btnSizer.AddButton(self.btnCancel)
  1896. btnSizer.AddButton(self.btnOk)
  1897. btnSizer.Realize()
  1898. sizer.Add(item=btnSizer, proportion=0,
  1899. flag=wx.ALIGN_RIGHT | wx.ALL, border=5)
  1900. panel.SetSizer(sizer)
  1901. sizer.Fit(self)
  1902. def GetValues(self, columns=None):
  1903. """!Return list of values (as strings).
  1904. """
  1905. valuelist = []
  1906. try:
  1907. float(self.xcoord.GetValue())
  1908. float(self.ycoord.GetValue())
  1909. float(self.ecoord.GetValue())
  1910. float(self.ncoord.GetValue())
  1911. except ValueError:
  1912. return valuelist
  1913. valuelist.append(self.xcoord.GetValue())
  1914. valuelist.append(self.ycoord.GetValue())
  1915. valuelist.append(self.ecoord.GetValue())
  1916. valuelist.append(self.ncoord.GetValue())
  1917. return valuelist
  1918. class GrSettingsDialog(wx.Dialog):
  1919. def __init__(self, parent, id, giface, title, pos=wx.DefaultPosition, size=wx.DefaultSize,
  1920. style=wx.DEFAULT_DIALOG_STYLE):
  1921. wx.Dialog.__init__(self, parent, id, title, pos, size, style)
  1922. """
  1923. Dialog to set profile text options: font, title
  1924. and font size, axis labels and font size
  1925. """
  1926. #
  1927. # initialize variables
  1928. #
  1929. self.parent = parent
  1930. self.new_src_map = src_map
  1931. self.new_tgt_map = { 'raster' : tgt_map['raster'],
  1932. 'vector' : tgt_map['vector'] }
  1933. self.sdfactor = 0
  1934. self.symbol = {}
  1935. self.methods = ["nearest",
  1936. "linear",
  1937. "linear_f",
  1938. "cubic",
  1939. "cubic_f",
  1940. "lanczos",
  1941. "lanczos_f"]
  1942. # notebook
  1943. notebook = wx.Notebook(parent=self, id=wx.ID_ANY, style=wx.BK_DEFAULT)
  1944. self.__CreateSymbologyPage(notebook)
  1945. self.__CreateRectificationPage(notebook)
  1946. # buttons
  1947. btnSave = wx.Button(self, wx.ID_SAVE)
  1948. btnApply = wx.Button(self, wx.ID_APPLY)
  1949. btnClose = wx.Button(self, wx.ID_CLOSE)
  1950. btnApply.SetDefault()
  1951. # bindings
  1952. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  1953. btnApply.SetToolTipString(_("Apply changes for the current session"))
  1954. btnSave.Bind(wx.EVT_BUTTON, self.OnSave)
  1955. btnSave.SetToolTipString(_("Apply and save changes to user settings file (default for next sessions)"))
  1956. btnClose.Bind(wx.EVT_BUTTON, self.OnClose)
  1957. btnClose.SetToolTipString(_("Close dialog"))
  1958. # sizers
  1959. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  1960. btnSizer.Add(btnApply, flag=wx.LEFT | wx.RIGHT, border=5)
  1961. btnSizer.Add(btnSave, flag=wx.LEFT | wx.RIGHT, border=5)
  1962. btnSizer.Add(btnClose, flag=wx.LEFT | wx.RIGHT, border=5)
  1963. # sizers
  1964. mainSizer = wx.BoxSizer(wx.VERTICAL)
  1965. mainSizer.Add(item=notebook, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  1966. mainSizer.Add(item=btnSizer, proportion=0,
  1967. flag=wx.ALIGN_RIGHT | wx.ALL, border=5)
  1968. # flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  1969. self.SetSizer(mainSizer)
  1970. mainSizer.Fit(self)
  1971. def __CreateSymbologyPage(self, notebook):
  1972. """!Create notebook page with symbology settings"""
  1973. panel = wx.Panel(parent=notebook, id=wx.ID_ANY)
  1974. notebook.AddPage(page=panel, text=_("Symbology"))
  1975. sizer = wx.BoxSizer(wx.VERTICAL)
  1976. rmsgridSizer = wx.GridBagSizer(vgap=5, hgap=5)
  1977. # highlight only highest forward RMS error
  1978. self.highlighthighest = wx.CheckBox(parent=panel, id=wx.ID_ANY,
  1979. label=_("Highlight highest RMS error only"))
  1980. hh = UserSettings.Get(group='gcpman', key='rms', subkey='highestonly')
  1981. self.highlighthighest.SetValue(hh)
  1982. rmsgridSizer.Add(item=self.highlighthighest, flag=wx.ALIGN_CENTER_VERTICAL, pos=(0, 0))
  1983. # RMS forward error threshold
  1984. rmslabel = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Highlight RMS error > M + SD * factor:"))
  1985. rmslabel.SetToolTip(wx.ToolTip(_("Highlight GCPs with an RMS error larger than \n"
  1986. "mean + standard deviation * given factor. \n"
  1987. "Recommended values for this factor are between 1 and 2.")))
  1988. rmsgridSizer.Add(item=rmslabel, flag=wx.ALIGN_CENTER_VERTICAL, pos=(1, 0))
  1989. sdfactor = UserSettings.Get(group='gcpman', key='rms', subkey='sdfactor')
  1990. self.rmsWin = wx.TextCtrl(parent=panel, id=wx.ID_ANY,
  1991. size=(70,-1), style=wx.TE_NOHIDESEL)
  1992. self.rmsWin.SetValue("%s" % str(sdfactor))
  1993. if (self.parent.highest_only == True):
  1994. self.rmsWin.Disable()
  1995. self.symbol['sdfactor'] = self.rmsWin.GetId()
  1996. rmsgridSizer.Add(item=self.rmsWin, flag=wx.ALIGN_RIGHT, pos=(1, 1))
  1997. rmsgridSizer.AddGrowableCol(1)
  1998. sizer.Add(item=rmsgridSizer, flag=wx.EXPAND | wx.ALL, border=5)
  1999. box = wx.StaticBox(parent=panel, id=wx.ID_ANY,
  2000. label=" %s " % _("Symbol settings"))
  2001. boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  2002. gridSizer = wx.GridBagSizer(vgap=5, hgap=5)
  2003. #
  2004. # general symbol color
  2005. #
  2006. row = 0
  2007. label = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Color:"))
  2008. gridSizer.Add(item=label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2009. col = UserSettings.Get(group='gcpman', key='symbol', subkey='color')
  2010. colWin = csel.ColourSelect(parent=panel, id=wx.ID_ANY,
  2011. colour=wx.Colour(col[0],
  2012. col[1],
  2013. col[2],
  2014. 255))
  2015. self.symbol['color'] = colWin.GetId()
  2016. gridSizer.Add(item=colWin,
  2017. flag=wx.ALIGN_RIGHT,
  2018. pos=(row, 1))
  2019. #
  2020. # symbol color for high forward RMS error
  2021. #
  2022. row += 1
  2023. label = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Color for high RMS error:"))
  2024. gridSizer.Add(item=label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2025. hcol = UserSettings.Get(group='gcpman', key='symbol', subkey='hcolor')
  2026. hcolWin = csel.ColourSelect(parent=panel, id=wx.ID_ANY,
  2027. colour=wx.Colour(hcol[0],
  2028. hcol[1],
  2029. hcol[2],
  2030. 255))
  2031. self.symbol['hcolor'] = hcolWin.GetId()
  2032. gridSizer.Add(item=hcolWin,
  2033. flag=wx.ALIGN_RIGHT,
  2034. pos=(row, 1))
  2035. #
  2036. # symbol color for selected GCP
  2037. #
  2038. row += 1
  2039. label = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Color for selected GCP:"))
  2040. gridSizer.Add(item=label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2041. scol = UserSettings.Get(group='gcpman', key='symbol', subkey='scolor')
  2042. scolWin = csel.ColourSelect(parent=panel, id=wx.ID_ANY,
  2043. colour=wx.Colour(scol[0],
  2044. scol[1],
  2045. scol[2],
  2046. 255))
  2047. self.symbol['scolor'] = scolWin.GetId()
  2048. gridSizer.Add(item=scolWin,
  2049. flag=wx.ALIGN_RIGHT,
  2050. pos=(row, 1))
  2051. #
  2052. # symbol color for unused GCP
  2053. #
  2054. row += 1
  2055. label = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Color for unused GCPs:"))
  2056. gridSizer.Add(item=label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2057. ucol = UserSettings.Get(group='gcpman', key='symbol', subkey='ucolor')
  2058. ucolWin = csel.ColourSelect(parent=panel, id=wx.ID_ANY,
  2059. colour=wx.Colour(ucol[0],
  2060. ucol[1],
  2061. ucol[2],
  2062. 255))
  2063. self.symbol['ucolor'] = ucolWin.GetId()
  2064. gridSizer.Add(item=ucolWin,
  2065. flag=wx.ALIGN_RIGHT,
  2066. pos=(row, 1))
  2067. # show unused GCPs
  2068. row += 1
  2069. self.showunused = wx.CheckBox(parent=panel, id=wx.ID_ANY,
  2070. label=_("Show unused GCPs"))
  2071. shuu = UserSettings.Get(group='gcpman', key='symbol', subkey='unused')
  2072. self.showunused.SetValue(shuu)
  2073. gridSizer.Add(item=self.showunused, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2074. #
  2075. # symbol size
  2076. #
  2077. row += 1
  2078. label = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Symbol size:"))
  2079. gridSizer.Add(item=label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2080. symsize = int(UserSettings.Get(group='gcpman', key='symbol', subkey='size'))
  2081. sizeWin = wx.SpinCtrl(parent=panel, id=wx.ID_ANY,
  2082. min=1, max=20)
  2083. sizeWin.SetValue(symsize)
  2084. self.symbol['size'] = sizeWin.GetId()
  2085. gridSizer.Add(item=sizeWin,
  2086. flag=wx.ALIGN_RIGHT,
  2087. pos=(row, 1))
  2088. #
  2089. # symbol width
  2090. #
  2091. row += 1
  2092. label = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Line width:"))
  2093. gridSizer.Add(item=label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2094. width = int(UserSettings.Get(group='gcpman', key='symbol', subkey='width'))
  2095. widWin = wx.SpinCtrl(parent=panel, id=wx.ID_ANY,
  2096. min=1, max=10)
  2097. widWin.SetValue(width)
  2098. self.symbol['width'] = widWin.GetId()
  2099. gridSizer.Add(item=widWin,
  2100. flag=wx.ALIGN_RIGHT,
  2101. pos=(row, 1))
  2102. gridSizer.AddGrowableCol(1)
  2103. boxSizer.Add(item=gridSizer, flag=wx.EXPAND)
  2104. sizer.Add(item=boxSizer, flag=wx.EXPAND | wx.ALL, border=5)
  2105. #
  2106. # maps to display
  2107. #
  2108. # source map to display
  2109. self.srcselection = Select(panel, id=wx.ID_ANY,
  2110. size=globalvar.DIALOG_GSELECT_SIZE, type='maptype', updateOnPopup = False)
  2111. self.parent.grwiz.SwitchEnv('source')
  2112. self.srcselection.SetElementList(maptype)
  2113. # filter out all maps not in group
  2114. self.srcselection.tcp.GetElementList(elements = self.parent.src_maps)
  2115. # target map(s) to display
  2116. self.parent.grwiz.SwitchEnv('target')
  2117. self.tgtrastselection = Select(panel, id=wx.ID_ANY,
  2118. size=globalvar.DIALOG_GSELECT_SIZE, type='raster', updateOnPopup = False)
  2119. self.tgtrastselection.SetElementList('cell')
  2120. self.tgtrastselection.GetElementList()
  2121. self.tgtvectselection = Select(panel, id=wx.ID_ANY,
  2122. size=globalvar.DIALOG_GSELECT_SIZE, type='vector', updateOnPopup = False)
  2123. self.tgtvectselection.SetElementList('vector')
  2124. self.tgtvectselection.GetElementList()
  2125. sizer.Add(item=wx.StaticText(parent=panel, id=wx.ID_ANY, label=_('Select source map to display:')),
  2126. proportion=0, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)
  2127. sizer.Add(item=self.srcselection, proportion=0,
  2128. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)
  2129. self.srcselection.SetValue(src_map)
  2130. sizer.Add(item=wx.StaticText(parent=panel, id=wx.ID_ANY, label=_('Select target raster map to display:')),
  2131. proportion=0, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)
  2132. sizer.Add(item=self.tgtrastselection, proportion=0,
  2133. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)
  2134. self.tgtrastselection.SetValue(tgt_map['raster'])
  2135. sizer.Add(item=wx.StaticText(parent=panel, id=wx.ID_ANY, label=_('Select target vector map to display:')),
  2136. proportion=0, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)
  2137. sizer.Add(item=self.tgtvectselection, proportion=0,
  2138. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)
  2139. self.tgtvectselection.SetValue(tgt_map['vector'])
  2140. # bindings
  2141. self.highlighthighest.Bind(wx.EVT_CHECKBOX, self.OnHighlight)
  2142. self.rmsWin.Bind(wx.EVT_TEXT, self.OnSDFactor)
  2143. self.srcselection.Bind(wx.EVT_TEXT, self.OnSrcSelection)
  2144. self.tgtrastselection.Bind(wx.EVT_TEXT, self.OnTgtRastSelection)
  2145. self.tgtvectselection.Bind(wx.EVT_TEXT, self.OnTgtVectSelection)
  2146. panel.SetSizer(sizer)
  2147. return panel
  2148. def __CreateRectificationPage(self, notebook):
  2149. """!Create notebook page with symbology settings"""
  2150. panel = wx.Panel(parent=notebook, id=wx.ID_ANY)
  2151. notebook.AddPage(page=panel, text=_("Rectification"))
  2152. sizer = wx.BoxSizer(wx.VERTICAL)
  2153. # transformation order
  2154. self.rb_grorder = wx.RadioBox(parent=panel, id=wx.ID_ANY,
  2155. label=" %s " % _("Select rectification order"),
  2156. choices=[_('1st order'), _('2nd order'), _('3rd order')],
  2157. majorDimension=wx.RA_SPECIFY_COLS)
  2158. sizer.Add(item=self.rb_grorder, proportion=0,
  2159. flag=wx.EXPAND | wx.ALL, border=5)
  2160. self.rb_grorder.SetSelection(self.parent.gr_order - 1)
  2161. # interpolation method
  2162. gridSizer = wx.GridBagSizer(vgap=5, hgap=5)
  2163. gridSizer.Add(item=wx.StaticText(parent=panel, id=wx.ID_ANY, label=_('Select interpolation method:')),
  2164. pos=(0,0), flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)
  2165. self.grmethod = wx.Choice(parent=panel, id=wx.ID_ANY,
  2166. choices = self.methods)
  2167. gridSizer.Add(item=self.grmethod, pos=(0,1),
  2168. flag=wx.ALIGN_RIGHT, border=5)
  2169. self.grmethod.SetStringSelection(self.parent.gr_method)
  2170. gridSizer.AddGrowableCol(1)
  2171. sizer.Add(item=gridSizer, flag=wx.EXPAND | wx.ALL, border=5)
  2172. # clip to region
  2173. self.check = wx.CheckBox(parent=panel, id=wx.ID_ANY,
  2174. label=_("clip to computational region in target location"))
  2175. sizer.Add(item=self.check, proportion=0,
  2176. flag=wx.EXPAND | wx.ALL, border=5)
  2177. self.check.SetValue(self.parent.clip_to_region)
  2178. # extension
  2179. sizer.Add(item=wx.StaticText(parent=panel, id=wx.ID_ANY, label=_('Extension for output maps:')),
  2180. proportion=0, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)
  2181. self.ext_txt = wx.TextCtrl(parent=panel, id=wx.ID_ANY, value="", size=(350,-1))
  2182. self.ext_txt.SetValue(self.parent.extension)
  2183. sizer.Add(item=self.ext_txt,
  2184. proportion=0, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)
  2185. # bindings
  2186. self.ext_txt.Bind(wx.EVT_TEXT, self.OnExtension)
  2187. self.Bind(wx.EVT_RADIOBOX, self.parent.OnGROrder, self.rb_grorder)
  2188. self.Bind(wx.EVT_CHOICE, self.OnMethod, self.grmethod)
  2189. self.Bind(wx.EVT_CHECKBOX, self.OnClipRegion, self.check)
  2190. panel.SetSizer(sizer)
  2191. return panel
  2192. def OnHighlight(self, event):
  2193. """!Checkbox 'highlighthighest' checked/unchecked"""
  2194. if self.highlighthighest.IsChecked():
  2195. self.parent.highest_only = True
  2196. self.rmsWin.Disable()
  2197. else:
  2198. self.parent.highest_only = False
  2199. self.rmsWin.Enable()
  2200. def OnSDFactor(self,event):
  2201. """!New factor for RMS threshold = M + SD * factor"""
  2202. try:
  2203. self.sdfactor = float(self.rmsWin.GetValue())
  2204. except ValueError:
  2205. return
  2206. if self.sdfactor <= 0:
  2207. GError(parent = self,
  2208. message=_('RMS threshold factor must be > 0'))
  2209. elif self.sdfactor < 1:
  2210. GError(parent = self,
  2211. message=_('RMS threshold factor is < 1\n'
  2212. 'Too many points might be highlighted'))
  2213. def OnSrcSelection(self,event):
  2214. """!Source map to display selected"""
  2215. global src_map
  2216. tmp_map = self.srcselection.GetValue()
  2217. if not tmp_map == '' and not tmp_map == src_map:
  2218. self.new_src_map = tmp_map
  2219. def OnTgtRastSelection(self,event):
  2220. """!Target map to display selected"""
  2221. global tgt_map
  2222. self.new_tgt_map['raster'] = self.tgtrastselection.GetValue()
  2223. def OnTgtVectSelection(self,event):
  2224. """!Target map to display selected"""
  2225. global tgt_map
  2226. self.new_tgt_map['vector'] = self.tgtvectselection.GetValue()
  2227. def OnMethod(self, event):
  2228. self.parent.gr_method = self.methods[event.GetSelection()]
  2229. def OnClipRegion(self, event):
  2230. self.parent.clip_to_region = event.IsChecked()
  2231. def OnExtension(self, event):
  2232. self.parent.extension = self.ext_txt.GetValue()
  2233. def UpdateSettings(self):
  2234. global src_map
  2235. global tgt_map
  2236. global maptype
  2237. layers = None
  2238. UserSettings.Set(group = 'gcpman', key = 'rms', subkey = 'highestonly',
  2239. value = self.highlighthighest.GetValue())
  2240. if self.sdfactor > 0:
  2241. UserSettings.Set(group='gcpman', key='rms', subkey='sdfactor',
  2242. value=self.sdfactor)
  2243. self.parent.sdfactor = self.sdfactor
  2244. if self.parent.rmsthresh > 0:
  2245. self.parent.rmsthresh = self.parent.rmsmean + self.parent.sdfactor * self.parent.rmssd
  2246. UserSettings.Set(group='gcpman', key='symbol', subkey='color',
  2247. value=tuple(wx.FindWindowById(self.symbol['color']).GetColour()))
  2248. UserSettings.Set(group='gcpman', key='symbol', subkey='hcolor',
  2249. value=tuple(wx.FindWindowById(self.symbol['hcolor']).GetColour()))
  2250. UserSettings.Set(group='gcpman', key='symbol', subkey='scolor',
  2251. value=tuple(wx.FindWindowById(self.symbol['scolor']).GetColour()))
  2252. UserSettings.Set(group='gcpman', key='symbol', subkey='ucolor',
  2253. value=tuple(wx.FindWindowById(self.symbol['ucolor']).GetColour()))
  2254. UserSettings.Set(group='gcpman', key='symbol', subkey='unused',
  2255. value=self.showunused.GetValue())
  2256. UserSettings.Set(group='gcpman', key='symbol', subkey='size',
  2257. value=wx.FindWindowById(self.symbol['size']).GetValue())
  2258. UserSettings.Set(group='gcpman', key='symbol', subkey='width',
  2259. value=wx.FindWindowById(self.symbol['width']).GetValue())
  2260. srcrender = False
  2261. srcrenderVector = False
  2262. tgtrender = False
  2263. tgtrenderVector = False
  2264. reload_target = False
  2265. if self.new_src_map != src_map:
  2266. # remove old layer
  2267. layers = self.parent.grwiz.SrcMap.GetListOfLayers()
  2268. self.parent.grwiz.SrcMap.DeleteLayer(layers[0])
  2269. src_map = self.new_src_map
  2270. if maptype == 'raster':
  2271. cmdlist = ['d.rast', 'map=%s' % src_map]
  2272. srcrender = True
  2273. else:
  2274. cmdlist = ['d.vect', 'map=%s' % src_map]
  2275. srcrenderVector = True
  2276. self.parent.grwiz.SwitchEnv('source')
  2277. name, found = utils.GetLayerNameFromCmd(cmdlist)
  2278. self.parent.grwiz.SrcMap.AddLayer(ltype=maptype, command=cmdlist, active=True,
  2279. name=name, hidden=False, opacity=1.0, render=False)
  2280. self.parent.grwiz.SwitchEnv('target')
  2281. if self.new_tgt_map['raster'] != tgt_map['raster'] or \
  2282. self.new_tgt_map['vector'] != tgt_map['vector']:
  2283. # remove all layers
  2284. layers = self.parent.grwiz.TgtMap.GetListOfLayers()
  2285. while layers:
  2286. self.parent.grwiz.TgtMap.DeleteLayer(layers[0])
  2287. del layers[0]
  2288. layers = self.parent.grwiz.TgtMap.GetListOfLayers()
  2289. #self.parent.grwiz.TgtMap.DeleteAllLayers()
  2290. reload_target = True
  2291. tgt_map['raster'] = self.new_tgt_map['raster']
  2292. tgt_map['vector'] = self.new_tgt_map['vector']
  2293. if tgt_map['raster'] != '':
  2294. cmdlist = ['d.rast', 'map=%s' % tgt_map['raster']]
  2295. name, found = utils.GetLayerNameFromCmd(cmdlist)
  2296. self.parent.grwiz.TgtMap.AddLayer(ltype='raster', command=cmdlist, active=True,
  2297. name=name, hidden=False, opacity=1.0, render=False)
  2298. tgtrender = True
  2299. if tgt_map['vector'] != '':
  2300. cmdlist = ['d.vect', 'map=%s' % tgt_map['vector']]
  2301. name, found = utils.GetLayerNameFromCmd(cmdlist)
  2302. self.parent.grwiz.TgtMap.AddLayer(ltype='vector', command=cmdlist, active=True,
  2303. name=name, hidden=False, opacity=1.0, render=False)
  2304. tgtrenderVector = True
  2305. if tgt_map['raster'] == '' and tgt_map['vector'] == '':
  2306. if self.parent.show_target == True:
  2307. self.parent.show_target = False
  2308. self.parent._mgr.GetPane("target").Hide()
  2309. self.parent._mgr.Update()
  2310. self.parent.activemap.SetSelection(0)
  2311. self.parent.activemap.Enable(False)
  2312. self.parent.GetMapToolbar().Enable('zoommenu', enable = False)
  2313. else:
  2314. if self.parent.show_target == False:
  2315. self.parent.show_target = True
  2316. self.parent._mgr.GetPane("target").Show()
  2317. self.parent._mgr.Update()
  2318. self.parent.activemap.SetSelection(0)
  2319. self.parent.activemap.Enable(True)
  2320. self.parent.GetMapToolbar().Enable('zoommenu', enable = True)
  2321. self.parent.TgtMapWindow.ZoomToMap(layers = self.parent.TgtMap.GetListOfLayers())
  2322. self.parent.UpdateColours(srcrender, srcrenderVector, tgtrender, tgtrenderVector)
  2323. self.parent.SetSettings()
  2324. def OnSave(self, event):
  2325. """!Button 'Save' pressed"""
  2326. self.UpdateSettings()
  2327. fileSettings = {}
  2328. UserSettings.ReadSettingsFile(settings=fileSettings)
  2329. fileSettings['gcpman'] = UserSettings.Get(group='gcpman')
  2330. file = UserSettings.SaveToFile(fileSettings)
  2331. self._giface.WriteLog(_('GCP Manager settings saved to file \'%s\'.') % file)
  2332. #self.Close()
  2333. def OnApply(self, event):
  2334. """!Button 'Apply' pressed"""
  2335. self.UpdateSettings()
  2336. #self.Close()
  2337. def OnClose(self, event):
  2338. """!Button 'Cancel' pressed"""
  2339. self.Close()