manager.py 108 KB

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